Skip to content

Commit

Permalink
Merge pull request #166 from Textalk/client-http-path-fix
Browse files Browse the repository at this point in the history
Fix client path for http request
  • Loading branch information
sirn-se authored Nov 2, 2022
2 parents fc1390a + 30fa741 commit 615c8c0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
11 changes: 8 additions & 3 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,15 @@ protected function connect(): void
->withScheme($this->socket_uri->getScheme() == 'wss' ? 'ssl' : 'tcp')
->withPort($this->socket_uri->getPort());

// Path must be absolute
$http_path = $this->socket_uri->getPath();
if ($http_path === '' || $http_path[0] !== '/') {
$http_path = "/{$http_path}";
}

$http_uri = (new Uri())
->withPath($this->socket_uri->getPath())
->withQuery($this->socket_uri->getQuery())
->withFragment($this->socket_uri->getFragment());
->withPath($http_path)
->withQuery($this->socket_uri->getQuery());

// Set the stream context options if they're already set in the config
if (isset($this->options['context'])) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ public function testClienExtendedUrl(): void
$this->assertTrue(MockSocket::isEmpty());
}

public function testClientNoPath(): void
{
MockSocket::initialize('client.connect-root', $this);
$client = new Client('ws://localhost:8000');
$client->send('Connect');
$this->assertTrue(MockSocket::isEmpty());
}

public function testClientRelativePath(): void
{
MockSocket::initialize('client.connect', $this);
$uri = new Uri('ws://localhost:8000');
$uri = $uri->withPath('my/mock/path');
$client = new Client($uri);
$client->send('Connect');
$this->assertTrue(MockSocket::isEmpty());
}

public function testClientWithTimeout(): void
{
MockSocket::initialize('client.connect-timeout', $this);
Expand Down
5 changes: 4 additions & 1 deletion tests/mock/MockSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static function handle($function, $params = [])
}
self::$asserter->assertEquals($current['function'], $function);
foreach ($current['params'] as $index => $param) {
if (isset($current['input-op'])) {
$param = self::op($current['input-op'], $params, $param);
}
self::$asserter->assertEquals($param, $params[$index], json_encode([$current, $params]));
}
if (isset($current['error'])) {
Expand Down Expand Up @@ -67,7 +70,7 @@ private static function op($op, $params, $data)
case 'key-save':
preg_match('#Sec-WebSocket-Key:\s(.*)$#mUi', $params[1], $matches);
self::$stored['sec-websocket-key'] = trim($matches[1]);
return $data;
return str_replace('{key}', self::$stored['sec-websocket-key'], $data);
case 'key-respond':
$key = self::$stored['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
$encoded = base64_encode(pack('H*', sha1($key)));
Expand Down
5 changes: 3 additions & 2 deletions tests/scripts/client.connect-extended.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
{
"function": "fwrite",
"params": [
"@mock-stream"
"@mock-stream",
"GET /my/mock/path?my_query=yes HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: websocket-client-php\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n"
],
"return-op": "key-save",
"input-op": "key-save",
"return": 224
},
{
Expand Down
59 changes: 59 additions & 0 deletions tests/scripts/client.connect-root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
{
"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",
"GET / HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: websocket-client-php\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n"
],
"input-op": "key-save",
"return": 224
},
{
"function": "fgets",
"params": [
"@mock-stream",
1024
],
"return-op": "key-respond",
"return": "HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {key}\r\n\r\n"
},
{
"function": "fwrite",
"params": [
"@mock-stream"
],
"return": 13
}
]
6 changes: 4 additions & 2 deletions tests/scripts/client.connect.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
},
{
"function": "fwrite",
"regexp": true,
"params": [
"@mock-stream"
"@mock-stream",
"GET /my/mock/path HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: websocket-client-php\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n"
],
"return-op": "key-save",
"input-op": "key-save",
"return": 199
},
{
Expand Down

0 comments on commit 615c8c0

Please sign in to comment.