Skip to content

Commit a719d94

Browse files
committed
Unify parsing URLs, enforce TCP transport and add default port
1 parent 169c878 commit a719d94

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/Clue/Redis/React/Factory.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ function($error) use ($client) {
5858

5959
public function createServer($address)
6060
{
61-
$parts = parse_url($address);
62-
if ($parts === false || !isset($parts['host']) || !isset($parts['port'])) {
63-
return When::reject(new InvalidArgumentException('Invalid server address given'));
64-
}
61+
$parts = $this->parseUrl($address);
6562

6663
$socket = new ServerSocket($this->loop);
6764
try {
@@ -79,15 +76,38 @@ public function createProtocol()
7976
return ProtocolFactory::create();
8077
}
8178

82-
private function connect($target)
79+
private function parseUrl($target)
8380
{
8481
if ($target === null) {
85-
$target = 'tcp://127.0.0.1:6379';
82+
$target = 'tcp://127.0.0.1';
83+
}
84+
if (strpos($target, '://') === false) {
85+
$target = 'tcp://' . $target;
8686
}
8787

8888
$parts = parse_url($target);
89-
if ($parts === false || !isset($parts['host']) || !isset($parts['port'])) {
90-
return When::reject(new InvalidArgumentException('Invalid target host given'));
89+
if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') {
90+
throw new Exception('Given URL can not be parsed');
91+
}
92+
93+
if (!isset($parts['port'])) {
94+
$parts['port'] = '6379';
95+
}
96+
97+
if ($parts['host'] === 'localhost') {
98+
$parts['host'] = '127.0.0.1';
99+
}
100+
101+
return $parts;
102+
}
103+
104+
private function connect($target)
105+
{
106+
try {
107+
$parts = $this->parseUrl($target);
108+
}
109+
catch (Exception $e) {
110+
return When::reject($e);
91111
}
92112

93113
if ($this->connector === null) {

tests/FactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testClientUnconnectableAddress()
7878

7979
public function testClientInvalidAddress()
8080
{
81-
$promise = $this->factory->createClient('invalid target');
81+
$promise = $this->factory->createClient('http://invalid target');
8282

8383
$this->expectPromiseReject($promise);
8484
}

0 commit comments

Comments
 (0)