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

web.FileResponse overrides content_type #2317

Closed
thelink2012 opened this issue Oct 12, 2017 · 5 comments
Closed

web.FileResponse overrides content_type #2317

thelink2012 opened this issue Oct 12, 2017 · 5 comments
Labels

Comments

@thelink2012
Copy link
Contributor

Long story short

When returning a web.FileResponse from a route, any changes to the content_type member is not taken into account when the file is served. Instead, the file extension is always used to determine the content type of the response.

Expected behaviour

Content-Type header of the response be that of web.FileResponse.content_type when such has been set.

Actual behaviour

Content-Type header is guessed from the file extension of the served file.

Steps to reproduce

Create a route that responds something along the lines of

    response = web.FileResponse('/path/to/some/image.png')
    response.content_type = 'application/octet-stream'
    return response

then curl -i it and notice the Content-Type is image/png.

Your environment

[~]$ uname -mrs
Linux 4.11.9-1-ARCH x86_64
[~]$ python --version
Python 3.6.2
[~]$ pip show aiohttp | grep Version
Version: 2.2.5
@thelink2012
Copy link
Contributor Author

I'm inclined to opening a PR to resolve this issue, suggestions are highly appreciated.

@asvetlov
Copy link
Member

The only problem is figuring out if content type was explicitly set.
User could do it by resp.content_type = '...' and resp.headers['Content-Type'] modifications.
Default value for property is 'application/octet-stream'.

Well, the following code should work:

if hdrs.CONTENT_TYPE not in self.headers:
        ct, encoding = mimetypes.guess_type(str(filepath))
        if not ct:
            ct = 'application/octet-stream'
        self.content_type = ct
        ....

@thelink2012
Copy link
Contributor Author

thelink2012 commented Oct 12, 2017

I think setting the content type header based on the path parameter during __init__ is a better idea. This follows the same behaviour as web.Response, and gives us access to the guessed content type from the property. As such, this seems like the least suprising way for web.FileResponse to behave.

Same goes for encoding.

Ok, I see the flaw behind this, nevermind.

@asvetlov
Copy link
Member

Fixed by #2319

@lock
Copy link

lock bot commented Oct 28, 2019

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].
[new issue]: https:/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants