Skip to content

Commit 4e35649

Browse files
committed
Support rediss:// URI scheme for secure TLS transport
1 parent 4fbd825 commit 4e35649

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ $factory = new Factory($loop, $connector);
107107
#### createClient()
108108

109109
The `createClient($redisUri = null)` method can be used to create a new [`Client`](#client).
110-
It helps with establishing a plain TCP/IP connection to Redis
110+
It helps with establishing a plain TCP/IP or secure TLS connection to Redis
111111
and optionally authenticating (AUTH) and selecting the right database (SELECT).
112112

113113
```php
@@ -121,7 +121,9 @@ $factory->createClient('redis://localhost:6379')->then(
121121
);
122122
```
123123

124-
The `$redisUri` can be given in the form `[redis://][:auth@]host[:port][/db]`.
124+
The `$redisUri` can be given in the
125+
[standard](https://www.iana.org/assignments/uri-schemes/prov/redis) form
126+
`[redis[s]://][:auth@]host[:port][/db]`.
125127
You can omit the URI scheme and port if you're connecting to the default port 6379:
126128

127129
```php
@@ -150,6 +152,13 @@ You can optionally include a path that will be used to select (SELECT command) t
150152
$factory->createClient('redis://localhost/2');
151153
```
152154

155+
You can use the [standard](https://www.iana.org/assignments/uri-schemes/prov/rediss)
156+
`rediss://` URI scheme if you're using a secure TLS proxy in front of Redis:
157+
158+
```php
159+
$factory->createClient('rediss://redis.example.com:6340');
160+
```
161+
153162
[Deprecated] You can omit the complete URI if you want to connect to the default
154163
address `redis://localhost:6379`. This legacy API will be removed in a future
155164
`v2.0.0` version, so it's highly recommended to upgrade to the above API.

src/Factory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function parseUrl($target)
109109
}
110110

111111
$parts = parse_url($target);
112-
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('tcp', 'redis'))) {
112+
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('tcp', 'redis', 'rediss'))) {
113113
throw new InvalidArgumentException('Given URL can not be parsed');
114114
}
115115

@@ -137,6 +137,10 @@ private function parseUrl($target)
137137
$parts['db'] = substr($parts['path'], 1);
138138
}
139139

140+
if ($parts['scheme'] === 'rediss') {
141+
$parts['host'] = 'tls://' . $parts['host'];
142+
}
143+
140144
unset($parts['scheme'], $parts['user'], $parts['pass'], $parts['path']);
141145

142146
return $parts;

tests/FactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@ public function testWillWriteAuthCommandIfRedisUriContainsUserInfo()
7373
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
7474
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
7575

76-
$this->connector->expects($this->once())->method('connect')->willReturn(Promise\resolve($stream));
77-
$this->factory->createClient('redis://hello:[email protected]');
76+
$this->connector->expects($this->once())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
77+
$this->factory->createClient('redis://hello:[email protected]');
78+
}
79+
80+
public function testWillWriteAuthCommandIfRedissUriContainsUserInfo()
81+
{
82+
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
83+
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
84+
85+
$this->connector->expects($this->once())->method('connect')->with('tls://example.com:6379')->willReturn(Promise\resolve($stream));
86+
$this->factory->createClient('rediss://hello:[email protected]');
7887
}
7988

8089
public function testWillWriteAuthCommandIfTcpUriContainsUserInfo()

0 commit comments

Comments
 (0)