Repository navigation
Allow middleware to set response headers in a dependable way - #393
meredithdutton wants to merge 2 commits into
Conversation
|
The test failure is in Googling suggests http://bugs.python.org/issue23063. I'm failing to get it to work with a clean master too, which confuses me as other Travis builds seem to be working. |
|
I understand your motivation but strongly dislike implementation. We need to control http status code and reason also. I don't like to push those values into requests -- they are definitely parts of response object. Instead I suggest inviting signals mechanism like Pyramid or Django has. On response on ResponseCreated or BeforeSendHeaders event response may be tuned before sending HTTP headers. |
|
That sounds entirely reasonable. I'll create another issue with a proposal for signals. |
I'd like to write middleware that can add response headers in all circumstances, including on streamed responses. The example at 57ebeae doesn't work in such a case as the response has already started.
This PR adds a
response_headersattribute toRequest, which are then added to the response headers whenStreamResponse.start(…)is called. Each header is only added if the key doesn't already exist inheaders, so that handlers can override any defaults that might have been set by middleware. I suspect that in most cases conflicts won't be an issue, and can be handled by having the request handler checkrequest.response_headersbefore attempting to set headers on the response directly.If this seems reasonable, I'll add more commits to the PR to document this functionality (on the
Requestref docs, and in the middleware section).