Skip to content

Commit 41bda5e

Browse files
committed
Support ?password and ?db URI query parameters
1 parent 4e35649 commit 41bda5e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ authentication mechanism does not employ a username, so you can pass the
137137
password "secret" as part of the URI like this:
138138

139139
```php
140+
// both forms are equivalent
140141
$factory->createClient('redis://ignored:secret@localhost');
142+
$factory->createClient('redis://localhost?password=secret');
141143
```
142144

143145
> Legacy notice: The `redis://` scheme is defined and preferred as of `v1.2.0`.
@@ -149,7 +151,9 @@ $factory->createClient('redis://ignored:secret@localhost');
149151
You can optionally include a path that will be used to select (SELECT command) the right database:
150152

151153
```php
154+
// both forms are equivalent
152155
$factory->createClient('redis://localhost/2');
156+
$factory->createClient('redis://localhost?db=2');
153157
```
154158

155159
You can use the [standard](https://www.iana.org/assignments/uri-schemes/prov/rediss)

src/Factory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ private function parseUrl($target)
141141
$parts['host'] = 'tls://' . $parts['host'];
142142
}
143143

144+
if (isset($parts['query'])) {
145+
$args = array();
146+
parse_str($parts['query'], $args);
147+
148+
if (isset($args['password'])) {
149+
$parts['auth'] = $args['password'];
150+
}
151+
152+
if (isset($args['db'])) {
153+
$parts['db'] = $args['db'];
154+
}
155+
}
156+
144157
unset($parts['scheme'], $parts['user'], $parts['pass'], $parts['path']);
145158

146159
return $parts;

tests/FactoryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public function testWillWriteSelectCommandIfTargetContainsPath()
6868
$this->factory->createClient('redis://127.0.0.1/demo');
6969
}
7070

71+
public function testWillWriteSelectCommandIfTargetContainsDbQueryParameter()
72+
{
73+
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
74+
$stream->expects($this->once())->method('write')->with("*2\r\n$6\r\nselect\r\n$1\r\n4\r\n");
75+
76+
$this->connector->expects($this->once())->method('connect')->willReturn(Promise\resolve($stream));
77+
$this->factory->createClient('redis://127.0.0.1?db=4');
78+
}
79+
7180
public function testWillWriteAuthCommandIfRedisUriContainsUserInfo()
7281
{
7382
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
@@ -77,6 +86,15 @@ public function testWillWriteAuthCommandIfRedisUriContainsUserInfo()
7786
$this->factory->createClient('redis://hello:[email protected]');
7887
}
7988

89+
public function testWillWriteAuthCommandIfTargetContainsPasswordQueryParameter()
90+
{
91+
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
92+
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$6\r\nsecret\r\n");
93+
94+
$this->connector->expects($this->once())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
95+
$this->factory->createClient('redis://example.com?password=secret');
96+
}
97+
8098
public function testWillWriteAuthCommandIfRedissUriContainsUserInfo()
8199
{
82100
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();

0 commit comments

Comments
 (0)