Skip to content

Commit

Permalink
Alerts panel: Only process HTML responses
Browse files Browse the repository at this point in the history
Closes #1959
  • Loading branch information
matthiask committed Jul 9, 2024
1 parent 9bcd6ca commit 982a127
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
12 changes: 2 additions & 10 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions debug_toolbar/panels/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down

0 comments on commit 982a127

Please sign in to comment.