From ca2147d804914aa13a64655e296322ac6a0fae2f Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 1 Aug 2019 10:42:47 -0400 Subject: [PATCH 1/4] refactor request method to use parse_headers --- adafruit_requests.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index dfb32a7..d964f63 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -224,23 +224,13 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo reason = "" if len(line) > 2: reason = line[2].rstrip() - while True: - line = sock.readline() - if not line or line == b"\r\n": - break - - # print("**line: ", line) - title, content = line.split(b": ", 1) - if title and content: - title = str(title.lower(), "utf-8") - content = str(content, "utf-8") - resp.headers[title] = content - - if line.startswith(b"Transfer-Encoding:"): - if b"chunked" in line: - raise ValueError("Unsupported " + line) - elif line.startswith(b"Location:") and not 200 <= status <= 299: - raise NotImplementedError("Redirects not yet supported") + self.parse_headers(sock) + + if line.startswith(b"Transfer-Encoding:"): + if b"chunked" in line: + raise ValueError("Unsupported " + line) + elif line.startswith(b"Location:") and not 200 <= status <= 299: + raise NotImplementedError("Redirects not yet supported") except: sock.close() From 18d5293d1c5a176078fadb82ebdc245e1f103658 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 1 Aug 2019 10:51:22 -0400 Subject: [PATCH 2/4] remove self --- adafruit_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index d964f63..642c55f 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -224,7 +224,7 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo reason = "" if len(line) > 2: reason = line[2].rstrip() - self.parse_headers(sock) + parse_headers(sock) if line.startswith(b"Transfer-Encoding:"): if b"chunked" in line: From 8a87e1b46d43971a5ecf8458a726c5d37ad6f113 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 Aug 2019 11:31:37 -0400 Subject: [PATCH 3/4] refactor per mcosti review --- adafruit_requests.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 642c55f..a6c9b76 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -224,12 +224,11 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo reason = "" if len(line) > 2: reason = line[2].rstrip() - parse_headers(sock) - - if line.startswith(b"Transfer-Encoding:"): - if b"chunked" in line: - raise ValueError("Unsupported " + line) - elif line.startswith(b"Location:") and not 200 <= status <= 299: + resp.headers = parse_headers(sock) + if resp.headers.get("transfer-encoding"): + if "chunked" in resp.headers.get("transfer-encoding"): + raise ValueError("Unsupported " + str(line[0])) + elif resp.headers.get("location") and not 200 <= status <= 299: raise NotImplementedError("Redirects not yet supported") except: From 07fef5e2c95314659c77b5507f94e585a56d1a3e Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 2 Aug 2019 12:00:00 -0400 Subject: [PATCH 4/4] transfer encoding header value in exception instead --- adafruit_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index a6c9b76..f9f5001 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -227,7 +227,7 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo resp.headers = parse_headers(sock) if resp.headers.get("transfer-encoding"): if "chunked" in resp.headers.get("transfer-encoding"): - raise ValueError("Unsupported " + str(line[0])) + raise ValueError("Unsupported " + resp.headers.get("transfer-encoding")) elif resp.headers.get("location") and not 200 <= status <= 299: raise NotImplementedError("Redirects not yet supported")