Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type header should be optional in Response #4612

Closed
rmedaer opened this issue Mar 6, 2020 · 5 comments · Fixed by #8858
Closed

Content-Type header should be optional in Response #4612

rmedaer opened this issue Mar 6, 2020 · 5 comments · Fixed by #8858
Labels

Comments

@rmedaer
Copy link

rmedaer commented Mar 6, 2020

🐞 Describe the bug
I'm not able to remove the Content-Type header in aiohttp.web.Response. However, according to HTTP RFC, the Content-Type header is not mandatory. Especially if you don't return content (e.g. 204 No Content)

💡 To Reproduce

from aiohttp.web import Response

async def handler(request):
    return Response(
        status=204,
        content_type=None
    )

💡 Expected behavior

Be able, some how, to remove the Content-Type header.

📋 Logs/tracebacks

Result from curl:

< HTTP/1.1 204 No Content
< Content-Length: 0
< Content-Type: application/octet-stream
< Date: Fri, 06 Mar 2020 11:32:46 GMT
< Server: Python/3.7 aiohttp/3.5.1
@rmedaer rmedaer added the bug label Mar 6, 2020
@borysvorona
Copy link
Member

borysvorona commented Mar 6, 2020

I do not agree with that, according to
https://tools.ietf.org/html/rfc2616#section-7.2.1

I think that an additional optional Response param can solve that issue.
For example:

aiohttp.web.Response(status=204, force_empty_content_type=True)

@rmedaer
Copy link
Author

rmedaer commented Mar 8, 2020

I do not agree with that, according to
https://tools.ietf.org/html/rfc2616#section-7.2.1

Could you expand on that, please ?

If I well understand the RFC, the entity-body is optional... Here is the relevant quotes where I highlighted some keywords and added some footnotes:

Section 7.2.1 Type (you quoted):

When2 an entity-body is included with a message, the data type of that body is determined via the header fields Content-Type (...)
Any HTTP/1.1 message containing an entity-body SHOULD1 include a Content-Type header field defining the media type of that body.

Section 7.2 Entity Body:

The entity-body (if any) (...)
An entity-body is only present in a message when a message-body is present3 (...)

If my understanding is correct, it means:

  1. A Content-Type is not mandatory
  2. A Content-Type header should be specified if an entity-body is included
  3. If there is not any message body, there is no entity-body

So, if not any body is returned, not any Content-Type header should be returned !

@asvetlov
Copy link
Member

This issue has been mentioned on aio-libs. There might be relevant details there:

https://aio-libs.discourse.group/t/optional-content-type-header/82/1

@alex-eri
Copy link
Contributor

Write custom Response subclass and fix setter

@content_type.setter

@rmedaer
Copy link
Author

rmedaer commented Mar 19, 2020

@alex-eri I don't understand why I should write a custom Response subclass for what should be a default behavior.

IMHO we should use response subclass to add extra feature/behavior, not to remove them.

I found a nice illustration of the issue in Chrome. Currently, if I return a (not-so) empty 404:

async def not_found(request):
    return Response(status=404)

# < HTTP/1.1 404 Not Found
# < Content-Type: application/octet-stream
# < Content-Length: 0
#  (...)

Chrome shows an invalid response (due to the Content-Type application/octet-stream)

invalid

If I remove the Content-Type and Content-Length headers (or if Content-Type is text/html), here is what Chrome shows:

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants