asgi-proxy-lib icon indicating copy to clipboard operation
asgi-proxy-lib copied to clipboard

Ability to show even more verbose output

Open simonw opened this issue 1 year ago • 1 comments

Full responses would be good to see.

simonw avatar Nov 06 '24 07:11 simonw

I prototyped this like so:

diff --git a/asgi_proxy/__init__.py b/asgi_proxy/__init__.py
index 98ab369..af8c8e5 100644
--- a/asgi_proxy/__init__.py
+++ b/asgi_proxy/__init__.py
@@ -17,6 +17,8 @@ def asgi_proxy(backend, log=None):
         # Replace host header
         headers["host"] = backend_host.encode()
 
+        headers["accept-encoding"] = "identity"
+
         url_bits = [backend, path]
         if query_string:
             url_bits.extend(("?", query_string))
@@ -30,7 +32,12 @@ def asgi_proxy(backend, log=None):
             body += message.get("body", b"")
             more_body = message.get("more_body", False)
 
-        async with AsyncClient() as client:
+        if log is not None:
+            log.info("Request: %s %s", method, url)
+            log.info("Headers: %s", headers)
+            log.info("Body: %s", body)
+
+        async with AsyncClient(timeout=None) as client:
             try:
                 # Stream it, in case of long streaming responses
                 async with client.stream(
@@ -40,28 +47,30 @@ def asgi_proxy(backend, log=None):
                         log.info(f"Request: {method} {url}")
                         log.info(f"Response: {resp.status_code} {resp.reason_phrase}")
                     # Start the response
-                    await send(
-                        {
-                            "type": "http.response.start",
-                            "status": resp.status_code,
-                            "headers": [
-                                (k.encode(), v.encode())
-                                for k, v in resp.headers.items()
-                            ],
-                        }
-                    )
+                    start = {
+                        "type": "http.response.start",
+                        "status": resp.status_code,
+                        "headers": [
+                            (k.encode(), v.encode()) for k, v in resp.headers.items()
+                        ],
+                    }
+                    await send(start)
+                    if log:
+                        log.info("Start response: %s", start)
+
                     # Stream the content
                     try:
                         # aiter_raw not aiter_bytes because we don't want
                         # content decoding to have been applied
                         async for chunk in resp.aiter_raw():
-                            await send(
-                                {
-                                    "type": "http.response.body",
-                                    "body": chunk,
-                                    "more_body": True,
-                                }
-                            )
+                            d = {
+                                "type": "http.response.body",
+                                "body": chunk,
+                                "more_body": True,
+                            }
+                            await send(d)
+                            if log is not None:
+                                log.info("Response chunk: %s", chunk)
                     except Exception as e:
                         # The client has disconnected
                         if log:

simonw avatar Nov 06 '24 07:11 simonw