From 5854ae1286af2d06e61feea65cfe11a352506dc6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 5 Aug 2018 13:27:18 +0300 Subject: [PATCH] urequests: Add ability to parse response headers. This is controlled by parse_headers param to request(), which defaults to True for compatibility with upstream requests. In this case, headers are available as .headers of Response objects. They are however normal (not case-insensitive) dict. If parse_headers=False, old behavior of ignore response headers is used, which saves memory on the dict. Finally, parse_headers can be a custom function which can e.g. parse only subset of headers (again, to save memory). --- python-ecosys/urequests/urequests.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python-ecosys/urequests/urequests.py b/python-ecosys/urequests/urequests.py index 75a14570..1dc379bc 100644 --- a/python-ecosys/urequests/urequests.py +++ b/python-ecosys/urequests/urequests.py @@ -33,7 +33,7 @@ class Response: return ujson.loads(self.content) -def request(method, url, data=None, json=None, headers={}, stream=None): +def request(method, url, data=None, json=None, headers={}, stream=None, parse_headers=True): try: proto, dummy, host, path = url.split("/", 3) except ValueError: @@ -55,6 +55,10 @@ def request(method, url, data=None, json=None, headers={}, stream=None): ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) ai = ai[0] + resp_d = None + if parse_headers is not False: + resp_d = {} + s = usocket.socket(ai[0], ai[1], ai[2]) try: s.connect(ai[-1]) @@ -98,6 +102,14 @@ def request(method, url, data=None, json=None, headers={}, stream=None): raise ValueError("Unsupported " + l) elif l.startswith(b"Location:") and not 200 <= status <= 299: raise NotImplementedError("Redirects not yet supported") + if parse_headers is False: + pass + elif parse_headers is True: + l = str(l, "utf-8") + k, v = l.split(":", 1) + resp_d[k] = v.strip() + else: + parse_headers(l, resp_d) except OSError: s.close() raise @@ -105,6 +117,8 @@ def request(method, url, data=None, json=None, headers={}, stream=None): resp = Response(s) resp.status_code = status resp.reason = reason + if resp_d is not None: + resp.headers = resp_d return resp