File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -54,12 +54,24 @@ protected function resolveAddress($address)
5454 if (strpos ($ address , ':// ' ) === false ) {
5555 $ address = 'udp:// ' . $ address ;
5656 }
57+
58+ // parse_url() does not accept null ports (random port assignment) => manually remove
59+ $ nullport = false ;
60+ if (substr ($ address , -2 ) === ':0 ' ) {
61+ $ address = substr ($ address , 0 , -2 );
62+ $ nullport = true ;
63+ }
64+
5765 $ parts = parse_url ($ address );
5866
5967 if (!$ parts || !isset ($ parts ['host ' ])) {
6068 return When::resolve ($ address );
6169 }
6270
71+ if ($ nullport ) {
72+ $ parts ['port ' ] = 0 ;
73+ }
74+
6375 // remove square brackets for IPv6 addresses
6476 $ host = trim ($ parts ['host ' ], '[] ' );
6577
Original file line number Diff line number Diff line change @@ -53,4 +53,16 @@ public function testCreateServer()
5353
5454 $ capturedServer ->close ();
5555 }
56+
57+ public function testCreateServerRandomPort ()
58+ {
59+ $ promise = $ this ->factory ->createServer ('127.0.0.1:0 ' );
60+
61+ $ capturedServer = $ this ->getValueFromResolvedPromise ($ promise );
62+ $ this ->assertInstanceOf ('Datagram\Socket ' , $ capturedServer );
63+
64+ $ this ->assertNotEquals ('127.0.0.1:0 ' , $ capturedServer ->getLocalAddress ());
65+
66+ $ capturedServer ->close ();
67+ }
5668}
You can’t perform that action at this time.
0 commit comments