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

Commit a4eecc6

Browse files
committed
Merge branch 'feature/156' into develop
Close #156 Fixes #76
2 parents cc57f66 + 3cb295c commit a4eecc6

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ All notable changes to this project will be documented in this file, in reverse
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#156](https://github.com/zendframework/zend-http/pull/156) changes how gzip and deflate decompression occur in responses, ensuring
14+
that if the Content-Length header reports 0, no decompression is attempted,
15+
and an empty string is returned.
1416

1517
### Deprecated
1618

src/Response.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ protected function decodeGzip($body)
564564
);
565565
}
566566

567+
if ($this->getHeaders()->has('content-length')
568+
&& 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) {
569+
return '';
570+
}
571+
567572
ErrorHandler::start();
568573
$return = gzinflate(substr($body, 10));
569574
$test = ErrorHandler::stop();
@@ -594,6 +599,11 @@ protected function decodeDeflate($body)
594599
);
595600
}
596601

602+
if ($this->getHeaders()->has('content-length')
603+
&& 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) {
604+
return '';
605+
}
606+
597607
/**
598608
* Some servers (IIS ?) send a broken deflate response, without the
599609
* RFC-required zlib header.

test/ResponseTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ public function testGzipResponse()
125125
$this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getContent()));
126126
}
127127

128+
public function testGzipResponseWithEmptyBody()
129+
{
130+
$responseTest = <<<'REQ'
131+
HTTP/1.1 200 OK
132+
Date: Sun, 25 Jun 2006 19:36:47 GMT
133+
Server: Apache
134+
X-powered-by: PHP/5.1.4-pl3-gentoo
135+
Content-encoding: gzip
136+
Vary: Accept-Encoding
137+
Content-length: 0
138+
Connection: close
139+
Content-type: text/html
140+
141+
REQ;
142+
143+
$res = Response::fromString($responseTest);
144+
145+
$this->assertEquals('gzip', $res->getHeaders()->get('Content-encoding')->getFieldValue());
146+
$this->assertSame('', $res->getBody());
147+
$this->assertSame('', $res->getContent());
148+
}
149+
128150
public function testDeflateResponse()
129151
{
130152
$responseTest = file_get_contents(__DIR__ . '/_files/response_deflate');
@@ -136,6 +158,28 @@ public function testDeflateResponse()
136158
$this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getContent()));
137159
}
138160

161+
public function testDeflateResponseWithEmptyBody()
162+
{
163+
$responseTest = <<<'REQ'
164+
HTTP/1.1 200 OK
165+
Date: Sun, 25 Jun 2006 19:38:02 GMT
166+
Server: Apache
167+
X-powered-by: PHP/5.1.4-pl3-gentoo
168+
Content-encoding: deflate
169+
Vary: Accept-Encoding
170+
Content-length: 0
171+
Connection: close
172+
Content-type: text/html
173+
174+
REQ;
175+
176+
$res = Response::fromString($responseTest);
177+
178+
$this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
179+
$this->assertSame('', $res->getBody());
180+
$this->assertSame('', $res->getContent());
181+
}
182+
139183
/**
140184
* Make sure wer can handle non-RFC complient "deflate" responses.
141185
*

0 commit comments

Comments
 (0)