Skip to content

Commit a8479e8

Browse files
minor symfony#38508 [HttpClient] Content doesn't get decoded when fetched from an exception (HypeMC)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Content doesn't get decoded when fetched from an exception | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - My previous PR symfony#38493 created a new bug, the content doesn't get decoded when fetched from an exception because the inflate resource gets unset: ```php <?php use Symfony\Component\HttpClient\CurlHttpClient; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; include __DIR__.'/symfony/vendor/autoload.php'; $client = new CurlHttpClient(); try { $client->request('GET', 'http://example.com/404'); } catch (ClientExceptionInterface $exception) { echo $exception->getResponse()->getContent(false); } ``` I've removed the part where the resource gets unset since it still might be used at some point. The test is implementation independent so I believe it should go in contracts, please correct me if I wrong. Also, I was unable to find a way to do the test without adding a new endpoint this time, any suggestions would be appreciated. Commits ------- 8fa4f85 Don't unset the inflate resource on close as it might still be needed
2 parents 1cef665 + 8fa4f85 commit a8479e8

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ public function __destruct()
225225
*/
226226
private function close(): void
227227
{
228-
$this->inflate = null;
229228
unset($this->multi->openHandles[$this->id], $this->multi->handlesActivity[$this->id]);
230229
curl_setopt($this->handle, \CURLOPT_PRIVATE, '_0');
231230

src/Symfony/Component/HttpClient/Response/NativeResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private function open(): void
193193
private function close(): void
194194
{
195195
unset($this->multi->openHandles[$this->id], $this->multi->handlesActivity[$this->id]);
196-
$this->handle = $this->buffer = $this->inflate = $this->onProgress = null;
196+
$this->handle = $this->buffer = $this->onProgress = null;
197197
}
198198

199199
/**

src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
header('Content-Type: application/json', true, 404);
6464
break;
6565

66+
case '/404-gzipped':
67+
header('Content-Type: text/plain', true, 404);
68+
ob_start('ob_gzhandler');
69+
echo 'some text';
70+
exit;
71+
6672
case '/301':
6773
if ('Basic Zm9vOmJhcg==' === $vars['HTTP_AUTHORIZATION']) {
6874
header('Location: http://127.0.0.1:8057/302', true, 301);

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,18 @@ public function testGetContentAfterDestruct()
835835
}
836836
}
837837

838+
public function testGetEncodedContentAfterDestruct()
839+
{
840+
$client = $this->getHttpClient(__FUNCTION__);
841+
842+
try {
843+
$client->request('GET', 'http://localhost:8057/404-gzipped');
844+
$this->fail(ClientExceptionInterface::class.' expected');
845+
} catch (ClientExceptionInterface $e) {
846+
$this->assertSame('some text', $e->getResponse()->getContent(false));
847+
}
848+
}
849+
838850
public function testProxy()
839851
{
840852
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)