Skip to content

Allow middleware to set response headers in a dependable way - #393

Closed
meredithdutton wants to merge 2 commits into
aio-libs:masterfrom
meredithdutton:guaranteed-response-headers
Closed

meredithdutton wants to merge 2 commits into
aio-libs:masterfrom
meredithdutton:guaranteed-response-headers

Conversation

@meredithdutton

Copy link
Copy Markdown
Contributor

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_headers attribute to Request, which are then added to the response headers when StreamResponse.start(…) is called. Each header is only added if the key doesn't already exist in headers, 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 check request.response_headers before 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 Request ref docs, and in the middleware section).

@meredithdutton

Copy link
Copy Markdown
Contributor Author

The test failure is in python setup.py check rm, which fails to parse README.rst with:

  File "~/envs/aiohttp/lib/python3.4/site-packages/docutils/parsers/rst/directives/body.py", line 149, in run
    self.state.document.settings.syntax_highlight)
AttributeError: 'Values' object has no attribute 'syntax_highlight'

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.

@asvetlov

asvetlov commented Jun 5, 2015

Copy link
Copy Markdown
Member

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.

@meredithdutton

Copy link
Copy Markdown
Contributor Author

That sounds entirely reasonable. I'll create another issue with a proposal for signals.

@meredithdutton meredithdutton mentioned this pull request Jun 7, 2015
@lock

lock Bot commented Oct 30, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock Bot added the outdated label Oct 30, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants