Skip to content

Commit d05dbaa

Browse files
authored
Merge pull request #158 from Textalk/v1.5-master
Handle read error during handshake
2 parents 4e0dba4 + ed8996e commit d05dbaa

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

docs/Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
> PHP version `^7.2|^8.0`
88
9+
### `1.5.8`
10+
11+
* Handle read error during handshake (@sirn-se)
12+
913
### `1.5.7`
1014

1115
* Large header block fix (@sirn-se)

lib/Client.php

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ function ($key, $value) {
180180
$response = '';
181181
do {
182182
$buffer = fgets($this->socket, 1024);
183+
if ($buffer === false) {
184+
$meta = stream_get_meta_data($this->socket);
185+
$message = 'Client handshake error';
186+
$this->logger->error($message, $meta);
187+
throw new ConnectionException($message);
188+
}
183189
$response .= $buffer;
184190
} while (substr_count($response, "\r\n\r\n") == 0);
185191

tests/ClientTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ public function testBrokenRead(): void
352352
$client->receive();
353353
}
354354

355+
public function testHandshakeError(): void
356+
{
357+
MockSocket::initialize('client.connect-handshake-error', $this);
358+
$client = new Client('ws://localhost:8000/my/mock/path');
359+
$this->expectException('WebSocket\ConnectionException');
360+
$this->expectExceptionCode(0);
361+
$this->expectExceptionMessage('Client handshake error');
362+
$client->send('Connect');
363+
}
364+
355365
public function testReadTimeout(): void
356366
{
357367
MockSocket::initialize('client.connect', $this);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[
2+
{
3+
"function": "stream_context_create",
4+
"params": [],
5+
"return": "@mock-stream-context"
6+
},
7+
{
8+
"function": "stream_socket_client",
9+
"params": [
10+
"tcp:\/\/localhost:8000",
11+
null,
12+
null,
13+
5,
14+
4,
15+
"@mock-stream-context"
16+
],
17+
"return": "@mock-stream"
18+
},
19+
{
20+
"function": "get_resource_type",
21+
"params": [
22+
"@mock-stream"
23+
],
24+
"return": "stream"
25+
},
26+
{
27+
"function": "stream_set_timeout",
28+
"params": [
29+
"@mock-stream",
30+
5
31+
],
32+
"return": true
33+
},
34+
{
35+
"function": "fwrite",
36+
"params": [
37+
"@mock-stream"
38+
],
39+
"return-op": "key-save",
40+
"return": 199
41+
},
42+
{
43+
"function": "fgets",
44+
"params": [
45+
"@mock-stream",
46+
1024
47+
],
48+
"return": false
49+
},
50+
{
51+
"function": "stream_get_meta_data",
52+
"params": [
53+
"@mock-stream"
54+
],
55+
"return": {
56+
"timed_out": true,
57+
"blocked": true,
58+
"eof": false,
59+
"stream_type": "tcp_socket\/ssl",
60+
"mode": "r+",
61+
"unread_bytes": 0,
62+
"seekable": false
63+
}
64+
}
65+
]

0 commit comments

Comments
 (0)