Merge pull request #749 from zachborboa/master

Ensure string response before gzip decode
This commit is contained in:
Zach Borboa
2022-12-12 06:18:41 -08:00
committed by GitHub
2 changed files with 27 additions and 7 deletions
+6 -7
View File
@@ -1022,14 +1022,12 @@ class Curl
{
if ($mixed === false) {
$this->defaultDecoder = false;
} elseif ($mixed === 'json') {
$this->defaultDecoder = '\Curl\Decoder::decodeJson';
} elseif ($mixed === 'xml') {
$this->defaultDecoder = '\Curl\Decoder::decodeXml';
} elseif (is_callable($mixed)) {
$this->defaultDecoder = $mixed;
} else {
if ($mixed === 'json') {
$this->defaultDecoder = '\Curl\Decoder::decodeJson';
} elseif ($mixed === 'xml') {
$this->defaultDecoder = '\Curl\Decoder::decodeXml';
}
}
}
@@ -2155,7 +2153,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'});
}
}