vendor/symfony/http-client/HttpClientTrait.php line 169

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpClient;
  11. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  12. use Symfony\Component\HttpClient\Exception\TransportException;
  13. /**
  14.  * Provides the common logic from writing HttpClientInterface implementations.
  15.  *
  16.  * All private methods are static to prevent implementers from creating memory leaks via circular references.
  17.  *
  18.  * @author Nicolas Grekas <p@tchwork.com>
  19.  */
  20. trait HttpClientTrait
  21. {
  22.     private static $CHUNK_SIZE 16372;
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public function withOptions(array $options): self
  27.     {
  28.         $clone = clone $this;
  29.         $clone->defaultOptions self::mergeDefaultOptions($options$this->defaultOptions);
  30.         return $clone;
  31.     }
  32.     /**
  33.      * Validates and normalizes method, URL and options, and merges them with defaults.
  34.      *
  35.      * @throws InvalidArgumentException When a not-supported option is found
  36.      */
  37.     private static function prepareRequest(?string $method, ?string $url, array $options, array $defaultOptions = [], bool $allowExtraOptions false): array
  38.     {
  39.         if (null !== $method) {
  40.             if (\strlen($method) !== strspn($method'ABCDEFGHIJKLMNOPQRSTUVWXYZ')) {
  41.                 throw new InvalidArgumentException(sprintf('Invalid HTTP method "%s", only uppercase letters are accepted.'$method));
  42.             }
  43.             if (!$method) {
  44.                 throw new InvalidArgumentException('The HTTP method cannot be empty.');
  45.             }
  46.         }
  47.         $options self::mergeDefaultOptions($options$defaultOptions$allowExtraOptions);
  48.         $buffer $options['buffer'] ?? true;
  49.         if ($buffer instanceof \Closure) {
  50.             $options['buffer'] = static function (array $headers) use ($buffer) {
  51.                 if (!\is_bool($buffer $buffer($headers))) {
  52.                     if (!\is_array($bufferInfo = @stream_get_meta_data($buffer))) {
  53.                         throw new \LogicException(sprintf('The closure passed as option "buffer" must return bool or stream resource, got "%s".'get_debug_type($buffer)));
  54.                     }
  55.                     if (false === strpbrk($bufferInfo['mode'], 'acew+')) {
  56.                         throw new \LogicException(sprintf('The stream returned by the closure passed as option "buffer" must be writeable, got mode "%s".'$bufferInfo['mode']));
  57.                     }
  58.                 }
  59.                 return $buffer;
  60.             };
  61.         } elseif (!\is_bool($buffer)) {
  62.             if (!\is_array($bufferInfo = @stream_get_meta_data($buffer))) {
  63.                 throw new InvalidArgumentException(sprintf('Option "buffer" must be bool, stream resource or Closure, "%s" given.'get_debug_type($buffer)));
  64.             }
  65.             if (false === strpbrk($bufferInfo['mode'], 'acew+')) {
  66.                 throw new InvalidArgumentException(sprintf('The stream in option "buffer" must be writeable, mode "%s" given.'$bufferInfo['mode']));
  67.             }
  68.         }
  69.         if (isset($options['json'])) {
  70.             if (isset($options['body']) && '' !== $options['body']) {
  71.                 throw new InvalidArgumentException('Define either the "json" or the "body" option, setting both is not supported.');
  72.             }
  73.             $options['body'] = self::jsonEncode($options['json']);
  74.             unset($options['json']);
  75.             if (!isset($options['normalized_headers']['content-type'])) {
  76.                 $options['normalized_headers']['content-type'] = ['Content-Type: application/json'];
  77.             }
  78.         }
  79.         if (!isset($options['normalized_headers']['accept'])) {
  80.             $options['normalized_headers']['accept'] = ['Accept: */*'];
  81.         }
  82.         if (isset($options['body'])) {
  83.             $options['body'] = self::normalizeBody($options['body']);
  84.             if (\is_string($options['body'])
  85.                 && (string) \strlen($options['body']) !== substr($h $options['normalized_headers']['content-length'][0] ?? ''16)
  86.                 && ('' !== $h || '' !== $options['body'])
  87.             ) {
  88.                 if ('chunked' === substr($options['normalized_headers']['transfer-encoding'][0] ?? ''\strlen('Transfer-Encoding: '))) {
  89.                     unset($options['normalized_headers']['transfer-encoding']);
  90.                     $options['body'] = self::dechunk($options['body']);
  91.                 }
  92.                 $options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: '\strlen($options['body']), 16)];
  93.             }
  94.         }
  95.         if (isset($options['peer_fingerprint'])) {
  96.             $options['peer_fingerprint'] = self::normalizePeerFingerprint($options['peer_fingerprint']);
  97.         }
  98.         // Validate on_progress
  99.         if (isset($options['on_progress']) && !\is_callable($onProgress $options['on_progress'])) {
  100.             throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.'get_debug_type($onProgress)));
  101.         }
  102.         if (\is_array($options['auth_basic'] ?? null)) {
  103.             $count \count($options['auth_basic']);
  104.             if ($count <= || $count 2) {
  105.                 throw new InvalidArgumentException(sprintf('Option "auth_basic" must contain 1 or 2 elements, "%s" given.'$count));
  106.             }
  107.             $options['auth_basic'] = implode(':'$options['auth_basic']);
  108.         }
  109.         if (!\is_string($options['auth_basic'] ?? '')) {
  110.             throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, "%s" given.'get_debug_type($options['auth_basic'])));
  111.         }
  112.         if (isset($options['auth_bearer'])) {
  113.             if (!\is_string($options['auth_bearer'])) {
  114.                 throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string, "%s" given.'get_debug_type($options['auth_bearer'])));
  115.             }
  116.             if (preg_match('{[^\x21-\x7E]}'$options['auth_bearer'])) {
  117.                 throw new InvalidArgumentException('Invalid character found in option "auth_bearer": '.json_encode($options['auth_bearer']).'.');
  118.             }
  119.         }
  120.         if (isset($options['auth_basic'], $options['auth_bearer'])) {
  121.             throw new InvalidArgumentException('Define either the "auth_basic" or the "auth_bearer" option, setting both is not supported.');
  122.         }
  123.         if (null !== $url) {
  124.             // Merge auth with headers
  125.             if (($options['auth_basic'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
  126.                 $options['normalized_headers']['authorization'] = ['Authorization: Basic '.base64_encode($options['auth_basic'])];
  127.             }
  128.             // Merge bearer with headers
  129.             if (($options['auth_bearer'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
  130.                 $options['normalized_headers']['authorization'] = ['Authorization: Bearer '.$options['auth_bearer']];
  131.             }
  132.             unset($options['auth_basic'], $options['auth_bearer']);
  133.             // Parse base URI
  134.             if (\is_string($options['base_uri'])) {
  135.                 $options['base_uri'] = self::parseUrl($options['base_uri']);
  136.             }
  137.             // Validate and resolve URL
  138.             $url self::parseUrl($url$options['query']);
  139.             $url self::resolveUrl($url$options['base_uri'], $defaultOptions['query'] ?? []);
  140.         }
  141.         // Finalize normalization of options
  142.         $options['http_version'] = (string) ($options['http_version'] ?? '') ?: null;
  143.         if ($options['timeout'] = (float) ($options['timeout'] ?? \ini_get('default_socket_timeout'))) {
  144.             $options['timeout'] = 172800.0// 2 days
  145.         }
  146.         $options['max_duration'] = isset($options['max_duration']) ? (float) $options['max_duration'] : 0;
  147.         $options['headers'] = array_merge(...array_values($options['normalized_headers']));
  148.         return [$url$options];
  149.     }
  150.     /**
  151.      * @throws InvalidArgumentException When an invalid option is found
  152.      */
  153.     private static function mergeDefaultOptions(array $options, array $defaultOptionsbool $allowExtraOptions false): array
  154.     {
  155.         $options['normalized_headers'] = self::normalizeHeaders($options['headers'] ?? []);
  156.         if ($defaultOptions['headers'] ?? false) {
  157.             $options['normalized_headers'] += self::normalizeHeaders($defaultOptions['headers']);
  158.         }
  159.         $options['headers'] = array_merge(...array_values($options['normalized_headers']) ?: [[]]);
  160.         if ($resolve $options['resolve'] ?? false) {
  161.             $options['resolve'] = [];
  162.             foreach ($resolve as $k => $v) {
  163.                 $options['resolve'][substr(self::parseUrl('http://'.$k)['authority'], 2)] = (string) $v;
  164.             }
  165.         }
  166.         // Option "query" is never inherited from defaults
  167.         $options['query'] = $options['query'] ?? [];
  168.         $options += $defaultOptions;
  169.         if (isset(self::$emptyDefaults)) {
  170.             foreach (self::$emptyDefaults as $k => $v) {
  171.                 if (!isset($options[$k])) {
  172.                     $options[$k] = $v;
  173.                 }
  174.             }
  175.         }
  176.         if (isset($defaultOptions['extra'])) {
  177.             $options['extra'] += $defaultOptions['extra'];
  178.         }
  179.         if ($resolve $defaultOptions['resolve'] ?? false) {
  180.             foreach ($resolve as $k => $v) {
  181.                 $options['resolve'] += [substr(self::parseUrl('http://'.$k)['authority'], 2) => (string) $v];
  182.             }
  183.         }
  184.         if ($allowExtraOptions || !$defaultOptions) {
  185.             return $options;
  186.         }
  187.         // Look for unsupported options
  188.         foreach ($options as $name => $v) {
  189.             if (\array_key_exists($name$defaultOptions) || 'normalized_headers' === $name) {
  190.                 continue;
  191.             }
  192.             if ('auth_ntlm' === $name) {
  193.                 if (!\extension_loaded('curl')) {
  194.                     $msg 'try installing the "curl" extension to use "%s" instead.';
  195.                 } else {
  196.                     $msg 'try using "%s" instead.';
  197.                 }
  198.                 throw new InvalidArgumentException(sprintf('Option "auth_ntlm" is not supported by "%s", '.$msg__CLASS__CurlHttpClient::class));
  199.             }
  200.             $alternatives = [];
  201.             foreach ($defaultOptions as $k => $v) {
  202.                 if (levenshtein($name$k) <= \strlen($name) / || str_contains($k$name)) {
  203.                     $alternatives[] = $k;
  204.                 }
  205.             }
  206.             throw new InvalidArgumentException(sprintf('Unsupported option "%s" passed to "%s", did you mean "%s"?'$name__CLASS__implode('", "'$alternatives ?: array_keys($defaultOptions))));
  207.         }
  208.         return $options;
  209.     }
  210.     /**
  211.      * @return string[][]
  212.      *
  213.      * @throws InvalidArgumentException When an invalid header is found
  214.      */
  215.     private static function normalizeHeaders(array $headers): array
  216.     {
  217.         $normalizedHeaders = [];
  218.         foreach ($headers as $name => $values) {
  219.             if (\is_object($values) && method_exists($values'__toString')) {
  220.                 $values = (string) $values;
  221.             }
  222.             if (\is_int($name)) {
  223.                 if (!\is_string($values)) {
  224.                     throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.'$nameget_debug_type($values)));
  225.                 }
  226.                 [$name$values] = explode(':'$values2);
  227.                 $values = [ltrim($values)];
  228.             } elseif (!is_iterable($values)) {
  229.                 if (\is_object($values)) {
  230.                     throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.'$nameget_debug_type($values)));
  231.                 }
  232.                 $values = (array) $values;
  233.             }
  234.             $lcName strtolower($name);
  235.             $normalizedHeaders[$lcName] = [];
  236.             foreach ($values as $value) {
  237.                 $normalizedHeaders[$lcName][] = $value $name.': '.$value;
  238.                 if (\strlen($value) !== strcspn($value"\r\n\0")) {
  239.                     throw new InvalidArgumentException(sprintf('Invalid header: CR/LF/NUL found in "%s".'$value));
  240.                 }
  241.             }
  242.         }
  243.         return $normalizedHeaders;
  244.     }
  245.     /**
  246.      * @param array|string|resource|\Traversable|\Closure $body
  247.      *
  248.      * @return string|resource|\Closure
  249.      *
  250.      * @throws InvalidArgumentException When an invalid body is passed
  251.      */
  252.     private static function normalizeBody($body)
  253.     {
  254.         if (\is_array($body)) {
  255.             array_walk_recursive($body$caster = static function (&$v) use (&$caster) {
  256.                 if (\is_object($v)) {
  257.                     if ($vars get_object_vars($v)) {
  258.                         array_walk_recursive($vars$caster);
  259.                         $v $vars;
  260.                     } elseif (method_exists($v'__toString')) {
  261.                         $v = (string) $v;
  262.                     }
  263.                 }
  264.             });
  265.             return http_build_query($body'''&');
  266.         }
  267.         if (\is_string($body)) {
  268.             return $body;
  269.         }
  270.         $generatorToCallable = static function (\Generator $body): \Closure {
  271.             return static function () use ($body) {
  272.                 while ($body->valid()) {
  273.                     $chunk $body->current();
  274.                     $body->next();
  275.                     if ('' !== $chunk) {
  276.                         return $chunk;
  277.                     }
  278.                 }
  279.                 return '';
  280.             };
  281.         };
  282.         if ($body instanceof \Generator) {
  283.             return $generatorToCallable($body);
  284.         }
  285.         if ($body instanceof \Traversable) {
  286.             return $generatorToCallable((static function ($body) { yield from $body; })($body));
  287.         }
  288.         if ($body instanceof \Closure) {
  289.             $r = new \ReflectionFunction($body);
  290.             $body $r->getClosure();
  291.             if ($r->isGenerator()) {
  292.                 $body $body(self::$CHUNK_SIZE);
  293.                 return $generatorToCallable($body);
  294.             }
  295.             return $body;
  296.         }
  297.         if (!\is_array(@stream_get_meta_data($body))) {
  298.             throw new InvalidArgumentException(sprintf('Option "body" must be string, stream resource, iterable or callable, "%s" given.'get_debug_type($body)));
  299.         }
  300.         return $body;
  301.     }
  302.     private static function dechunk(string $body): string
  303.     {
  304.         $h fopen('php://temp''w+');
  305.         stream_filter_append($h'dechunk'\STREAM_FILTER_WRITE);
  306.         fwrite($h$body);
  307.         $body stream_get_contents($h, -10);
  308.         rewind($h);
  309.         ftruncate($h0);
  310.         if (fwrite($h'-') && '' !== stream_get_contents($h, -10)) {
  311.             throw new TransportException('Request body has broken chunked encoding.');
  312.         }
  313.         return $body;
  314.     }
  315.     /**
  316.      * @param string|string[] $fingerprint
  317.      *
  318.      * @throws InvalidArgumentException When an invalid fingerprint is passed
  319.      */
  320.     private static function normalizePeerFingerprint($fingerprint): array
  321.     {
  322.         if (\is_string($fingerprint)) {
  323.             switch (\strlen($fingerprint str_replace(':'''$fingerprint))) {
  324.                 case 32$fingerprint = ['md5' => $fingerprint]; break;
  325.                 case 40$fingerprint = ['sha1' => $fingerprint]; break;
  326.                 case 44$fingerprint = ['pin-sha256' => [$fingerprint]]; break;
  327.                 case 64$fingerprint = ['sha256' => $fingerprint]; break;
  328.                 default: throw new InvalidArgumentException(sprintf('Cannot auto-detect fingerprint algorithm for "%s".'$fingerprint));
  329.             }
  330.         } elseif (\is_array($fingerprint)) {
  331.             foreach ($fingerprint as $algo => $hash) {
  332.                 $fingerprint[$algo] = 'pin-sha256' === $algo ? (array) $hash str_replace(':'''$hash);
  333.             }
  334.         } else {
  335.             throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, "%s" given.'get_debug_type($fingerprint)));
  336.         }
  337.         return $fingerprint;
  338.     }
  339.     /**
  340.      * @param mixed $value
  341.      *
  342.      * @throws InvalidArgumentException When the value cannot be json-encoded
  343.      */
  344.     private static function jsonEncode($valueint $flags nullint $maxDepth 512): string
  345.     {
  346.         $flags $flags ?? (\JSON_HEX_TAG \JSON_HEX_APOS \JSON_HEX_AMP \JSON_HEX_QUOT \JSON_PRESERVE_ZERO_FRACTION);
  347.         try {
  348.             $value json_encode($value$flags | (\PHP_VERSION_ID >= 70300 \JSON_THROW_ON_ERROR 0), $maxDepth);
  349.         } catch (\JsonException $e) {
  350.             throw new InvalidArgumentException('Invalid value for "json" option: '.$e->getMessage());
  351.         }
  352.         if (\PHP_VERSION_ID 70300 && \JSON_ERROR_NONE !== json_last_error() && (false === $value || !($flags \JSON_PARTIAL_OUTPUT_ON_ERROR))) {
  353.             throw new InvalidArgumentException('Invalid value for "json" option: '.json_last_error_msg());
  354.         }
  355.         return $value;
  356.     }
  357.     /**
  358.      * Resolves a URL against a base URI.
  359.      *
  360.      * @see https://tools.ietf.org/html/rfc3986#section-5.2.2
  361.      *
  362.      * @throws InvalidArgumentException When an invalid URL is passed
  363.      */
  364.     private static function resolveUrl(array $url, ?array $base, array $queryDefaults = []): array
  365.     {
  366.         if (null !== $base && '' === ($base['scheme'] ?? '').($base['authority'] ?? '')) {
  367.             throw new InvalidArgumentException(sprintf('Invalid "base_uri" option: host or scheme is missing in "%s".'implode(''$base)));
  368.         }
  369.         if (null === $url['scheme'] && (null === $base || null === $base['scheme'])) {
  370.             throw new InvalidArgumentException(sprintf('Invalid URL: scheme is missing in "%s". Did you forget to add "http(s)://"?'implode(''$base ?? $url)));
  371.         }
  372.         if (null === $base && '' === $url['scheme'].$url['authority']) {
  373.             throw new InvalidArgumentException(sprintf('Invalid URL: no "base_uri" option was provided and host or scheme is missing in "%s".'implode(''$url)));
  374.         }
  375.         if (null !== $url['scheme']) {
  376.             $url['path'] = self::removeDotSegments($url['path'] ?? '');
  377.         } else {
  378.             if (null !== $url['authority']) {
  379.                 $url['path'] = self::removeDotSegments($url['path'] ?? '');
  380.             } else {
  381.                 if (null === $url['path']) {
  382.                     $url['path'] = $base['path'];
  383.                     $url['query'] = $url['query'] ?? $base['query'];
  384.                 } else {
  385.                     if ('/' !== $url['path'][0]) {
  386.                         if (null === $base['path']) {
  387.                             $url['path'] = '/'.$url['path'];
  388.                         } else {
  389.                             $segments explode('/'$base['path']);
  390.                             array_splice($segments, -11, [$url['path']]);
  391.                             $url['path'] = implode('/'$segments);
  392.                         }
  393.                     }
  394.                     $url['path'] = self::removeDotSegments($url['path']);
  395.                 }
  396.                 $url['authority'] = $base['authority'];
  397.                 if ($queryDefaults) {
  398.                     $url['query'] = '?'.self::mergeQueryString(substr($url['query'] ?? ''1), $queryDefaultsfalse);
  399.                 }
  400.             }
  401.             $url['scheme'] = $base['scheme'];
  402.         }
  403.         if ('' === ($url['path'] ?? '')) {
  404.             $url['path'] = '/';
  405.         }
  406.         if ('?' === ($url['query'] ?? '')) {
  407.             $url['query'] = null;
  408.         }
  409.         return $url;
  410.     }
  411.     /**
  412.      * Parses a URL and fixes its encoding if needed.
  413.      *
  414.      * @throws InvalidArgumentException When an invalid URL is passed
  415.      */
  416.     private static function parseUrl(string $url, array $query = [], array $allowedSchemes = ['http' => 80'https' => 443]): array
  417.     {
  418.         if (false === $parts parse_url($url)) {
  419.             throw new InvalidArgumentException(sprintf('Malformed URL "%s".'$url));
  420.         }
  421.         if ($query) {
  422.             $parts['query'] = self::mergeQueryString($parts['query'] ?? null$querytrue);
  423.         }
  424.         $port $parts['port'] ?? 0;
  425.         if (null !== $scheme $parts['scheme'] ?? null) {
  426.             if (!isset($allowedSchemes[$scheme strtolower($scheme)])) {
  427.                 throw new InvalidArgumentException(sprintf('Unsupported scheme in "%s".'$url));
  428.             }
  429.             $port $allowedSchemes[$scheme] === $port $port;
  430.             $scheme .= ':';
  431.         }
  432.         if (null !== $host $parts['host'] ?? null) {
  433.             if (!\defined('INTL_IDNA_VARIANT_UTS46') && preg_match('/[\x80-\xFF]/'$host)) {
  434.                 throw new InvalidArgumentException(sprintf('Unsupported IDN "%s", try enabling the "intl" PHP extension or running "composer require symfony/polyfill-intl-idn".'$host));
  435.             }
  436.             $host \defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_ascii($host\IDNA_DEFAULT \IDNA_USE_STD3_RULES \IDNA_CHECK_BIDI \IDNA_CHECK_CONTEXTJ \IDNA_NONTRANSITIONAL_TO_ASCII\INTL_IDNA_VARIANT_UTS46) ?: strtolower($host) : strtolower($host);
  437.             $host .= $port ':'.$port '';
  438.         }
  439.         foreach (['user''pass''path''query''fragment'] as $part) {
  440.             if (!isset($parts[$part])) {
  441.                 continue;
  442.             }
  443.             if (str_contains($parts[$part], '%')) {
  444.                 // https://tools.ietf.org/html/rfc3986#section-2.3
  445.                 $parts[$part] = preg_replace_callback('/%(?:2[DE]|3[0-9]|[46][1-9A-F]|5F|[57][0-9A]|7E)++/i', function ($m) { return rawurldecode($m[0]); }, $parts[$part]);
  446.             }
  447.             // https://tools.ietf.org/html/rfc3986#section-3.3
  448.             $parts[$part] = preg_replace_callback("#[^-A-Za-z0-9._~!$&/'()*+,;=:@%]++#", function ($m) { return rawurlencode($m[0]); }, $parts[$part]);
  449.         }
  450.         return [
  451.             'scheme' => $scheme,
  452.             'authority' => null !== $host '//'.(isset($parts['user']) ? $parts['user'].(isset($parts['pass']) ? ':'.$parts['pass'] : '').'@' '').$host null,
  453.             'path' => isset($parts['path'][0]) ? $parts['path'] : null,
  454.             'query' => isset($parts['query']) ? '?'.$parts['query'] : null,
  455.             'fragment' => isset($parts['fragment']) ? '#'.$parts['fragment'] : null,
  456.         ];
  457.     }
  458.     /**
  459.      * Removes dot-segments from a path.
  460.      *
  461.      * @see https://tools.ietf.org/html/rfc3986#section-5.2.4
  462.      */
  463.     private static function removeDotSegments(string $path)
  464.     {
  465.         $result '';
  466.         while (!\in_array($path, ['''.''..'], true)) {
  467.             if ('.' === $path[0] && (str_starts_with($path$p '../') || str_starts_with($path$p './'))) {
  468.                 $path substr($path\strlen($p));
  469.             } elseif ('/.' === $path || str_starts_with($path'/./')) {
  470.                 $path substr_replace($path'/'03);
  471.             } elseif ('/..' === $path || str_starts_with($path'/../')) {
  472.                 $i strrpos($result'/');
  473.                 $result $i substr($result0$i) : '';
  474.                 $path substr_replace($path'/'04);
  475.             } else {
  476.                 $i strpos($path'/'1) ?: \strlen($path);
  477.                 $result .= substr($path0$i);
  478.                 $path substr($path$i);
  479.             }
  480.         }
  481.         return $result;
  482.     }
  483.     /**
  484.      * Merges and encodes a query array with a query string.
  485.      *
  486.      * @throws InvalidArgumentException When an invalid query-string value is passed
  487.      */
  488.     private static function mergeQueryString(?string $queryString, array $queryArraybool $replace): ?string
  489.     {
  490.         if (!$queryArray) {
  491.             return $queryString;
  492.         }
  493.         $query = [];
  494.         if (null !== $queryString) {
  495.             foreach (explode('&'$queryString) as $v) {
  496.                 if ('' !== $v) {
  497.                     $k urldecode(explode('='$v2)[0]);
  498.                     $query[$k] = (isset($query[$k]) ? $query[$k].'&' '').$v;
  499.                 }
  500.             }
  501.         }
  502.         if ($replace) {
  503.             foreach ($queryArray as $k => $v) {
  504.                 if (null === $v) {
  505.                     unset($query[$k]);
  506.                 }
  507.             }
  508.         }
  509.         $queryString http_build_query($queryArray'''&'\PHP_QUERY_RFC3986);
  510.         $queryArray = [];
  511.         if ($queryString) {
  512.             foreach (explode('&'$queryString) as $v) {
  513.                 $queryArray[rawurldecode(explode('='$v2)[0])] = $v;
  514.             }
  515.         }
  516.         return implode('&'$replace array_replace($query$queryArray) : ($query $queryArray));
  517.     }
  518.     /**
  519.      * Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
  520.      */
  521.     private static function getProxy(?string $proxy, array $url, ?string $noProxy): ?array
  522.     {
  523.         if (null === $proxy) {
  524.             // Ignore HTTP_PROXY except on the CLI to work around httpoxy set of vulnerabilities
  525.             $proxy $_SERVER['http_proxy'] ?? (\in_array(\PHP_SAPI, ['cli''phpdbg'], true) ? $_SERVER['HTTP_PROXY'] ?? null null) ?? $_SERVER['all_proxy'] ?? $_SERVER['ALL_PROXY'] ?? null;
  526.             if ('https:' === $url['scheme']) {
  527.                 $proxy $_SERVER['https_proxy'] ?? $_SERVER['HTTPS_PROXY'] ?? $proxy;
  528.             }
  529.         }
  530.         if (null === $proxy) {
  531.             return null;
  532.         }
  533.         $proxy = (parse_url($proxy) ?: []) + ['scheme' => 'http'];
  534.         if (!isset($proxy['host'])) {
  535.             throw new TransportException('Invalid HTTP proxy: host is missing.');
  536.         }
  537.         if ('http' === $proxy['scheme']) {
  538.             $proxyUrl 'tcp://'.$proxy['host'].':'.($proxy['port'] ?? '80');
  539.         } elseif ('https' === $proxy['scheme']) {
  540.             $proxyUrl 'ssl://'.$proxy['host'].':'.($proxy['port'] ?? '443');
  541.         } else {
  542.             throw new TransportException(sprintf('Unsupported proxy scheme "%s": "http" or "https" expected.'$proxy['scheme']));
  543.         }
  544.         $noProxy $noProxy ?? $_SERVER['no_proxy'] ?? $_SERVER['NO_PROXY'] ?? '';
  545.         $noProxy $noProxy preg_split('/[\s,]+/'$noProxy) : [];
  546.         return [
  547.             'url' => $proxyUrl,
  548.             'auth' => isset($proxy['user']) ? 'Basic '.base64_encode(rawurldecode($proxy['user']).':'.rawurldecode($proxy['pass'] ?? '')) : null,
  549.             'no_proxy' => $noProxy,
  550.         ];
  551.     }
  552.     private static function shouldBuffer(array $headers): bool
  553.     {
  554.         if (null === $contentType $headers['content-type'][0] ?? null) {
  555.             return false;
  556.         }
  557.         if (false !== $i strpos($contentType';')) {
  558.             $contentType substr($contentType0$i);
  559.         }
  560.         return $contentType && preg_match('#^(?:text/|application/(?:.+\+)?(?:json|xml)$)#i'$contentType);
  561.     }
  562. }