diff --git a/lib/Client.php b/lib/Client.php index 8cefaaa..89e7810 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -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)) { diff --git a/tests/mock/mock-socket.php b/tests/mock/mock-socket.php index 1d6889a..a038933 100644 --- a/tests/mock/mock-socket.php +++ b/tests/mock/mock-socket.php @@ -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(); diff --git a/tests/scripts/client.connect-authed.json b/tests/scripts/client.connect-authed.json index 6f75b6b..b89ca53 100644 --- a/tests/scripts/client.connect-authed.json +++ b/tests/scripts/client.connect-authed.json @@ -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", diff --git a/tests/scripts/client.connect-context.json b/tests/scripts/client.connect-context.json index 0fcc279..02ad443 100644 --- a/tests/scripts/client.connect-context.json +++ b/tests/scripts/client.connect-context.json @@ -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", diff --git a/tests/scripts/client.connect-extended.json b/tests/scripts/client.connect-extended.json index 4659e3e..7ae225b 100644 --- a/tests/scripts/client.connect-extended.json +++ b/tests/scripts/client.connect-extended.json @@ -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", diff --git a/tests/scripts/client.connect-headers.json b/tests/scripts/client.connect-headers.json index 69808ce..5657c43 100644 --- a/tests/scripts/client.connect-headers.json +++ b/tests/scripts/client.connect-headers.json @@ -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", diff --git a/tests/scripts/client.connect-invalid-key.json b/tests/scripts/client.connect-invalid-key.json index 6472317..4f71a32 100644 --- a/tests/scripts/client.connect-invalid-key.json +++ b/tests/scripts/client.connect-invalid-key.json @@ -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" } ] \ No newline at end of file diff --git a/tests/scripts/client.connect-invalid-upgrade.json b/tests/scripts/client.connect-invalid-upgrade.json index 28cbc7e..a2b8455 100644 --- a/tests/scripts/client.connect-invalid-upgrade.json +++ b/tests/scripts/client.connect-invalid-upgrade.json @@ -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" } ] \ No newline at end of file diff --git a/tests/scripts/client.connect-persistent.json b/tests/scripts/client.connect-persistent.json index d95af01..61a7af9 100644 --- a/tests/scripts/client.connect-persistent.json +++ b/tests/scripts/client.connect-persistent.json @@ -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", diff --git a/tests/scripts/client.connect-timeout.json b/tests/scripts/client.connect-timeout.json index c1ae6b5..9a2c38a 100644 --- a/tests/scripts/client.connect-timeout.json +++ b/tests/scripts/client.connect-timeout.json @@ -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", diff --git a/tests/scripts/client.connect.json b/tests/scripts/client.connect.json index eb7ecd2..1cfd487 100644 --- a/tests/scripts/client.connect.json +++ b/tests/scripts/client.connect.json @@ -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", diff --git a/tests/scripts/client.reconnect.json b/tests/scripts/client.reconnect.json index 9d505fe..9344d55 100644 --- a/tests/scripts/client.reconnect.json +++ b/tests/scripts/client.reconnect.json @@ -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",