-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 response headers for compressed file requests #8485
Fix response headers for compressed file requests #8485
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #8485 +/- ##
==========================================
- Coverage 97.61% 97.61% -0.01%
==========================================
Files 107 107
Lines 33103 33104 +1
Branches 3894 3892 -2
==========================================
Hits 32314 32314
- Misses 570 571 +1
Partials 219 219
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this looks correct. @bdraco Think you could double check it?
Will try to take a look soon. Hurricane Beryl is causing tons of problems in Houston right now so may not be today |
I added a couple extra sentences to the change notes just in case users need to adjust clients to the probable behavior of no longer decompressing the files. Also, does this need to backport to 3.9 as well or are patches done for 3.9? |
I don’t think we are doing any more 3.9s so no need to backport |
The code looks good but I still haven’t been able to test it as all my equipment in Houston is on emergency power. Grid power is expected to come back tomorrow |
Thanks and no rush. The rain is currently headed my way for the next few days, albeit with much less intensity now, so hopefully I won't be saying the same. I'm trying to work on #8182 in the meantime, so any advice there would be appreciated. |
Going to give this a test now |
backported to 3.10 for testing as bdraco@db1eaec |
Downgrading back to 3.9.x and running the same test now |
Tried with a bzip2ed jpeg on 3.9 The content-type that comes back is Trying with this patch now |
Tried with a bzip2ed jpeg on 3.10 with this patch The content-type that comes back is The behavior is the same as 3.9 baseline |
If you request "file.svg.gz", then I would not expect it to render since the content type should be "application/gzip". That case is really no different than downloading "file.tar.gz". Images would only render if you request it without the compression extension. |
I'm confused by this one. Was the request with or without the
Either way, browser wouldn't render it since they don't support |
Request was with the bz2 extension
That was what I had expected as well but it was |
Can you share that test for me to repro? |
|
Tests with HA won't work for compressed files because HA incorrectly overrides the content type: content_type = (mimetypes.guess_type(rel_url))[
0
] or "application/octet-stream" |
I'll investigate the issue in HA this weekend (or first thing Monday) and see if I can come up with a solution so I can test this. |
I can make a quick fix PR for HA by just removing the code to cache and override the But I wasn't going to bother with it because I wanted to fix #8182 too, which may end up rendering much more of that code unnecessary. |
HA change to allow this to take effect: home-assistant/core#121831 |
In addition to @bdraco's suggestion that should improve performance, I also moved the lookup down (after any possible 416 responses) and avoided the string conversion ( Another potential performance tweak would be to put the type lookup in parallel with opening the file. |
Mypy is complaining |
Its definitely defined and |
LGTM once https://github.com/aio-libs/aiohttp/pull/8485/files#r1676667798 is worked out 👍 Thanks @steverep |
FWIW... python/cpython#121658 |
I don't know if either you had anything to do with it, but it was just added to typeshed: Not sure if the change will get synced into the 1.11 release or not. |
Ah, I see, it was result of the docs bug report. |
Backport to 3.10: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply c086795 on top of patchback/backports/3.10/c086795452bc8fe9c5a476dc1d6b9d5a3120dc7c/pr-8485 Backporting merged PR #8485 into master
🤖 @patchback |
(cherry picked from commit c086795) Co-authored-by: Steve Repsher <steverep@users.noreply.github.com>
@bdraco FYI, the use of |
This is much nicer for async. In HA we preload mine types at bootstrap in the executor for this reason |
I mentioned above potentially another small improvement by putting that large table lookup concurrent with opening the file, e.g.: guess_type_fut = loop.run_in_executor(None, CONTENT_TYPES.guess_type, self._path)
# later on...
open_fut = loop.run_in_executor(None, file_path.open, "rb")
fobj,ct = await asyncio.gather([open_fut, guess_type_fut]) Worth it? Is there a benchmark in place to test it? |
What do these changes do?
Currently the server responds to requests for compressed files with a
Content-Encoding
header based on the extension. This is not correct as the encoding is supposed to be a transformation applied to the content type, and the type should be a representation of the request (i.e. if the request is for a compressed file, then the type should represent a compressed file). The reasoning is further explained in #4462 (comment).Note this also addresses problems when non-standard or unsupported encodings are involved. The server will currently respond to "/hello.txt.bz2" with encoding "bzip2" to an
aiohttp
client, despite the fact that it's not supported or sent in theAccept-Encoding
header. That header check is completely bypassed ATM.Are there changes in behavior for the user?
Requests for static compressed files will no longer be decompressed by most clients (when the encoding is supported). However this is not a server behavior change so much as an expected reaction to the server correction.
Is it a substantial burden for the maintainers to support this?
No
Related issue number
Fixes #4462 and helps advance #8104 (by eliminating some ignorance of
Accept-Encoding
)Checklist
CONTRIBUTORS.txt
CHANGES/
foldername it
<issue_or_pr_num>.<type>.rst
(e.g.588.bugfix.rst
)if you don't have an issue number, change it to the pull request
number after creating the PR
.bugfix
: A bug fix for something the maintainers deemed animproper undesired behavior that got corrected to match
pre-agreed expectations.
.feature
: A new behavior, public APIs. That sort of stuff..deprecation
: A declaration of future API removals and breakingchanges in behavior.
.breaking
: When something public is removed in a breaking way.Could be deprecated in an earlier release.
.doc
: Notable updates to the documentation structure or buildprocess.
.packaging
: Notes for downstreams about unobvious side effectsand tooling. Changes in the test invocation considerations and
runtime assumptions.
.contrib
: Stuff that affects the contributor experience. e.g.Running tests, building the docs, setting up the development
environment.
.misc
: Changes that are hard to assign to any of the abovecategories.
Make sure to use full sentences with correct case and punctuation,
for example:
Use the past tense or the present tense a non-imperative mood,
referring to what's changed compared to the last released version
of this project.