Skip to content

Commit

Permalink
Merge pull request #4 from spiral/hotfix/connection-modification
Browse files Browse the repository at this point in the history
Fix lowercase formatting in unix socket
  • Loading branch information
SerafimArts authored Mar 11, 2021
2 parents 2ddd4d0 + 9b49b3d commit 3258892
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
20 changes: 13 additions & 7 deletions src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
11 changes: 9 additions & 2 deletions src/SocketRelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
* <code>
* $relay = new SocketRelay("localhost", 7000);
* $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET);
* </code>
*
* @param string $address Localhost, ip address or hostname.
* @param int|null $port Ignored for UNIX sockets.
Expand All @@ -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'",
Expand All @@ -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'",
Expand Down
14 changes: 9 additions & 5 deletions tests/Goridge/StaticFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,32 @@ 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'],
['unix://rpc.sock'],
['unix:///tmp/rpc.sock'],
['tcp://localhost:abc'],
['pipes://stdin:stdout'],
// in different register
['UnIx:///tmp/RPC.sock'],
['TCP://Domain.com:42'],
['PIPeS://stdIn:stdErr'],
];
}

Expand Down

0 comments on commit 3258892

Please sign in to comment.