Skip to content
Myr

Drop MessageLoggerASGIMiddleware. (#573)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 43331cf · Browse files · 2 files+1 added−66 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+1 added−66 removed

  • other:+1 added−66 removed

2 files+1 added−66 removed

modified:httpx/dispatch/asgi.py+1 added-5 removed
2 unmodified lines
3from ..concurrency.asyncio import AsyncioBackend4from ..concurrency.base import ConcurrencyBackend5from ..config import CertTypes, TimeoutTypes, VerifyTypes6from ..models import Request, Response7from ..utils import MessageLoggerASGIMiddleware, get_logger7from .base import Dispatcher810logger = get_logger(__name__)91210class ASGIDispatch(Dispatcher):11 """12 A custom dispatcher that handles sending requests directly to an ASGI app.13
63 unmodified lines
77 "server": request.url.host,78 "client": self.client,79 "root_path": self.root_path,80 }84 app = MessageLoggerASGIMiddleware(self.app, logger=logger)81 status_code = None82 headers = None83 body_parts = []84 request_stream = request.stream()
29 unmodified lines
114 if not more_body:115 response_complete = True116117 try:122 await app(scope, receive, send)118 await self.app(scope, receive, send)119 except Exception:120 if self.raise_app_exceptions or not response_complete:121 raise122
11 unmodified lines
modified:httpx/utils.py-61 removed
337 unmodified lines
338 def elapsed(self) -> timedelta:339 if self.end is None:340 return timedelta(seconds=perf_counter() - self.start)341 return timedelta(seconds=self.end - self.start)342343344ASGI_PLACEHOLDER_FORMAT = {345 "body": "<{length} bytes>",346 "bytes": "<{length} bytes>",347 "text": "<{length} chars>",348}349350351def asgi_message_with_placeholders(message: dict) -> dict:352 """353 Return an ASGI message, with any body-type content omitted and replaced354 with a placeholder.355 """356 new_message = message.copy()357358 for attr in ASGI_PLACEHOLDER_FORMAT:359 if attr in message:360 content = message[attr]361 placeholder = ASGI_PLACEHOLDER_FORMAT[attr].format(length=len(content))362 new_message[attr] = placeholder363364 if "headers" in message:365 new_message["headers"] = list(obfuscate_sensitive_headers(message["headers"]))366367 return new_message368369370class MessageLoggerASGIMiddleware:371 def __init__(self, app: typing.Callable, logger: Logger) -> None:372 self.app = app373 self.logger = logger374375 async def __call__(376 self, scope: dict, receive: typing.Callable, send: typing.Callable377 ) -> None:378 async def inner_receive() -> dict:379 message = await receive()380 logged_message = asgi_message_with_placeholders(message)381 self.logger.trace(f"sent {kv_format(**logged_message)}")382 return message383384 async def inner_send(message: dict) -> None:385 logged_message = asgi_message_with_placeholders(message)386 self.logger.trace(f"received {kv_format(**logged_message)}")387 await send(message)388389 logged_scope = dict(scope)390 if "headers" in scope:391 logged_scope["headers"] = list(392 obfuscate_sensitive_headers(scope["headers"])393 )394 self.logger.trace(f"started {kv_format(**logged_scope)}")395396 try:397 await self.app(scope, inner_receive, inner_send)398 except BaseException as exc:399 self.logger.trace("raised_exception")400 raise exc from None401 else:402 self.logger.trace("completed")