From 848d5791ed8c97ce4462dafe56b33834c4e96e83 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 27 Jun 2021 16:06:36 +0200 Subject: [PATCH] fix some general awfulness with http header handling --- respodns/ip_info.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/respodns/ip_info.py b/respodns/ip_info.py index 1abab39..365a2a8 100644 --- a/respodns/ip_info.py +++ b/respodns/ip_info.py @@ -36,6 +36,9 @@ class IpInfoByIpApi(IpInfoBase): def _parse_headers(self, it): # returns an error message as a string, or None. + x_cooldown = None + x_remaining = None + for line in it: if line == b"": break # end of header @@ -106,27 +109,17 @@ class IpInfoByIpApi(IpInfoBase): response = await reader.read() lines = response.splitlines() - err = None it = iter(lines) - - line = next(it) - if line != b"HTTP/1.1 200 OK": - self.cooldown = time() + 60 - err = "not ok" - it = () # exhaust iterator (not really) - - x_cooldown = None - x_remaining = None - - if err is None: + if next(it, None) == b"HTTP/1.1 200 OK": err = self._parse_headers(it) - - for i, line in enumerate(it): - if i == 0: - code = line - else: + code = next(it, None) + if code is None: + err = "missing body" + if next(it, None) is not None: err = "too many lines" - break + else: + err = "not ok" + self.cooldown = time() + 60 writer.close()