-
Notifications
You must be signed in to change notification settings - Fork 364
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
Update BINARY_SUPPORT to use Content-Encoding to identify if data is binary #971
Conversation
Awesome, thanks! Is there anything preventing to merge this ? |
@paulnicolet There's nothing blocking that I'm aware of. @jneves could this be merged? |
Hi @jneves -- we're using this code from @Quidge in a forked version of Zappa because of a need to enable compression, and it's working very well for us. If this PR could be merged in, that would allow us to move off of our fork and back onto the official Zappa project which we're really looking forward to so that we don't have to maintain our own fork. 🙂 |
@jneves rebased this PR atop the (great!) newer black changes to the codebase. |
@jneves is this getting any movement? Anything I can do to help get this merged? |
@jneves we've been using this code on a fork (https://github.com/tackle-io/Zappa) for over 3 months and are eager to get it merged. This enhancement has helped with large payload that exceed the lambda max response size (6 MB), since it allows for us to zip the response in the lambda. Let me know if there's anything I can do to help getting the approved and merge. Thanks! |
@jneves It's been several months -- we'd really love to get this merged in so we can cut off of our own Zappa fork. |
d073683
to
3501ea2
Compare
No functional change to the Zappa codebase is introduced by this commit. This area of the application was untested. Tests introduced to ensure new behavior discussed in zappa#908 does not cause a regression.
When using _whitenoise_ for caching, which provides compression, binary types may include mimetypes, "text/", "application/json": - response.mimetype.startswith("text/") - response.mimetype == "application/json" Assuming that Content-Encoding will be set (as whitenoise apparently does) this allows compression to be applied by the application for "text/" and "application/json". About Content-Encoding: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding --- The above commit message and functional code change to zappa/handler.py was written by GH user @monkut and a PR was provided with Miserlou/Zappa#2170. That PR was not merged in before the fork from Miserlou/zappa to Zappa/zappa. This commit copies the code from that PR, adds a comment line referencing the new migrated issue, and additionally adds tests to confirm behavior. Co-authored-by: monkut <[email protected]>
@javulticat would you please re-review when you get a chance? Behavior I started thinking about when fiddling with this: what do you think should happen when a csv file is sent back to the client, say via A vanilla Flask application (no additional The end result is that a file uploaded through I haven't seen any issues raised around this in the last year or so, but it seems wrong. The csv file would be binary so I think we'd want to encode as base64 for that too. Thoughts? |
@Quidge conversation resolved - thanks for improving the tests! I'm not sure I fully understand the question being asked around CSVs. Could you try restating? One point that may or may not help answer the question is that a CSV is a plain text file. So I'd imagine the handling of CSVs ( |
CSV (and plain text) can be in any character encoding, it is not guaranteed that it's ASCII or UTF-8. |
@zeevt that's my understanding as well, so I believe it would be fair to say that this is a potential concern for any text file, correct? I was confused because @Quidge's message implied this was a concern specific to CSVs (which I definitely may have just misinterpreted - sorry!) If so, would a solution be to sniff the encoding of plain text and use that to decode it using the proper encoding (and, if that fails, fall back on using base64-encoded bytes)? Something like: import chardet
# If plain text
try:
encoding = chardet.detect(response.data)["encoding"]
decoded = response.data.decode(encoding)
except Exception:
# base64 encode the bytes ? |
@javulticat You understood me correctly, yes. I have two concerns with this, now that I thought about it a tiny bit more:
I now think it's only safe to decode |
Migrated to: closing |
…ata is binary (#1155) * 🔧 migrate #971 to lastest master * 🎨 run black/isort * ♻️ refactor to allow for other binary ignore types based on mimetype. (currently openapi schema can't be passed as text. * 🎨 run black/fix flake8 * 🔧 add EXCEPTION_HANDLER setting * 🐛 fix zappa_returndict["body"] assignment * 📝 add temp debug info * 🔥 delete unnecessary print statements * ♻️ Update comments and minor refactor for clarity * ♻️ refactor for ease of testing and clarity * 🎨 fix flake8 * ✨ add `additional_text_mimetypes` setting ✅ add testcases for additional_text_mimetypes handling * 🔧 Expand default text mimetypes mentioned in #1023 ♻️ define "DEFAULT_TEXT_MIMETYPES" and move to utilities.py * 🎨 run black/isort * 🎨 run black/isort * 🎨 remove unnecesasry comment (black now reformats code) 🎨 change commented lines to docstring for test app
… if data is binary (zappa#1155) * 🔧 migrate zappa#971 to lastest master * 🎨 run black/isort * ♻️ refactor to allow for other binary ignore types based on mimetype. (currently openapi schema can't be passed as text. * 🎨 run black/fix flake8 * 🔧 add EXCEPTION_HANDLER setting * 🐛 fix zappa_returndict["body"] assignment * 📝 add temp debug info * 🔥 delete unnecessary print statements * ♻️ Update comments and minor refactor for clarity * ♻️ refactor for ease of testing and clarity * 🎨 fix flake8 * ✨ add `additional_text_mimetypes` setting ✅ add testcases for additional_text_mimetypes handling * 🔧 Expand default text mimetypes mentioned in zappa#1023 ♻️ define "DEFAULT_TEXT_MIMETYPES" and move to utilities.py * 🎨 run black/isort * 🎨 run black/isort * 🎨 remove unnecesasry comment (black now reformats code) 🎨 change commented lines to docstring for test app
Description
This PR re-implements the same functional change that was shown in a PR from the pre-fork Miserlou/zappa repository.
The change in that PR didn't make it into the Zappa 52 release from what I can tell. My company has been using that PR's code (in combination with the
flask_compress
library) for a couple weeks without issue so I'm opening this PR that copies that code.Additional tests are added for coverage. I'm not confident that the test organization that I used is correct or idiomatic for this codebase -- please critique and I'll change if wanted!
GitHub Issues
#908
Checklist I found in the contributors documentation:
Before you submit this PR, please make sure that you meet these criteria:
Did you read the contributing guide?
If this is a non-trivial commit, did you open a ticket for discussion?
Did you put the URL for that ticket in a comment in the code?
If you made a new function, did you write a good docstring for it?
Did you avoid putting "_" in front of your new function for no reason?
Did you write a test for your new code?
Did the Travis build pass?
Did you improve (or at least not significantly reduce) the amount of code test coverage?
Did you make sure this code actually works on Lambda, as well as locally?
Did you test this code with all of Python 3.6, Python 3.7 and Python 3.8 ?
Does this commit ONLY relate to the issue at hand and have your linter shit all over the code?