Handle HTTP versions malformed due to non-integer major/minor numbers.

This commit is contained in:
Aldo Cortesi 2012-07-21 17:27:23 +12:00
parent 2387d2e8ed
commit 29f907ecf9
2 changed files with 7 additions and 2 deletions

View File

@ -145,8 +145,11 @@ def parse_http_protocol(s):
if "." not in version: if "." not in version:
return None return None
major, minor = version.split('.') major, minor = version.split('.')
try:
major = int(major) major = int(major)
minor = int(minor) minor = int(minor)
except ValueError:
return None
return major, minor return major, minor

View File

@ -106,6 +106,8 @@ def test_read_http_body():
def test_parse_http_protocol(): def test_parse_http_protocol():
assert http.parse_http_protocol("HTTP/1.1") == (1, 1) assert http.parse_http_protocol("HTTP/1.1") == (1, 1)
assert http.parse_http_protocol("HTTP/0.0") == (0, 0) assert http.parse_http_protocol("HTTP/0.0") == (0, 0)
assert not http.parse_http_protocol("HTTP/a.1")
assert not http.parse_http_protocol("HTTP/1.a")
assert not http.parse_http_protocol("foo/0.0") assert not http.parse_http_protocol("foo/0.0")
assert not http.parse_http_protocol("HTTP/x") assert not http.parse_http_protocol("HTTP/x")