Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lowercase formatting in unix socket #4

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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