relay: add http request debugger

This commit is contained in:
kaniini 2018-11-18 00:07:36 +00:00
parent dfee3d3658
commit 2f1eed51a1
3 changed files with 16 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import simplejson as json
import cgi import cgi
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from .database import DATABASE from .database import DATABASE
from .http_debug import http_debug
# generate actor keys if not present # 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) 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: async with session.post(inbox, data=data, headers=headers) as resp:
resp_payload = await resp.text() resp_payload = await resp.text()
logging.debug('%r >> resp %r', inbox, resp_payload) logging.debug('%r >> resp %r', inbox, resp_payload)

12
relay/http_debug.py Normal file
View file

@ -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

View file

@ -1,5 +1,6 @@
import aiohttp import aiohttp
from .database import DATABASE from .database import DATABASE
from .http_debug import http_debug
ACTORS = DATABASE.get("actors", {}) ACTORS = DATABASE.get("actors", {})
@ -7,7 +8,7 @@ async def fetch_actor(uri, force=False):
if uri in ACTORS and not force: if uri in ACTORS and not force:
return ACTORS[uri] 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: async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp:
ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None)) ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None))
DATABASE["actors"] = ACTORS DATABASE["actors"] = ACTORS