aboutsummaryrefslogtreecommitdiffstats
path: root/debug_toolbar/panels/headers.py
blob: de47c0e7e93206528472b6b71e6723d47fbd3148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import unicode_literals

try:
    from collections import OrderedDict
except ImportError:
    from django.utils.datastructures import SortedDict as OrderedDict
from django.utils.translation import ugettext_lazy as _
from debug_toolbar.panels import DebugPanel


class HeaderDebugPanel(DebugPanel):
    """
    A panel to display HTTP headers.
    """
    name = 'Header'
    template = 'debug_toolbar/panels/headers.html'
    has_content = True
    # List of environment variables we want to display
    environ_filter = set((
        'CONTENT_LENGTH',
        'CONTENT_TYPE',
        'DJANGO_SETTINGS_MODULE',
        'GATEWAY_INTERFACE',
        'QUERY_STRING',
        'PATH_INFO',
        'PYTHONPATH',
        'REMOTE_ADDR',
        'REMOTE_HOST',
        'REQUEST_METHOD',
        'SCRIPT_NAME',
        'SERVER_NAME',
        'SERVER_PORT',
        'SERVER_PROTOCOL',
        'SERVER_SOFTWARE',
        'TZ',
    ))

    def nav_title(self):
        return _('Headers')

    def title(self):
        return _('Headers')

    def url(self):
        return ''

    def process_request(self, request):
        wsgi_env = list(sorted(request.META.items()))
        self.headers = OrderedDict(
            (unmangle(k), v) for (k, v) in wsgi_env if k.startswith('HTTP_'))
        if 'Cookie' in self.headers:
            self.headers['Cookie'] = '<< see Request Vars panel >>'
        self.environ = OrderedDict(
            (k, v) for (k, v) in wsgi_env if k in self.environ_filter)

    def process_response(self, request, response):
        self.record_stats({
            'headers': self.headers,
            'environ': self.environ,
        })


def unmangle(wsgi_key):
    return wsgi_key[5:].replace('_', '-').title()