From 8827f028179ff9bfc80f9c25b50e37cbc0dee37d Mon Sep 17 00:00:00 2001 From: monkut Date: Wed, 8 Dec 2021 12:43:50 +0900 Subject: [PATCH 1/5] :wrench: attempt to add binary encoding support for use with whitenoise --- zappa/handler.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/zappa/handler.py b/zappa/handler.py index 41336dc15..fac810097 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -583,6 +583,8 @@ def handler(self, event, context): ) if response.data: + content_encoding = response.headers.get("Content-Encoding", None) + binary_encodings = ("gzip", "compress", "deflate", "br") if ( settings.BINARY_SUPPORT and not response.mimetype.startswith("text/") @@ -592,6 +594,11 @@ def handler(self, event, context): response.data ).decode("utf-8") zappa_returndict["isBase64Encoded"] = True + elif settings.BINARY_SUPPORT and content_encoding in binary_encodings: + zappa_returndict["body"] = base64.b64encode( + response.data + ).decode("utf-8") + zappa_returndict["isBase64Encoded"] = True else: zappa_returndict["body"] = response.get_data(as_text=True) From 9592b406e70f469c30db384631dab0a94c3a18eb Mon Sep 17 00:00:00 2001 From: monkut Date: Wed, 8 Dec 2021 13:12:52 +0900 Subject: [PATCH 2/5] :wrench: attempt to support woff (fonts) with whitenoise --- zappa/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zappa/handler.py b/zappa/handler.py index fac810097..c9194dea4 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -588,7 +588,7 @@ def handler(self, event, context): if ( settings.BINARY_SUPPORT and not response.mimetype.startswith("text/") - and response.mimetype != "application/json" + and response.mimetype not in ("application/json", "application/font-woff") ): zappa_returndict["body"] = base64.b64encode( response.data From 99aa4696fef638ecc70ce5f9b553fc85dbf41a63 Mon Sep 17 00:00:00 2001 From: monkut Date: Wed, 8 Dec 2021 13:23:46 +0900 Subject: [PATCH 3/5] :wrench: attempt to support woff (fonts) with whitenoise --- zappa/handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zappa/handler.py b/zappa/handler.py index c9194dea4..eeafba0e9 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -584,6 +584,7 @@ def handler(self, event, context): if response.data: content_encoding = response.headers.get("Content-Encoding", None) + print(content_encoding) binary_encodings = ("gzip", "compress", "deflate", "br") if ( settings.BINARY_SUPPORT From c37887130bcbcf80655a695de33c99ddfc040c32 Mon Sep 17 00:00:00 2001 From: monkut Date: Wed, 8 Dec 2021 13:33:41 +0900 Subject: [PATCH 4/5] :wrench: remove debug --- zappa/handler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zappa/handler.py b/zappa/handler.py index eeafba0e9..fac810097 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -584,12 +584,11 @@ def handler(self, event, context): if response.data: content_encoding = response.headers.get("Content-Encoding", None) - print(content_encoding) binary_encodings = ("gzip", "compress", "deflate", "br") if ( settings.BINARY_SUPPORT and not response.mimetype.startswith("text/") - and response.mimetype not in ("application/json", "application/font-woff") + and response.mimetype != "application/json" ): zappa_returndict["body"] = base64.b64encode( response.data From 5490e9043d5b61472ee4cbfbc5d8b33bb7647664 Mon Sep 17 00:00:00 2001 From: monkut Date: Wed, 8 Dec 2021 14:18:36 +0900 Subject: [PATCH 5/5] :wrench: minor cleanup --- zappa/handler.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/zappa/handler.py b/zappa/handler.py index fac810097..45fcb4221 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -583,8 +583,6 @@ def handler(self, event, context): ) if response.data: - content_encoding = response.headers.get("Content-Encoding", None) - binary_encodings = ("gzip", "compress", "deflate", "br") if ( settings.BINARY_SUPPORT and not response.mimetype.startswith("text/") @@ -594,7 +592,7 @@ def handler(self, event, context): response.data ).decode("utf-8") zappa_returndict["isBase64Encoded"] = True - elif settings.BINARY_SUPPORT and content_encoding in binary_encodings: + elif settings.BINARY_SUPPORT and response.headers.get("Content-Encoding", None): zappa_returndict["body"] = base64.b64encode( response.data ).decode("utf-8")