Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

#76 Check content-length before to decode response body #156

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

Filter by extension

Filter by extension

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