diff --git a/HTTP/Request2/Response.php b/HTTP/Request2/Response.php index 8e9cff8..7ff0912 100644 --- a/HTTP/Request2/Response.php +++ b/HTTP/Request2/Response.php @@ -218,7 +218,7 @@ public static function getDefaultReasonPhrase($code = null) */ public function __construct($statusLine, $bodyEncoded = true, $effectiveUrl = null) { - if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { + if (!preg_match('!^HTTP/(\d\.\d) (\d{3})( [^\r\n]*)?!', $statusLine, $m)) { throw new HTTP_Request2_MessageException( "Malformed response: {$statusLine}", HTTP_Request2_Exception::MALFORMED_RESPONSE @@ -226,7 +226,7 @@ public function __construct($statusLine, $bodyEncoded = true, $effectiveUrl = nu } $this->version = $m[1]; $this->code = intval($m[2]); - $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code); + $this->reasonPhrase = trim($m[3]) ?: self::getDefaultReasonPhrase($this->code); $this->bodyEncoded = (bool)$bodyEncoded; $this->effectiveUrl = (string)$effectiveUrl; } diff --git a/tests/Request2/ResponseTest.php b/tests/Request2/ResponseTest.php index 7d2dd49..44c00ee 100644 --- a/tests/Request2/ResponseTest.php +++ b/tests/Request2/ResponseTest.php @@ -41,6 +41,21 @@ public function testParseStatusLine() $this->assertEquals(222, $response2->getStatus()); $this->assertEquals('Nishtyak!', $response2->getReasonPhrase()); + $response3 = new HTTP_Request2_Response('HTTP/1.1 200 '); + $this->assertEquals('1.1', $response3->getVersion()); + $this->assertEquals(200, $response3->getStatus()); + $this->assertEquals('OK', $response3->getReasonPhrase()); + + $response4 = new HTTP_Request2_Response("HTTP/1.1 200 \r\n"); + $this->assertEquals('1.1', $response4->getVersion()); + $this->assertEquals(200, $response4->getStatus()); + $this->assertEquals('OK', $response4->getReasonPhrase()); + + $response5 = new HTTP_Request2_Response("HTTP/1.1 200 \r\n"); + $this->assertEquals('1.1', $response5->getVersion()); + $this->assertEquals(200, $response5->getStatus()); + $this->assertEquals('OK', $response5->getReasonPhrase()); + new HTTP_Request2_Response('Invalid status line'); }