From 9cf0c48264176b34da485833ec9e4aca36d49d54 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Mon, 2 Jan 2017 14:07:11 -0500 Subject: [PATCH 1/2] Supporting empty Content-Length headers by ignoring them. Fixes #72 --- CHANGELOG.md | 6 ++++++ lib/Message.php | 7 +++---- lib/Version.php | 2 +- tests/HTTP/MessageTest.php | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) 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/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(); From 0c046809ab0d0f922b1a876ce4f9e32dc17a9c49 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Mon, 2 Jan 2017 14:12:37 -0500 Subject: [PATCH 2/2] ctype extension is required --- composer.json | 1 + 1 file changed, 1 insertion(+) 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" },