Skip to content

Commit 6c6b226

Browse files
Quidgemonkut
authored andcommitted
Use Content-Encoding to identify if data is binary
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]>
1 parent b5942d9 commit 6c6b226

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

tests/test_handler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ def test_wsgi_script_binary_support_base64_behavior(self):
286286
self.assertTrue(response["isBase64Encoded"])
287287
self.assertTrue(is_base64(response["body"]))
288288

289+
content_encoded_json = {
290+
**text_plain_event,
291+
**{"path": "/content_encoding_header_json1"},
292+
}
293+
294+
response = lh.handler(content_encoded_json, None)
295+
296+
self.assertEqual(response["statusCode"], 200)
297+
self.assertIn("isBase64Encoded", response)
298+
self.assertTrue(response["isBase64Encoded"])
299+
self.assertTrue(is_base64(response["body"]))
300+
301+
content_encoded_text_arbitrary = {
302+
**text_plain_event,
303+
**{"path": "/content_encoding_header_textarbitrary1"},
304+
}
305+
306+
response = lh.handler(content_encoded_text_arbitrary, None)
307+
308+
self.assertEqual(response["statusCode"], 200)
309+
self.assertIn("isBase64Encoded", response)
310+
self.assertTrue(response["isBase64Encoded"])
311+
self.assertTrue(is_base64(response["body"]))
312+
289313
def test_wsgi_script_on_cognito_event_request(self):
290314
"""
291315
Ensure that requests sent by cognito behave sensibly

tests/test_wsgi_binary_support_app.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@ def json_mimetype_response_1():
2929
@app.route("/arbitrarybinary_mimetype_response1", methods=["GET"])
3030
def arbitrary_mimetype_response_1():
3131
return Response(response=b"some binary data", mimetype="arbitrary/binary_mimetype")
32+
33+
34+
@app.route("/content_encoding_header_json1", methods=["GET"])
35+
def response_with_content_encoding_mimetype1():
36+
return Response(
37+
response=json.dumps({"some": "data"}),
38+
mimetype="application/json",
39+
headers={"Content-Encoding": "gzip"},
40+
)
41+
42+
43+
@app.route("/content_encoding_header_textarbitrary1", methods=["GET"])
44+
def response_with_content_encoding_mimetype2():
45+
return Response(
46+
response="OK",
47+
mimetype="text/arbitrary",
48+
headers={"Content-Encoding": "shouldnt_matter"},
49+
)

zappa/handler.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,17 @@ def handler(self, event, context):
583583
)
584584

585585
if response.data:
586-
if (
586+
if settings.BINARY_SUPPORT and response.headers.get(
587+
"Content-Encoding"
588+
):
589+
# We could have a text response that's gzip
590+
# encoded. Therefore, we base-64 encode it.
591+
# Related: https://github.com/zappa/Zappa/issues/908
592+
zappa_returndict["body"] = base64.b64encode(
593+
response.data
594+
).decode("utf-8")
595+
zappa_returndict["isBase64Encoded"] = True
596+
elif (
587597
settings.BINARY_SUPPORT
588598
and not response.mimetype.startswith("text/")
589599
and response.mimetype != "application/json"

0 commit comments

Comments
 (0)