Skip to content

Commit

Permalink
Fix RFC3986 normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 27, 2024
1 parent 39bc836 commit faf7859
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
43 changes: 28 additions & 15 deletions docs/uri/7.0/rfc3986.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,6 @@ echo $uri->toNormalizedString(); //displays 'example://a/b/c/%7Bfoo%7D?foo%5B%5D
echo $uri->toDisplayString(); //displays 'example://a/b/c/{foo}?foo[]=bar'
````

File specific representation are added to allow representing Unix and Windows Path.

```php
use League\Uri\Uri;

$uri = Uri::new('file:///c:/windows/My%20Documents%20100%2520/foo.txt');
echo $uri->toWindowsPath(); //display 'c:\windows\My Documents 100%20\foo.txt'

$uri = Uri::new('file:///path%20empty/bar');
echo $uri->toUnixPath(); // display '/path empty/bar'

$uri = Uri::new('file://localhost/etc/fstab');
echo $uri->toRfc8089(); //display 'file:/etc/fstab'
```

HTML specific representation are added to allow adding URI to your HTML/Markdown page.

```php
Expand Down Expand Up @@ -192,6 +177,34 @@ echo $uri->toLinkFieldValue(['rel' => 'stylesheet']);
//display 'https://example.com/my/css/v1.3 ;rel=stylesheet'
```

File specific representation are added to allow representing Unix and Windows Path.

```php
use League\Uri\Uri;

$uri = Uri::new('file:///c:/windows/My%20Documents%20100%2520/foo.txt');
echo $uri->toWindowsPath(); //display 'c:\windows\My Documents 100%20\foo.txt'

$uri = Uri::new('file:///path%20empty/bar');
echo $uri->toUnixPath(); // display '/path empty/bar'

$uri = Uri::new('file://localhost/etc/fstab');
echo $uri->toRfc8089(); //display 'file:/etc/fstab'
```

<p class="message-notice">For any other scheme other than <code>file</code>, the method will return <code>null</code></p>

Last, but not least if you have a `Data URI` you can store the actual data into a file using the `toFileContents` method

```php
$uri = Uri::new('data:text/plain;charset=utf-8;base64,SGVsbG8gd29ybGQh');
$uri->toFileContents(destination: 'my/path/file.txt'); //returns the number of bytes stored

echo file_get_contents('my/path/file.txt'); //will return 'Hello world!'
```

<p class="message-notice">For any other scheme other than <code>data</code>, the method will return <code>null</code></p>

## Accessing URI properties

Let's examine the result of building a URI:
Expand Down
11 changes: 1 addition & 10 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use League\Uri\Exceptions\MissingFeature;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Idna\Converter as IdnaConverter;
use League\Uri\IPv4\Converter as IPv4Converter;
use League\Uri\IPv6\Converter as IPv6Converter;
use League\Uri\UriTemplate\TemplateCanNotBeExpanded;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
Expand Down Expand Up @@ -1013,15 +1012,7 @@ private function normalizeHost(): ?string
return null;
}

$host = $this->host;
$hostIp = IPv4Converter::fromEnvironment()->toDecimal($host);

return IdnaConverter::toUnicode((string)IPv6Converter::compress(match (true) {
'' === $host,
null === $hostIp,
$host === $hostIp => $host,
default => $hostIp,
}))->domain();
return IdnaConverter::toUnicode((string)IPv6Converter::compress($this->host))->domain();
}

/**
Expand Down
8 changes: 0 additions & 8 deletions uri/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,14 +760,6 @@ public static function getOriginProvider(): array
'uri' => Uri::new('blob:https://mozilla.org:443/'),
'expectedOrigin' => 'https://mozilla.org',
],
'normalized ipv4' => [
'uri' => 'https://0:443/',
'expectedOrigin' => 'https://0.0.0.0',
],
'normalized ipv4 with object' => [
'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

0 comments on commit faf7859

Please sign in to comment.