diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eb269a..3367af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All Notable changes to `League\Uri\Components` will be documented in this file +## Next + +### Added + +- None + +### Fixed + +- Bug fix Host normalization `League\Uri\Schemes\AbstractUri::formatHost` see [issue #5](https://github.com/thephpleague/uri-parser/issue/5) + +### Deprecated + +- None + +### Removed + +- None + ## 1.0.3 - 2017-03-06 ### Added @@ -10,7 +28,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file ### Fixed -- Bug fix `$_SERVER['SERVER_PORT']` valui with `League\Uri\Schemes\Http::createFromServer` see [#PR1](https://github.com/thephpleague/uri-schemes/pull/1) +- Bug fix `$_SERVER['SERVER_PORT']` value with `League\Uri\Schemes\Http::createFromServer` see [#PR1](https://github.com/thephpleague/uri-schemes/pull/1) ### Deprecated diff --git a/src/AbstractUri.php b/src/AbstractUri.php index 8aa3d2c..8c8d0f9 100644 --- a/src/AbstractUri.php +++ b/src/AbstractUri.php @@ -330,22 +330,30 @@ protected static function urlEncodeMatch(array $matches): string /** * Format the Host component * - * @param string|null $component + * - convert each registered name label to its IDNA ASCII form only if it contains none valid label characters + * - convert each label to its lower case representation for normalization + * + * @param string|null $host * * @return string|null */ - protected function formatHost($component) + protected function formatHost($host) { - if ('' == $component) { - return $component; + if ('' == $host || false !== strpos($host, ']')) { + return $host; } - $component = strtolower($component); - if (false !== strpos($component, ']')) { - return $component; + $component = ''; + $valid_ascii_label_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'; + foreach (explode('.', strtolower($host)) as $label) { + if (strlen($label) !== strspn($label, $valid_ascii_label_characters)) { + $label = (string) idn_to_ascii($label, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46); + } + + $component .= $label.'.'; } - return implode('.', array_map('idn_to_ascii', explode('.', $component))); + return substr($component, 0, -1); } /**