Skip to content

Commit

Permalink
Adding IPv6 compression to BaseUri
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 19, 2024
1 parent c8ff642 commit bcef26d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use League\Uri\Exceptions\MissingFeature;
use League\Uri\Idna\Converter;
use League\Uri\IPv4\Converter as IPv4Converter;
use League\Uri\IPv6\Converter as IPv6Converter;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
Expand Down Expand Up @@ -556,6 +557,10 @@ final protected static function formatHost(Psr7UriInterface|UriInterface $uri):
$converted = null;
}

if (false === filter_var($converted, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$converted = IPv6Converter::compress($host);
}

return match (true) {
null !== $converted => $uri->withHost($converted),
'' === $host,
Expand Down
4 changes: 4 additions & 0 deletions BaseUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ public static function getOriginProvider(): array
'uri' => Uri::new('https://0:443/'),
'expectedOrigin' => 'https://0.0.0.0',
],
'compressed ipv6' => [
'uri' => 'https://[1050:0000:0000:0000:0005:0000:300c:326b]:443/',
'expectedOrigin' => 'https://[1050::5:0:300c:326b]',
],
];
}

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All Notable changes to `League\Uri` will be documented in this file

- Adding `SensitiveParameter` attribute in the `Uri` and the `BaseUri` class.
- Improve PSR-7 `Http` class implementation.
- `BaseUri::from` will compress the IPv6 host to its compressed form if possible.

### Deprecated

Expand Down

0 comments on commit bcef26d

Please sign in to comment.