You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

68 lines
2.1 KiB

  1. <?php
  2. namespace GuzzleHttp;
  3. use GuzzleHttp\Exception\InvalidArgumentException;
  4. use Psr\Http\Message\UriInterface;
  5. final class Utils
  6. {
  7. /**
  8. * Wrapper for the hrtime() or microtime() functions
  9. * (depending on the PHP version, one of the two is used)
  10. *
  11. * @return float|mixed UNIX timestamp
  12. *
  13. * @internal
  14. */
  15. public static function currentTime()
  16. {
  17. return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
  18. }
  19. /**
  20. * @param int $options
  21. *
  22. * @return UriInterface
  23. * @throws InvalidArgumentException
  24. *
  25. * @internal
  26. */
  27. public static function idnUriConvert(UriInterface $uri, $options = 0)
  28. {
  29. if ($uri->getHost()) {
  30. $idnaVariant = defined('INTL_IDNA_VARIANT_UTS46') ? INTL_IDNA_VARIANT_UTS46 : 0;
  31. $asciiHost = $idnaVariant === 0
  32. ? idn_to_ascii($uri->getHost(), $options)
  33. : idn_to_ascii($uri->getHost(), $options, $idnaVariant, $info);
  34. if ($asciiHost === false) {
  35. $errorBitSet = isset($info['errors']) ? $info['errors'] : 0;
  36. $errorConstants = array_filter(array_keys(get_defined_constants()), function ($name) {
  37. return substr($name, 0, 11) === 'IDNA_ERROR_';
  38. });
  39. $errors = [];
  40. foreach ($errorConstants as $errorConstant) {
  41. if ($errorBitSet & constant($errorConstant)) {
  42. $errors[] = $errorConstant;
  43. }
  44. }
  45. $errorMessage = 'IDN conversion failed';
  46. if ($errors) {
  47. $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')';
  48. }
  49. throw new InvalidArgumentException($errorMessage);
  50. } else {
  51. if ($uri->getHost() !== $asciiHost) {
  52. // Replace URI only if the ASCII version is different
  53. $uri = $uri->withHost($asciiHost);
  54. }
  55. }
  56. }
  57. return $uri;
  58. }
  59. }