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

Fix web.FileResponse overriding user supplied Content-Type #2319

Merged
merged 1 commit into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Daniel García
Daniel Nelson
Danny Song
David Michael Brown
Denilson Amorim
Denis Matiychuk
Dima Veselov
Dimitar Dimitrov
Expand Down
10 changes: 7 additions & 3 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ def prepare(self, request):
self._length_check = False
return (yield from super().prepare(request))

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

status = HTTPOk.status_code
file_size = st.st_size
Expand Down
1 change: 1 addition & 0 deletions changes/2317.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix web.FileResponse overriding user supplied Content-Type
48 changes: 48 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,54 @@ def handler(request):
resp.close()


@asyncio.coroutine
def test_static_file_custom_content_type(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent / 'hello.txt.gz')

@asyncio.coroutine
def handler(request):
resp = sender(filepath, chunk_size=16)
resp.content_type = 'application/pdf'
return resp

app = web.Application()
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

resp = yield from client.get('/')
assert resp.status == 200
body = yield from resp.read()
with filepath.open('rb') as f:
content = f.read()
assert content == body
assert resp.headers['Content-Type'] == 'application/pdf'
assert resp.headers.get('Content-Encoding') is None
resp.close()


@asyncio.coroutine
def test_static_file_custom_content_type_compress(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent / 'hello.txt')

@asyncio.coroutine
def handler(request):
resp = sender(filepath, chunk_size=16)
resp.content_type = 'application/pdf'
return resp

app = web.Application()
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

resp = yield from client.get('/')
assert resp.status == 200
body = yield from resp.read()
assert b'hello aiohttp\n' == body
assert resp.headers['Content-Type'] == 'application/pdf'
assert resp.headers.get('Content-Encoding') == 'gzip'
resp.close()


@asyncio.coroutine
def test_static_file_with_content_encoding(loop, test_client, sender):
filepath = pathlib.Path(__file__).parent / 'hello.txt.gz'
Expand Down