Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 30, 2023

Conversation

mensler
Copy link
Contributor

@mensler mensler commented Mar 16, 2022

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.

Copy link
Member

@sad-spirit sad-spirit left a comment

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

@@ -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})(?: (.+))?!', trim($statusLine), $m)) {
Copy link
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
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
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
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.

None yet

2 participants