Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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):
def setup_unix_server(handle_func, loop=None):
log.log("in setup_unix_server")
app = web.Application(loop=loop)
app.router.add_post('/{tail:.*}', handle(handle_func))
return app
shutdown_timeout=0.1)
loop.run_until_complete(uds_sock.start())
try:
try:
log.log("CHMOD 666 {0}".format(phony_socket_path))
os.chmod(phony_socket_path, 0o666)
log.log("phony socket permissions: {0}"
.format(oct(os.stat(phony_socket_path).st_mode)))
log.log("sym-linking {0} to {1}".format(
socket_path, phony_socket_path))
os.symlink(os.path.basename(phony_socket_path), socket_path)
log.log("socket permissions: {0}"
.format(oct(os.stat(socket_path).st_mode)))
except (Exception, BaseException) as ex:
log.log(str(ex))
raise ex
try:
log.log("starting infinite loop")
loop.run_forever()
except web.GracefulExit:
pass
finally:
loop.run_until_complete(app_runner.cleanup())
if hasattr(loop, 'shutdown_asyncgens'):
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
srv = app.AsyncHTTPServer(name="fdk", router=rtr)
start_serving, server_forever = srv.run(sock=sock, loop=loop)
try:
log.log("CHMOD 666 {0}".format(phony_socket_path))
os.chmod(phony_socket_path, 0o666)
log.log("phony socket permissions: {0}"
.format(oct(os.stat(phony_socket_path).st_mode)))
log.log("calling '.start_serving()'")
start_serving()
log.log("sym-linking {0} to {1}".format(
socket_path, phony_socket_path))
os.symlink(os.path.basename(phony_socket_path), socket_path)
log.log("socket permissions: {0}"
.format(oct(os.stat(socket_path).st_mode)))
log.log("starting infinite loop")
except (Exception, BaseException) as ex:
log.log(str(ex))
raise ex
server_forever()
async def close(response_writer: asyncio.StreamWriter):
log.log("closing response writer")
try:
response_writer.close()
log.log("connection closed")
await response_writer.wait_closed()
log.log("awaiting for connection to be closed")
except Exception as ex:
print(str(ex), file=sys.stderr, flush=True)
log.log("starting unix socket site")
uds_sock = web.UnixSite(
app_runner, phony_socket_path,
shutdown_timeout=0.1)
loop.run_until_complete(uds_sock.start())
try:
try:
log.log("CHMOD 666 {0}".format(phony_socket_path))
os.chmod(phony_socket_path, 0o666)
log.log("phony socket permissions: {0}"
.format(oct(os.stat(phony_socket_path).st_mode)))
log.log("sym-linking {0} to {1}".format(
socket_path, phony_socket_path))
os.symlink(os.path.basename(phony_socket_path), socket_path)
log.log("socket permissions: {0}"
.format(oct(os.stat(socket_path).st_mode)))
except (Exception, BaseException) as ex:
log.log(str(ex))
raise ex
try:
log.log("starting infinite loop")
loop.run_forever()
except web.GracefulExit:
pass
finally:
loop.run_until_complete(app_runner.cleanup())
if hasattr(loop, 'shutdown_asyncgens'):
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()