8000 [HttpClient] fix reading the body after a ClientException by nicolas-grekas · Pull Request #38530 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] fix reading the body after a ClientException #38530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Symfony/Component/HttpClient/Internal/Canary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient\Internal;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
final class Canary
{
private $canceller;

public function __construct(\Closure $canceller)
{
$this->canceller = $canceller;
}

public function cancel()
{
if (($canceller = $this->canceller) instanceof \Closure) {
$this->canceller = null;
$canceller();
}
}

public function __destruct()
{
$this->cancel();
}
}
69 changes: 29 additions & 40 deletions src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpClient\Chunk\FirstChunk;
use Symfony\Component\HttpClient\Chunk\InformationalChunk;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\Canary;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Component\HttpClient\Internal\CurlClientState;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -149,6 +150,31 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
// Schedule the request in a non-blocking way
$multi->openHandles[$id] = [$ch, $options];
curl_multi_add_handle($multi->handle, $ch);

$this->canary = new Canary(static function () use ($ch, $multi, $id) {
unset($multi->openHandles[$id], $multi->handlesActivity[$id]);
curl_setopt($ch, \CURLOPT_PRIVATE, '_0');

if (self::$performing) {
return;
}

curl_multi_remove_handle($multi->handle, $ch);
curl_setopt_array($ch, [
\CURLOPT_NOPROGRESS => true,
\CURLOPT_PROGRESSFUNCTION => null,
\CURLOPT_HEADERFUNCTION => null,
\CURLOPT_WRITEFUNCTION => null,
\CURLOPT_READFUNCTION => null,
\CURLOPT_INFILE => null,
]);

if (!$multi->openHandles) {
// Schedule DNS cache eviction for the next request
$multi->dnsCache->evictions = $multi->dnsCache->evictions ?: $multi->dnsCache->removals;
$multi->dnsCache->removals = $multi->dnsCache->hostnames = [];
}
});
}

/**
Expand Down Expand Up @@ -199,48 +225,11 @@ public function getContent(bool $throw = true): string

public function __destruct()
{
try {
if (null === $this->timeout) {
return; // Unused pushed response
}

$this->doDestruct();
} finally {
$multi = clone $this->multi;

$this->close();

if (!$this->multi->openHandles) {
// Schedule DNS cache eviction for the next request
$this->multi->dnsCache->evictions = $this->multi->dnsCache->evictions ?: $this->multi->dnsCache->removals;
$this->multi->dnsCache->removals = $this->multi->dnsCache->hostnames = [];
}

$this->multi = $multi;
}
}

/**
* {@inheritdoc}
*/
private function close(): void
{
unset($this->multi->openHandles[$this->id], $this->multi->handlesActivity[$this->id]);
curl_setopt($this->handle, \CURLOPT_PRIVATE, '_0');

if (self::$performing) {
return;
if (null === $this->timeout) {
return; // Unused pushed response
}

curl_multi_remove_handle($this->multi->handle, $this->handle);
curl_setopt_array($this->handle, [
\CURLOPT_NOPROGRESS => true,
\CURLOPT_PROGRESSFUNCTION => null,
\CURLOPT_HEADERFUNCTION => null,
\CURLOPT_WRITEFUNCTION => null,
\CURLOPT_READFUNCTION => null,
\CURLOPT_INFILE => null,
]);
$this->doDestruct();
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/Symfony/Component/HttpClient/Response/NativeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Chunk\FirstChunk;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\Canary;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Component\HttpClient\Internal\NativeClientState;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -43,7 +44,7 @@ final class NativeResponse implements ResponseInterface
public function __construct(NativeClientState $multi, $context, string $url, array $options, array &$info, callable $resolveRedirect, ?callable $onProgress, ?LoggerInterface $logger)
{
$this->multi = $multi;
$this->id = (int) $context;
$this->id = $id = (int) $context;
$this->context = $context;
$this->url = $url;
$this->logger = $logger;
Expand All @@ -63,6 +64,10 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
$this->initializer = static function (self $response) {
return null === $response->remaining;
};

$this->canary = new Canary(static function () use ($multi, $id) {
unset($multi->openHandles[$id], $multi->handlesActivity[$id]);
});
}

/**
Expand All @@ -88,17 +93,11 @@ public function __destruct()
try {
$this->doDestruct();
} finally {
$multi = clone $this->multi;

$this->close();

// Clear the DNS cache when all requests completed
if (0 >= --$this->multi->responseCount) {
$this->multi->responseCount = 0;
$this->multi->dnsCache = [];
}

$this->multi = $multi;
}
}

Expand Down Expand Up @@ -192,8 +191,8 @@ private function open(): void
*/
private function close(): void
{
unset($this->multi->openHandles[$this->id], $this->multi->handlesActivity[$this->id]);
$this->handle = $this->buffer = $this->onProgress = null;
$this->canary->cancel();
$this->handle = $this->buffer = $this->inflate = $this->onProgress = null;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/HttpClient/Response/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ trait ResponseTrait
{
private $logger;
private $headers = [];
private $canary;

/**
* @var callable|null A callback that initializes the two previous properties
Expand Down Expand Up @@ -207,7 +208,11 @@ public function toStream(bool $throw = true)
/**
* Closes the response and all its network handles.
*/
abstract protected function close(): void;
private function close(): void
{
$this->canary->cancel();
$this->inflate = null;
}

/**
* Adds pending responses to the activity list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function testHandleIsRemovedOnException()
// The response content-type mustn't be json as that calls getContent
// @see src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php:58
$this->assertStringNotContainsString('json', $e->getResponse()->getHeaders(false)['content-type'][0] ?? '');
unset($e);

$r = new \ReflectionProperty($client, 'multi');
$r->setAccessible(true);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
case '/404-gzipped':
header('Content-Type: text/plain', true, 404);
ob_start('ob_gzhandler');
@ob_flush();
flush();
usleep(300000);
echo 'some text';
exit;

Expand Down
0