From 982a1271c452ae382c5a851a5484ed3056ef47be Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Tue, 9 Jul 2024 15:58:52 +0200 Subject: [PATCH] Alerts panel: Only process HTML responses Closes #1959 --- debug_toolbar/middleware.py | 12 ++---------- debug_toolbar/panels/alerts.py | 4 ++-- debug_toolbar/utils.py | 13 +++++++++++++ docs/changes.rst | 1 + 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 65b5282c5..b089d1484 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -11,9 +11,7 @@ from debug_toolbar import settings as dt_settings from debug_toolbar.toolbar import DebugToolbar -from debug_toolbar.utils import clear_stack_trace_caches - -_HTML_TYPES = ("text/html", "application/xhtml+xml") +from debug_toolbar.utils import clear_stack_trace_caches, is_processable_html_response def show_toolbar(request): @@ -102,13 +100,7 @@ def __call__(self, request): response.headers[header] = value # Check for responses where the toolbar can't be inserted. - content_encoding = response.get("Content-Encoding", "") - content_type = response.get("Content-Type", "").split(";")[0] - if ( - getattr(response, "streaming", False) - or content_encoding != "" - or content_type not in _HTML_TYPES - ): + if not is_processable_html_response(response): return response # Insert the toolbar in the response. diff --git a/debug_toolbar/panels/alerts.py b/debug_toolbar/panels/alerts.py index e640dcdd5..51334820d 100644 --- a/debug_toolbar/panels/alerts.py +++ b/debug_toolbar/panels/alerts.py @@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _ from debug_toolbar.panels import Panel +from debug_toolbar.utils import is_processable_html_response class FormParser(HTMLParser): @@ -138,8 +139,7 @@ def check_invalid_file_form_configuration(self, html_content): return self.alerts def generate_stats(self, request, response): - # check if streaming response - if getattr(response, "streaming", True): + if not is_processable_html_response(response): return html_content = response.content.decode(response.charset) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index 3a9d0882e..1e75cced2 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -353,3 +353,16 @@ def get_stack_trace(*, skip=0): def clear_stack_trace_caches(): if hasattr(_local_data, "stack_trace_recorder"): del _local_data.stack_trace_recorder + + +_HTML_TYPES = ("text/html", "application/xhtml+xml") + + +def is_processable_html_response(response): + content_encoding = response.get("Content-Encoding", "") + content_type = response.get("Content-Type", "").split(";")[0] + return ( + not getattr(response, "streaming", False) + and content_encoding == "" + and content_type in _HTML_TYPES + ) diff --git a/docs/changes.rst b/docs/changes.rst index d47f69784..e845f3b3b 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -6,6 +6,7 @@ Pending * Changed ordering (and grammatical number) of panels and their titles in documentation to match actual panel ordering and titles. +* Skipped processing the alerts panel when response isn't a HTML response. 4.4.5 (2024-07-05) ------------------