Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

headers.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from __future__ import absolute_import, unicode_literals
  2. from collections import OrderedDict
  3. from django.utils.translation import ugettext_lazy as _
  4. from debug_toolbar.panels import Panel
  5. class HeadersPanel(Panel):
  6. """
  7. A panel to display HTTP headers.
  8. """
  9. # List of environment variables we want to display
  10. ENVIRON_FILTER = set((
  11. 'CONTENT_LENGTH',
  12. 'CONTENT_TYPE',
  13. 'DJANGO_SETTINGS_MODULE',
  14. 'GATEWAY_INTERFACE',
  15. 'QUERY_STRING',
  16. 'PATH_INFO',
  17. 'PYTHONPATH',
  18. 'REMOTE_ADDR',
  19. 'REMOTE_HOST',
  20. 'REQUEST_METHOD',
  21. 'SCRIPT_NAME',
  22. 'SERVER_NAME',
  23. 'SERVER_PORT',
  24. 'SERVER_PROTOCOL',
  25. 'SERVER_SOFTWARE',
  26. 'TZ',
  27. ))
  28. title = _("Headers")
  29. template = 'debug_toolbar/panels/headers.html'
  30. def process_request(self, request):
  31. wsgi_env = list(sorted(request.META.items()))
  32. self.request_headers = OrderedDict(
  33. (unmangle(k), v) for (k, v) in wsgi_env if is_http_header(k))
  34. if 'Cookie' in self.request_headers:
  35. self.request_headers['Cookie'] = '=> see Request panel'
  36. self.environ = OrderedDict(
  37. (k, v) for (k, v) in wsgi_env if k in self.ENVIRON_FILTER)
  38. self.record_stats({
  39. 'request_headers': self.request_headers,
  40. 'environ': self.environ,
  41. })
  42. def generate_stats(self, request, response):
  43. self.response_headers = OrderedDict(sorted(response.items()))
  44. self.record_stats({
  45. 'response_headers': self.response_headers,
  46. })
  47. def is_http_header(wsgi_key):
  48. # The WSGI spec says that keys should be str objects in the environ dict,
  49. # but this isn't true in practice. See issues #449 and #482.
  50. return isinstance(wsgi_key, str) and wsgi_key.startswith('HTTP_')
  51. def unmangle(wsgi_key):
  52. return wsgi_key[5:].replace('_', '-').title()