File tree Expand file tree Collapse file tree 3 files changed +16
-2
lines changed Expand file tree Collapse file tree 3 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 93
93
r"{http_version}"
94
94
r" "
95
95
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}))?"
98
102
.format (** globals ()))
99
103
100
104
HEXDIG = r"[0-9A-Fa-f]"
Original file line number Diff line number Diff line change @@ -74,6 +74,9 @@ def maybe_read_from_SEND_RESPONSE_server(buf):
74
74
if not lines :
75
75
raise LocalProtocolError ("no response line received" )
76
76
matches = validate (status_line_re , lines [0 ])
77
+ # Tolerate missing reason phrases
78
+ if matches ["reason" ] is None :
79
+ matches ["reason" ] = b""
77
80
status_code = matches ["status_code" ] = int (matches ["status_code" ])
78
81
class_ = InformationalResponse if status_code < 200 else Response
79
82
return class_ (headers = list (_decode_header_lines (lines [1 :])), ** matches )
Original file line number Diff line number Diff line change @@ -142,6 +142,13 @@ def test_readers_unusual():
142
142
Response (status_code = 200 , headers = [("Foo" , "" )],
143
143
http_version = "1.0" , reason = b"OK" ))
144
144
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
+
145
152
# obsolete line folding
146
153
tr (READERS [CLIENT , IDLE ],
147
154
b"HEAD /foo HTTP/1.1\r \n "
You can’t perform that action at this time.
0 commit comments