Skip to content

Fix default reason phrase if header line ends with CRLF - #26

Merged
sad-spirit merged 2 commits into
pear:trunkfrom
mensler:fix-default-reason-phrase
Oct 30, 2023
Merged

sad-spirit merged 2 commits into
pear:trunkfrom
mensler:fix-default-reason-phrase

Conversation

@mensler

@mensler mensler commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

I discovered that the reason phrase is empty if it's not part of the status line and the Curl adapter is used. In this case the line ends with CRLF (it does not, if the Socket adapter is used), like shown in the tests.

@sad-spirit sad-spirit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update your fix for better conformance to RFC 2616

Comment thread HTTP/Request2/Response.php Outdated
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})(?: (.+))?!', trim($statusLine), $m)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at section 6.1 of RFC 2616 which gives Status-Line as

       Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

and later the Reason-Phrase as

      Reason-Phrase  = *<TEXT, excluding CR, LF>

both my regex and your fix is incorrect. The former treats Reason-Phrase as optional (rather than mandatory but possibly empty) and the latter essentially allows Status-Line to start with whitespace. Can you fix the regex instead? I guess changing the part for the Reason-Phrase from (?: (.+))? to something like space here(.*) will work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, space here[^\\r\\n]* of course.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I got that right, but I changed it now to allow empty reason phrase (or spaces) and use the default then.

@sad-spirit
sad-spirit merged commit 7f515e8 into pear:trunk Oct 30, 2023
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Nov 4, 2023
2.6.0 (2023-11-01)

* Tested on PHP 8.2 and 8.3
* Use psalm for static analysis, several minor issues fixed
* Correctly parse HTTP status line with an empty reason-phrase
  (see pear/HTTP_Request2#26)
* Updated Public Suffix List
msk pushed a commit to msk/pkgsrc that referenced this pull request May 11, 2026
2.6.0 (2023-11-01)

* Tested on PHP 8.2 and 8.3
* Use psalm for static analysis, several minor issues fixed
* Correctly parse HTTP status line with an empty reason-phrase
  (see pear/HTTP_Request2#26)
* Updated Public Suffix List
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request May 13, 2026
2.6.0 (2023-11-01)

* Tested on PHP 8.2 and 8.3
* Use psalm for static analysis, several minor issues fixed
* Correctly parse HTTP status line with an empty reason-phrase
  (see pear/HTTP_Request2#26)
* Updated Public Suffix List
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Oct 13, 2026
2.6.0 (2023-11-01)

* Tested on PHP 8.2 and 8.3
* Use psalm for static analysis, several minor issues fixed
* Correctly parse HTTP status line with an empty reason-phrase
  (see pear/HTTP_Request2#26)
* Updated Public Suffix List
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants