in core/actionProxy/owplatform/knative.py [0:0]
def postProcessResponse(requestHeaders, response):
CONTENT_TYPE = 'Content-Type'
content_types = {
'json': 'application/json',
'html': 'text/html',
}
statusCode = response.status
headers = {}
body = response.get_json() or {}
contentTypeInHeaders = False
# if a status code is specified set and remove from the body
# of the response
if 'statusCode' in body:
statusCode = body['statusCode']
del body['statusCode']
if 'headers' in body:
headers = body['headers']
del body['headers']
# content-type vs Content-Type
# make Content-Type standard
if CONTENT_TYPE.lower() in headers:
headers[CONTENT_TYPE] = headers[CONTENT_TYPE.lower()]
del headers[CONTENT_TYPE.lower()]
# if there is no content type specified make it html for string bodies
# and json for non-string bodies
if not CONTENT_TYPE in headers:
if isinstance(body, str):
headers[CONTENT_TYPE] = content_types['html']
else:
headers[CONTENT_TYPE] = content_types['json']
else:
contentTypeInHeaders = True
# a json object containing statusCode, headers, and body is what we expect from a web action
# so we only want to return the actual body
if 'body' in body:
body = body['body']
# if we are returning an image that is base64 encoded, we actually want to return the image
if contentTypeInHeaders and 'image' in headers[CONTENT_TYPE]:
body = base64.b64decode(body)
headers['Content-Transfer-Encoding'] = 'binary'
else:
body = dumps(body)
if statusCode == 200 and len(body) == 0:
statusCode = 204 # no content status code
if 'Access-Control-Allow-Origin' not in headers:
headers['Access-Control-Allow-Origin'] = '*'
if 'Access-Control-Allow-Methods' not in headers:
headers['Access-Control-Allow-Methods'] = 'OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH'
if 'Access-Control-Allow-Headers' not in headers:
headers['Access-Control-Allow-Headers'] = 'Authorization, Origin, X - Requested - With, Content - Type, Accept, User - Agent'
if 'Access-Control-Request-Headers' in requestHeaders:
headers['Access-Control-Request-Headers'] = requestHeaders['Access-Control-Request-Headers']
return flask.Response(body, statusCode, headers)