From 29f907ecf98468a89b5a7575b539938dc6741a8e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 21 Jul 2012 17:27:23 +1200 Subject: [PATCH] Handle HTTP versions malformed due to non-integer major/minor numbers. --- netlib/http.py | 7 +++++-- test/test_http.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/netlib/http.py b/netlib/http.py index acd9d85e3..88e66ce46 100644 --- a/netlib/http.py +++ b/netlib/http.py @@ -145,8 +145,11 @@ def parse_http_protocol(s): if "." not in version: return None major, minor = version.split('.') - major = int(major) - minor = int(minor) + try: + major = int(major) + minor = int(minor) + except ValueError: + return None return major, minor diff --git a/test/test_http.py b/test/test_http.py index 206fc4dfa..0174a4aa5 100644 --- a/test/test_http.py +++ b/test/test_http.py @@ -106,6 +106,8 @@ def test_read_http_body(): def test_parse_http_protocol(): assert http.parse_http_protocol("HTTP/1.1") == (1, 1) 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("HTTP/x")