From 703fdea1a56961031ea37f7a9b7bf4d9f51f48ed Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 2 Aug 2021 16:45:40 +0200 Subject: [PATCH] asgiapp: lowercase header names, refs #4717 (#4722) --- mitmproxy/addons/asgiapp.py | 4 ++-- test/mitmproxy/addons/test_asgiapp.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mitmproxy/addons/asgiapp.py b/mitmproxy/addons/asgiapp.py index b40f3d366..22286b302 100644 --- a/mitmproxy/addons/asgiapp.py +++ b/mitmproxy/addons/asgiapp.py @@ -76,11 +76,11 @@ def make_scope(flow: http.HTTPFlow) -> dict: }, "http_version": HTTP_VERSION_MAP.get(flow.request.http_version, "1.1"), "method": flow.request.method, - "scheme": flow.request.scheme, + "scheme": flow.request.scheme.upper(), "path": path, "raw_path": flow.request.path, "query_string": query_string, - "headers": list(list(x) for x in flow.request.headers.fields), + "headers": [(name.lower(), value) for (name, value) in flow.request.headers.fields], "client": flow.client_conn.peername, "extensions": { "mitmproxy.master": ctx.master, diff --git a/test/mitmproxy/addons/test_asgiapp.py b/test/mitmproxy/addons/test_asgiapp.py index 4eb15adb3..e6b041516 100644 --- a/test/mitmproxy/addons/test_asgiapp.py +++ b/test/mitmproxy/addons/test_asgiapp.py @@ -27,6 +27,11 @@ def request_check(): return json.dumps(args) +@tapp.route("/requestbody", methods=["POST"]) +def request_body(): + return json.dumps({"body": request.data.decode()}) + + @tapp.route("/error") def error(): raise ValueError("An exception...") @@ -71,6 +76,14 @@ async def test_asgi_full(): body = await reader.readuntil(b"}") assert body == b'{"param1": "1", "param2": "2"}' + reader, writer = await asyncio.open_connection(*proxy_addr) + req = f"POST http://testapp:80/requestbody HTTP/1.1\r\nContent-Length: 6\r\n\r\nHello!" + writer.write(req.encode()) + header = await reader.readuntil(b"\r\n\r\n") + assert header.startswith(b"HTTP/1.1 200 OK") + body = await reader.readuntil(b"}") + assert body == b'{"body": "Hello!"}' + reader, writer = await asyncio.open_connection(*proxy_addr) req = f"GET http://errapp:80/?foo=bar HTTP/1.1\r\n\r\n" writer.write(req.encode())