Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
log.log("entering handle")
if not isinstance(handle_code, customer_code.Function):
sys.exit(
'\n\n\nWARNING!\n\n'
'Your code is not compatible the the latest FDK!\n\n'
'Update Dockerfile entry point to:\n'
'ENTRYPOINT["/python/bin/fdk", "", {0}]\n\n'
'if __name__ == "__main__":\n\tfdk.handle(handler)\n\n'
'syntax no longer supported!\n'
'Update your code as soon as possible!'
'\n\n\n'.format(handle_code.__name__))
loop = asyncio.get_event_loop()
format_def = os.environ.get(constants.FN_FORMAT)
lsnr = os.environ.get(constants.FN_LISTENER)
log.log("{0} is set, value: {1}".
format(constants.FN_FORMAT, format_def))
if lsnr is None:
sys.exit("{0} is not set".format(constants.FN_LISTENER))
log.log("{0} is set, value: {1}".
format(constants.FN_LISTENER, lsnr))
if format_def == constants.HTTPSTREAM:
start(handle_code, lsnr, loop=loop)
else:
sys.exit("incompatible function format!")
async def inner(*args, **kwargs):
try:
return await action(*args, **kwargs)
except h11.ProtocolError as ex:
log.log(str(ex))
traceback.print_exc(file=sys.stderr)
sys.exit("Fn <-> FDK connectivity issue: ".format(str(ex)))
def start(handle_code: customer_code.Function,
uds: str,
loop: asyncio.AbstractEventLoop=None):
"""
Unix domain socket HTTP server entry point
:param handle_code: customer's code
:type handle_code: fdk.customer_code.Function
:param uds: path to a Unix domain socket
:type uds: str
:param loop: event loop
:type loop: asyncio.AbstractEventLoop
:return: None
"""
log.log("in http_stream.start")
socket_path = os.path.normpath(str(uds).lstrip("unix:"))
socket_dir, socket_file = os.path.split(socket_path)
if socket_file == "":
sys.exit("malformed FN_LISTENER env var "
"value: {0}".format(socket_path))
phony_socket_path = os.path.join(
socket_dir, "phony" + socket_file)
log.log("deleting socket files if they exist")
try:
os.remove(socket_path)
os.remove(phony_socket_path)
except OSError:
pass
'\n\n\n'.format(handle_code.__name__))
loop = asyncio.get_event_loop()
format_def = os.environ.get(constants.FN_FORMAT)
lsnr = os.environ.get(constants.FN_LISTENER)
log.log("{0} is set, value: {1}".
format(constants.FN_FORMAT, format_def))
if lsnr is None:
sys.exit("{0} is not set".format(constants.FN_LISTENER))
log.log("{0} is set, value: {1}".
format(constants.FN_LISTENER, lsnr))
if format_def == constants.HTTPSTREAM:
start(handle_code, lsnr, loop=loop)
else:
sys.exit("incompatible function format!")
def handle(handle_code: customer_code.Function):
"""
FDK entry point
:param handle_code: customer's code
:type handle_code: fdk.customer_code.Function
:return: None
"""
log.log("entering handle")
if not isinstance(handle_code, customer_code.Function):
sys.exit(
'\n\n\nWARNING!\n\n'
'Your code is not compatible the the latest FDK!\n\n'
'Update Dockerfile entry point to:\n'
'ENTRYPOINT["/python/bin/fdk", "", {0}]\n\n'
'if __name__ == "__main__":\n\tfdk.handle(handler)\n\n'
'syntax no longer supported!\n'
'Update your code as soon as possible!'
'\n\n\n'.format(handle_code.__name__))
loop = asyncio.get_event_loop()
format_def = os.environ.get(constants.FN_FORMAT)
lsnr = os.environ.get(constants.FN_LISTENER)
log.log("{0} is set, value: {1}".
format(constants.FN_FORMAT, format_def))
def main():
if len(sys.argv) < 3:
print("Usage: fdk-tcp-debug [entrypoint]")
sys.exit("at least func module must be specified")
if not os.path.exists(sys.argv[2]):
sys.exit("Module: {0} doesn't exist".format(sys.argv[1]))
if len(sys.argv) > 3:
handler = customer_code.Function(
sys.argv[2], entrypoint=sys.argv[3])
else:
handler = customer_code.Function(sys.argv[1])
tcp_debug.handle(handler, port=int(sys.argv[1]))
def main():
if len(sys.argv) < 3:
print("Usage: fdk-tcp-debug [entrypoint]")
sys.exit("at least func module must be specified")
if not os.path.exists(sys.argv[2]):
sys.exit("Module: {0} doesn't exist".format(sys.argv[1]))
if len(sys.argv) > 3:
handler = customer_code.Function(
sys.argv[2], entrypoint=sys.argv[3])
else:
handler = customer_code.Function(sys.argv[1])
tcp_debug.handle(handler, port=int(sys.argv[1]))
async def pure_handler(request):
log.log("in pure_handler")
data = None
if request.has_body:
log.log("has body: {}".format(request.has_body))
log.log("request comes with data")
data = await request.content.read()
response = await runner.handle_request(
handle_func, constants.HTTPSTREAM,
request=request, data=data)
log.log("request execution completed")
headers = response.context().GetResponseHeaders()
response_content_type = headers.get(
constants.CONTENT_TYPE, "application/json"
)
headers.set(constants.CONTENT_TYPE, response_content_type)
kwargs = {
"headers": headers.http_raw()
}
sdata = serialize_response_data(
Handles a function's request
:param handler_code: customer's code
:type handler_code: fdk.customer_code.Function
:param format_def: function's format
:type format_def: str
:param kwargs: request-specific parameters
:type kwargs: dict
:return: function's response
:rtype: fdk.response.Response
"""
log.log("in handle_request")
ctx, body = context.context_from_format(format_def, **kwargs)
log.log("context provisioned")
try:
response_data = await with_deadline(ctx, handler_code, body)
log.log("function result obtained")
if isinstance(response_data, response.Response):
return response_data
headers = ctx.GetResponseHeaders()
log.log("response headers obtained")
return response.Response(
ctx, response_data=response_data,
headers=headers, status_code=200)
except (Exception, TimeoutError) as ex:
log.log("exception appeared: {0}".format(ex))
traceback.print_exc(file=sys.stderr)
return errors.DispatchException(ctx, 502, str(ex)).response()
:param connection: h11 request parser
:type connection: h11.Connection
:param request_reader: async stream reader
:type request_reader: asyncio.StreamReader
:return: request and body
:rtype tuple
"""
# TODO(denismakogon): replace io.BytesIO with asyncio.StreamReader
# this change will be required when an FDK will enforce customer's
# function to be a coroutine
request, body = None, io.BytesIO()
log.log("starting process_chunk")
while True:
log.log("process_chunk: reading chunk of data from async reader")
buf = await request_reader.read(constants.ASYNC_IO_READ_BUFFER)
log.log("process_chunk: buffer filled")
connection.receive_data(buf)
log.log("process_chunk: sending data to h11")
while True:
event = connection.next_event()
log.log("process_chunk: event type {0}"
.format(type(event)))
if isinstance(event, h11.Request):
request = event
if isinstance(event, h11.Data):
body.write(event.data)
if isinstance(event, h11.EndOfMessage):
return request, body
if isinstance(event, (h11.NEED_DATA, h11.PAUSED)):
log.log("requiring more data or connection paused")
break
if isinstance(event, h11.ConnectionClosed):