Skip to content

Commit

Permalink
Fix parsing Ipv6 (#403)
Browse files Browse the repository at this point in the history
* Fixing IPv6

* Add test for IPv6 Uris

* Bugfixing

Co-authored-by: Alexander Berl <a.berl@outlook.com>
  • Loading branch information
Nyholm and albe committed Mar 21, 2021
1 parent aa50bd7 commit 38dde7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public function __construct($uri = '')
*/
private static function parse($url)
{
// If IPv6
$prefix = '';
if (preg_match('%^(.*://\[[0-9:a-f]+\])(.*?)$%', $url, $matches)) {
$prefix = $matches[1];
$url = $matches[2];
}

$encodedUrl = preg_replace_callback(
'%[^:/@?&=#]+%usD',
static function ($matches) {
Expand All @@ -102,7 +109,7 @@ static function ($matches) {
$url
);

$result = parse_url($encodedUrl);
$result = parse_url($prefix.$encodedUrl);

if ($result === false) {
return false;
Expand Down
11 changes: 11 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,17 @@ public function testInternationalizedDomainName()
$uri = new Uri('https://яндекAс.рф');
self::assertSame('яндекaс.рф', $uri->getHost());
}

public function testIPv6Host()
{
$uri = new Uri('https://[2a00:f48:1008::212:183:10]');
self::assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());

$uri = new Uri('http://[2a00:f48:1008::212:183:10]:56?foo=bar');
self::assertSame('[2a00:f48:1008::212:183:10]', $uri->getHost());
self::assertSame(56, $uri->getPort());
self::assertSame('foo=bar', $uri->getQuery());
}
}

class ExtendedUriTest extends Uri
Expand Down

0 comments on commit 38dde7b

Please sign in to comment.