From fef1c91f53dd76cb738544d936952f45fd482ca3 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Mon, 4 Jan 2021 16:15:14 -0700 Subject: [PATCH 01/16] fix potential none reference --- flask_lambda.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index 1a57560..159d46b 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -40,14 +40,15 @@ def make_environ(event): environ = {} - for hdr_name, hdr_value in event['headers'].items(): - hdr_name = hdr_name.replace('-', '_').upper() - if hdr_name in ['CONTENT_TYPE', 'CONTENT_LENGTH']: - environ[hdr_name] = hdr_value - continue - - http_hdr_name = 'HTTP_%s' % hdr_name - environ[http_hdr_name] = hdr_value + if event['headers'] is not None: + for hdr_name, hdr_value in event['headers'].items(): + hdr_name = hdr_name.replace('-', '_').upper() + if hdr_name in ['CONTENT_TYPE', 'CONTENT_LENGTH']: + environ[hdr_name] = hdr_value + continue + + http_hdr_name = 'HTTP_%s' % hdr_name + environ[http_hdr_name] = hdr_value qs = event['queryStringParameters'] From 38b38bf8193ee73a9e592255022a95d74c6c7964 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Mon, 4 Jan 2021 16:36:48 -0700 Subject: [PATCH 02/16] initialize necessary variables in environ in case they didn't come from headers --- flask_lambda.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 159d46b..3bbc3f5 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -38,7 +38,11 @@ def make_environ(event): - environ = {} + environ = { + 'HTTP_HOST': '', + 'HTTP_X_FORWARDED_PORT': '', + 'HTTP_X_FORWARDED_PROTO': '' + } if event['headers'] is not None: for hdr_name, hdr_value in event['headers'].items(): From 4910b246e603aea7d0b74ef700795feda71cd767 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Thu, 7 Jan 2021 16:44:46 -0700 Subject: [PATCH 03/16] Join all of the parts of the iterator to get the entire response instead of only returning the first 8192 bytes in the filewrapper buffer. --- flask_lambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 3bbc3f5..534f8a2 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -103,7 +103,7 @@ def __call__(self, event, context): response = LambdaResponse() - body = next(self.wsgi_app( + body = b''.join(self.wsgi_app( make_environ(event), response.start_response )) From 8fec411031834f858ed436144508457f6a68d524 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 15:11:54 -0700 Subject: [PATCH 04/16] base64 encode all response bodies --- flask_lambda.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 534f8a2..cc0c6b5 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -15,6 +15,7 @@ # under the License. import sys +import base64 try: from urllib import urlencode @@ -111,5 +112,6 @@ def __call__(self, event, context): return { 'statusCode': response.status, 'headers': response.response_headers, - 'body': body + 'body': base64.b64encode(body).decode('utf-8'), + 'isBase64Encoded': True } From 34d5d3c0445d8395e22e52543865c4d9e5f5748a Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 17:12:09 -0700 Subject: [PATCH 05/16] only base64 encode binary data --- flask_lambda.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index cc0c6b5..16c80a9 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -109,9 +109,13 @@ def __call__(self, event, context): response.start_response )) + content_type = response.response_headers['Content-Type'] + if not content_type or not content_type.startswith('text') or 'json' not in content_type or 'xml' not in content_type: + body = base64.b64encode(body).decode('utf-8') + return { 'statusCode': response.status, 'headers': response.response_headers, - 'body': base64.b64encode(body).decode('utf-8'), + 'body': body, 'isBase64Encoded': True } From 97093185ceeacc2caab0ea46f1d99bfd28179a8e Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 17:24:34 -0700 Subject: [PATCH 06/16] use in instead of contains --- flask_lambda.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 16c80a9..59e408d 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -110,9 +110,10 @@ def __call__(self, event, context): )) content_type = response.response_headers['Content-Type'] - if not content_type or not content_type.startswith('text') or 'json' not in content_type or 'xml' not in content_type: + if 'text' not in content_type or 'json' not in content_type or 'xml' not in content_type: body = base64.b64encode(body).decode('utf-8') + print(response.response_headers) return { 'statusCode': response.status, 'headers': response.response_headers, From a8ca450134ecb7aebe575569c70fbb8da6c37dd0 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 17:30:34 -0700 Subject: [PATCH 07/16] and not or... --- flask_lambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 59e408d..f92adef 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -110,7 +110,7 @@ def __call__(self, event, context): )) content_type = response.response_headers['Content-Type'] - if 'text' not in content_type or 'json' not in content_type or 'xml' not in content_type: + if 'text' not in content_type and 'json' not in content_type and 'xml' not in content_type: body = base64.b64encode(body).decode('utf-8') print(response.response_headers) From 11ae551b6cc953067b25f094455d41b27ebf183c Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 20:55:55 -0700 Subject: [PATCH 08/16] also exclude javascript and anything with a charset because that's obviously text --- flask_lambda.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index f92adef..ba37c28 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -110,7 +110,11 @@ def __call__(self, event, context): )) content_type = response.response_headers['Content-Type'] - if 'text' not in content_type and 'json' not in content_type and 'xml' not in content_type: + if 'text' not in content_type \ + and 'json' not in content_type \ + and 'xml' not in content_type\ + and 'javascript' not in content_type\ + and 'charset=' not in content_type: body = base64.b64encode(body).decode('utf-8') print(response.response_headers) From 9096747560e55ca498db6318ef4e7499ffbd8250 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 20 Jan 2021 22:25:39 -0700 Subject: [PATCH 09/16] dont' always set base64 encoded to true --- flask_lambda.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index ba37c28..91feb7f 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -110,17 +110,19 @@ def __call__(self, event, context): )) content_type = response.response_headers['Content-Type'] + + response = { + 'statusCode': response.status, + 'headers': response.response_headers, + 'body': body, + 'isBase64Encoded': False + } if 'text' not in content_type \ and 'json' not in content_type \ and 'xml' not in content_type\ and 'javascript' not in content_type\ and 'charset=' not in content_type: - body = base64.b64encode(body).decode('utf-8') + response['body'] = base64.b64encode(body).decode('utf-8') + response['isBase64Encoded'] = True - print(response.response_headers) - return { - 'statusCode': response.status, - 'headers': response.response_headers, - 'body': body, - 'isBase64Encoded': True - } + return response From df0777f795b0506ba0e623693984c86363b1bacd Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Thu, 21 Jan 2021 10:05:22 -0700 Subject: [PATCH 10/16] don't base64 encode, it doesn't work... --- flask_lambda.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index 91feb7f..534f8a2 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -15,7 +15,6 @@ # under the License. import sys -import base64 try: from urllib import urlencode @@ -109,20 +108,8 @@ def __call__(self, event, context): response.start_response )) - content_type = response.response_headers['Content-Type'] - - response = { + return { 'statusCode': response.status, 'headers': response.response_headers, - 'body': body, - 'isBase64Encoded': False + 'body': body } - if 'text' not in content_type \ - and 'json' not in content_type \ - and 'xml' not in content_type\ - and 'javascript' not in content_type\ - and 'charset=' not in content_type: - response['body'] = base64.b64encode(body).decode('utf-8') - response['isBase64Encoded'] = True - - return response From ab31c3ca5c79c0e71c5e260b1ba2ef9caa0f8890 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Fri, 22 Jan 2021 10:43:41 -0700 Subject: [PATCH 11/16] debugging --- flask_lambda.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/flask_lambda.py b/flask_lambda.py index 534f8a2..9df87e6 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -15,6 +15,7 @@ # under the License. import sys +import json try: from urllib import urlencode @@ -95,6 +96,12 @@ def start_response(self, status, response_headers, exc_info=None): class FlaskLambda(Flask): def __call__(self, event, context): + print('*'*10 + 'Start Event' + '*'*10) + print(json.dumps(event, indent=2)) + print('*'*10 + 'End Event' + '*'*10) + print('*'*10 + 'Start Event' + '*'*10) + print(json.dumps(context, indent=2)) + print('*'*10 + 'End Event' + '*'*10) if 'httpMethod' not in event: # In this "context" `event` is `environ` and # `context` is `start_response`, meaning the request didn't From 3572dcbc543967c8c10e43a5b31bbb4ac0c2f0b3 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Fri, 22 Jan 2021 15:39:47 -0700 Subject: [PATCH 12/16] context is not serializable --- flask_lambda.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index 9df87e6..1f16e13 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -99,9 +99,6 @@ def __call__(self, event, context): print('*'*10 + 'Start Event' + '*'*10) print(json.dumps(event, indent=2)) print('*'*10 + 'End Event' + '*'*10) - print('*'*10 + 'Start Event' + '*'*10) - print(json.dumps(context, indent=2)) - print('*'*10 + 'End Event' + '*'*10) if 'httpMethod' not in event: # In this "context" `event` is `environ` and # `context` is `start_response`, meaning the request didn't From bc8af0ad4103f3d25dd9afcc63bd2be9d82c3a98 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Sun, 24 Jan 2021 11:53:07 -0700 Subject: [PATCH 13/16] don't pretty print because it's hard to read in cloudwatch --- flask_lambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 1f16e13..4ff1473 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -97,7 +97,7 @@ def start_response(self, status, response_headers, exc_info=None): class FlaskLambda(Flask): def __call__(self, event, context): print('*'*10 + 'Start Event' + '*'*10) - print(json.dumps(event, indent=2)) + print(json.dumps(event)) print('*'*10 + 'End Event' + '*'*10) if 'httpMethod' not in event: # In this "context" `event` is `environ` and From 114f2ad63026c1364fd6e97f926b83d4f81e9609 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Mon, 25 Jan 2021 12:11:15 -0700 Subject: [PATCH 14/16] print the response for debugging --- flask_lambda.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index 4ff1473..8579dfc 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -96,9 +96,7 @@ def start_response(self, status, response_headers, exc_info=None): class FlaskLambda(Flask): def __call__(self, event, context): - print('*'*10 + 'Start Event' + '*'*10) print(json.dumps(event)) - print('*'*10 + 'End Event' + '*'*10) if 'httpMethod' not in event: # In this "context" `event` is `environ` and # `context` is `start_response`, meaning the request didn't @@ -112,8 +110,10 @@ def __call__(self, event, context): response.start_response )) - return { + res = { 'statusCode': response.status, 'headers': response.response_headers, 'body': body } + print(json.dumps(res)) + return res From 8da2e39b4ce3139c8060266f16d29268b721a862 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Fri, 29 Jan 2021 16:45:23 -0700 Subject: [PATCH 15/16] remove debug logging --- flask_lambda.py | 1 - 1 file changed, 1 deletion(-) diff --git a/flask_lambda.py b/flask_lambda.py index 8579dfc..12cd347 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -115,5 +115,4 @@ def __call__(self, event, context): 'headers': response.response_headers, 'body': body } - print(json.dumps(res)) return res From e097c7f8a7316891d160ef6c45fb3c04f9bfe91f Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Fri, 29 Jan 2021 16:49:54 -0700 Subject: [PATCH 16/16] base64 encode response body it's it's not text --- flask_lambda.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/flask_lambda.py b/flask_lambda.py index 12cd347..31647c7 100644 --- a/flask_lambda.py +++ b/flask_lambda.py @@ -15,7 +15,7 @@ # under the License. import sys -import json +import base64 try: from urllib import urlencode @@ -96,7 +96,6 @@ def start_response(self, status, response_headers, exc_info=None): class FlaskLambda(Flask): def __call__(self, event, context): - print(json.dumps(event)) if 'httpMethod' not in event: # In this "context" `event` is `environ` and # `context` is `start_response`, meaning the request didn't @@ -113,6 +112,18 @@ def __call__(self, event, context): res = { 'statusCode': response.status, 'headers': response.response_headers, - 'body': body + 'body': body, + 'isBase64Encoded': False } + + content_type = response.response_headers['Content-Type'] + if 'text' not in content_type \ + and 'json' not in content_type \ + and 'xml' not in content_type\ + and 'javascript' not in content_type\ + and 'charset=' not in content_type: + res['body'] = base64.b64encode(body).decode('utf-8') + res['isBase64Encoded'] = True + + print(response.response_headers) return res