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