Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function parseBody($content)
return;
}

if (strtolower($headers['Content-Type']) == 'application/x-www-form-urlencoded') {
elseif (strpos(strtolower($headers['Content-Type']), 'application/x-www-form-urlencoded') === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Why did you choose to go for === 0over !== false? My preference would be the latter, that leaves room for an client messing up and adding a space in front of it. (Just nitpicking a bit here.)

Copy link
Author

Choose a reason for hiding this comment

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

Either way is fine, and I can change it if you want.

I can't find anywhere in the the official documents that specifies that the header name must appear as the first character on a line, although I've never seen white space before a header in practice.

Perhaps this may be the least ambiguous:

strpos(strtolower(trim($headers['Content-Type'])), 'application/x-www-form-urlencoded') === 0

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, leave it as is. Was mostly nitpicking because I've seen clients (and servers) pull really odd stunts so trying to be a bit more defensive and prepared for such occasions.

parse_str(urldecode($content), $result);
$this->request->setPost($result);

Expand Down
29 changes: 27 additions & 2 deletions tests/RequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ public function testShouldReceiveMultiPartBody()
$this->assertEquals(2, count($request->getFiles()['files']));
}

public function testShouldReceivePostInBody()
/**
* @dataProvider shouldReceivePostInBodyDataProvider
*/
public function testShouldReceivePostInBody($postData)
{
$request = null;
$body = null;
Expand All @@ -154,7 +157,7 @@ public function testShouldReceivePostInBody()
$body = $parsedBodyBuffer;
});

$parser->feed($this->createPostWithContent());
$parser->feed($postData);

$this->assertInstanceOf('React\Http\Request', $request);
$this->assertSame('', $body);
Expand All @@ -164,6 +167,14 @@ public function testShouldReceivePostInBody()
);
}

public function shouldReceivePostInBodyDataProvider()
{
return [
[$this->createPostWithContent()],
[$this->createPostWithContentUtf8Charset()]
];
}

public function testHeaderOverflowShouldEmitError()
{
$error = null;
Expand Down Expand Up @@ -260,6 +271,20 @@ private function createPostWithContent()
return $data;
}

private function createPostWithContentUtf8Charset()
{
$data = "POST /foo?bar=baz HTTP/1.1\r\n";
$data .= "Host: localhost:8080\r\n";
$data .= "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:32.0) Gecko/20100101 Firefox/32.0\r\n";
$data .= "Connection: close\r\n";
$data .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind creating a separate test for this instead of adjusting an existing one so we can test this specific use case?

Copy link
Author

Choose a reason for hiding this comment

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

Will do. I'll have that commit shortly.

Copy link
Member

Choose a reason for hiding this comment

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

Splendid!

Copy link
Author

Choose a reason for hiding this comment

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

@WyriHaximus Working on this now (sorry for the delay); however, since we are effectively testing the same exact thing as RequestParserTest::testShouldReceivePostInBody() already does, would keeping the single test but creating a dataProvider for that test with multiple variations of headers work?

Copy link
Member

Choose a reason for hiding this comment

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

@caseyamcl No worries we all have a life outside github; Yes that has my preference. That was we can easily include more test cases by simple adding more entries in the data provider 👍

Copy link
Author

Choose a reason for hiding this comment

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

Life outside of Github? Nevaheardofit. Anyhow, I added a commit tonight. Let me know if you need any more changes.

$data .= "Content-Length: 79\r\n";
$data .= "\r\n";
$data .= "user=single&user2=second&users%5B%5D=first+in+array&users%5B%5D=second+in+array\r\n";

return $data;
}

private function createMultipartRequest()
{
$data = "POST / HTTP/1.1\r\n";
Expand Down