diff --git a/src/Response.php b/src/Response.php index 52f6a2722e..f3547195c3 100644 --- a/src/Response.php +++ b/src/Response.php @@ -564,6 +564,11 @@ protected function decodeGzip($body) ); } + if ($this->getHeaders()->has('content-length') + && 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) { + return ''; + } + ErrorHandler::start(); $return = gzinflate(substr($body, 10)); $test = ErrorHandler::stop(); @@ -594,6 +599,11 @@ protected function decodeDeflate($body) ); } + if ($this->getHeaders()->has('content-length') + && 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) { + return ''; + } + /** * Some servers (IIS ?) send a broken deflate response, without the * RFC-required zlib header. diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 339716f787..4df72fb148 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -125,6 +125,28 @@ public function testGzipResponse() $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getContent())); } + public function testGzipResponseWithEmptyBody() + { + $responseTest = <<<'REQ' +HTTP/1.1 200 OK +Date: Sun, 25 Jun 2006 19:36:47 GMT +Server: Apache +X-powered-by: PHP/5.1.4-pl3-gentoo +Content-encoding: gzip +Vary: Accept-Encoding +Content-length: 0 +Connection: close +Content-type: text/html + +REQ; + + $res = Response::fromString($responseTest); + + $this->assertEquals('gzip', $res->getHeaders()->get('Content-encoding')->getFieldValue()); + $this->assertSame('', $res->getBody()); + $this->assertSame('', $res->getContent()); + } + public function testDeflateResponse() { $responseTest = file_get_contents(__DIR__ . '/_files/response_deflate'); @@ -136,6 +158,28 @@ public function testDeflateResponse() $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getContent())); } + public function testDeflateResponseWithEmptyBody() + { + $responseTest = <<<'REQ' +HTTP/1.1 200 OK +Date: Sun, 25 Jun 2006 19:38:02 GMT +Server: Apache +X-powered-by: PHP/5.1.4-pl3-gentoo +Content-encoding: deflate +Vary: Accept-Encoding +Content-length: 0 +Connection: close +Content-type: text/html + +REQ; + + $res = Response::fromString($responseTest); + + $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue()); + $this->assertSame('', $res->getBody()); + $this->assertSame('', $res->getContent()); + } + /** * Make sure wer can handle non-RFC complient "deflate" responses. *