Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[zendframework/zendframework#4555] Do not double-decode gzipped respo…
Browse files Browse the repository at this point in the history
…nses via cURL

- cURL's CURLOPT_ENCODING setting can allow auto-decoding a response.
  Detect if set appropriately, and, if so, strip the Content-Encoding
  header in the response.
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
$this->response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->response);
}

// cURL can automatically handle content encoding; prevent double-decoding from occurring
if (isset($this->config['curloptions'][CURLOPT_ENCODING])
&& '' == $this->config['curloptions'][CURLOPT_ENCODING]
&& stripos($this->response, "Content-Encoding: gzip\r\n")
) {
$this->response = str_ireplace("Content-Encoding: gzip\r\n", '', $this->response);
}

// Eliminate multiple HTTP responses.
do {
$parts = preg_split('|(?:\r?\n){2}|m', $this->response, 2);
Expand Down
18 changes: 18 additions & 0 deletions test/Client/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,22 @@ public function testAuthorizeHeader()

$this->assertContains($header, $curlInfo['request_header'], 'Expecting valid basic authorization header');
}

/**
* @group 4555
*/
public function testResponseDoesNotDoubleDecodeGzippedBody()
{
$this->client->setUri($this->baseuri . 'testCurlGzipData.php');
$adapter = new Adapter\Curl();
$adapter->setOptions(array(
'curloptions' => array(
CURLOPT_ENCODING => '',
),
));
$this->client->setAdapter($adapter);
$this->client->setMethod('GET');
$this->client->send();
$this->assertEquals('Success', $this->client->getResponse()->getBody());
}
}
3 changes: 3 additions & 0 deletions test/Client/_files/testCurlGzipData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
header('Content-Encoding: gzip');
echo gzcompress('Success');

0 comments on commit b6cf95e

Please sign in to comment.