From 7f515e81eae0b35e09976812fe036455955d4476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Clemens=20Wei=C3=9F?= Date: Mon, 30 Oct 2023 23:34:49 +0100 Subject: [PATCH] Fix default reason phrase if header line ends with CRLF (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix default reason phrase if header line ends with CRLF * Allow empty reason phrase, use default then --------- Co-authored-by: Clemens Weiß --- HTTP/Request2/Response.php | 4 ++-- tests/Request2/ResponseTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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'); }