Skip to content

Commit

Permalink
Merge pull request #158 from Textalk/v1.5-master
Browse files Browse the repository at this point in the history
Handle read error during handshake
  • Loading branch information
sirn-se authored Apr 26, 2022
2 parents 4e0dba4 + ed8996e commit d05dbaa
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

> PHP version `^7.2|^8.0`
### `1.5.8`

* Handle read error during handshake (@sirn-se)

### `1.5.7`

* Large header block fix (@sirn-se)
Expand Down
6 changes: 6 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ function ($key, $value) {
$response = '';
do {
$buffer = fgets($this->socket, 1024);
if ($buffer === false) {
$meta = stream_get_meta_data($this->socket);
$message = 'Client handshake error';
$this->logger->error($message, $meta);
throw new ConnectionException($message);
}
$response .= $buffer;
} while (substr_count($response, "\r\n\r\n") == 0);

Expand Down
10 changes: 10 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public function testBrokenRead(): void
$client->receive();
}

public function testHandshakeError(): void
{
MockSocket::initialize('client.connect-handshake-error', $this);
$client = new Client('ws://localhost:8000/my/mock/path');
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Client handshake error');
$client->send('Connect');
}

public function testReadTimeout(): void
{
MockSocket::initialize('client.connect', $this);
Expand Down
65 changes: 65 additions & 0 deletions tests/scripts/client.connect-handshake-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"function": "stream_context_create",
"params": [],
"return": "@mock-stream-context"
},
{
"function": "stream_socket_client",
"params": [
"tcp:\/\/localhost:8000",
null,
null,
5,
4,
"@mock-stream-context"
],
"return": "@mock-stream"
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "stream_set_timeout",
"params": [
"@mock-stream",
5
],
"return": true
},
{
"function": "fwrite",
"params": [
"@mock-stream"
],
"return-op": "key-save",
"return": 199
},
{
"function": "fgets",
"params": [
"@mock-stream",
1024
],
"return": false
},
{
"function": "stream_get_meta_data",
"params": [
"@mock-stream"
],
"return": {
"timed_out": true,
"blocked": true,
"eof": false,
"stream_type": "tcp_socket\/ssl",
"mode": "r+",
"unread_bytes": 0,
"seekable": false
}
}
]

0 comments on commit d05dbaa

Please sign in to comment.