diff options
| author | Aymeric Augustin | 2014-02-13 22:02:00 +0100 | 
|---|---|---|
| committer | Aymeric Augustin | 2014-02-13 22:23:55 +0100 | 
| commit | f1a30456cf2a85bf45e5bc0d8c95fd2ac3fc23a7 (patch) | |
| tree | bcee75b358d66de5e76bf0b39b45446056fa1809 /debug_toolbar | |
| parent | 9dc89cc9642a71bbb069d636e369131ce69d4e8e (diff) | |
| download | django-debug-toolbar-f1a30456cf2a85bf45e5bc0d8c95fd2ac3fc23a7.tar.bz2 | |
Adjust toolbar insertion code to avoid .lower().
Fix #531.
Diffstat (limited to 'debug_toolbar')
| -rw-r--r-- | debug_toolbar/middleware.py | 21 | 
1 files changed, 14 insertions, 7 deletions
| diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index b6099d3..7f83ccb 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -4,6 +4,7 @@ Debug Toolbar middleware  from __future__ import absolute_import, unicode_literals +import re  import threading  from django.conf import settings @@ -106,13 +107,19 @@ class DebugToolbarMiddleware(object):          # Insert the toolbar in the response.          content = force_text(response.content, encoding=settings.DEFAULT_CHARSET) -        try: -            insert_at = content.lower().rindex(dt_settings.CONFIG['INSERT_BEFORE'].lower()) -        except ValueError: -            pass -        else: -            toolbar_content = toolbar.render_toolbar() -            response.content = content[:insert_at] + toolbar_content + content[insert_at:] +        insert_before = dt_settings.CONFIG['INSERT_BEFORE'] +        try:                    # Python >= 2.7 +            pattern = re.escape(insert_before) +            bits = re.split(pattern, content, flags=re.IGNORECASE) +        except TypeError:       # Python < 2.7 +            pattern = '(.+?)(%s|$)' % re.escape(insert_before) +            matches = re.findall(pattern, content, flags=re.DOTALL | re.IGNORECASE) +            bits = [m[0] for m in matches if m[1] == insert_before] +            # When the body ends with a newline, there's two trailing groups. +            bits.append(''.join(m[0] for m in matches if m[1] == '')) +        if len(bits) > 1: +            bits[-2] += toolbar.render_toolbar() +            response.content = insert_before.join(bits)              if response.get('Content-Length', None):                  response['Content-Length'] = len(response.content)          return response | 
