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

  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\Canary;
  16. use Symfony\Component\HttpClient\Internal\ClientState;
  17. use Symfony\Component\HttpClient\Internal\CurlClientState;
  18. use Symfony\Contracts\HttpClient\ResponseInterface;
  19. /**
  20.  * @author Nicolas Grekas <p@tchwork.com>
  21.  *
  22.  * @internal
  23.  */
  24. final class CurlResponse implements ResponseInterfaceStreamableInterface
  25. {
  26.     use CommonResponseTrait {
  27.         getContent as private doGetContent;
  28.     }
  29.     use TransportResponseTrait;
  30.     private CurlClientState $multi;
  31.     /**
  32.      * @var resource
  33.      */
  34.     private $debugBuffer;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(CurlClientState $multi\CurlHandle|string $ch, array $options nullLoggerInterface $logger nullstring $method 'GET', callable $resolveRedirect nullint $curlVersion nullstring $originalUrl null)
  39.     {
  40.         $this->multi $multi;
  41.         if ($ch instanceof \CurlHandle) {
  42.             $this->handle $ch;
  43.             $this->debugBuffer fopen('php://temp''w+');
  44.             if (0x074000 === $curlVersion) {
  45.                 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.');
  46.             } else {
  47.                 curl_setopt($ch\CURLOPT_VERBOSEtrue);
  48.                 curl_setopt($ch\CURLOPT_STDERR$this->debugBuffer);
  49.             }
  50.         } else {
  51.             $this->info['url'] = $ch;
  52.             $ch $this->handle;
  53.         }
  54.         $this->id $id = (int) $ch;
  55.         $this->logger $logger;
  56.         $this->shouldBuffer $options['buffer'] ?? true;
  57.         $this->timeout $options['timeout'] ?? null;
  58.         $this->info['http_method'] = $method;
  59.         $this->info['user_data'] = $options['user_data'] ?? null;
  60.         $this->info['max_duration'] = $options['max_duration'] ?? null;
  61.         $this->info['start_time'] ??= microtime(true);
  62.         $this->info['original_url'] = $originalUrl ?? $this->info['url'] ?? curl_getinfo($ch\CURLINFO_EFFECTIVE_URL);
  63.         $info = &$this->info;
  64.         $headers = &$this->headers;
  65.         $debugBuffer $this->debugBuffer;
  66.         if (!$info['response_headers']) {
  67.             // Used to keep track of what we're waiting for
  68.             curl_setopt($ch\CURLOPT_PRIVATE\in_array($method, ['GET''HEAD''OPTIONS''TRACE'], true) && 1.0 < (float) ($options['http_version'] ?? 1.1) ? 'H2' 'H0'); // H = headers + retry counter
  69.         }
  70.         curl_setopt($ch\CURLOPT_HEADERFUNCTION, static function ($chstring $data) use (&$info, &$headers$options$multi$id, &$location$resolveRedirect$logger): int {
  71.             return self::parseHeaderLine($ch$data$info$headers$options$multi$id$location$resolveRedirect$logger);
  72.         });
  73.         if (null === $options) {
  74.             // Pushed response: buffer until requested
  75.             curl_setopt($ch\CURLOPT_WRITEFUNCTION, static function ($chstring $data) use ($multi$id): int {
  76.                 $multi->handlesActivity[$id][] = $data;
  77.                 curl_pause($ch\CURLPAUSE_RECV);
  78.                 return \strlen($data);
  79.             });
  80.             return;
  81.         }
  82.         $execCounter $multi->execCounter;
  83.         $this->info['pause_handler'] = static function (float $duration) use ($ch$multi$execCounter) {
  84.             if ($duration) {
  85.                 if ($execCounter === $multi->execCounter) {
  86.                     $multi->execCounter = !\is_float($execCounter) ? $execCounter \PHP_INT_MIN;
  87.                     curl_multi_remove_handle($multi->handle$ch);
  88.                 }
  89.                 $lastExpiry end($multi->pauseExpiries);
  90.                 $multi->pauseExpiries[(int) $ch] = $duration += microtime(true);
  91.                 if (false !== $lastExpiry && $lastExpiry $duration) {
  92.                     asort($multi->pauseExpiries);
  93.                 }
  94.                 curl_pause($ch\CURLPAUSE_ALL);
  95.             } else {
  96.                 unset($multi->pauseExpiries[(int) $ch]);
  97.                 curl_pause($ch\CURLPAUSE_CONT);
  98.                 curl_multi_add_handle($multi->handle$ch);
  99.             }
  100.         };
  101.         $this->inflate = !isset($options['normalized_headers']['accept-encoding']);
  102.         curl_pause($ch\CURLPAUSE_CONT);
  103.         if ($onProgress $options['on_progress']) {
  104.             $url = isset($info['url']) ? ['url' => $info['url']] : [];
  105.             curl_setopt($ch\CURLOPT_NOPROGRESSfalse);
  106.             curl_setopt($ch\CURLOPT_PROGRESSFUNCTION, static function ($ch$dlSize$dlNow) use ($onProgress, &$info$url$multi$debugBuffer) {
  107.                 try {
  108.                     rewind($debugBuffer);
  109.                     $debug = ['debug' => stream_get_contents($debugBuffer)];
  110.                     $onProgress($dlNow$dlSize$url curl_getinfo($ch) + $info $debug);
  111.                 } catch (\Throwable $e) {
  112.                     $multi->handlesActivity[(int) $ch][] = null;
  113.                     $multi->handlesActivity[(int) $ch][] = $e;
  114.                     return 1// Abort the request
  115.                 }
  116.                 return null;
  117.             });
  118.         }
  119.         curl_setopt($ch\CURLOPT_WRITEFUNCTION, static function ($chstring $data) use ($multi$id): int {
  120.             if ('H' === (curl_getinfo($ch\CURLINFO_PRIVATE)[0] ?? null)) {
  121.                 $multi->handlesActivity[$id][] = null;
  122.                 $multi->handlesActivity[$id][] = new TransportException(sprintf('Unsupported protocol for "%s"'curl_getinfo($ch\CURLINFO_EFFECTIVE_URL)));
  123.                 return 0;
  124.             }
  125.             curl_setopt($ch\CURLOPT_WRITEFUNCTION, static function ($chstring $data) use ($multi$id): int {
  126.                 $multi->handlesActivity[$id][] = $data;
  127.                 return \strlen($data);
  128.             });
  129.             $multi->handlesActivity[$id][] = $data;
  130.             return \strlen($data);
  131.         });
  132.         $this->initializer = static function (self $response) {
  133.             $waitFor curl_getinfo($ch $response->handle\CURLINFO_PRIVATE);
  134.             return 'H' === $waitFor[0];
  135.         };
  136.         // Schedule the request in a non-blocking way
  137.         $multi->lastTimeout null;
  138.         $multi->openHandles[$id] = [$ch$options];
  139.         curl_multi_add_handle($multi->handle$ch);
  140.         $this->canary = new Canary(static function () use ($ch$multi$id) {
  141.             unset($multi->pauseExpiries[$id], $multi->openHandles[$id], $multi->handlesActivity[$id]);
  142.             curl_setopt($ch\CURLOPT_PRIVATE'_0');
  143.             if ($multi->performing) {
  144.                 return;
  145.             }
  146.             curl_multi_remove_handle($multi->handle$ch);
  147.             curl_setopt_array($ch, [
  148.                 \CURLOPT_NOPROGRESS => true,
  149.                 \CURLOPT_PROGRESSFUNCTION => null,
  150.                 \CURLOPT_HEADERFUNCTION => null,
  151.                 \CURLOPT_WRITEFUNCTION => null,
  152.                 \CURLOPT_READFUNCTION => null,
  153.                 \CURLOPT_INFILE => null,
  154.             ]);
  155.             if (!$multi->openHandles) {
  156.                 // Schedule DNS cache eviction for the next request
  157.                 $multi->dnsCache->evictions $multi->dnsCache->evictions ?: $multi->dnsCache->removals;
  158.                 $multi->dnsCache->removals $multi->dnsCache->hostnames = [];
  159.             }
  160.         });
  161.     }
  162.     public function getInfo(string $type null): mixed
  163.     {
  164.         if (!$info $this->finalInfo) {
  165.             $info array_merge($this->infocurl_getinfo($this->handle));
  166.             $info['url'] = $this->info['url'] ?? $info['url'];
  167.             $info['redirect_url'] = $this->info['redirect_url'] ?? null;
  168.             // workaround curl not subtracting the time offset for pushed responses
  169.             if (isset($this->info['url']) && $info['start_time'] / 1000 $info['total_time']) {
  170.                 $info['total_time'] -= $info['starttransfer_time'] ?: $info['total_time'];
  171.                 $info['starttransfer_time'] = 0.0;
  172.             }
  173.             rewind($this->debugBuffer);
  174.             $info['debug'] = stream_get_contents($this->debugBuffer);
  175.             $waitFor curl_getinfo($this->handle\CURLINFO_PRIVATE);
  176.             if ('H' !== $waitFor[0] && 'C' !== $waitFor[0]) {
  177.                 curl_setopt($this->handle\CURLOPT_VERBOSEfalse);
  178.                 rewind($this->debugBuffer);
  179.                 ftruncate($this->debugBuffer0);
  180.                 $this->finalInfo $info;
  181.             }
  182.         }
  183.         return null !== $type $info[$type] ?? null $info;
  184.     }
  185.     public function getContent(bool $throw true): string
  186.     {
  187.         $performing $this->multi->performing;
  188.         $this->multi->performing $performing || '_0' === curl_getinfo($this->handle\CURLINFO_PRIVATE);
  189.         try {
  190.             return $this->doGetContent($throw);
  191.         } finally {
  192.             $this->multi->performing $performing;
  193.         }
  194.     }
  195.     public function __destruct()
  196.     {
  197.         try {
  198.             if (null === $this->timeout) {
  199.                 return; // Unused pushed response
  200.             }
  201.             $this->doDestruct();
  202.         } finally {
  203.             if (\is_resource($this->handle) || $this->handle instanceof \CurlHandle) {
  204.                 curl_setopt($this->handle\CURLOPT_VERBOSEfalse);
  205.             }
  206.         }
  207.     }
  208.     private static function schedule(self $response, array &$runningResponses): void
  209.     {
  210.         if (isset($runningResponses[$i = (int) $response->multi->handle])) {
  211.             $runningResponses[$i][1][$response->id] = $response;
  212.         } else {
  213.             $runningResponses[$i] = [$response->multi, [$response->id => $response]];
  214.         }
  215.         if ('_0' === curl_getinfo($ch $response->handle\CURLINFO_PRIVATE)) {
  216.             // Response already completed
  217.             $response->multi->handlesActivity[$response->id][] = null;
  218.             $response->multi->handlesActivity[$response->id][] = null !== $response->info['error'] ? new TransportException($response->info['error']) : null;
  219.         }
  220.     }
  221.     /**
  222.      * @param CurlClientState $multi
  223.      */
  224.     private static function perform(ClientState $multi, array &$responses null): void
  225.     {
  226.         if ($multi->performing) {
  227.             if ($responses) {
  228.                 $response current($responses);
  229.                 $multi->handlesActivity[(int) $response->handle][] = null;
  230.                 $multi->handlesActivity[(int) $response->handle][] = new TransportException(sprintf('Userland callback cannot use the client nor the response while processing "%s".'curl_getinfo($response->handle\CURLINFO_EFFECTIVE_URL)));
  231.             }
  232.             return;
  233.         }
  234.         try {
  235.             $multi->performing true;
  236.             ++$multi->execCounter;
  237.             $active 0;
  238.             while (\CURLM_CALL_MULTI_PERFORM === ($err curl_multi_exec($multi->handle$active))) {
  239.             }
  240.             if (\CURLM_OK !== $err) {
  241.                 throw new TransportException(curl_multi_strerror($err));
  242.             }
  243.             while ($info curl_multi_info_read($multi->handle)) {
  244.                 if (\CURLMSG_DONE !== $info['msg']) {
  245.                     continue;
  246.                 }
  247.                 $result $info['result'];
  248.                 $id = (int) $ch $info['handle'];
  249.                 $waitFor = @curl_getinfo($ch\CURLINFO_PRIVATE) ?: '_0';
  250.                 if (\in_array($result, [\CURLE_SEND_ERROR\CURLE_RECV_ERROR/* CURLE_HTTP2 */ 16/* CURLE_HTTP2_STREAM */ 92], true) && $waitFor[1] && 'C' !== $waitFor[0]) {
  251.                     curl_multi_remove_handle($multi->handle$ch);
  252.                     $waitFor[1] = (string) ((int) $waitFor[1] - 1); // decrement the retry counter
  253.                     curl_setopt($ch\CURLOPT_PRIVATE$waitFor);
  254.                     curl_setopt($ch\CURLOPT_FORBID_REUSEtrue);
  255.                     if (=== curl_multi_add_handle($multi->handle$ch)) {
  256.                         continue;
  257.                     }
  258.                 }
  259.                 if (\CURLE_RECV_ERROR === $result && 'H' === $waitFor[0] && 400 <= ($responses[(int) $ch]->info['http_code'] ?? 0)) {
  260.                     $multi->handlesActivity[$id][] = new FirstChunk();
  261.                 }
  262.                 $multi->handlesActivity[$id][] = null;
  263.                 $multi->handlesActivity[$id][] = \in_array($result, [\CURLE_OK\CURLE_TOO_MANY_REDIRECTS], true) || '_0' === $waitFor || curl_getinfo($ch\CURLINFO_SIZE_DOWNLOAD) === curl_getinfo($ch\CURLINFO_CONTENT_LENGTH_DOWNLOAD) ? null : new TransportException(ucfirst(curl_error($ch) ?: curl_strerror($result)).sprintf(' for "%s".'curl_getinfo($ch\CURLINFO_EFFECTIVE_URL)));
  264.             }
  265.         } finally {
  266.             $multi->performing false;
  267.         }
  268.     }
  269.     /**
  270.      * @param CurlClientState $multi
  271.      */
  272.     private static function select(ClientState $multifloat $timeout): int
  273.     {
  274.         if ($multi->pauseExpiries) {
  275.             $now microtime(true);
  276.             foreach ($multi->pauseExpiries as $id => $pauseExpiry) {
  277.                 if ($now $pauseExpiry) {
  278.                     $timeout min($timeout$pauseExpiry $now);
  279.                     break;
  280.                 }
  281.                 unset($multi->pauseExpiries[$id]);
  282.                 curl_pause($multi->openHandles[$id][0], \CURLPAUSE_CONT);
  283.                 curl_multi_add_handle($multi->handle$multi->openHandles[$id][0]);
  284.             }
  285.         }
  286.         if (!== $selected curl_multi_select($multi->handle$timeout)) {
  287.             return $selected;
  288.         }
  289.         if ($multi->pauseExpiries && $timeout -= microtime(true) - $now) {
  290.             usleep((int) (1E6 $timeout));
  291.         }
  292.         return 0;
  293.     }
  294.     /**
  295.      * Parses header lines as curl yields them to us.
  296.      */
  297.     private static function parseHeaderLine($chstring $data, array &$info, array &$headers, ?array $optionsCurlClientState $multiint $id, ?string &$location, ?callable $resolveRedirect, ?LoggerInterface $logger): int
  298.     {
  299.         if (!str_ends_with($data"\r\n")) {
  300.             return 0;
  301.         }
  302.         $waitFor = @curl_getinfo($ch\CURLINFO_PRIVATE) ?: '_0';
  303.         if ('H' !== $waitFor[0]) {
  304.             return \strlen($data); // Ignore HTTP trailers
  305.         }
  306.         $statusCode curl_getinfo($ch\CURLINFO_RESPONSE_CODE);
  307.         if ($statusCode !== $info['http_code'] && !preg_match("#^HTTP/\d+(?:\.\d+)? {$statusCode}(?: |\r\n$)#"$data)) {
  308.             return \strlen($data); // Ignore headers from responses to CONNECT requests
  309.         }
  310.         if ("\r\n" !== $data) {
  311.             // Regular header line: add it to the list
  312.             self::addResponseHeaders([substr($data0, -2)], $info$headers);
  313.             if (!str_starts_with($data'HTTP/')) {
  314.                 if (=== stripos($data'Location:')) {
  315.                     $location trim(substr($data9, -2));
  316.                 }
  317.                 return \strlen($data);
  318.             }
  319.             if (\function_exists('openssl_x509_read') && $certinfo curl_getinfo($ch\CURLINFO_CERTINFO)) {
  320.                 $info['peer_certificate_chain'] = array_map('openssl_x509_read'array_column($certinfo'Cert'));
  321.             }
  322.             if (300 <= $info['http_code'] && $info['http_code'] < 400) {
  323.                 if (curl_getinfo($ch\CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
  324.                     curl_setopt($ch\CURLOPT_FOLLOWLOCATIONfalse);
  325.                 } elseif (303 === $info['http_code'] || ('POST' === $info['http_method'] && \in_array($info['http_code'], [301302], true))) {
  326.                     curl_setopt($ch\CURLOPT_POSTFIELDS'');
  327.                 }
  328.             }
  329.             return \strlen($data);
  330.         }
  331.         // End of headers: handle informational responses, redirects, etc.
  332.         if (200 $statusCode) {
  333.             $multi->handlesActivity[$id][] = new InformationalChunk($statusCode$headers);
  334.             $location null;
  335.             return \strlen($data);
  336.         }
  337.         $info['redirect_url'] = null;
  338.         if (300 <= $statusCode && $statusCode 400 && null !== $location) {
  339.             if ($noContent 303 === $statusCode || ('POST' === $info['http_method'] && \in_array($statusCode, [301302], true))) {
  340.                 $info['http_method'] = 'HEAD' === $info['http_method'] ? 'HEAD' 'GET';
  341.                 curl_setopt($ch\CURLOPT_CUSTOMREQUEST$info['http_method']);
  342.             }
  343.             if (null === $info['redirect_url'] = $resolveRedirect($ch$location$noContent)) {
  344.                 $options['max_redirects'] = curl_getinfo($ch\CURLINFO_REDIRECT_COUNT);
  345.                 curl_setopt($ch\CURLOPT_FOLLOWLOCATIONfalse);
  346.                 curl_setopt($ch\CURLOPT_MAXREDIRS$options['max_redirects']);
  347.             } else {
  348.                 $url parse_url($location ?? ':');
  349.                 if (isset($url['host']) && null !== $ip $multi->dnsCache->hostnames[$url['host'] = strtolower($url['host'])] ?? null) {
  350.                     // Populate DNS cache for redirects if needed
  351.                     $port $url['port'] ?? ('http' === ($url['scheme'] ?? parse_url(curl_getinfo($ch\CURLINFO_EFFECTIVE_URL), \PHP_URL_SCHEME)) ? 80 443);
  352.                     curl_setopt($ch\CURLOPT_RESOLVE, ["{$url['host']}:$port:$ip"]);
  353.                     $multi->dnsCache->removals["-{$url['host']}:$port"] = "-{$url['host']}:$port";
  354.                 }
  355.             }
  356.         }
  357.         if (401 === $statusCode && isset($options['auth_ntlm']) && === strncasecmp($headers['www-authenticate'][0] ?? '''NTLM '5)) {
  358.             // Continue with NTLM auth
  359.         } elseif ($statusCode 300 || 400 <= $statusCode || null === $location || curl_getinfo($ch\CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
  360.             // Headers and redirects completed, time to get the response's content
  361.             $multi->handlesActivity[$id][] = new FirstChunk();
  362.             if ('HEAD' === $info['http_method'] || \in_array($statusCode, [204304], true)) {
  363.                 $waitFor '_0'// no content expected
  364.                 $multi->handlesActivity[$id][] = null;
  365.                 $multi->handlesActivity[$id][] = null;
  366.             } else {
  367.                 $waitFor[0] = 'C'// C = content
  368.             }
  369.             curl_setopt($ch\CURLOPT_PRIVATE$waitFor);
  370.         } elseif (null !== $info['redirect_url'] && $logger) {
  371.             $logger->info(sprintf('Redirecting: "%s %s"'$info['http_code'], $info['redirect_url']));
  372.         }
  373.         $location null;
  374.         return \strlen($data);
  375.     }
  376. }