Skip to content

Commit 86df723

Browse files
FuNK3Ydpgeorge
FuNK3Y
authored andcommitted
aiohttp: Fix header case sensitivity.
According to RFC https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 header names are case-insensitive. This commit makes sure that the module behaves consistently regardless of the casing of "Content-type" and "Content-Length" (other headers are not considered by the module). Without this fix, the client seems to wait for the connection termination (~10 seconds) prior to returning any content if the casing of "Content-Length" is different. Signed-off-by: FuNK3Y <[email protected]>
1 parent 43ad7c5 commit 86df723

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

python-ecosys/aiohttp/aiohttp/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ class ClientResponse:
1818
def __init__(self, reader):
1919
self.content = reader
2020

21+
def _get_header(self, keyname, default):
22+
for k in self.headers:
23+
if k.lower() == keyname:
24+
return self.headers[k]
25+
return default
26+
2127
def _decode(self, data):
22-
c_encoding = self.headers.get("Content-Encoding")
28+
c_encoding = self._get_header("content-encoding", None)
2329
if c_encoding in ("gzip", "deflate", "gzip,deflate"):
2430
try:
2531
import deflate
@@ -39,10 +45,10 @@ async def read(self, sz=-1):
3945
return self._decode(await self.content.read(sz))
4046

4147
async def text(self, encoding="utf-8"):
42-
return (await self.read(int(self.headers.get("Content-Length", -1)))).decode(encoding)
48+
return (await self.read(int(self._get_header("content-length", -1)))).decode(encoding)
4349

4450
async def json(self):
45-
return _json.loads(await self.read(int(self.headers.get("Content-Length", -1))))
51+
return _json.loads(await self.read(int(self._get_header("content-length", -1))))
4652

4753
def __repr__(self):
4854
return "<ClientResponse %d %s>" % (self.status, self.headers)

python-ecosys/aiohttp/manifest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
metadata(
22
description="HTTP client module for MicroPython asyncio module",
3-
version="0.0.3",
3+
version="0.0.4",
44
pypi="aiohttp",
55
)
66

0 commit comments

Comments
 (0)