Skip to content

Commit 982a127

Browse files
committed
Alerts panel: Only process HTML responses
Closes #1959
1 parent 9bcd6ca commit 982a127

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

debug_toolbar/middleware.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
from debug_toolbar import settings as dt_settings
1313
from debug_toolbar.toolbar import DebugToolbar
14-
from debug_toolbar.utils import clear_stack_trace_caches
15-
16-
_HTML_TYPES = ("text/html", "application/xhtml+xml")
14+
from debug_toolbar.utils import clear_stack_trace_caches, is_processable_html_response
1715

1816

1917
def show_toolbar(request):
@@ -102,13 +100,7 @@ def __call__(self, request):
102100
response.headers[header] = value
103101

104102
# Check for responses where the toolbar can't be inserted.
105-
content_encoding = response.get("Content-Encoding", "")
106-
content_type = response.get("Content-Type", "").split(";")[0]
107-
if (
108-
getattr(response, "streaming", False)
109-
or content_encoding != ""
110-
or content_type not in _HTML_TYPES
111-
):
103+
if not is_processable_html_response(response):
112104
return response
113105

114106
# Insert the toolbar in the response.

debug_toolbar/panels/alerts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.utils.translation import gettext_lazy as _
44

55
from debug_toolbar.panels import Panel
6+
from debug_toolbar.utils import is_processable_html_response
67

78

89
class FormParser(HTMLParser):
@@ -138,8 +139,7 @@ def check_invalid_file_form_configuration(self, html_content):
138139
return self.alerts
139140

140141
def generate_stats(self, request, response):
141-
# check if streaming response
142-
if getattr(response, "streaming", True):
142+
if not is_processable_html_response(response):
143143
return
144144

145145
html_content = response.content.decode(response.charset)

debug_toolbar/utils.py

+13
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,16 @@ def get_stack_trace(*, skip=0):
353353
def clear_stack_trace_caches():
354354
if hasattr(_local_data, "stack_trace_recorder"):
355355
del _local_data.stack_trace_recorder
356+
357+
358+
_HTML_TYPES = ("text/html", "application/xhtml+xml")
359+
360+
361+
def is_processable_html_response(response):
362+
content_encoding = response.get("Content-Encoding", "")
363+
content_type = response.get("Content-Type", "").split(";")[0]
364+
return (
365+
not getattr(response, "streaming", False)
366+
and content_encoding == ""
367+
and content_type in _HTML_TYPES
368+
)

docs/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Pending
66

77
* Changed ordering (and grammatical number) of panels and their titles in
88
documentation to match actual panel ordering and titles.
9+
* Skipped processing the alerts panel when response isn't a HTML response.
910

1011
4.4.5 (2024-07-05)
1112
------------------

0 commit comments

Comments
 (0)