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
Provenance
- Commit
095b691- Tree
dabf548- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
43331cf
2 files changed+1 added−66 removed
modified:httpx/dispatch/asgi.py+1 added-5 removed
2 unmodified lines3 from ..concurrency.asyncio import AsyncioBackend4 from ..concurrency.base import ConcurrencyBackend5 from ..config import CertTypes, TimeoutTypes, VerifyTypes6 from ..models import Request, Response7-from ..utils import MessageLoggerASGIMiddleware, get_logger7 from .base import Dispatcher8 10-logger = get_logger(__name__)9 12-10 class ASGIDispatch(Dispatcher):11 """12 A custom dispatcher that handles sending requests directly to an ASGI app.13 63 unmodified lines77 "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 lines114 if not more_body:115 response_complete = True116 117 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 linesmodified:httpx/utils.py-61 removed
337 unmodified lines338 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)342-343-344-ASGI_PLACEHOLDER_FORMAT = {345- "body": "<{length} bytes>",346- "bytes": "<{length} bytes>",347- "text": "<{length} chars>",348-}349-350-351-def 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()357-358- 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] = placeholder363-364- if "headers" in message:365- new_message["headers"] = list(obfuscate_sensitive_headers(message["headers"]))366-367- return new_message368-369-370-class MessageLoggerASGIMiddleware:371- def __init__(self, app: typing.Callable, logger: Logger) -> None:372- self.app = app373- self.logger = logger374-375- 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 message383-384- 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)388-389- 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)}")395-396- 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")