Skip to content

Commit

Permalink
Merge pull request #71 from blat/2.16.x
Browse files Browse the repository at this point in the history
Fix content deflating for IIS HTTP responses
  • Loading branch information
Ocramius authored Nov 11, 2022
2 parents 7300482 + 02ab6d2 commit 838825d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use function gzuncompress;
use function hexdec;
use function implode;
use function in_array;
use function is_array;
use function is_float;
use function is_numeric;
use function is_scalar;
use function ord;
use function preg_match;
use function sprintf;
use function strlen;
Expand Down Expand Up @@ -648,7 +650,7 @@ protected function decodeDeflate($body)
*/
$zlibHeader = unpack('n', substr($body, 0, 2));

if ($zlibHeader[1] % 31 === 0) {
if ($zlibHeader[1] % 31 === 0 && ord($body[0]) === 0x78 && in_array(ord($body[1]), [0x01, 0x5e, 0x9c, 0xda])) {
return gzuncompress($body);
}
return gzinflate($body);
Expand Down
16 changes: 16 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@ public function testNonStandardDeflateResponseLaminas6040()
$this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getContent()));
}

/**
* Make sure there no confusion with complient "deflate" responses.
*
* @link https://framework.zend.com/issues/browse/ZF-12457.html
*/
public function testStandardDeflateResponseLaminas12457()
{
$responseTest = file_get_contents(__DIR__ . '/_files/response_deflate_iis_valid');

$res = Response::fromString($responseTest);

$this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
$this->assertEquals('fa2f670a2da7cd7f0aee953ce5785fa8', md5($res->getBody()));
$this->assertEquals('992ec500e8332df89bbd9b8e998ec8c9', md5($res->getContent()));
}

public function testChunkedResponse()
{
$responseTest = file_get_contents(__DIR__ . '/_files/response_chunked');
Expand Down
Binary file added test/_files/response_deflate_iis_valid
Binary file not shown.

0 comments on commit 838825d

Please sign in to comment.