Skip to content

Commit

Permalink
Update IDN algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 6, 2017
1 parent 5bf5db0 commit 84a5c0a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
18 changes: 18 additions & 0 deletions 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

## 1.0.1 - 2017-02-06

### Added

- None

### Fixed

- Update idn to ascii algorithm from INTL_IDNA_VARIANT_2003 to INTL_IDNA_VARIANT_UTS46

### Deprecated

- None

### Remove

- None

## 1.0.0 - 2017-01-17

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^1.0",
"phpunit/phpunit" : "^5.0"
"phpunit/phpunit" : "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected function validate(string $host = null): array
}

if ($this->isValidHostname($host)) {
return array_reverse(array_map('idn_to_utf8', explode('.', strtolower($host))));
return array_reverse(array_map([$this, 'toIdn'], explode('.', strtolower($host))));
}

throw new Exception(sprintf('The submitted host `%s` is invalid', $host));
Expand Down Expand Up @@ -341,7 +341,7 @@ public function keys(...$args): array
return array_keys($this->data);
}

return array_keys($this->data, idn_to_utf8($this->validateString($args[0])), true);
return array_keys($this->data, $this->toIdn($this->validateString($args[0])), true);
}

/**
Expand Down Expand Up @@ -374,7 +374,7 @@ public function getContent(int $enc_type = ComponentInterface::RFC3986_ENCODING)
}

if ($enc_type != ComponentInterface::RFC3987_ENCODING) {
return $this->format(array_map('idn_to_ascii', $this->data), $this->is_absolute);
return $this->format(array_map([$this, 'toAscii'], $this->data), $this->is_absolute);
}

return $this->format($this->data, $this->is_absolute);
Expand Down
33 changes: 32 additions & 1 deletion src/HostInfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,42 @@ protected function isValidIpv6Hostname(string $ipv6): bool
*/
protected function isValidHostname(string $host): bool
{
$labels = array_map('idn_to_ascii', explode('.', $host));
$labels = array_map([$this, 'toAscii'], explode('.', $host));

return 127 > count($labels) && $labels === array_filter($labels, [$this, 'isValidLabel']);
}

/**
* Convert domain name to IDNA ASCII form.
*
* @param string $label
*
* @return string
*/
protected function toAscii(string $label)
{
$res = idn_to_ascii($label, 0, INTL_IDNA_VARIANT_UTS46);
if (false !== $res) {
return $res;
}

return '';
}


/**
* Convert domain name to IDNA ASCII form.
*
* @param string $label
*
* @return string
*/
protected function toIdn(string $label)
{
return idn_to_utf8($label, 0, INTL_IDNA_VARIANT_UTS46);
}


/**
* Returns whether the host label is valid
*
Expand Down

0 comments on commit 84a5c0a

Please sign in to comment.