async def webhook_handler(request: web.Request): payload = await request.json() auth = BasicAuth.decode(request.headers["Authorization"]) return web.json_response({ "username": auth.login, "password": auth.password, "payload": payload })
async def func() -> web.Response: request_ip = RequestHandler.get_request_ip( request, context) if request.headers.get('Authorization'): try: request._cache['auth'] = BasicAuth.decode( request.headers.get('Authorization')) except ValueError: pass if access_log: timer = time.time() response = web.Response( status=503, # type: ignore headers={}) # type: web.Response try: response = await handler(request) response.headers[hdrs.SERVER] = server_header or '' except web.HTTPException as e: error_handler = context.get('_http_error_handler', {}).get(e.status, None) if error_handler: response = await error_handler(request) response.headers[hdrs.SERVER] = server_header or '' else: response = e response.headers[hdrs.SERVER] = server_header or '' response.body = str(e).encode('utf-8') except Exception as e: error_handler = context.get('_http_error_handler', {}).get(500, None) logging.getLogger('exception').exception( 'Uncaught exception: {}'.format(str(e))) if error_handler: response = await error_handler(request) response.headers[hdrs.SERVER] = server_header or '' else: response = web.HTTPInternalServerError( ) # type: ignore response.headers[hdrs.SERVER] = server_header or '' response.body = b'' finally: if not request.transport: response = web.Response( status=499, # type: ignore headers={}) # type: web.Response response._eof_sent = True if access_log: request_time = time.time() - timer version_string = None if isinstance(request.version, HttpVersion): version_string = 'HTTP/{}.{}'.format( request.version.major, request.version.minor) if not request._cache.get('is_websocket'): status_code = response.status if response is not None else 500 ignore_logging = getattr( handler, 'ignore_logging', False) if ignore_logging is True: pass elif isinstance( ignore_logging, (list, tuple)) and status_code in ignore_logging: pass else: logging.getLogger('transport.http').info( '[{}] [{}] {} {} "{} {}{}{}" {} {} "{}" {}' .format( RequestHandler.colorize_status( 'http', status_code), RequestHandler.colorize_status( status_code), request_ip, '"{}"'.format( request._cache['auth'].login. replace('"', '')) if request._cache.get('auth') and getattr(request._cache.get('auth'), 'login', None) else '-', request.method, request.path, '?{}'.format(request.query_string) if request.query_string else '', ' {}'.format(version_string) if version_string else '', response.content_length if response is not None and response.content_length is not None else '-', request.content_length if request.content_length is not None else '-', request.headers.get( 'User-Agent', '').replace('"', ''), '{0:.5f}s'.format( round(request_time, 5)))) else: logging.getLogger('transport.http').info( '[{}] {} {} "CLOSE {}{}" {} "{}" {}'. format( RequestHandler.colorize_status( 'websocket', 101), request_ip, '"{}"'.format(request._cache['auth']. login.replace('"', '')) if request._cache.get('auth') and getattr(request._cache.get('auth'), 'login', None) else '-', request.path, '?{}'.format(request.query_string) if request.query_string else '', request._cache.get( 'websocket_uuid', ''), request.headers.get('User-Agent', '').replace( '"', ''), '{0:.5f}s'.format( round(request_time, 5)))) if isinstance( response, (web.HTTPException, web.HTTPInternalServerError)): raise response return response
async def func() -> web.Response: if request.transport: peername = request.transport.get_extra_info('peername') request_ip = None if peername: request_ip, _ = peername if request.headers.get(real_ip_header) and request_ip and len(real_ip_from): if any([ipaddress.ip_address(request_ip) in ipaddress.ip_network(cidr) for cidr in real_ip_from]): request_ip = request.headers.get(real_ip_header).split(',')[0].strip().split(' ')[0].strip() request.request_ip = request_ip request.auth = None request.is_websocket = False if request.headers.get('Authorization'): try: request.auth = BasicAuth.decode(request.headers.get('Authorization')) except ValueError: pass if access_log: timer = time.time() response = None try: response = await handler(request) response.headers[hdrs.SERVER] = server_header or '' except web.HTTPException as e: error_handler = context.get('_http_error_handler', {}).get(e.status, None) if error_handler: response = await error_handler(request) response.headers[hdrs.SERVER] = server_header or '' else: response = e response.headers[hdrs.SERVER] = server_header or '' response.body = str(e).encode('utf-8') except Exception as e: error_handler = context.get('_http_error_handler', {}).get(500, None) if not context.get('log_level') or context.get('log_level') in ['DEBUG']: traceback.print_exception(e.__class__, e, e.__traceback__) if error_handler: response = await error_handler(request) response.headers[hdrs.SERVER] = server_header or '' else: response = web.HTTPInternalServerError() response.headers[hdrs.SERVER] = server_header or '' response.body = b'' finally: if not request.transport: response = web.Response(status=499) response._eof_sent = True if access_log: request_time = time.time() - timer version_string = None if isinstance(request.version, HttpVersion): version_string = 'HTTP/{}.{}'.format(request.version.major, request.version.minor) if not request.is_websocket: logging.getLogger('transport.http').info('[http] [{}] {} {} "{} {}{}{}" {} {} "{}" {}'.format( response.status if response is not None else 500, request.request_ip, '"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-', request.method, request.path, '?{}'.format(request.query_string) if request.query_string else '', ' {}'.format(version_string) if version_string else '', response.content_length if response is not None and response.content_length is not None else '-', request.content_length if request.content_length is not None else '-', request.headers.get('User-Agent', '').replace('"', ''), '{0:.5f}s'.format(round(request_time, 5)) )) else: logging.getLogger('transport.http').info('[websocket] {} {} "CLOSE {}{}" {} "{}" {}'.format( request.request_ip, '"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-', request.path, '?{}'.format(request.query_string) if request.query_string else '', request.websocket_uuid, request.headers.get('User-Agent', '').replace('"', ''), '{0:.5f}s'.format(round(request_time, 5)) )) return response