Fix #747: Ensure string response before gzip decode

This commit is contained in:
Zach Borboa
2022-12-12 05:53:53 -08:00
parent c5a471ff2d
commit d8ed09f266
2 changed files with 23 additions and 1 deletions
+2 -1
View File
@@ -2155,7 +2155,8 @@ class Curl
}
}
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip') {
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip' &&
is_string($response)) {
// Use @ to suppress message "Warning gzdecode(): data error".
$decoded_response = @gzdecode($response);
if ($decoded_response !== false) {
+21
View File
@@ -4411,4 +4411,25 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('gzip', $test->curl->responseHeaders['content-encoding']);
$this->assertEquals('<html><body>not gzip-encoded</body></html>', $test->curl->response);
}
public function testGzipDecodingNonStringResponseWithoutError()
{
$test = new Test();
$test->curl->setDefaultDecoder(function () {
$response = new \stdClass();
$response->{'abc'} = 'foo';
$response->{'123'} = 'bar';
return $response;
});
$test->server('json_response', 'POST', [
'headers' => [
'content-type: text/html; charset=utf-8',
'content-encoding: gzip',
],
]);
$this->assertEquals('text/html; charset=utf-8', $test->curl->responseHeaders['content-type']);
$this->assertEquals('gzip', $test->curl->responseHeaders['content-encoding']);
$this->assertEquals('foo', $test->curl->response->{'abc'});
$this->assertEquals('bar', $test->curl->response->{'123'});
}
}