Skip to content

Commit 1712325

Browse files
author
Sören Jensen
authored
Merge pull request #154 from Textalk/v1.5-master
Fix; allow header block larger than 1024 bytes
2 parents 569e7d4 + 5ae666e commit 1712325

12 files changed

+49
-41
lines changed

lib/Client.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ function ($key, $value) {
177177
$this->write($header);
178178

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

182186
// Validate response.
183187
if (!preg_match('#Sec-WebSocket-Accept:\s(.*)$#mUi', $response, $matches)) {

tests/mock/mock-socket.php

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ function fread()
5656
$args = func_get_args();
5757
return MockSocket::handle('fread', $args);
5858
}
59+
function fgets()
60+
{
61+
$args = func_get_args();
62+
return MockSocket::handle('fgets', $args);
63+
}
5964
function stream_context_create()
6065
{
6166
$args = func_get_args();

tests/scripts/client.connect-authed.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
"return": 248
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
5150
},
5251
{
5352
"function": "fwrite",

tests/scripts/client.connect-context.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
"return": 199
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
5150
},
5251
{
5352
"function": "fwrite",

tests/scripts/client.connect-extended.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
"return": 224
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
5150
},
5251
{
5352
"function": "fwrite",

tests/scripts/client.connect-headers.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@
4040
"return": 255
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"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"
50+
},
51+
{
52+
"function": "fgets",
53+
"params": [
54+
"@mock-stream",
55+
1024
56+
],
57+
"return-op": "key-respond",
58+
"return": "Next234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\r\n\r\n"
5159
},
5260
{
5361
"function": "fwrite",

tests/scripts/client.connect-invalid-key.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@
3939
"return": 199
4040
},
4141
{
42-
"function": "stream_get_line",
42+
"function": "fgets",
4343
"params": [
4444
"@mock-stream",
45-
1024,
46-
"\r\n\r\n"
45+
1024
4746
],
48-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: BAD"
47+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: BAD\r\n\r\n"
4948
}
5049
]

tests/scripts/client.connect-invalid-upgrade.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@
3939
"return": 199
4040
},
4141
{
42-
"function": "stream_get_line",
42+
"function": "fgets",
4343
"params": [
4444
"@mock-stream",
45-
1024,
46-
"\r\n\r\n"
45+
1024
4746
],
48-
"return": "Invalid upgrade response"
47+
"return": "Invalid upgrade response\r\n\r\n"
4948
}
5049
]

tests/scripts/client.connect-persistent.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@
5454
"return": 248
5555
},
5656
{
57-
"function": "stream_get_line",
57+
"function": "fgets",
5858
"params": [
5959
"@mock-stream",
60-
1024,
61-
"\r\n\r\n"
60+
1024
6261
],
6362
"return-op": "key-respond",
64-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
63+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
6564
},
6665
{
6766
"function": "fwrite",

tests/scripts/client.connect-timeout.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
"return": 199
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
5150
},
5251
{
5352
"function": "fwrite",

tests/scripts/client.connect.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
"return": 199
4141
},
4242
{
43-
"function": "stream_get_line",
43+
"function": "fgets",
4444
"params": [
4545
"@mock-stream",
46-
1024,
47-
"\r\n\r\n"
46+
1024
4847
],
4948
"return-op": "key-respond",
50-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
49+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
5150
},
5251
{
5352
"function": "fwrite",

tests/scripts/client.reconnect.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@
5454
"return": 199
5555
},
5656
{
57-
"function": "stream_get_line",
57+
"function": "fgets",
5858
"params": [
5959
"@mock-stream",
60-
1024,
61-
"\r\n\r\n"
60+
1024
6261
],
6362
"return-op": "key-respond",
64-
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}"
63+
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
6564
},
6665
{
6766
"function": "fread",

0 commit comments

Comments
 (0)