From acbe716802c74175b1fd5dd3e5fcae88ecc3a364 Mon Sep 17 00:00:00 2001 From: Casey McLaughlin Date: Tue, 11 Aug 2015 22:14:09 -0400 Subject: [PATCH 1/3] When checking 'application/x-www-form-urlencoded', check only beginning of string in case there is additional info (such as charset encoding) --- src/RequestParser.php | 2 +- tests/RequestParserTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RequestParser.php b/src/RequestParser.php index c213ec23..33d8cc40 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -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) { parse_str(urldecode($content), $result); $this->request->setPost($result); diff --git a/tests/RequestParserTest.php b/tests/RequestParserTest.php index 728166b3..e68d640c 100644 --- a/tests/RequestParserTest.php +++ b/tests/RequestParserTest.php @@ -252,7 +252,7 @@ private function createPostWithContent() $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\r\n"; + $data .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"; $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"; From 8fa4cf9209ddaf9b9f53dc563649a6c1e78f591b Mon Sep 17 00:00:00 2001 From: Casey McLaughlin Date: Tue, 11 Aug 2015 22:14:09 -0400 Subject: [PATCH 2/3] When checking 'application/x-www-form-urlencoded', check only beginning of string in case there is additional info (such as charset encoding) --- src/RequestParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RequestParser.php b/src/RequestParser.php index 33d8cc40..c691dfec 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -133,7 +133,7 @@ public function parseBody($content) return; } - elseif (strpos(strtolower($headers['Content-Type']), 'application/x-www-form-urlencoded') == 0) { + elseif (strpos(strtolower($headers['Content-Type']), 'application/x-www-form-urlencoded') === 0) { parse_str(urldecode($content), $result); $this->request->setPost($result); From 2fef878782c76907aa625caba4cc7e2a2f27d79e Mon Sep 17 00:00:00 2001 From: Casey McLaughlin Date: Thu, 10 Sep 2015 21:15:00 -0400 Subject: [PATCH 3/3] Refactored Request Parser Test --- tests/RequestParserTest.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/RequestParserTest.php b/tests/RequestParserTest.php index e68d640c..b5f140b1 100644 --- a/tests/RequestParserTest.php +++ b/tests/RequestParserTest.php @@ -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; @@ -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); @@ -164,6 +167,14 @@ public function testShouldReceivePostInBody() ); } + public function shouldReceivePostInBodyDataProvider() + { + return [ + [$this->createPostWithContent()], + [$this->createPostWithContentUtf8Charset()] + ]; + } + public function testHeaderOverflowShouldEmitError() { $error = null; @@ -247,6 +258,20 @@ private function createAdvancedPostRequest($content = '', $len = 0) } private function createPostWithContent() + { + $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\r\n"; + $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 createPostWithContentUtf8Charset() { $data = "POST /foo?bar=baz HTTP/1.1\r\n"; $data .= "Host: localhost:8080\r\n";