asgiapp: lowercase header names, refs #4717 (#4722)

This commit is contained in:
Maximilian Hils 2021-08-02 16:45:40 +02:00 committed by GitHub
parent 648a799525
commit 703fdea1a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -76,11 +76,11 @@ def make_scope(flow: http.HTTPFlow) -> dict:
}, },
"http_version": HTTP_VERSION_MAP.get(flow.request.http_version, "1.1"), "http_version": HTTP_VERSION_MAP.get(flow.request.http_version, "1.1"),
"method": flow.request.method, "method": flow.request.method,
"scheme": flow.request.scheme, "scheme": flow.request.scheme.upper(),
"path": path, "path": path,
"raw_path": flow.request.path, "raw_path": flow.request.path,
"query_string": query_string, "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, "client": flow.client_conn.peername,
"extensions": { "extensions": {
"mitmproxy.master": ctx.master, "mitmproxy.master": ctx.master,

View File

@ -27,6 +27,11 @@ def request_check():
return json.dumps(args) return json.dumps(args)
@tapp.route("/requestbody", methods=["POST"])
def request_body():
return json.dumps({"body": request.data.decode()})
@tapp.route("/error") @tapp.route("/error")
def error(): def error():
raise ValueError("An exception...") raise ValueError("An exception...")
@ -71,6 +76,14 @@ async def test_asgi_full():
body = await reader.readuntil(b"}") body = await reader.readuntil(b"}")
assert body == b'{"param1": "1", "param2": "2"}' 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) reader, writer = await asyncio.open_connection(*proxy_addr)
req = f"GET http://errapp:80/?foo=bar HTTP/1.1\r\n\r\n" req = f"GET http://errapp:80/?foo=bar HTTP/1.1\r\n\r\n"
writer.write(req.encode()) writer.write(req.encode())