Skip to content

Commit b1ed1ce

Browse files
committed
Assign empty remote address if socket address is unknown (Unix sockets)
1 parent e5cd54c commit b1ed1ce

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Ratchet/Server/IoServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function handleConnect(SocketConnection $conn) {
9797

9898
$conn->decor->resourceId = (int)$conn->stream;
9999

100-
$uri = $conn->getRemoteAddress();
100+
$uri = (string) $conn->getRemoteAddress();
101101
$conn->decor->remoteAddress = trim(
102102
parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST),
103103
'[]'

tests/unit/Server/IoServerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ public function testOnOpen() {
6363
//$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
6464
}
6565

66+
public function testHandleOpenWithoutRemoteAddressAssignsEmptyRemoteAddress() {
67+
$this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('Ratchet\\ConnectionInterface'));
68+
69+
$conn = $this->getMockBuilder('React\\Socket\\ConnectionInterface')->getMock();
70+
$conn->expects($this->once())->method('getRemoteAddress')->willReturn(null);
71+
72+
// assign dynamic property without raising notice on PHP 8.2+
73+
set_error_handler(function () { }, E_DEPRECATED);
74+
$conn->stream = STDOUT;
75+
restore_error_handler();
76+
77+
$this->server->handleConnect($conn);
78+
79+
$this->assertSame('', $conn->decor->remoteAddress);
80+
$this->assertSame((int) STDOUT, $conn->decor->resourceId);
81+
}
82+
6683
/**
6784
* @requires extension sockets
6885
*/

0 commit comments

Comments
 (0)