Skip to content

Commit

Permalink
Fix default reason phrase if header line ends with CRLF (#26)
Browse files Browse the repository at this point in the history
* Fix default reason phrase if header line ends with CRLF

* Allow empty reason phrase, use default then

---------

Co-authored-by: Clemens Weiß <[email protected]>
  • Loading branch information
mensler and Clemens Weiß authored Oct 30, 2023
1 parent ac2ba5a commit 7f515e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions HTTP/Request2/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ 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
);
}
$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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Request2/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit 7f515e8

Please sign in to comment.