Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use internal functions (#2548)
- Loading branch information
1 parent
400cefd
commit 9189d46
Showing
11 changed files
with
111 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
namespace GuzzleHttp; | ||
|
||
use GuzzleHttp\Exception\InvalidArgumentException; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
final class Utils | ||
{ | ||
/** | ||
* Wrapper for the hrtime() or microtime() functions | ||
* (depending on the PHP version, one of the two is used) | ||
* | ||
* @return float|mixed UNIX timestamp | ||
* | ||
* @internal | ||
*/ | ||
public static function currentTime() | ||
{ | ||
return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true); | ||
} | ||
|
||
/** | ||
* @param int $options | ||
* | ||
* @return UriInterface | ||
* @throws InvalidArgumentException | ||
* | ||
* @internal | ||
*/ | ||
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); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace GuzzleHttp\Test; | ||
|
||
use GuzzleHttp\Psr7; | ||
use GuzzleHttp\Utils; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InternalUtilsTest extends TestCase | ||
{ | ||
public function testCurrentTime() | ||
{ | ||
self::assertGreaterThan(0, Utils::currentTime()); | ||
} | ||
|
||
public function testIdnConvert() | ||
{ | ||
if (!extension_loaded('intl')) { | ||
self::markTestSkipped('intl PHP extension is not loaded'); | ||
} | ||
|
||
$uri = Psr7\uri_for('https://яндекс.рф/images'); | ||
$uri = Utils::idnUriConvert($uri); | ||
self::assertSame('xn--d1acpjx3f.xn--p1ai', $uri->getHost()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters