Skip to content

Commit

Permalink
[6.5] Fix various intl icu issues (#2626)
Browse files Browse the repository at this point in the history
* Fix issues when compiled against old intl icu

* Fixes

* Bumped minimum polyfill-intl-idn version

* Skip conversion for ASCII domains

* Lock version to symfony/polyfill-intl-idn:1.17.0

Co-authored-by: Nyholm <tobias.nyholm@gmail.com>
  • Loading branch information
GrahamCampbell and Nyholm committed May 23, 2020
1 parent c8162be commit d3f2c17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": ">=5.5",
"ext-json": "*",
"symfony/polyfill-intl-idn": "^1.11",
"symfony/polyfill-intl-idn": "1.17.0",
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.6.1"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -1310,6 +1310,11 @@ parameters:
count: 1
path: src/UriTemplate.php

-
message: "#^Method GuzzleHttp\\\\Utils\\:\\:idnToAsci\\(\\) has parameter \\$info with no value type specified in iterable type array\\.$#"
count: 1
path: src/Utils.php

-
message: "#^Function GuzzleHttp\\\\uri_template\\(\\) has parameter \\$variables with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
30 changes: 26 additions & 4 deletions src/Utils.php
Expand Up @@ -3,6 +3,7 @@

use GuzzleHttp\Exception\InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Symfony\Polyfill\Intl\Idn\Idn;

final class Utils
{
Expand Down Expand Up @@ -30,10 +31,7 @@ public static function currentTime()
public static function idnUriConvert(UriInterface $uri, $options = 0)
{
if ($uri->getHost()) {
$idnaVariant = defined('INTL_IDNA_VARIANT_UTS46') ? INTL_IDNA_VARIANT_UTS46 : 0;
$asciiHost = $idnaVariant === 0
? idn_to_ascii($uri->getHost(), $options)
: idn_to_ascii($uri->getHost(), $options, $idnaVariant, $info);
$asciiHost = self::idnToAsci($uri->getHost(), $options, $info);
if ($asciiHost === false) {
$errorBitSet = isset($info['errors']) ? $info['errors'] : 0;

Expand Down Expand Up @@ -64,4 +62,28 @@ public static function idnUriConvert(UriInterface $uri, $options = 0)

return $uri;
}

/**
* @param string $domain
* @param int $options
* @param array $info
*
* @return string|false
*/
private static function idnToAsci($domain, $options, &$info = [])
{
if (\preg_match('%^[ -~]+$%', $domain) === 1) {
return $domain;
}

if (\extension_loaded('intl') && defined('INTL_IDNA_VARIANT_UTS46')) {
return \idn_to_ascii($domain, $options, INTL_IDNA_VARIANT_UTS46, $info);
}

/*
* The Idn class is marked as @internal. We've locked the version to
* symfony/polyfill-intl-idn to avoid issues in the future.
*/
return Idn::idn_to_ascii($domain, $options, Idn::INTL_IDNA_VARIANT_UTS46, $info);
}
}

0 comments on commit d3f2c17

Please sign in to comment.