def __call__(self, chain: HandlerChain, context: RequestContext, response: Response): request = context.request # a werkzeug Request consumes form/multipart data from the socket stream, so we need to restore the payload here data = restore_payload(request) # TODO: rethink whether this proxy handling is necessary context.request.headers[ HEADER_LOCALSTACK_REQUEST_URL] = context.request.base_url result = self.forward_request( context, method=request.method, path=request.full_path if request.query_string else request.path, data=data, headers=request.headers, ) if type(result) == int: chain.respond(status_code=result) return if isinstance(result, tuple): # special case for Kinesis SubscribeToShard if len(result) == 2: response.status_code = 200 response.set_response(result[0]) response.headers.update(dict(result[1])) chain.stop() return if isinstance(result, RequestsResponse): response.status_code = result.status_code response.set_response(result.content) # make sure headers are set after the content, so potential content-length headers are overwritten response.headers.update(dict(result.headers)) # make sure content-length is re-calculated correctly, unless it's a HEAD request if request.method != "HEAD": length = response.calculate_content_length() if length is not None: response.headers["Content-Length"] = str(length) chain.stop() return raise ValueError("cannot create response for result %s" % result)
def __call__(self, chain: HandlerChain, context: RequestContext, response: Response) -> None: if (not config.DISABLE_CORS_CHECKS and self.should_enforce_self_managed_service(context) and not self.is_cors_origin_allowed(context.request.headers)): LOG.info( "Blocked CORS request from forbidden origin %s", context.request.headers.get("origin") or context.request.headers.get("referer"), ) response.status_code = 403 chain.terminate()
def __call__( self, chain: HandlerChain, exception: Exception, context: RequestContext, response: Response, ): if response.data: # response already set return LOG.debug("setting internal failure response for %s", exception) response.status_code = 500 response.set_json( { "message": "Unexpected exception", "error": str(exception), "type": str(exception.__class__.__name__), } )
def populate_default_response(self, response: Response): response.status_code = self.status_code response.data = self.body response.headers.update(self.headers)