Skip to content

Commit 8fbee0e

Browse files
authored
Merge pull request #656 from php-enqueue/redis-dsn-and-password
Redis dsn and password fixes
2 parents 69c5c80 + 2ebf48d commit 8fbee0e

9 files changed

Lines changed: 98 additions & 17 deletions

File tree

‎pkg/dsn/Dsn.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@ public static function parse(string $dsn): array
226226
$schemeExtensions = array_values($schemeParts);
227227

228228
$user = parse_url($dsn, PHP_URL_USER) ?: null;
229+
if (is_string($user)) {
230+
$user = rawurldecode($user);
231+
}
232+
229233
$password = parse_url($dsn, PHP_URL_PASS) ?: null;
234+
if (is_string($password)) {
235+
$password = rawurldecode($password);
236+
}
230237

231238
$path = parse_url($dsn, PHP_URL_PATH) ?: null;
232239
if ($path) {

‎pkg/dsn/Tests/DsnTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,36 @@ public function testShouldParseExpectedNumberOfMultipleDsns()
382382
$this->assertCount(3, $dsns);
383383
}
384384

385+
public function testShouldParseDsnWithOnlyUser()
386+
{
387+
$dsn = Dsn::parseFirst('foo://user@host');
388+
389+
$this->assertSame('user', $dsn->getUser());
390+
$this->assertNull($dsn->getPassword());
391+
$this->assertSame('foo', $dsn->getScheme());
392+
$this->assertSame('host', $dsn->getHost());
393+
}
394+
395+
public function testShouldUrlEncodeUser()
396+
{
397+
$dsn = Dsn::parseFirst('foo://us%3Aer@host');
398+
399+
$this->assertSame('us:er', $dsn->getUser());
400+
$this->assertNull($dsn->getPassword());
401+
$this->assertSame('foo', $dsn->getScheme());
402+
$this->assertSame('host', $dsn->getHost());
403+
}
404+
405+
public function testShouldUrlEncodePassword()
406+
{
407+
$dsn = Dsn::parseFirst('foo://user:pass%3Aword@host');
408+
409+
$this->assertSame('user', $dsn->getUser());
410+
$this->assertSame('pass:word', $dsn->getPassword());
411+
$this->assertSame('foo', $dsn->getScheme());
412+
$this->assertSame('host', $dsn->getHost());
413+
}
414+
385415
public static function provideSchemes()
386416
{
387417
yield [':', '', '', []];

‎pkg/enqueue/Client/Resources.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function getKnownDrivers(): array
8181
'packages' => ['enqueue/enqueue', 'enqueue/gps'],
8282
];
8383
$map[] = [
84-
'schemes' => ['redis'],
84+
'schemes' => ['redis', 'rediss'],
8585
'driverClass' => RedisDriver::class,
8686
'requiredSchemeExtensions' => [],
8787
'packages' => ['enqueue/enqueue', 'enqueue/redis'],

‎pkg/enqueue/Resources.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static function getKnownConnections(): array
143143
'package' => 'enqueue/rdkafka',
144144
];
145145
$map[RedisConnectionFactory::class] = [
146-
'schemes' => ['redis'],
146+
'schemes' => ['redis', 'rediss'],
147147
'supportedSchemeExtensions' => ['predis', 'phpredis'],
148148
'package' => 'enqueue/redis',
149149
];

‎pkg/enqueue/Tests/ResourcesTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testShouldGetAvailableConnectionsInExpectedFormat()
3333

3434
$connectionInfo = $availableConnections[RedisConnectionFactory::class];
3535
$this->assertArrayHasKey('schemes', $connectionInfo);
36-
$this->assertSame(['redis'], $connectionInfo['schemes']);
36+
$this->assertSame(['redis', 'rediss'], $connectionInfo['schemes']);
3737

3838
$this->assertArrayHasKey('supportedSchemeExtensions', $connectionInfo);
3939
$this->assertSame(['predis', 'phpredis'], $connectionInfo['supportedSchemeExtensions']);
@@ -51,7 +51,7 @@ public function testShouldGetKnownConnectionsInExpectedFormat()
5151

5252
$connectionInfo = $availableConnections[RedisConnectionFactory::class];
5353
$this->assertArrayHasKey('schemes', $connectionInfo);
54-
$this->assertSame(['redis'], $connectionInfo['schemes']);
54+
$this->assertSame(['redis', 'rediss'], $connectionInfo['schemes']);
5555

5656
$this->assertArrayHasKey('supportedSchemeExtensions', $connectionInfo);
5757
$this->assertSame(['predis', 'phpredis'], $connectionInfo['supportedSchemeExtensions']);

‎pkg/redis/PRedis.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function eval(string $script, array $keys = [], array $args = [])
5959
// mixed eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
6060
return call_user_func_array([$this->redis, 'eval'], array_merge([$script, count($keys)], $keys, $args));
6161
} catch (PRedisServerException $e) {
62-
throw new ServerException('eval command has failed', null, $e);
62+
throw new ServerException('eval command has failed', 0, $e);
6363
}
6464
}
6565

@@ -68,7 +68,7 @@ public function zadd(string $key, string $value, float $score): int
6868
try {
6969
return $this->redis->zadd($key, [$value => $score]);
7070
} catch (PRedisServerException $e) {
71-
throw new ServerException('zadd command has failed', null, $e);
71+
throw new ServerException('zadd command has failed', 0, $e);
7272
}
7373
}
7474

@@ -77,7 +77,7 @@ public function zrem(string $key, string $value): int
7777
try {
7878
return $this->redis->zrem($key, [$value]);
7979
} catch (PRedisServerException $e) {
80-
throw new ServerException('zrem command has failed', null, $e);
80+
throw new ServerException('zrem command has failed', 0, $e);
8181
}
8282
}
8383

@@ -86,7 +86,7 @@ public function lpush(string $key, string $value): int
8686
try {
8787
return $this->redis->lpush($key, [$value]);
8888
} catch (PRedisServerException $e) {
89-
throw new ServerException('lpush command has failed', null, $e);
89+
throw new ServerException('lpush command has failed', 0, $e);
9090
}
9191
}
9292

@@ -99,7 +99,7 @@ public function brpop(array $keys, int $timeout): ?RedisResult
9999

100100
return null;
101101
} catch (PRedisServerException $e) {
102-
throw new ServerException('brpop command has failed', null, $e);
102+
throw new ServerException('brpop command has failed', 0, $e);
103103
}
104104
}
105105

@@ -112,7 +112,7 @@ public function rpop(string $key): ?RedisResult
112112

113113
return null;
114114
} catch (PRedisServerException $e) {
115-
throw new ServerException('rpop command has failed', null, $e);
115+
throw new ServerException('rpop command has failed', 0, $e);
116116
}
117117
}
118118

‎pkg/redis/PhpRedis.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function eval(string $script, array $keys = [], array $args = [])
3131
try {
3232
return $this->redis->eval($script, array_merge($keys, $args), count($keys));
3333
} catch (\RedisException $e) {
34-
throw new ServerException('eval command has failed', null, $e);
34+
throw new ServerException('eval command has failed', 0, $e);
3535
}
3636
}
3737

@@ -40,7 +40,7 @@ public function zadd(string $key, string $value, float $score): int
4040
try {
4141
return $this->redis->zAdd($key, $score, $value);
4242
} catch (\RedisException $e) {
43-
throw new ServerException('zadd command has failed', null, $e);
43+
throw new ServerException('zadd command has failed', 0, $e);
4444
}
4545
}
4646

@@ -49,7 +49,7 @@ public function zrem(string $key, string $value): int
4949
try {
5050
return $this->redis->zRem($key, $value);
5151
} catch (\RedisException $e) {
52-
throw new ServerException('zrem command has failed', null, $e);
52+
throw new ServerException('zrem command has failed', 0, $e);
5353
}
5454
}
5555

@@ -58,7 +58,7 @@ public function lpush(string $key, string $value): int
5858
try {
5959
return $this->redis->lPush($key, $value);
6060
} catch (\RedisException $e) {
61-
throw new ServerException('lpush command has failed', null, $e);
61+
throw new ServerException('lpush command has failed', 0, $e);
6262
}
6363
}
6464

@@ -71,7 +71,7 @@ public function brpop(array $keys, int $timeout): ?RedisResult
7171

7272
return null;
7373
} catch (\RedisException $e) {
74-
throw new ServerException('brpop command has failed', null, $e);
74+
throw new ServerException('brpop command has failed', 0, $e);
7575
}
7676
}
7777

@@ -84,7 +84,7 @@ public function rpop(string $key): ?RedisResult
8484

8585
return null;
8686
} catch (\RedisException $e) {
87-
throw new ServerException('rpop command has failed', null, $e);
87+
throw new ServerException('rpop command has failed', 0, $e);
8888
}
8989
}
9090

‎pkg/redis/RedisConnectionFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function parseDsn(string $dsn): array
135135
'port' => $dsn->getPort(),
136136
'path' => $dsn->getPath(),
137137
'database' => $database,
138-
'password' => $dsn->getPassword(),
138+
'password' => $dsn->getPassword() ?: $dsn->getUser() ?: $dsn->getString('password'),
139139
'async' => $dsn->getBool('async'),
140140
'persistent' => $dsn->getBool('persistent'),
141141
'timeout' => $dsn->getFloat('timeout'),

‎pkg/redis/Tests/RedisConnectionFactoryConfigTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,50 @@ public static function provideConfigs()
315315
],
316316
];
317317

318+
// password as user
319+
yield [
320+
'redis://asdfqwer1234asdf@foo',
321+
[
322+
'host' => 'foo',
323+
'scheme' => 'redis',
324+
'port' => 6379,
325+
'timeout' => 5.,
326+
'database' => null,
327+
'password' => 'asdfqwer1234asdf',
328+
'scheme_extensions' => [],
329+
'path' => null,
330+
'async' => false,
331+
'persistent' => false,
332+
'lazy' => true,
333+
'read_write_timeout' => null,
334+
'predis_options' => null,
335+
'ssl' => null,
336+
'redelivery_delay' => 300,
337+
],
338+
];
339+
340+
// password as query parameter
341+
yield [
342+
'redis:?password=asdfqwer1234asdf',
343+
[
344+
'host' => '127.0.0.1',
345+
'scheme' => 'redis',
346+
'port' => 6379,
347+
'timeout' => 5.,
348+
'database' => null,
349+
'password' => 'asdfqwer1234asdf',
350+
'scheme_extensions' => [],
351+
'path' => null,
352+
'async' => false,
353+
'persistent' => false,
354+
'lazy' => true,
355+
'read_write_timeout' => null,
356+
'predis_options' => null,
357+
'ssl' => null,
358+
'redelivery_delay' => 300,
359+
],
360+
];
361+
318362
// from predis doc
319363

320364
yield [

0 commit comments

Comments
 (0)