From 2f1eed51a130e7c1ba9433ec5c0137589d5dcb63 Mon Sep 17 00:00:00 2001 From: kaniini Date: Sun, 18 Nov 2018 00:07:36 +0000 Subject: [PATCH] relay: add http request debugger --- relay/actor.py | 3 ++- relay/http_debug.py | 12 ++++++++++++ relay/remote_actor.py | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 relay/http_debug.py diff --git a/relay/actor.py b/relay/actor.py index 8ddcf0e..99a7dda 100644 --- a/relay/actor.py +++ b/relay/actor.py @@ -9,6 +9,7 @@ import simplejson as json import cgi from Crypto.PublicKey import RSA from .database import DATABASE +from .http_debug import http_debug # generate actor keys if not present @@ -86,7 +87,7 @@ async def push_message_to_actor(actor, message, our_key_id): logging.debug('%r >> %r', inbox, message) - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session: async with session.post(inbox, data=data, headers=headers) as resp: resp_payload = await resp.text() logging.debug('%r >> resp %r', inbox, resp_payload) diff --git a/relay/http_debug.py b/relay/http_debug.py new file mode 100644 index 0000000..4bb87c3 --- /dev/null +++ b/relay/http_debug.py @@ -0,0 +1,12 @@ +import logging +import aiohttp + + +async def on_request_start(session, trace_config_ctx, params): + logging.debug("HTTP START [%r], [%r]", session, params) + + +def http_debug(): + trace_config = aiohttp.TraceConfig() + trace_config.on_request_start.append(on_request_start) + return trace_config diff --git a/relay/remote_actor.py b/relay/remote_actor.py index 28e49b7..a8b271f 100644 --- a/relay/remote_actor.py +++ b/relay/remote_actor.py @@ -1,5 +1,6 @@ import aiohttp from .database import DATABASE +from .http_debug import http_debug ACTORS = DATABASE.get("actors", {}) @@ -7,7 +8,7 @@ async def fetch_actor(uri, force=False): if uri in ACTORS and not force: return ACTORS[uri] - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session: async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp: ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None)) DATABASE["actors"] = ACTORS