Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add original content encoding and length to responses #1409

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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