diff --git a/CHANGELOG.md b/CHANGELOG.md index d40f65d..63d85af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------ diff --git a/composer.json b/composer.json index b061194..507d5d2 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "require" : { "php" : ">=5.4", "ext-mbstring" : "*", + "ext-ctype" : "*", "sabre/event" : ">=1.0.0,<4.0.0", "sabre/uri" : "~1.0" }, diff --git a/lib/Message.php b/lib/Message.php index 0d73451..45bd183 100644 --- a/lib/Message.php +++ b/lib/Message.php @@ -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); } - } /** diff --git a/lib/Version.php b/lib/Version.php index 789ee45..a5a4274 100644 --- a/lib/Version.php +++ b/lib/Version.php @@ -14,6 +14,6 @@ class Version { /** * Full version number */ - const VERSION = '4.2.1'; + const VERSION = '4.2.2'; } diff --git a/tests/HTTP/MessageTest.php b/tests/HTTP/MessageTest.php index 0ca19ba..cb5aadc 100644 --- a/tests/HTTP/MessageTest.php +++ b/tests/HTTP/MessageTest.php @@ -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();