Skip to content

Commit

Permalink
Defer response headers processing to finished callback
Browse files Browse the repository at this point in the history
  • Loading branch information
COREHACK committed May 27, 2017
1 parent 5f5fe37 commit dc20111
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -121,3 +121,4 @@ Contributors
- Kashif Iftikhar, 2016-07-01
- Dan Clark, 2017-05-22
- Jens Carl, 2017-05-22
- Andrey Tretyakov, 2017-05-27
17 changes: 15 additions & 2 deletions pyramid_debugtoolbar/panels/headers.py
Expand Up @@ -15,16 +15,29 @@ class HeaderDebugPanel(DebugPanel):
nav_title = title

def __init__(self, request):
self.request = request
self.request_headers = [
(text_(k), text_(v)) for k, v in sorted(request.headers.items())
]

def process_response(self, response):
def finished_callback(request):
self.process_response_deferred(response)

def prepare_finished_callback(request):
""" Best effort to be the last in case there are other callbacks
mutating the response """
self.request.add_finished_callback(finished_callback)

self.request.add_finished_callback(prepare_finished_callback)
self.data = {'request_headers': self.request_headers,
'response_headers': []}

def process_response_deferred(self, response):
response_headers = [
(text_(k), text_(v)) for k, v in sorted(response.headerlist)
]
self.data = {'request_headers': self.request_headers,
'response_headers': response_headers}
self.data['response_headers'] = response_headers

def includeme(config):
config.add_debugtoolbar_panel(HeaderDebugPanel)

0 comments on commit dc20111

Please sign in to comment.