Skip to content

Commit d0cbd16

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 5bfa5dd commit d0cbd16

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
@@ -285,6 +285,30 @@ def test_wsgi_script_binary_support_base64_behavior(self):
285285
self.assertTrue(response["isBase64Encoded"])
286286
self.assertTrue(is_base64(response["body"]))
287287

288+
content_encoded_json = {
289+
**text_plain_event,
290+
**{"path": "/content_encoding_header_json1"},
291+
}
292+
293+
response = lh.handler(content_encoded_json, None)
294+
295+
self.assertEqual(response["statusCode"], 200)
296+
self.assertIn("isBase64Encoded", response)
297+
self.assertTrue(response["isBase64Encoded"])
298+
self.assertTrue(is_base64(response["body"]))
299+
300+
content_encoded_text_arbitrary = {
301+
**text_plain_event,
302+
**{"path": "/content_encoding_header_textarbitrary1"},
303+
}
304+
305+
response = lh.handler(content_encoded_text_arbitrary, None)
306+
307+
self.assertEqual(response["statusCode"], 200)
308+
self.assertIn("isBase64Encoded", response)
309+
self.assertTrue(response["isBase64Encoded"])
310+
self.assertTrue(is_base64(response["body"]))
311+
288312
def test_wsgi_script_on_cognito_event_request(self):
289313
"""
290314
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
@@ -28,3 +28,21 @@ def json_mimetype_response_1():
2828
@app.route("/arbitrarybinary_mimetype_response1", methods=["GET"])
2929
def arbitrary_mimetype_response_1():
3030
return Response(response=b"some binary data", mimetype="arbitrary/binary_mimetype")
31+
32+
33+
@app.route("/content_encoding_header_json1", methods=["GET"])
34+
def response_with_content_encoding_mimetype1():
35+
return Response(
36+
response=json.dumps({"some": "data"}),
37+
mimetype="application/json",
38+
headers={"Content-Encoding": "gzip"},
39+
)
40+
41+
42+
@app.route("/content_encoding_header_textarbitrary1", methods=["GET"])
43+
def response_with_content_encoding_mimetype2():
44+
return Response(
45+
response="OK",
46+
mimetype="text/arbitrary",
47+
headers={"Content-Encoding": "shouldnt_matter"},
48+
)

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)