fix some general awfulness with http header handling

This commit is contained in:
Connor Olding 2021-06-27 16:06:36 +02:00
parent f0e6f2d6da
commit 848d5791ed

View File

@ -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()