vendor/symfony/http-client/Response/CurlResponse.php line 176

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\Response;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpClient\Chunk\FirstChunk;
  13. use Symfony\Component\HttpClient\Chunk\InformationalChunk;
  14. use Symfony\Component\HttpClient\Exception\TransportException;
  15. use Symfony\Component\HttpClient\Internal\ClientState;
  16. use Symfony\Component\HttpClient\Internal\CurlClientState;
  17. use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
  18. use Symfony\Contracts\HttpClient\ResponseInterface;
  19. /**
  20.  * @author Nicolas Grekas <p@tchwork.com>
  21.  *
  22.  * @internal
  23.  */
  24. final class CurlResponse implements ResponseInterface
  25. {
  26.     use ResponseTrait {
  27.         getContent as private doGetContent;
  28.     }
  29.     private static $performing false;
  30.     private $multi;
  31.     private $debugBuffer;
  32.     /**
  33.      * @param \CurlHandle|resource|string $ch
  34.      *
  35.      * @internal
  36.      */
  37.     public function __construct(CurlClientState $multi$ch, array $options nullLoggerInterface $logger nullstring $method 'GET', callable $resolveRedirect nullint $curlVersion null)
  38.     {
  39.         $this->multi $multi;
  40.         if (\is_resource($ch) || $ch instanceof \CurlHandle) {
  41.             $this->handle $ch;
  42.             $this->debugBuffer fopen('php://temp''w+');
  43.             if (0x074000 === $curlVersion) {
  44.                 fwrite($this->debugBuffer'Due to a bug in curl 7.64.0, the debug log is disabled; use another version to work around the issue.');
  45.             } else {
  46.                 curl_setopt($chCURLOPT_VERBOSEtrue);
  47.                 curl_setopt($chCURLOPT_STDERR$this->debugBuffer);
  48.             }
  49.         } else {
  50.             $this->info['url'] = $ch;
  51.             $ch $this->handle;
  52.         }
  53.         $this->id $id = (int) $ch;
  54.         $this->logger $logger;
  55.         $this->shouldBuffer $options['buffer'] ?? true;
  56.         $this->timeout $options['timeout'] ?? null;
  57.         $this->info['http_method'] = $method;
  58.         $this->info['user_data'] = $options['user_data'] ?? null;
  59.         $this->info['start_time'] = $this->info['start_time'] ?? microtime(true);
  60.         $info = &$this->info;
  61.         $headers = &$this->headers;
  62.         $debugBuffer $this->debugBuffer;
  63.         if (!$info['response_headers']) {
  64.             // Used to keep track of what we're waiting for
  65.             curl_setopt($chCURLOPT_PRIVATE, \in_array($method, ['GET''HEAD''OPTIONS''TRACE'], true) && 1.0 < (float) ($options['http_version'] ?? 1.1) ? 'H2' 'H0'); // H = headers + retry counter
  66.         }
  67.         curl_setopt($chCURLOPT_HEADERFUNCTION, static function ($chstring $data) use (&$info, &$headers$options$multi$id, &$location$resolveRedirect$logger): int {
  68.             if (!== substr_compare($data"\r\n", -2)) {
  69.                 return 0;
  70.             }
  71.             $len 0;
  72.             foreach (explode("\r\n"substr($data0, -2)) as $data) {
  73.                 $len += self::parseHeaderLine($ch$data$info$headers$options$multi$id$location$resolveRedirect$logger);
  74.             }
  75.             return $len;
  76.         });
  77.         if (null === $options) {
  78.             // Pushed response: buffer until requested
  79.             curl_setopt($chCURLOPT_WRITEFUNCTION, static function ($chstring $data) use ($multi$id): int {
  80.                 $multi->handlesActivity[$id][] = $data;
  81.                 curl_pause($chCURLPAUSE_RECV);
  82.                 return \strlen($data);
  83.             });
  84.             return;
  85.         }
  86.         $this->inflate = !isset($options['normalized_headers']['accept-encoding']);
  87.         curl_pause($chCURLPAUSE_CONT);
  88.         if ($onProgress $options['on_progress']) {
  89.             $url = isset($info['url']) ? ['url' => $info['url']] : [];
  90.             curl_setopt($chCURLOPT_NOPROGRESSfalse);
  91.             curl_setopt($chCURLOPT_PROGRESSFUNCTION, static function ($ch$dlSize$dlNow) use ($onProgress, &$info$url$multi$debugBuffer) {
  92.                 try {
  93.                     rewind($debugBuffer);
  94.                     $debug = ['debug' => stream_get_contents($debugBuffer)];
  95.                     $onProgress($dlNow$dlSize$url curl_getinfo($ch) + $info $debug);
  96.                 } catch (\Throwable $e) {
  97.                     $multi->handlesActivity[(int) $ch][] = null;
  98.                     $multi->handlesActivity[(int) $ch][] = $e;
  99.                     return 1// Abort the request
  100.                 }
  101.                 return null;
  102.             });
  103.         }
  104.         curl_setopt($chCURLOPT_WRITEFUNCTION, static function ($chstring $data) use ($multi$id): int {
  105.             $multi->handlesActivity[$id][] = $data;
  106.             return \strlen($data);
  107.         });
  108.         $this->initializer = static function (self $response) {
  109.             $waitFor curl_getinfo($ch $response->handleCURLINFO_PRIVATE);
  110.             return 'H' === $waitFor[0];
  111.         };
  112.         // Schedule the request in a non-blocking way
  113.         $multi->openHandles[$id] = [$ch$options];
  114.         curl_multi_add_handle($multi->handle$ch);
  115.     }
  116.     /**
  117.      * {@inheritdoc}
  118.      */
  119.     public function getInfo(string $type null)
  120.     {
  121.         if (!$info $this->finalInfo) {
  122.             $info array_merge($this->infocurl_getinfo($this->handle));
  123.             $info['url'] = $this->info['url'] ?? $info['url'];
  124.             $info['redirect_url'] = $this->info['redirect_url'] ?? null;
  125.             // workaround curl not subtracting the time offset for pushed responses
  126.             if (isset($this->info['url']) && $info['start_time'] / 1000 $info['total_time']) {
  127.                 $info['total_time'] -= $info['starttransfer_time'] ?: $info['total_time'];
  128.                 $info['starttransfer_time'] = 0.0;
  129.             }
  130.             rewind($this->debugBuffer);
  131.             $info['debug'] = stream_get_contents($this->debugBuffer);
  132.             $waitFor curl_getinfo($this->handleCURLINFO_PRIVATE);
  133.             if ('H' !== $waitFor[0] && 'C' !== $waitFor[0]) {
  134.                 curl_setopt($this->handleCURLOPT_VERBOSEfalse);
  135.                 rewind($this->debugBuffer);
  136.                 ftruncate($this->debugBuffer0);
  137.                 $this->finalInfo $info;
  138.             }
  139.         }
  140.         return null !== $type $info[$type] ?? null $info;
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function getContent(bool $throw true): string
  146.     {
  147.         $performing self::$performing;
  148.         self::$performing $performing || '_0' === curl_getinfo($this->handleCURLINFO_PRIVATE);
  149.         try {
  150.             return $this->doGetContent($throw);
  151.         } finally {
  152.             self::$performing $performing;
  153.         }
  154.     }
  155.     public function __destruct()
  156.     {
  157.         try {
  158.             if (null === $this->timeout) {
  159.                 return; // Unused pushed response
  160.             }
  161.             $e null;
  162.             $this->doDestruct();
  163.         } catch (HttpExceptionInterface $e) {
  164.             throw $e;
  165.         } finally {
  166.             if ($e ?? false) {
  167.                 throw $e;
  168.             }
  169.             $this->close();
  170.             if (!$this->multi->openHandles) {
  171.                 // Schedule DNS cache eviction for the next request
  172.                 $this->multi->dnsCache->evictions $this->multi->dnsCache->evictions ?: $this->multi->dnsCache->removals;
  173.                 $this->multi->dnsCache->removals $this->multi->dnsCache->hostnames = [];
  174.             }
  175.         }
  176.     }
  177.     /**
  178.      * {@inheritdoc}
  179.      */
  180.     private function close(): void
  181.     {
  182.         $this->inflate null;
  183.         unset($this->multi->openHandles[$this->id], $this->multi->handlesActivity[$this->id]);
  184.         curl_setopt($this->handleCURLOPT_PRIVATE'_0');
  185.         if (self::$performing) {
  186.             return;
  187.         }
  188.         curl_multi_remove_handle($this->multi->handle$this->handle);
  189.         curl_setopt_array($this->handle, [
  190.             CURLOPT_NOPROGRESS => true,
  191.             CURLOPT_PROGRESSFUNCTION => null,
  192.             CURLOPT_HEADERFUNCTION => null,
  193.             CURLOPT_WRITEFUNCTION => null,
  194.             CURLOPT_READFUNCTION => null,
  195.             CURLOPT_INFILE => null,
  196.         ]);
  197.     }
  198.     /**
  199.      * {@inheritdoc}
  200.      */
  201.     private static function schedule(self $response, array &$runningResponses): void
  202.     {
  203.         if (isset($runningResponses[$i = (int) $response->multi->handle])) {
  204.             $runningResponses[$i][1][$response->id] = $response;
  205.         } else {
  206.             $runningResponses[$i] = [$response->multi, [$response->id => $response]];
  207.         }
  208.         if ('_0' === curl_getinfo($ch $response->handleCURLINFO_PRIVATE)) {
  209.             // Response already completed
  210.             $response->multi->handlesActivity[$response->id][] = null;
  211.             $response->multi->handlesActivity[$response->id][] = null !== $response->info['error'] ? new TransportException($response->info['error']) : null;
  212.         }
  213.     }
  214.     /**
  215.      * {@inheritdoc}
  216.      *
  217.      * @param CurlClientState $multi
  218.      */
  219.     private static function perform(ClientState $multi, array &$responses null): void
  220.     {
  221.         if (self::$performing) {
  222.             if ($responses) {
  223.                 $response current($responses);
  224.                 $multi->handlesActivity[(int) $response->handle][] = null;
  225.                 $multi->handlesActivity[(int) $response->handle][] = new TransportException(sprintf('Userland callback cannot use the client nor the response while processing "%s".'curl_getinfo($response->handleCURLINFO_EFFECTIVE_URL)));
  226.             }
  227.             return;
  228.         }
  229.         try {
  230.             self::$performing true;
  231.             $active 0;
  232.             while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($multi->handle$active));
  233.             while ($info curl_multi_info_read($multi->handle)) {
  234.                 $result $info['result'];
  235.                 $id = (int) $ch $info['handle'];
  236.                 $waitFor = @curl_getinfo($chCURLINFO_PRIVATE) ?: '_0';
  237.                 if (\in_array($result, [CURLE_SEND_ERRORCURLE_RECV_ERROR/*CURLE_HTTP2*/ 16/*CURLE_HTTP2_STREAM*/ 92], true) && $waitFor[1] && 'C' !== $waitFor[0]) {
  238.                     curl_multi_remove_handle($multi->handle$ch);
  239.                     $waitFor[1] = (string) ((int) $waitFor[1] - 1); // decrement the retry counter
  240.                     curl_setopt($chCURLOPT_PRIVATE$waitFor);
  241.                     if ('1' === $waitFor[1]) {
  242.                         curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
  243.                     }
  244.                     if (=== curl_multi_add_handle($multi->handle$ch)) {
  245.                         continue;
  246.                     }
  247.                 }
  248.                 $multi->handlesActivity[$id][] = null;
  249.                 $multi->handlesActivity[$id][] = \in_array($result, [CURLE_OKCURLE_TOO_MANY_REDIRECTS], true) || '_0' === $waitFor || curl_getinfo($chCURLINFO_SIZE_DOWNLOAD) === curl_getinfo($chCURLINFO_CONTENT_LENGTH_DOWNLOAD) ? null : new TransportException(sprintf('%s for "%s".'curl_strerror($result), curl_getinfo($chCURLINFO_EFFECTIVE_URL)));
  250.             }
  251.         } finally {
  252.             self::$performing false;
  253.         }
  254.     }
  255.     /**
  256.      * {@inheritdoc}
  257.      *
  258.      * @param CurlClientState $multi
  259.      */
  260.     private static function select(ClientState $multifloat $timeout): int
  261.     {
  262.         if (\PHP_VERSION_ID 70123 || (70200 <= \PHP_VERSION_ID && \PHP_VERSION_ID 70211)) {
  263.             // workaround https://bugs.php.net/76480
  264.             $timeout min($timeout0.01);
  265.         }
  266.         return curl_multi_select($multi->handle$timeout);
  267.     }
  268.     /**
  269.      * Parses header lines as curl yields them to us.
  270.      */
  271.     private static function parseHeaderLine($chstring $data, array &$info, array &$headers, ?array $optionsCurlClientState $multiint $id, ?string &$location, ?callable $resolveRedirect, ?LoggerInterface $logger, &$content null): int
  272.     {
  273.         $waitFor = @curl_getinfo($chCURLINFO_PRIVATE) ?: '_0';
  274.         if ('H' !== $waitFor[0]) {
  275.             return \strlen($data); // Ignore HTTP trailers
  276.         }
  277.         if ('' !== $data) {
  278.             try {
  279.                 // Regular header line: add it to the list
  280.                 self::addResponseHeaders([$data], $info$headers);
  281.             } catch (TransportException $e) {
  282.                 $multi->handlesActivity[$id][] = null;
  283.                 $multi->handlesActivity[$id][] = $e;
  284.                 return \strlen($data);
  285.             }
  286.             if (!== strpos($data'HTTP/')) {
  287.                 if (=== stripos($data'Location:')) {
  288.                     $location trim(substr($data9));
  289.                 }
  290.                 return \strlen($data);
  291.             }
  292.             if (\function_exists('openssl_x509_read') && $certinfo curl_getinfo($chCURLINFO_CERTINFO)) {
  293.                 $info['peer_certificate_chain'] = array_map('openssl_x509_read'array_column($certinfo'Cert'));
  294.             }
  295.             if (300 <= $info['http_code'] && $info['http_code'] < 400) {
  296.                 if (curl_getinfo($chCURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
  297.                     curl_setopt($chCURLOPT_FOLLOWLOCATIONfalse);
  298.                 } elseif (303 === $info['http_code'] || ('POST' === $info['http_method'] && \in_array($info['http_code'], [301302], true))) {
  299.                     $info['http_method'] = 'HEAD' === $info['http_method'] ? 'HEAD' 'GET';
  300.                     curl_setopt($chCURLOPT_POSTFIELDS'');
  301.                 }
  302.             }
  303.             return \strlen($data);
  304.         }
  305.         // End of headers: handle informational responses, redirects, etc.
  306.         if (200 $statusCode curl_getinfo($chCURLINFO_RESPONSE_CODE)) {
  307.             $multi->handlesActivity[$id][] = new InformationalChunk($statusCode$headers);
  308.             $location null;
  309.             return \strlen($data);
  310.         }
  311.         $info['redirect_url'] = null;
  312.         if (300 <= $statusCode && $statusCode 400 && null !== $location) {
  313.             if (null === $info['redirect_url'] = $resolveRedirect($ch$location)) {
  314.                 $options['max_redirects'] = curl_getinfo($chCURLINFO_REDIRECT_COUNT);
  315.                 curl_setopt($chCURLOPT_FOLLOWLOCATIONfalse);
  316.                 curl_setopt($chCURLOPT_MAXREDIRS$options['max_redirects']);
  317.             } else {
  318.                 $url parse_url($location ?? ':');
  319.                 if (isset($url['host']) && null !== $ip $multi->dnsCache->hostnames[$url['host'] = strtolower($url['host'])] ?? null) {
  320.                     // Populate DNS cache for redirects if needed
  321.                     $port $url['port'] ?? ('http' === ($url['scheme'] ?? parse_url(curl_getinfo($chCURLINFO_EFFECTIVE_URL), PHP_URL_SCHEME)) ? 80 443);
  322.                     curl_setopt($chCURLOPT_RESOLVE, ["{$url['host']}:$port:$ip"]);
  323.                     $multi->dnsCache->removals["-{$url['host']}:$port"] = "-{$url['host']}:$port";
  324.                 }
  325.             }
  326.         }
  327.         if (401 === $statusCode && isset($options['auth_ntlm']) && === strncasecmp($headers['www-authenticate'][0] ?? '''NTLM '5)) {
  328.             // Continue with NTLM auth
  329.         } elseif ($statusCode 300 || 400 <= $statusCode || null === $location || curl_getinfo($chCURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
  330.             // Headers and redirects completed, time to get the response's content
  331.             $multi->handlesActivity[$id][] = new FirstChunk();
  332.             if ('HEAD' === $info['http_method'] || \in_array($statusCode, [204304], true)) {
  333.                 $waitFor '_0'// no content expected
  334.                 $multi->handlesActivity[$id][] = null;
  335.                 $multi->handlesActivity[$id][] = null;
  336.             } else {
  337.                 $waitFor[0] = 'C'// C = content
  338.             }
  339.             curl_setopt($chCURLOPT_PRIVATE$waitFor);
  340.         } elseif (null !== $info['redirect_url'] && $logger) {
  341.             $logger->info(sprintf('Redirecting: "%s %s"'$info['http_code'], $info['redirect_url']));
  342.         }
  343.         $location null;
  344.         return \strlen($data);
  345.     }
  346. }