Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting empty Content-Length headers by ignoring them. #73

Merged
merged 2 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this lib already depend on the ctype ext?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternativ could be a cast to int

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. I didn't realize it was an extension. I'll add it to the require list to make sure this is captured

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast to int would turn an empty string into a 0, and instead I want an invalid value to be ignored altogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$i = (int) $contentLength;
If ($i > 0 && $i != $contentLength)
...

But as you already depend on ctypes its fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah not a bad one =)

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