Skip to content

Commit 6071491

Browse files
authored
Merge pull request #32 from njsmith/missing-reason-phrase
Tolerate missing reason phrases
2 parents 392c1d1 + 84ec638 commit 6071491

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

h11/_abnf.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@
9393
r"{http_version}"
9494
r" "
9595
r"(?P<status_code>{status_code})"
96-
r" "
97-
r"(?P<reason>{reason_phrase})"
96+
# However, there are apparently a few too many servers out there that just
97+
# leave out the reason phrase:
98+
# https://github.com/scrapy/scrapy/issues/345#issuecomment-281756036
99+
# https://github.com/seanmonstar/httparse/issues/29
100+
# so make it optional. ?: is a non-capturing group.
101+
r"(?: (?P<reason>{reason_phrase}))?"
98102
.format(**globals()))
99103

100104
HEXDIG = r"[0-9A-Fa-f]"

h11/_readers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def maybe_read_from_SEND_RESPONSE_server(buf):
7474
if not lines:
7575
raise LocalProtocolError("no response line received")
7676
matches = validate(status_line_re, lines[0])
77+
# Tolerate missing reason phrases
78+
if matches["reason"] is None:
79+
matches["reason"] = b""
7780
status_code = matches["status_code"] = int(matches["status_code"])
7881
class_ = InformationalResponse if status_code < 200 else Response
7982
return class_(headers=list(_decode_header_lines(lines[1:])), **matches)

h11/tests/test_io.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ def test_readers_unusual():
142142
Response(status_code=200, headers=[("Foo", "")],
143143
http_version="1.0", reason=b"OK"))
144144

145+
# Tolerate broken servers that leave off the response code
146+
tr(READERS[SERVER, SEND_RESPONSE],
147+
b"HTTP/1.0 200\r\n"
148+
b"Foo: bar\r\n\r\n",
149+
Response(status_code=200, headers=[("Foo", "bar")],
150+
http_version="1.0", reason=b""))
151+
145152
# obsolete line folding
146153
tr(READERS[CLIENT, IDLE],
147154
b"HEAD /foo HTTP/1.1\r\n"

0 commit comments

Comments
 (0)