From 97537417f01c17903fb4cebd59991eea57faa5e6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 2 Mar 2013 16:57:38 +1300 Subject: [PATCH] Factor out http.parse_response_line --- netlib/http.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/netlib/http.py b/netlib/http.py index 58993686b..bc09c8a10 100644 --- a/netlib/http.py +++ b/netlib/http.py @@ -274,6 +274,20 @@ def read_http_body_response(rfile, headers, limit): return read_http_body(500, rfile, headers, all, limit) +def parse_response_line(line): + parts = line.strip().split(" ", 2) + if len(parts) == 2: # handle missing message gracefully + parts.append("") + if len(parts) != 3: + return None + proto, code, msg = parts + try: + code = int(code) + except ValueError: + return None + return (proto, code, msg) + + def read_response(rfile, method, body_size_limit): """ Return an (httpversion, code, msg, headers, content) tuple. @@ -283,19 +297,13 @@ def read_response(rfile, method, body_size_limit): line = rfile.readline() if not line: raise HttpErrorConnClosed(502, "Server disconnect.") - parts = line.strip().split(" ", 2) - if len(parts) == 2: # handle missing message gracefully - parts.append("") - if not len(parts) == 3: + parts = parse_response_line(line) + if not parts: raise HttpError(502, "Invalid server response: %s"%repr(line)) proto, code, msg = parts httpversion = parse_http_protocol(proto) if httpversion is None: raise HttpError(502, "Invalid HTTP version in line: %s"%repr(proto)) - try: - code = int(code) - except ValueError: - raise HttpError(502, "Invalid server response: %s"%repr(line)) headers = read_headers(rfile) if headers is None: raise HttpError(502, "Invalid headers.")