diff --git a/CurlHttpClient.php b/CurlHttpClient.php index 67a6c1d..244d880 100644 --- a/CurlHttpClient.php +++ b/CurlHttpClient.php @@ -236,8 +236,12 @@ public function request(string $method, string $url, array $options = []): Respo } if (!\is_string($body)) { + if (isset($options['auth_ntlm'])) { + $curlopts[\CURLOPT_FORBID_REUSE] = true; // Reusing NTLM connections requires seeking capability, which only string bodies support + } + if (\is_resource($body)) { - $curlopts[\CURLOPT_INFILE] = $body; + $curlopts[\CURLOPT_READDATA] = $body; } else { $curlopts[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) { static $eof = false; @@ -316,6 +320,9 @@ public function request(string $method, string $url, array $options = []): Respo } foreach ($curlopts as $opt => $value) { + if (\PHP_INT_SIZE === 8 && \defined('CURLOPT_INFILESIZE_LARGE') && \CURLOPT_INFILESIZE === $opt && $value >= 1 << 31) { + $opt = \CURLOPT_INFILESIZE_LARGE; + } if (null !== $value && !curl_setopt($ch, $opt, $value) && \CURLOPT_CERTINFO !== $opt && (!\defined('CURLOPT_HEADEROPT') || \CURLOPT_HEADEROPT !== $opt)) { $constantName = $this->findConstantName($opt); throw new TransportException(\sprintf('Curl option "%s" is not supported.', $constantName ?? $opt)); @@ -472,7 +479,7 @@ private function validateExtraCurlOptions(array $options): void \CURLOPT_RESOLVE => 'resolve', \CURLOPT_NOSIGNAL => 'timeout', \CURLOPT_HTTPHEADER => 'headers', - \CURLOPT_INFILE => 'body', + \CURLOPT_READDATA => 'body', \CURLOPT_READFUNCTION => 'body', \CURLOPT_INFILESIZE => 'body', \CURLOPT_POSTFIELDS => 'body', diff --git a/HttpClientTrait.php b/HttpClientTrait.php index 9709e0c..07f257a 100644 --- a/HttpClientTrait.php +++ b/HttpClientTrait.php @@ -355,9 +355,11 @@ private static function normalizeBody($body, array &$normalizedHeaders = []) } }); - $body = http_build_query($body, '', '&'); + if ('' === $body = http_build_query($body, '', '&')) { + return ''; + } - if ('' === $body || !$streams && !str_contains($normalizedHeaders['content-type'][0] ?? '', 'multipart/form-data')) { + if (!$streams && !str_contains($normalizedHeaders['content-type'][0] ?? '', 'multipart/form-data')) { if (!str_contains($normalizedHeaders['content-type'][0] ?? '', 'application/x-www-form-urlencoded')) { $normalizedHeaders['content-type'] = ['Content-Type: application/x-www-form-urlencoded']; } diff --git a/HttplugClient.php b/HttplugClient.php index 8e1dc1c..dad01dc 100644 --- a/HttplugClient.php +++ b/HttplugClient.php @@ -224,23 +224,44 @@ private function sendPsr7Request(RequestInterface $request, ?bool $buffer = null { try { $body = $request->getBody(); + $headers = $request->getHeaders(); - if ($body->isSeekable()) { - try { - $body->seek(0); - } catch (\RuntimeException) { - // ignore - } + $size = $request->getHeader('content-length')[0] ?? -1; + if (0 > $size && 0 < $size = $body->getSize() ?? -1) { + $headers['Content-Length'] = [$size]; } - $headers = $request->getHeaders(); - if (!$request->hasHeader('content-length') && 0 <= $size = $body->getSize() ?? -1) { - $headers['Content-Length'] = [$size]; + if (0 === $size) { + $body = ''; + } elseif (0 < $size && $size < 1 << 21) { + if ($body->isSeekable()) { + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } + } + + $body = $body->getContents(); + } else { + $body = static function (int $size) use ($body) { + if ($body->isSeekable()) { + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } + } + + while (!$body->eof()) { + yield $body->read($size); + } + }; } $options = [ 'headers' => $headers, - 'body' => static fn (int $size) => $body->read($size), + 'body' => $body, 'buffer' => $buffer, ]; diff --git a/Psr18Client.php b/Psr18Client.php index a2a1923..5ab4a8d 100644 --- a/Psr18Client.php +++ b/Psr18Client.php @@ -88,23 +88,44 @@ public function sendRequest(RequestInterface $request): ResponseInterface { try { $body = $request->getBody(); + $headers = $request->getHeaders(); - if ($body->isSeekable()) { - try { - $body->seek(0); - } catch (\RuntimeException) { - // ignore - } + $size = $request->getHeader('content-length')[0] ?? -1; + if (0 > $size && 0 < $size = $body->getSize() ?? -1) { + $headers['Content-Length'] = [$size]; } - $headers = $request->getHeaders(); - if (!$request->hasHeader('content-length') && 0 <= $size = $body->getSize() ?? -1) { - $headers['Content-Length'] = [$size]; + if (0 === $size) { + $body = ''; + } elseif (0 < $size && $size < 1 << 21) { + if ($body->isSeekable()) { + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } + } + + $body = $body->getContents(); + } else { + $body = static function (int $size) use ($body) { + if ($body->isSeekable()) { + try { + $body->seek(0); + } catch (\RuntimeException) { + // ignore + } + } + + while (!$body->eof()) { + yield $body->read($size); + } + }; } $options = [ 'headers' => $headers, - 'body' => static fn (int $size) => $body->read($size), + 'body' => $body, ]; if ('1.0' === $request->getProtocolVersion()) { diff --git a/Response/AmpResponseV4.php b/Response/AmpResponseV4.php index 23ab503..31ea22d 100644 --- a/Response/AmpResponseV4.php +++ b/Response/AmpResponseV4.php @@ -181,19 +181,17 @@ private static function schedule(self $response, array &$runningResponses): void /** * @param AmpClientStateV4 $multi */ - private static function perform(ClientState $multi, ?array &$responses = null): void + private static function perform(ClientState $multi, ?array $responses = null): void { - if ($responses) { - foreach ($responses as $response) { - try { - if ($response->info['start_time']) { - $response->info['total_time'] = microtime(true) - $response->info['start_time']; - ($response->onProgress)(); - } - } catch (\Throwable $e) { - $multi->handlesActivity[$response->id][] = null; - $multi->handlesActivity[$response->id][] = $e; + foreach ($responses ?? [] as $response) { + try { + if ($response->info['start_time']) { + $response->info['total_time'] = microtime(true) - $response->info['start_time']; + ($response->onProgress)(); } + } catch (\Throwable $e) { + $multi->handlesActivity[$response->id][] = null; + $multi->handlesActivity[$response->id][] = $e; } } } diff --git a/Response/AmpResponseV5.php b/Response/AmpResponseV5.php index 4f70851..8f56c76 100644 --- a/Response/AmpResponseV5.php +++ b/Response/AmpResponseV5.php @@ -162,7 +162,7 @@ private static function schedule(self $response, array &$runningResponses): void /** * @param AmpClientStateV5 $multi */ - private static function perform(ClientState $multi, ?array &$responses = null): void + private static function perform(ClientState $multi, ?array $responses = null): void { if ($responses) { foreach ($responses as $response) { diff --git a/Response/AsyncResponse.php b/Response/AsyncResponse.php index faf96a7..3008336 100644 --- a/Response/AsyncResponse.php +++ b/Response/AsyncResponse.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpClient\Response; use Symfony\Component\HttpClient\Chunk\ErrorChunk; -use Symfony\Component\HttpClient\Chunk\FirstChunk; use Symfony\Component\HttpClient\Chunk\LastChunk; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Contracts\HttpClient\ChunkInterface; @@ -245,7 +244,7 @@ public static function stream(iterable $responses, ?float $timeout = null, ?stri $wrappedResponses[] = $r->response; if ($r->stream) { - yield from self::passthruStream($response = $r->response, $r, new FirstChunk(), $asyncMap); + yield from self::passthruStream($response = $r->response, $r, $asyncMap, new LastChunk()); if (!isset($asyncMap[$response])) { array_pop($wrappedResponses); @@ -276,15 +275,9 @@ public static function stream(iterable $responses, ?float $timeout = null, ?stri } if (!$r->passthru) { - if (null !== $chunk->getError() || $chunk->isLast()) { - unset($asyncMap[$response]); - } elseif (null !== $r->content && '' !== ($content = $chunk->getContent()) && \strlen($content) !== fwrite($r->content, $content)) { - $chunk = new ErrorChunk($r->offset, new TransportException(\sprintf('Failed writing %d bytes to the response buffer.', \strlen($content)))); - $r->info['error'] = $chunk->getError(); - $r->response->cancel(); - } + $r->stream = (static fn () => yield $chunk)(); + yield from self::passthruStream($response, $r, $asyncMap); - yield $r => $chunk; continue; } @@ -347,13 +340,13 @@ private static function passthru(HttpClientInterface $client, self $r, ChunkInte } $r->stream = $stream; - yield from self::passthruStream($response, $r, null, $asyncMap); + yield from self::passthruStream($response, $r, $asyncMap); } /** * @param \SplObjectStorage|null $asyncMap */ - private static function passthruStream(ResponseInterface $response, self $r, ?ChunkInterface $chunk, ?\SplObjectStorage $asyncMap): \Generator + private static function passthruStream(ResponseInterface $response, self $r, ?\SplObjectStorage $asyncMap, ?ChunkInterface $chunk = null): \Generator { while (true) { try { diff --git a/Response/CurlResponse.php b/Response/CurlResponse.php index 996bd5b..e35132d 100644 --- a/Response/CurlResponse.php +++ b/Response/CurlResponse.php @@ -269,11 +269,11 @@ private static function schedule(self $response, array &$runningResponses): void /** * @param CurlClientState $multi */ - private static function perform(ClientState $multi, ?array &$responses = null): void + private static function perform(ClientState $multi, ?array $responses = null): void { if ($multi->performing) { if ($responses) { - $response = current($responses); + $response = $responses[array_key_first($responses)]; $multi->handlesActivity[(int) $response->handle][] = null; $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))); } diff --git a/Response/MockResponse.php b/Response/MockResponse.php index 6c68196..1598833 100644 --- a/Response/MockResponse.php +++ b/Response/MockResponse.php @@ -176,7 +176,7 @@ protected static function schedule(self $response, array &$runningResponses): vo $runningResponses[0][1][$response->id] = $response; } - protected static function perform(ClientState $multi, array &$responses): void + protected static function perform(ClientState $multi, array $responses): void { foreach ($responses as $response) { $id = $response->id; diff --git a/Response/NativeResponse.php b/Response/NativeResponse.php index b67b2cf..6d63085 100644 --- a/Response/NativeResponse.php +++ b/Response/NativeResponse.php @@ -229,7 +229,7 @@ private static function schedule(self $response, array &$runningResponses): void /** * @param NativeClientState $multi */ - private static function perform(ClientState $multi, ?array &$responses = null): void + private static function perform(ClientState $multi, ?array $responses = null): void { foreach ($multi->openHandles as $i => [$pauseExpiry, $h, $buffer, $onProgress]) { if ($pauseExpiry) { diff --git a/Response/TransportResponseTrait.php b/Response/TransportResponseTrait.php index 314b391..1d6f941 100644 --- a/Response/TransportResponseTrait.php +++ b/Response/TransportResponseTrait.php @@ -92,7 +92,7 @@ abstract protected static function schedule(self $response, array &$runningRespo /** * Performs all pending non-blocking operations. */ - abstract protected static function perform(ClientState $multi, array &$responses): void; + abstract protected static function perform(ClientState $multi, array $responses): void; /** * Waits for network activity. @@ -150,10 +150,15 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen $lastActivity = hrtime(true) / 1E9; $elapsedTimeout = 0; - if ($fromLastTimeout = 0.0 === $timeout && '-0' === (string) $timeout) { - $timeout = null; - } elseif ($fromLastTimeout = 0 > $timeout) { - $timeout = -$timeout; + if ((0.0 === $timeout && '-0' === (string) $timeout) || 0 > $timeout) { + $timeout = $timeout ? -$timeout : null; + + /** @var ClientState $multi */ + foreach ($runningResponses as [$multi]) { + if (null !== $multi->lastTimeout) { + $elapsedTimeout = max($elapsedTimeout, $lastActivity - $multi->lastTimeout); + } + } } while (true) { @@ -162,8 +167,7 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen $timeoutMin = $timeout ?? \INF; /** @var ClientState $multi */ - foreach ($runningResponses as $i => [$multi]) { - $responses = &$runningResponses[$i][1]; + foreach ($runningResponses as $i => [$multi, &$responses]) { self::perform($multi, $responses); foreach ($responses as $j => $response) { @@ -171,26 +175,25 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen $timeoutMin = min($timeoutMin, $response->timeout, 1); $chunk = false; - if ($fromLastTimeout && null !== $multi->lastTimeout) { - $elapsedTimeout = hrtime(true) / 1E9 - $multi->lastTimeout; - } - if (isset($multi->handlesActivity[$j])) { $multi->lastTimeout = null; + $elapsedTimeout = 0; } elseif (!isset($multi->openHandles[$j])) { + $hasActivity = true; unset($responses[$j]); continue; } elseif ($elapsedTimeout >= $timeoutMax) { $multi->handlesActivity[$j] = [new ErrorChunk($response->offset, \sprintf('Idle timeout reached for "%s".', $response->getInfo('url')))]; $multi->lastTimeout ??= $lastActivity; + $elapsedTimeout = $timeoutMax; } else { continue; } - while ($multi->handlesActivity[$j] ?? false) { - $hasActivity = true; - $elapsedTimeout = 0; + $lastActivity = null; + $hasActivity = true; + while ($multi->handlesActivity[$j] ?? false) { if (\is_string($chunk = array_shift($multi->handlesActivity[$j]))) { if (null !== $response->inflate && false === $chunk = @inflate_add($response->inflate, $chunk)) { $multi->handlesActivity[$j] = [null, new TransportException(\sprintf('Error while processing content unencoding for "%s".', $response->getInfo('url')))]; @@ -227,7 +230,6 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen } } elseif ($chunk instanceof ErrorChunk) { unset($responses[$j]); - $elapsedTimeout = $timeoutMax; } elseif ($chunk instanceof FirstChunk) { if ($response->logger) { $info = $response->getInfo(); @@ -278,10 +280,12 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) { // Ensure transport exceptions are always thrown $chunk->getContent(); + throw new \LogicException('A transport exception should have been thrown.'); } } if (!$responses) { + $hasActivity = true; unset($runningResponses[$i]); } @@ -295,7 +299,7 @@ public static function stream(iterable $responses, ?float $timeout = null): \Gen } if ($hasActivity) { - $lastActivity = hrtime(true) / 1E9; + $lastActivity ??= hrtime(true) / 1E9; continue; } diff --git a/Tests/AsyncDecoratorTraitTest.php b/Tests/AsyncDecoratorTraitTest.php index 1096a9e..b3cb83b 100644 --- a/Tests/AsyncDecoratorTraitTest.php +++ b/Tests/AsyncDecoratorTraitTest.php @@ -231,6 +231,20 @@ public function testBufferPurePassthru() $this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent()); $this->assertStringContainsString('HTTP_HOST', $response->getContent()); + + $client = new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface { + use AsyncDecoratorTrait; + + public function request(string $method, string $url, array $options = []): ResponseInterface + { + return new AsyncResponse($this->client, $method, $url, $options); + } + }; + + $response = $client->request('GET', 'http://localhost:8057/'); + + $this->assertStringContainsString('SERVER_PROTOCOL', $response->getContent()); + $this->assertStringContainsString('HTTP_HOST', $response->getContent()); } public function testRetryTimeout()