Skip to content

Commit

Permalink
Merge pull request #154 from Textalk/v1.5-master
Browse files Browse the repository at this point in the history
Fix; allow header block larger than 1024 bytes
  • Loading branch information
Sören Jensen authored Mar 29, 2022
2 parents 569e7d4 + 5ae666e commit 1712325
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 41 deletions.
6 changes: 5 additions & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ function ($key, $value) {
$this->write($header);

// Get server response header (terminated with double CR+LF).
$response = stream_get_line($this->socket, 1024, "\r\n\r\n");
$response = '';
do {
$buffer = fgets($this->socket, 1024);
$response .= $buffer;
} while (substr_count($response, "\r\n\r\n") == 0);

// Validate response.
if (!preg_match('#Sec-WebSocket-Accept:\s(.*)$#mUi', $response, $matches)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/mock/mock-socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function fread()
$args = func_get_args();
return MockSocket::handle('fread', $args);
}
function fgets()
{
$args = func_get_args();
return MockSocket::handle('fgets', $args);
}
function stream_context_create()
{
$args = func_get_args();
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-authed.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"return": 248
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-context.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-extended.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"return": 224
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
16 changes: 12 additions & 4 deletions tests/scripts/client.connect-headers.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@
"return": 255
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\nX-Very-Long_Header: This is added to provoke split reads of headers in client 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\r\n"
},
{
"function": "fgets",
"params": [
"@mock-stream",
1024
],
"return-op": "key-respond",
"return": "Next234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-invalid-key.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: BAD"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: BAD\r\n\r\n"
}
]
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-invalid-upgrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return": "Invalid upgrade response"
"return": "Invalid upgrade response\r\n\r\n"
}
]
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-persistent.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@
"return": 248
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect-timeout.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.connect.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
Expand Down
7 changes: 3 additions & 4 deletions tests/scripts/client.reconnect.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@
"return": 199
},
{
"function": "stream_get_line",
"function": "fgets",
"params": [
"@mock-stream",
1024,
"\r\n\r\n"
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fread",
Expand Down

0 comments on commit 1712325

Please sign in to comment.