Skip to content

Commit

Permalink
zendframework#76 Check content-length before to decode response body
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu committed Oct 13, 2018
1 parent bdfe6ca commit 8ee196b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
*
Expand Down

0 comments on commit 8ee196b

Please sign in to comment.