mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 12:14:36 +00:00
Merge pull request #749 from zachborboa/master
Ensure string response before gzip decode
This commit is contained in:
+6
-7
@@ -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) {
|
||||
|
||||
@@ -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'});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user