Skip to content

Commit 9fb5527

Browse files
committed
Enable binary responses in the http-templates
This was not possible previously, and resulted in a byte array being converted to a string and creating an invalid file. To opt in, set the content type appropriately as: application/octet-stream. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 0ca73dc commit 9fb5527

File tree

2 files changed

+68
-44
lines changed

2 files changed

+68
-44
lines changed

template/python3-http-armhf/index.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,50 @@ class Context:
1919
def __init__(self):
2020
self.hostname = os.getenv('HOSTNAME', 'localhost')
2121

22-
def format_status_code(resp):
23-
if 'statusCode' in resp:
24-
return resp['statusCode']
22+
def format_status_code(res):
23+
if 'statusCode' in res:
24+
return res['statusCode']
2525

2626
return 200
2727

28-
def format_body(resp):
29-
if 'body' not in resp:
28+
def format_body(res, content_type):
29+
if content_type == 'application/octet-stream':
30+
return res['body']
31+
32+
if 'body' not in res:
3033
return ""
31-
elif type(resp['body']) == dict:
32-
return jsonify(resp['body'])
34+
elif type(res['body']) == dict:
35+
return jsonify(res['body'])
3336
else:
34-
return str(resp['body'])
37+
return str(res['body'])
3538

36-
def format_headers(resp):
37-
if 'headers' not in resp:
39+
def format_headers(res):
40+
if 'headers' not in res:
3841
return []
39-
elif type(resp['headers']) == dict:
42+
elif type(res['headers']) == dict:
4043
headers = []
41-
for key in resp['headers'].keys():
42-
header_tuple = (key, resp['headers'][key])
44+
for key in res['headers'].keys():
45+
header_tuple = (key, res['headers'][key])
4346
headers.append(header_tuple)
4447
return headers
4548

46-
return resp['headers']
49+
return res['headers']
50+
51+
def get_content_type(res):
52+
content_type = ""
53+
if 'headers' in res:
54+
content_type = res['headers'].get('Content-type', '')
55+
return content_type
4756

48-
def format_response(resp):
49-
if resp == None:
57+
def format_response(res):
58+
if res == None:
5059
return ('', 200)
5160

52-
statusCode = format_status_code(resp)
53-
body = format_body(resp)
54-
headers = format_headers(resp)
61+
statusCode = format_status_code(res)
62+
content_type = get_content_type(res)
63+
body = format_body(res, content_type)
64+
65+
headers = format_headers(res)
5566

5667
return (body, statusCode, headers)
5768

@@ -60,10 +71,11 @@ def format_response(resp):
6071
def call_handler(path):
6172
event = Event()
6273
context = Context()
74+
6375
response_data = handler.handle(event, context)
6476

65-
resp = format_response(response_data)
66-
return resp
77+
res = format_response(response_data)
78+
return res
6779

6880
if __name__ == '__main__':
6981
serve(app, host='0.0.0.0', port=5000)

template/python3-http-debian/index.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,52 @@ def __init__(self):
1717

1818
class Context:
1919
def __init__(self):
20-
self.hostname = os.environ['HOSTNAME']
20+
self.hostname = os.getenv('HOSTNAME', 'localhost')
2121

22-
def format_status_code(resp):
23-
if 'statusCode' in resp:
24-
return resp['statusCode']
22+
def format_status_code(res):
23+
if 'statusCode' in res:
24+
return res['statusCode']
2525

2626
return 200
2727

28-
def format_body(resp):
29-
if 'body' not in resp:
28+
def format_body(res, content_type):
29+
if content_type == 'application/octet-stream':
30+
return res['body']
31+
32+
if 'body' not in res:
3033
return ""
31-
elif type(resp['body']) == dict:
32-
return jsonify(resp['body'])
34+
elif type(res['body']) == dict:
35+
return jsonify(res['body'])
3336
else:
34-
return str(resp['body'])
37+
return str(res['body'])
3538

36-
def format_headers(resp):
37-
if 'headers' not in resp:
39+
def format_headers(res):
40+
if 'headers' not in res:
3841
return []
39-
elif type(resp['headers']) == dict:
42+
elif type(res['headers']) == dict:
4043
headers = []
41-
for key in resp['headers'].keys():
42-
header_tuple = (key, resp['headers'][key])
44+
for key in res['headers'].keys():
45+
header_tuple = (key, res['headers'][key])
4346
headers.append(header_tuple)
4447
return headers
4548

46-
return resp['headers']
49+
return res['headers']
50+
51+
def get_content_type(res):
52+
content_type = ""
53+
if 'headers' in res:
54+
content_type = res['headers'].get('Content-type', '')
55+
return content_type
4756

48-
def format_response(resp):
49-
if resp == None:
57+
def format_response(res):
58+
if res == None:
5059
return ('', 200)
5160

52-
statusCode = format_status_code(resp)
53-
body = format_body(resp)
54-
headers = format_headers(resp)
61+
statusCode = format_status_code(res)
62+
content_type = get_content_type(res)
63+
body = format_body(res, content_type)
64+
65+
headers = format_headers(res)
5566

5667
return (body, statusCode, headers)
5768

@@ -60,10 +71,11 @@ def format_response(resp):
6071
def call_handler(path):
6172
event = Event()
6273
context = Context()
74+
6375
response_data = handler.handle(event, context)
6476

65-
resp = format_response(response_data)
66-
return resp
77+
res = format_response(response_data)
78+
return res
6779

6880
if __name__ == '__main__':
69-
serve(app, host='0.0.0.0', port=5000)
81+
serve(app, host='0.0.0.0', port=5000)

0 commit comments

Comments
 (0)