Skip to content

Commit 918ce7b

Browse files
authored
Merge pull request php-curl-class#749 from zachborboa/master
Ensure string response before gzip decode
2 parents 7b2ae8a + 05ede8f commit 918ce7b

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/Curl/Curl.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,14 +1022,12 @@ public function setDefaultDecoder($mixed = 'json')
10221022
{
10231023
if ($mixed === false) {
10241024
$this->defaultDecoder = false;
1025+
} elseif ($mixed === 'json') {
1026+
$this->defaultDecoder = '\Curl\Decoder::decodeJson';
1027+
} elseif ($mixed === 'xml') {
1028+
$this->defaultDecoder = '\Curl\Decoder::decodeXml';
10251029
} elseif (is_callable($mixed)) {
10261030
$this->defaultDecoder = $mixed;
1027-
} else {
1028-
if ($mixed === 'json') {
1029-
$this->defaultDecoder = '\Curl\Decoder::decodeJson';
1030-
} elseif ($mixed === 'xml') {
1031-
$this->defaultDecoder = '\Curl\Decoder::decodeXml';
1032-
}
10331031
}
10341032
}
10351033

@@ -2155,7 +2153,8 @@ private function parseResponse($response_headers, $raw_response)
21552153
}
21562154
}
21572155

2158-
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip') {
2156+
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip' &&
2157+
is_string($response)) {
21592158
// Use @ to suppress message "Warning gzdecode(): data error".
21602159
$decoded_response = @gzdecode($response);
21612160
if ($decoded_response !== false) {

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,4 +4411,25 @@ public function testGzipDecodingFailureWithoutWarning()
44114411
$this->assertEquals('gzip', $test->curl->responseHeaders['content-encoding']);
44124412
$this->assertEquals('<html><body>not gzip-encoded</body></html>', $test->curl->response);
44134413
}
4414+
4415+
public function testGzipDecodingNonStringResponseWithoutError()
4416+
{
4417+
$test = new Test();
4418+
$test->curl->setDefaultDecoder(function () {
4419+
$response = new \stdClass();
4420+
$response->{'abc'} = 'foo';
4421+
$response->{'123'} = 'bar';
4422+
return $response;
4423+
});
4424+
$test->server('json_response', 'POST', [
4425+
'headers' => [
4426+
'content-type: text/html; charset=utf-8',
4427+
'content-encoding: gzip',
4428+
],
4429+
]);
4430+
$this->assertEquals('text/html; charset=utf-8', $test->curl->responseHeaders['content-type']);
4431+
$this->assertEquals('gzip', $test->curl->responseHeaders['content-encoding']);
4432+
$this->assertEquals('foo', $test->curl->response->{'abc'});
4433+
$this->assertEquals('bar', $test->curl->response->{'123'});
4434+
}
44144435
}

0 commit comments

Comments
 (0)