Skip to content

Tolerate missing reason phrases #32

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

Merged
merged 1 commit into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions h11/_abnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@
r"{http_version}"
r" "
r"(?P<status_code>{status_code})"
r" "
r"(?P<reason>{reason_phrase})"
# However, there are apparently a few too many servers out there that just
# leave out the reason phrase:
# https://github.com/scrapy/scrapy/issues/345#issuecomment-281756036
# https://github.com/seanmonstar/httparse/issues/29
# so make it optional. ?: is a non-capturing group.
r"(?: (?P<reason>{reason_phrase}))?"
.format(**globals()))

HEXDIG = r"[0-9A-Fa-f]"
Expand Down
3 changes: 3 additions & 0 deletions h11/_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def maybe_read_from_SEND_RESPONSE_server(buf):
if not lines:
raise LocalProtocolError("no response line received")
matches = validate(status_line_re, lines[0])
# Tolerate missing reason phrases
if matches["reason"] is None:
matches["reason"] = b""
status_code = matches["status_code"] = int(matches["status_code"])
class_ = InformationalResponse if status_code < 200 else Response
return class_(headers=list(_decode_header_lines(lines[1:])), **matches)
Expand Down
7 changes: 7 additions & 0 deletions h11/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ def test_readers_unusual():
Response(status_code=200, headers=[("Foo", "")],
http_version="1.0", reason=b"OK"))

# Tolerate broken servers that leave off the response code
tr(READERS[SERVER, SEND_RESPONSE],
b"HTTP/1.0 200\r\n"
b"Foo: bar\r\n\r\n",
Response(status_code=200, headers=[("Foo", "bar")],
http_version="1.0", reason=b""))

# obsolete line folding
tr(READERS[CLIENT, IDLE],
b"HEAD /foo HTTP/1.1\r\n"
Expand Down