From 9b49b3d80f21b3fb6ab27846ae58ca3b4fffe3c2 Mon Sep 17 00:00:00 2001 From: SerafimArts Date: Thu, 11 Mar 2021 16:58:17 +0300 Subject: [PATCH] Fix lowercase formatting in unix socket (and little cs improvements). --- src/Relay.php | 20 +++++++++++++------- src/SocketRelay.php | 11 +++++++++-- tests/Goridge/StaticFactoryTest.php | 14 +++++++++----- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Relay.php b/src/Relay.php index 2243311..c13ce80 100644 --- a/src/Relay.php +++ b/src/Relay.php @@ -37,19 +37,25 @@ public static function create(string $connection): RelayInterface return new StreamRelay(STDIN, STDOUT); } - if (!preg_match(self::CONNECTION_EXP, strtolower($connection), $match)) { + if (!preg_match(self::CONNECTION_EXP, $connection, $match)) { throw new Exception\RelayFactoryException('unsupported connection format'); } - switch ($match['protocol']) { + $protocol = strtolower($match['protocol']); + + switch ($protocol) { case self::TCP_SOCKET: //fall through case self::UNIX_SOCKET: - return new SocketRelay( - $match['arg1'], - isset($match['arg2']) ? (int)$match['arg2'] : null, - $match['protocol'] === self::TCP_SOCKET ? SocketRelay::SOCK_TCP : SocketRelay::SOCK_UNIX - ); + $socketType = $protocol === self::TCP_SOCKET + ? SocketRelay::SOCK_TCP + : SocketRelay::SOCK_UNIX; + + $port = isset($match['arg2']) + ? (int)$match['arg2'] + : null; + + return new SocketRelay($match['arg1'], $port, $socketType); case self::PIPES: if (!isset($match['arg2'])) { diff --git a/src/SocketRelay.php b/src/SocketRelay.php index 050c641..0c95c83 100644 --- a/src/SocketRelay.php +++ b/src/SocketRelay.php @@ -39,8 +39,10 @@ class SocketRelay extends Relay implements StringableRelayInterface /** * Example: - * $relay = new SocketRelay("localhost", 7000); - * $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET); + * + * $relay = new SocketRelay("localhost", 7000); + * $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET); + * * * @param string $address Localhost, ip address or hostname. * @param int|null $port Ignored for UNIX sockets. @@ -56,6 +58,9 @@ public function __construct(string $address, ?int $port = null, int $type = self switch ($type) { case self::SOCK_TCP: + // TCP address should always be in lowercase + $address = strtolower($address); + if ($port === null) { throw new Exception\InvalidArgumentException(sprintf( "no port given for TPC socket on '%s'", @@ -70,9 +75,11 @@ public function __construct(string $address, ?int $port = null, int $type = self )); } break; + case self::SOCK_UNIX: $port = null; break; + default: throw new Exception\InvalidArgumentException(sprintf( "undefined connection type %s on '%s'", diff --git a/tests/Goridge/StaticFactoryTest.php b/tests/Goridge/StaticFactoryTest.php index 7ccb6be..9cdb1cf 100644 --- a/tests/Goridge/StaticFactoryTest.php +++ b/tests/Goridge/StaticFactoryTest.php @@ -46,21 +46,21 @@ public function testFormat(string $connection, bool $expectedException = false): public function formatProvider(): iterable { return [ - //format invalid + // format invalid ['tcp:localhost:', true], ['tcp:/localhost:', true], ['tcp//localhost:', true], ['tcp//localhost', true], - //unknown provider + // unknown provider ['test://localhost', true], - //pipes require 2 args + // pipes require 2 args ['pipes://localhost:', true], ['pipes://localhost', true], - //invalid resources + // invalid resources ['pipes://stdin:test', true], ['pipes://test:stdout', true], ['pipes://test:test', true], - //valid format + // valid format ['tcp://localhost'], ['tcp://localhost:123'], ['unix://localhost:123'], @@ -68,6 +68,10 @@ public function formatProvider(): iterable ['unix:///tmp/rpc.sock'], ['tcp://localhost:abc'], ['pipes://stdin:stdout'], + // in different register + ['UnIx:///tmp/RPC.sock'], + ['TCP://Domain.com:42'], + ['PIPeS://stdIn:stdErr'], ]; }