Skip to content

Commit 7b07ab5

Browse files
committed
Add automatic gzip decoding of response (php-curl-class#740)
1 parent 77af156 commit 7b07ab5

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/Curl/Curl.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,8 @@ private function parseRequestHeaders($raw_headers)
21242124
* Returns the original raw response unless a default decoder has been set.
21252125
* If the response content-type cannot be determined:
21262126
* Returns the original raw response.
2127+
* If the response content-encoding is gzip:
2128+
* Returns the response gzip-decoded.
21272129
*/
21282130
private function parseResponse($response_headers, $raw_response)
21292131
{
@@ -2148,6 +2150,13 @@ private function parseResponse($response_headers, $raw_response)
21482150
}
21492151
}
21502152

2153+
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip') {
2154+
$decoded_response = gzdecode($response);
2155+
if ($decoded_response !== false) {
2156+
$response = $decoded_response;
2157+
}
2158+
}
2159+
21512160
return $response;
21522161
}
21532162

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,4 +4385,15 @@ public function testBeforeSendEachRequest()
43854385
$this->assertEquals(5, $test->curl->retries);
43864386
$this->assertFalse($test->curl->error);
43874387
}
4388+
4389+
public function testGzipDecoding()
4390+
{
4391+
$test = new Test();
4392+
$test->server('json_response', 'POST', [
4393+
'key' => 'content-encoding',
4394+
'value' => 'gzip',
4395+
'body' => gzencode('hello'),
4396+
]);
4397+
$this->assertEquals('hello', $test->curl->response);
4398+
}
43884399
}

tests/PHPCurlClass/server.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,22 @@
197197
$value = 'application/json';
198198
}
199199

200+
if (isset($_POST['body'])) {
201+
$body = $_POST['body'];
202+
} else {
203+
$body = json_encode([
204+
'null' => null,
205+
'true' => true,
206+
'false' => false,
207+
'integer' => 1,
208+
'float' => 3.14,
209+
'empty' => '',
210+
'string' => 'string',
211+
]);
212+
}
213+
200214
header($key . ': ' . $value);
201-
echo json_encode([
202-
'null' => null,
203-
'true' => true,
204-
'false' => false,
205-
'integer' => 1,
206-
'float' => 3.14,
207-
'empty' => '',
208-
'string' => 'string',
209-
]);
215+
echo $body;
210216
exit;
211217
} elseif ($test === 'xml_response') {
212218
$key = $_POST['key'];

0 commit comments

Comments
 (0)