From: Tom Christie Date: Sat, 30 Nov 2019 14:28:39 +0000 (+0000) Subject: Drop MessageLoggerASGIMiddleware. (#573) X-Git-Tag: 0.9.0~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=095b69184ab9ba63eacf6272780bd2a89d1bec94;p=thirdparty%2Fhttpx.git Drop MessageLoggerASGIMiddleware. (#573) --- diff --git a/httpx/dispatch/asgi.py b/httpx/dispatch/asgi.py index f9cbb35f..ee75debb 100644 --- a/httpx/dispatch/asgi.py +++ b/httpx/dispatch/asgi.py @@ -4,11 +4,8 @@ from ..concurrency.asyncio import AsyncioBackend from ..concurrency.base import ConcurrencyBackend from ..config import CertTypes, TimeoutTypes, VerifyTypes from ..models import Request, Response -from ..utils import MessageLoggerASGIMiddleware, get_logger from .base import Dispatcher -logger = get_logger(__name__) - class ASGIDispatch(Dispatcher): """ @@ -81,7 +78,6 @@ class ASGIDispatch(Dispatcher): "client": self.client, "root_path": self.root_path, } - app = MessageLoggerASGIMiddleware(self.app, logger=logger) status_code = None headers = None body_parts = [] @@ -119,7 +115,7 @@ class ASGIDispatch(Dispatcher): response_complete = True try: - await app(scope, receive, send) + await self.app(scope, receive, send) except Exception: if self.raise_app_exceptions or not response_complete: raise diff --git a/httpx/utils.py b/httpx/utils.py index 07277a34..9d3aa223 100644 --- a/httpx/utils.py +++ b/httpx/utils.py @@ -339,64 +339,3 @@ class ElapsedTimer: if self.end is None: return timedelta(seconds=perf_counter() - self.start) return timedelta(seconds=self.end - self.start) - - -ASGI_PLACEHOLDER_FORMAT = { - "body": "<{length} bytes>", - "bytes": "<{length} bytes>", - "text": "<{length} chars>", -} - - -def asgi_message_with_placeholders(message: dict) -> dict: - """ - Return an ASGI message, with any body-type content omitted and replaced - with a placeholder. - """ - new_message = message.copy() - - for attr in ASGI_PLACEHOLDER_FORMAT: - if attr in message: - content = message[attr] - placeholder = ASGI_PLACEHOLDER_FORMAT[attr].format(length=len(content)) - new_message[attr] = placeholder - - if "headers" in message: - new_message["headers"] = list(obfuscate_sensitive_headers(message["headers"])) - - return new_message - - -class MessageLoggerASGIMiddleware: - def __init__(self, app: typing.Callable, logger: Logger) -> None: - self.app = app - self.logger = logger - - async def __call__( - self, scope: dict, receive: typing.Callable, send: typing.Callable - ) -> None: - async def inner_receive() -> dict: - message = await receive() - logged_message = asgi_message_with_placeholders(message) - self.logger.trace(f"sent {kv_format(**logged_message)}") - return message - - async def inner_send(message: dict) -> None: - logged_message = asgi_message_with_placeholders(message) - self.logger.trace(f"received {kv_format(**logged_message)}") - await send(message) - - logged_scope = dict(scope) - if "headers" in scope: - logged_scope["headers"] = list( - obfuscate_sensitive_headers(scope["headers"]) - ) - self.logger.trace(f"started {kv_format(**logged_scope)}") - - try: - await self.app(scope, inner_receive, inner_send) - except BaseException as exc: - self.logger.trace("raised_exception") - raise exc from None - else: - self.logger.trace("completed")