Skip to content

Commit ba711b0

Browse files
committed
[dsn] allow specify special chars in user and password.
fixes php-enqueue/enqueue-dev#652
1 parent 4b2f800 commit ba711b0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Dsn.php

+7
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) {

Tests/DsnTest.php

+30
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 [':', '', '', []];

0 commit comments

Comments
 (0)