From e884a47edd86d5a0ae0592de180b2a895e91638e Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sun, 8 Dec 2019 21:30:23 +0200 Subject: [PATCH] Added idn function inside functions --- src/Client.php | 33 +------------------------------- src/RedirectMiddleware.php | 5 +++-- src/functions.php | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/Client.php b/src/Client.php index db4062f94..aa88eb18c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -215,38 +215,7 @@ private function buildUri($uri, array $config) $uri = Psr7\UriResolver::resolve(Psr7\uri_for($config['base_uri']), $uri); } - if ($uri->getHost() && isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) { - $idnOptions = ($config['idn_conversion'] === true) ? IDNA_DEFAULT : $config['idn_conversion']; - - $asciiHost = idn_to_ascii($uri->getHost(), $idnOptions, INTL_IDNA_VARIANT_UTS46, $info); - if ($asciiHost === false) { - $errorBitSet = isset($info['errors']) ? $info['errors'] : 0; - - $errorConstants = array_filter(array_keys(get_defined_constants()), function ($name) { - return substr($name, 0, 11) === 'IDNA_ERROR_'; - }); - - $errors = []; - foreach ($errorConstants as $errorConstant) { - if ($errorBitSet & constant($errorConstant)) { - $errors[] = $errorConstant; - } - } - - $errorMessage = 'IDN conversion failed'; - if ($errors) { - $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')'; - } - - throw new InvalidArgumentException($errorMessage); - } else { - if ($uri->getHost() !== $asciiHost) { - // Replace URI only if the ASCII version is different - $uri = $uri->withHost($asciiHost); - } - } - } - + $uri = _idn_uri_convert($uri, $config); return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; } diff --git a/src/RedirectMiddleware.php b/src/RedirectMiddleware.php index 5a0edd572..9f5cf6f74 100644 --- a/src/RedirectMiddleware.php +++ b/src/RedirectMiddleware.php @@ -13,7 +13,7 @@ * Request redirect middleware. * * Apply this middleware like other middleware using - * {@see GuzzleHttp\Middleware::redirect()}. + * {@see \GuzzleHttp\Middleware::redirect()}. */ class RedirectMiddleware { @@ -190,7 +190,8 @@ public function modifyRequest( $modify['body'] = ''; } - $modify['uri'] = $this->redirectUri($request, $response, $protocols); + $uri = $this->redirectUri($request, $response, $protocols); + $modify['uri'] = _idn_uri_convert($uri, $options); Psr7\rewind_body($request); // Add the Referer header if it is told to do so and only diff --git a/src/functions.php b/src/functions.php index aff69d557..003e2c5a3 100644 --- a/src/functions.php +++ b/src/functions.php @@ -1,10 +1,12 @@ getHost() && isset($options['idn_conversion']) && ($options['idn_conversion'] !== false)) { + $idnOptions = ($options['idn_conversion'] === true) ? IDNA_DEFAULT : $options['idn_conversion']; + + $asciiHost = idn_to_ascii($uri->getHost(), $idnOptions, INTL_IDNA_VARIANT_UTS46, $info); + if ($asciiHost === false) { + $errorBitSet = isset($info['errors']) ? $info['errors'] : 0; + + $errorConstants = array_filter(array_keys(get_defined_constants()), function ($name) { + return substr($name, 0, 11) === 'IDNA_ERROR_'; + }); + + $errors = []; + foreach ($errorConstants as $errorConstant) { + if ($errorBitSet & constant($errorConstant)) { + $errors[] = $errorConstant; + } + } + + $errorMessage = 'IDN conversion failed'; + if ($errors) { + $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')'; + } + + throw new InvalidArgumentException($errorMessage); + } else { + if ($uri->getHost() !== $asciiHost) { + // Replace URI only if the ASCII version is different + $uri = $uri->withHost($asciiHost); + } + } + } + + return $uri; +} \ No newline at end of file