{"action":"created","issue":{"url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/393","labels_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/393/labels{/name}","comments_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/393/comments","events_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/393/events","html_url":"https://github.com/KeepSafe/aiohttp/pull/393","id":85293545,"number":393,"title":"Allow middleware to set response headers in a dependable way","user":{"login":"alexsdutton","id":238690,"avatar_url":"https://avatars.githubusercontent.com/u/238690?v=3","gravatar_id":"","url":"https://api.github.com/users/alexsdutton","html_url":"https://github.com/alexsdutton","followers_url":"https://api.github.com/users/alexsdutton/followers","following_url":"https://api.github.com/users/alexsdutton/following{/other_user}","gists_url":"https://api.github.com/users/alexsdutton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexsdutton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexsdutton/subscriptions","organizations_url":"https://api.github.com/users/alexsdutton/orgs","repos_url":"https://api.github.com/users/alexsdutton/repos","events_url":"https://api.github.com/users/alexsdutton/events{/privacy}","received_events_url":"https://api.github.com/users/alexsdutton/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-04T21:48:08Z","updated_at":"2015-06-05T12:25:29Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/KeepSafe/aiohttp/pulls/393","html_url":"https://github.com/KeepSafe/aiohttp/pull/393","diff_url":"https://github.com/KeepSafe/aiohttp/pull/393.diff","patch_url":"https://github.com/KeepSafe/aiohttp/pull/393.patch"},"body":"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.\r\n\r\nThis 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.\r\n\r\nIf 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)."},"comment":{"url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/comments/109278334","html_url":"https://github.com/KeepSafe/aiohttp/pull/393#issuecomment-109278334","issue_url":"https://api.github.com/repos/KeepSafe/aiohttp/issues/393","id":109278334,"user":{"login":"asvetlov","id":356399,"avatar_url":"https://avatars.githubusercontent.com/u/356399?v=3","gravatar_id":"","url":"https://api.github.com/users/asvetlov","html_url":"https://github.com/asvetlov","followers_url":"https://api.github.com/users/asvetlov/followers","following_url":"https://api.github.com/users/asvetlov/following{/other_user}","gists_url":"https://api.github.com/users/asvetlov/gists{/gist_id}","starred_url":"https://api.github.com/users/asvetlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asvetlov/subscriptions","organizations_url":"https://api.github.com/users/asvetlov/orgs","repos_url":"https://api.github.com/users/asvetlov/repos","events_url":"https://api.github.com/users/asvetlov/events{/privacy}","received_events_url":"https://api.github.com/users/asvetlov/received_events","type":"User","site_admin":false},"created_at":"2015-06-05T12:25:29Z","updated_at":"2015-06-05T12:25:29Z","body":"I understand your motivation but strongly dislike implementation. We need to control http status code and reason also. I don't list to push those values into requests -- they are definitely parts of response object.\r\n\r\nInstead 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."}}