Utils.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace GuzzleHttp;
  3. use GuzzleHttp\Exception\InvalidArgumentException;
  4. use GuzzleHttp\Handler\CurlHandler;
  5. use GuzzleHttp\Handler\CurlMultiHandler;
  6. use GuzzleHttp\Handler\Proxy;
  7. use GuzzleHttp\Handler\StreamHandler;
  8. use Psr\Http\Message\UriInterface;
  9. final class Utils
  10. {
  11. /**
  12. * Debug function used to describe the provided value type and class.
  13. *
  14. * @param mixed $input
  15. *
  16. * @return string Returns a string containing the type of the variable and
  17. * if a class is provided, the class name.
  18. */
  19. public static function describeType($input): string
  20. {
  21. switch (\gettype($input)) {
  22. case 'object':
  23. return 'object(' . \get_class($input) . ')';
  24. case 'array':
  25. return 'array(' . \count($input) . ')';
  26. default:
  27. \ob_start();
  28. \var_dump($input);
  29. // normalize float vs double
  30. /** @var string $varDumpContent */
  31. $varDumpContent = \ob_get_clean();
  32. return \str_replace('double(', 'float(', \rtrim($varDumpContent));
  33. }
  34. }
  35. /**
  36. * Parses an array of header lines into an associative array of headers.
  37. *
  38. * @param iterable $lines Header lines array of strings in the following
  39. * format: "Name: Value"
  40. */
  41. public static function headersFromLines(iterable $lines): array
  42. {
  43. $headers = [];
  44. foreach ($lines as $line) {
  45. $parts = \explode(':', $line, 2);
  46. $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null;
  47. }
  48. return $headers;
  49. }
  50. /**
  51. * Returns a debug stream based on the provided variable.
  52. *
  53. * @param mixed $value Optional value
  54. *
  55. * @return resource
  56. */
  57. public static function debugResource($value = null)
  58. {
  59. if (\is_resource($value)) {
  60. return $value;
  61. }
  62. if (\defined('STDOUT')) {
  63. return \STDOUT;
  64. }
  65. $resource = \fopen('php://output', 'w');
  66. if (false === $resource) {
  67. throw new \RuntimeException('Can not open php output for writing to debug the resource.');
  68. }
  69. return $resource;
  70. }
  71. /**
  72. * Chooses and creates a default handler to use based on the environment.
  73. *
  74. * The returned handler is not wrapped by any default middlewares.
  75. *
  76. * @throws \RuntimeException if no viable Handler is available.
  77. *
  78. * @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the best handler for the given system.
  79. */
  80. public static function chooseHandler(): callable
  81. {
  82. $handler = null;
  83. if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) {
  84. $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
  85. } elseif (\function_exists('curl_exec')) {
  86. $handler = new CurlHandler();
  87. } elseif (\function_exists('curl_multi_exec')) {
  88. $handler = new CurlMultiHandler();
  89. }
  90. if (\ini_get('allow_url_fopen')) {
  91. $handler = $handler
  92. ? Proxy::wrapStreaming($handler, new StreamHandler())
  93. : new StreamHandler();
  94. } elseif (!$handler) {
  95. throw new \RuntimeException('GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.');
  96. }
  97. return $handler;
  98. }
  99. /**
  100. * Get the default User-Agent string to use with Guzzle.
  101. */
  102. public static function defaultUserAgent(): string
  103. {
  104. return sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION);
  105. }
  106. /**
  107. * Returns the default cacert bundle for the current system.
  108. *
  109. * First, the openssl.cafile and curl.cainfo php.ini settings are checked.
  110. * If those settings are not configured, then the common locations for
  111. * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X
  112. * and Windows are checked. If any of these file locations are found on
  113. * disk, they will be utilized.
  114. *
  115. * Note: the result of this function is cached for subsequent calls.
  116. *
  117. * @throws \RuntimeException if no bundle can be found.
  118. *
  119. * @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+.
  120. */
  121. public static function defaultCaBundle(): string
  122. {
  123. static $cached = null;
  124. static $cafiles = [
  125. // Red Hat, CentOS, Fedora (provided by the ca-certificates package)
  126. '/etc/pki/tls/certs/ca-bundle.crt',
  127. // Ubuntu, Debian (provided by the ca-certificates package)
  128. '/etc/ssl/certs/ca-certificates.crt',
  129. // FreeBSD (provided by the ca_root_nss package)
  130. '/usr/local/share/certs/ca-root-nss.crt',
  131. // SLES 12 (provided by the ca-certificates package)
  132. '/var/lib/ca-certificates/ca-bundle.pem',
  133. // OS X provided by homebrew (using the default path)
  134. '/usr/local/etc/openssl/cert.pem',
  135. // Google app engine
  136. '/etc/ca-certificates.crt',
  137. // Windows?
  138. 'C:\\windows\\system32\\curl-ca-bundle.crt',
  139. 'C:\\windows\\curl-ca-bundle.crt',
  140. ];
  141. if ($cached) {
  142. return $cached;
  143. }
  144. if ($ca = \ini_get('openssl.cafile')) {
  145. return $cached = $ca;
  146. }
  147. if ($ca = \ini_get('curl.cainfo')) {
  148. return $cached = $ca;
  149. }
  150. foreach ($cafiles as $filename) {
  151. if (\file_exists($filename)) {
  152. return $cached = $filename;
  153. }
  154. }
  155. throw new \RuntimeException(
  156. <<< EOT
  157. No system CA bundle could be found in any of the the common system locations.
  158. PHP versions earlier than 5.6 are not properly configured to use the system's
  159. CA bundle by default. In order to verify peer certificates, you will need to
  160. supply the path on disk to a certificate bundle to the 'verify' request
  161. option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
  162. need a specific certificate bundle, then Mozilla provides a commonly used CA
  163. bundle which can be downloaded here (provided by the maintainer of cURL):
  164. https://curl.haxx.se/ca/cacert.pem. Once
  165. you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
  166. ini setting to point to the path to the file, allowing you to omit the 'verify'
  167. request option. See https://curl.haxx.se/docs/sslcerts.html for more
  168. information.
  169. EOT
  170. );
  171. }
  172. /**
  173. * Creates an associative array of lowercase header names to the actual
  174. * header casing.
  175. */
  176. public static function normalizeHeaderKeys(array $headers): array
  177. {
  178. $result = [];
  179. foreach (\array_keys($headers) as $key) {
  180. $result[\strtolower($key)] = $key;
  181. }
  182. return $result;
  183. }
  184. /**
  185. * Returns true if the provided host matches any of the no proxy areas.
  186. *
  187. * This method will strip a port from the host if it is present. Each pattern
  188. * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a
  189. * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" ==
  190. * "baz.foo.com", but ".foo.com" != "foo.com").
  191. *
  192. * Areas are matched in the following cases:
  193. * 1. "*" (without quotes) always matches any hosts.
  194. * 2. An exact match.
  195. * 3. The area starts with "." and the area is the last part of the host. e.g.
  196. * '.mit.edu' will match any host that ends with '.mit.edu'.
  197. *
  198. * @param string $host Host to check against the patterns.
  199. * @param string[] $noProxyArray An array of host patterns.
  200. *
  201. * @throws InvalidArgumentException
  202. */
  203. public static function isHostInNoProxy(string $host, array $noProxyArray): bool
  204. {
  205. if (\strlen($host) === 0) {
  206. throw new InvalidArgumentException('Empty host provided');
  207. }
  208. // Strip port if present.
  209. if (\strpos($host, ':')) {
  210. /** @var string[] $hostParts will never be false because of the checks above */
  211. $hostParts = \explode($host, ':', 2);
  212. $host = $hostParts[0];
  213. }
  214. foreach ($noProxyArray as $area) {
  215. // Always match on wildcards.
  216. if ($area === '*') {
  217. return true;
  218. } elseif (empty($area)) {
  219. // Don't match on empty values.
  220. continue;
  221. } elseif ($area === $host) {
  222. // Exact matches.
  223. return true;
  224. }
  225. // Special match if the area when prefixed with ".". Remove any
  226. // existing leading "." and add a new leading ".".
  227. $area = '.' . \ltrim($area, '.');
  228. if (\substr($host, -(\strlen($area))) === $area) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. /**
  235. * Wrapper for json_decode that throws when an error occurs.
  236. *
  237. * @param string $json JSON data to parse
  238. * @param bool $assoc When true, returned objects will be converted
  239. * into associative arrays.
  240. * @param int $depth User specified recursion depth.
  241. * @param int $options Bitmask of JSON decode options.
  242. *
  243. * @return object|array|string|int|float|bool|null
  244. *
  245. * @throws InvalidArgumentException if the JSON cannot be decoded.
  246. *
  247. * @link https://www.php.net/manual/en/function.json-decode.php
  248. */
  249. public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
  250. {
  251. $data = \json_decode($json, $assoc, $depth, $options);
  252. if (\JSON_ERROR_NONE !== \json_last_error()) {
  253. throw new InvalidArgumentException('json_decode error: ' . \json_last_error_msg());
  254. }
  255. return $data;
  256. }
  257. /**
  258. * Wrapper for JSON encoding that throws when an error occurs.
  259. *
  260. * @param mixed $value The value being encoded
  261. * @param int $options JSON encode option bitmask
  262. * @param int $depth Set the maximum depth. Must be greater than zero.
  263. *
  264. * @throws InvalidArgumentException if the JSON cannot be encoded.
  265. *
  266. * @link https://www.php.net/manual/en/function.json-encode.php
  267. */
  268. public static function jsonEncode($value, int $options = 0, int $depth = 512): string
  269. {
  270. $json = \json_encode($value, $options, $depth);
  271. if (\JSON_ERROR_NONE !== \json_last_error()) {
  272. throw new InvalidArgumentException('json_encode error: ' . \json_last_error_msg());
  273. }
  274. /** @var string */
  275. return $json;
  276. }
  277. /**
  278. * Wrapper for the hrtime() or microtime() functions
  279. * (depending on the PHP version, one of the two is used)
  280. *
  281. * @return float UNIX timestamp
  282. *
  283. * @internal
  284. */
  285. public static function currentTime(): float
  286. {
  287. return (float) \function_exists('hrtime') ? \hrtime(true) / 1e9 : \microtime(true);
  288. }
  289. /**
  290. * @throws InvalidArgumentException
  291. *
  292. * @internal
  293. */
  294. public static function idnUriConvert(UriInterface $uri, int $options = 0): UriInterface
  295. {
  296. if ($uri->getHost()) {
  297. $asciiHost = self::idnToAsci($uri->getHost(), $options, $info);
  298. if ($asciiHost === false) {
  299. $errorBitSet = $info['errors'] ?? 0;
  300. $errorConstants = array_filter(array_keys(get_defined_constants()), static function ($name) {
  301. return substr($name, 0, 11) === 'IDNA_ERROR_';
  302. });
  303. $errors = [];
  304. foreach ($errorConstants as $errorConstant) {
  305. if ($errorBitSet & constant($errorConstant)) {
  306. $errors[] = $errorConstant;
  307. }
  308. }
  309. $errorMessage = 'IDN conversion failed';
  310. if ($errors) {
  311. $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')';
  312. }
  313. throw new InvalidArgumentException($errorMessage);
  314. }
  315. if ($uri->getHost() !== $asciiHost) {
  316. // Replace URI only if the ASCII version is different
  317. $uri = $uri->withHost($asciiHost);
  318. }
  319. }
  320. return $uri;
  321. }
  322. /**
  323. * @internal
  324. */
  325. public static function getenv(string $name): ?string
  326. {
  327. if (isset($_SERVER[$name])) {
  328. return (string) $_SERVER[$name];
  329. }
  330. if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== false && $value !== null) {
  331. return (string) $value;
  332. }
  333. return null;
  334. }
  335. /**
  336. * @return string|false
  337. */
  338. private static function idnToAsci(string $domain, int $options, ?array &$info = [])
  339. {
  340. if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) {
  341. return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info);
  342. }
  343. throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old');
  344. }
  345. }