Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from thephpleague/bugfix/host-formatting
Browse files Browse the repository at this point in the history
Improve Host formatting
  • Loading branch information
nyamsprod committed Apr 18, 2017
2 parents 1be3f88 + 741d028 commit 2f561b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
24 changes: 16 additions & 8 deletions src/AbstractUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 2f561b6

Please sign in to comment.