Skip to content

Commit

Permalink
Merge pull request #73 from fruux/fix-72
Browse files Browse the repository at this point in the history
Supporting empty Content-Length headers by ignoring them.
  • Loading branch information
evert authored Jan 2, 2017
2 parents 54f9e8e + 0c04680 commit dd50e72
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
ChangeLog
=========

4.2.2 (2017-01-02)
------------------

* #72: Handling clients that send invalid `Content-Length` headers.


4.2.1 (2016-01-06)
------------------

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require" : {
"php" : ">=5.4",
"ext-mbstring" : "*",
"ext-ctype" : "*",
"sabre/event" : ">=1.0.0,<4.0.0",
"sabre/uri" : "~1.0"
},
Expand Down
7 changes: 3 additions & 4 deletions lib/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ function getBodyAsString() {
return '';
}
$contentLength = $this->getHeader('Content-Length');
if (null === $contentLength) {
return stream_get_contents($body);
} else {
if (is_int($contentLength) || ctype_digit($contentLength)) {
return stream_get_contents($body, $contentLength);
} else {
return stream_get_contents($body);
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Version {
/**
* Full version number
*/
const VERSION = '4.2.1';
const VERSION = '4.2.2';

}
22 changes: 22 additions & 0 deletions tests/HTTP/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ function testLongStreamToStringBody() {

}

/**
* Some clients include a content-length header, but the header is empty.
* This is definitely broken behavior, but we should support it.
*/
function testEmptyContentLengthHeader() {

$body = fopen('php://memory', 'r+');
fwrite($body, 'abcdefg');
fseek($body, 2);

$message = new MessageMock();
$message->setBody($body);
$message->setHeader('Content-Length', '');

$this->assertEquals(
'cdefg',
$message->getBodyAsString()
);

}


function testGetEmptyBodyStream() {

$message = new MessageMock();
Expand Down

0 comments on commit dd50e72

Please sign in to comment.