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
65
66
|
from __future__ import absolute_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 Panel
class HeadersPanel(Panel):
"""
A panel to display HTTP headers.
"""
# 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',
))
title = _("Headers")
template = 'debug_toolbar/panels/headers.html'
def process_request(self, request):
wsgi_env = list(sorted(request.META.items()))
self.request_headers = OrderedDict(
(unmangle(k), v) for (k, v) in wsgi_env if is_http_header(k))
if 'Cookie' in self.request_headers:
self.request_headers['Cookie'] = '=> see Request panel'
self.environ = OrderedDict(
(k, v) for (k, v) in wsgi_env if k in self.ENVIRON_FILTER)
self.record_stats({
'request_headers': self.request_headers,
'environ': self.environ,
})
def process_response(self, request, response):
self.response_headers = OrderedDict(sorted(response.items()))
self.record_stats({
'response_headers': self.response_headers,
})
def is_http_header(wsgi_key):
# The WSGI spec says that keys should be str objects in the environ dict,
# but this isn't true in practice. See issues #449 and #482.
return isinstance(wsgi_key, str) and wsgi_key.startswith('HTTP_')
def unmangle(wsgi_key):
return wsgi_key[5:].replace('_', '-').title()
|