Skip to content

Commit

Permalink
Merge pull request #1409 from jeskew/feature/report-original-length-o…
Browse files Browse the repository at this point in the history
…n-decode

Add original content encoding and length to responses
  • Loading branch information
mtdowling committed Mar 5, 2016
2 parents 739b9c8 + 1803e73 commit 30f8346
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Handler/EasyHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ public function createResponse()
if (!empty($this->options['decode_content'])
&& isset($normalizedKeys['content-encoding'])
) {
$headers['x-encoded-content-encoding']
= $headers[$normalizedKeys['content-encoding']];
unset($headers[$normalizedKeys['content-encoding']]);
if (isset($normalizedKeys['content-length'])) {
$headers['x-encoded-content-length']
= $headers[$normalizedKeys['content-length']];

$bodyLength = (int) $this->sink->getSize();
if ($bodyLength) {
$headers[$normalizedKeys['content-length']] = $bodyLength;
Expand Down
5 changes: 5 additions & 0 deletions src/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ private function checkDecode(array $options, array $headers, $stream)
$stream = new Psr7\InflateStream(
Psr7\stream_for($stream)
);
$headers['x-encoded-content-encoding']
= $headers[$normalizedKeys['content-encoding']];
// Remove content-encoding header
unset($headers[$normalizedKeys['content-encoding']]);
// Fix content-length header
if (isset($normalizedKeys['content-length'])) {
$headers['x-encoded-content-length']
= $headers[$normalizedKeys['content-length']];

$length = (int) $stream->getSize();
if ($length == 0) {
unset($headers[$normalizedKeys['content-length']]);
Expand Down
17 changes: 17 additions & 0 deletions tests/Handler/CurlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ public function testDecodesGzippedResponses()
$this->assertFalse($sent->hasHeader('Accept-Encoding'));
}

public function testReportsOriginalSizeAndContentEncodingAfterDecoding()
{
$this->addDecodeResponse();
$handler = new Handler\CurlMultiHandler();
$request = new Psr7\Request('GET', Server::$url);
$response = $handler($request, ['decode_content' => true]);
$response = $response->wait();
$this->assertSame(
'gzip',
$response->getHeaderLine('x-encoded-content-encoding')
);
$this->assertSame(
strlen(gzencode('test')),
(int) $response->getHeaderLine('x-encoded-content-length')
);
}

public function testDecodesGzippedResponsesWithHeader()
{
$this->addDecodeResponse();
Expand Down
24 changes: 24 additions & 0 deletions tests/Handler/StreamHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ public function testAutomaticallyDecompressGzip()
$this->assertTrue(!$response->hasHeader('content-length') || $response->getHeaderLine('content-length') == $response->getBody()->getSize());
}

public function testReportsOriginalSizeAndContentEncodingAfterDecoding()
{
Server::flush();
$content = gzencode('test');
Server::enqueue([
new Response(200, [
'Content-Encoding' => 'gzip',
'Content-Length' => strlen($content),
], $content)
]);
$handler = new StreamHandler();
$request = new Request('GET', Server::$url);
$response = $handler($request, ['decode_content' => true])->wait();

$this->assertSame(
'gzip',
$response->getHeaderLine('x-encoded-content-encoding')
);
$this->assertSame(
strlen($content),
(int) $response->getHeaderLine('x-encoded-content-length')
);
}

public function testDoesNotForceGzipDecode()
{
Server::flush();
Expand Down

0 comments on commit 30f8346

Please sign in to comment.