Skip to content

Alerts panel: Only process HTML responses #1960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading