Permalink
Browse files

Parse request body correctly with crlf

  • Loading branch information...
1 parent d8fd1d5 commit 786fe7056cca44a5b1c3433df3962a280b8b1a03 @igorw committed Aug 26, 2012
Showing with 20 additions and 3 deletions.
  1. +17 −0 src/parse-request-test.php
  2. +3 −3 src/parse-request.php
View
17 src/parse-request-test.php
@@ -18,3 +18,20 @@
];
assert($expected === parseRequest($request));
+
+$request = implode("\r\n", [
+ 'POST / HTTP/1.1',
+ 'Host: igor.io',
+ '',
+ "foo\r\nbar",
+]);
+
+$expected = [
+ 'method' => 'POST',
+ 'path' => '/',
+ 'protocol' => 'HTTP/1.1',
+ 'headers' => ['Host' => 'igor.io'],
+ 'body' => "foo\r\nbar",
+];
+
+assert($expected === parseRequest($request));
View
6 src/parse-request.php
@@ -2,7 +2,9 @@
function parseRequest($request)
{
- $lines = explode("\r\n", $request);
+ list($head, $body) = explode("\r\n\r\n", $request, 2);
+
+ $lines = explode("\r\n", $head);
$requestLine = array_shift($lines);
list($method, $path, $protocol) = explode(' ', $requestLine);
@@ -13,7 +15,5 @@ function parseRequest($request)
$headers[trim($name)] = trim($value);
}
- $body = array_shift($lines);
-
return compact('method', 'path', 'protocol', 'headers', 'body');
}

0 comments on commit 786fe70

Please sign in to comment.