async def send(self, message: Message, send: Send, request_headers: Headers): if message["type"] != "http.response.start": await send(message) return message.setdefault("headers", []) headers = MutableHeaders(scope=message) headers.update(self.__headers) await send(message)
def headers(): h = MutableHeaders() h.update({ HeaderKeys.correlation_id: dummy_correlation_id, HeaderKeys.request_id: dummy_request_id, HeaderKeys.date: dummy_date, HeaderKeys.user_agent: dummy_user_agent, HeaderKeys.forwarded_for: dummy_forwarded_for, }) return h
async def send_cached(message): # Borrowing from: # https://github.com/encode/starlette/blob/master/starlette/middleware/cors.py if message["type"] != "http.response.start": return await send(message) message.setdefault("headers", []) headers = MutableHeaders(scope=message) headers.update({ 'Cache-Control': f'max-age={max_age}', 'Expires': http_date(time.time() + max_age), }) await send(message)
async def send(self, message, send=None, origin=None): if message["type"] != "http.response.start": await send(message) return message.setdefault("headers", []) headers = MutableHeaders(message["headers"]) # If we only allow specific origins, then we have to mirror back # the Origin header in the response. if not self.allow_all_origins and self.is_allowed_origin( origin=origin): headers["Access-Control-Allow-Origin"] = origin headers.update(self.simple_headers) await send(message)
async def send(self, message, send, request_headers) -> None: if message["type"] != "http.response.start": await send(message) return message.setdefault("headers", []) headers = MutableHeaders(scope=message) headers.update(self.simple_headers) origin = request_headers["Origin"] has_cookie = "cookie" in request_headers if self.allow_all_origins and has_cookie: headers["Access-Control-Allow-Origin"] = origin elif not self.allow_all_origins and \ self.is_allowed_origin(origin=origin): headers["Access-Control-Allow-Origin"] = origin headers.add_vary_header("Origin") await send(message)
async def send( self, message: Message, send: Send, request_headers: Headers ) -> None: if message["type"] != "http.response.start": await send(message) return message.setdefault("headers", []) headers = MutableHeaders(scope=message) headers.update(self.simple_headers) origin = request_headers["Origin"] has_cookie = "cookie" in request_headers # If request includes any cookie headers, then we must respond # with the specific origin instead of '*'. if self.allow_all_origins and has_cookie: headers["Access-Control-Allow-Origin"] = origin # If we only allow specific origins, then we have to mirror back # the Origin header in the response. elif not self.allow_all_origins and self.is_allowed_origin(origin=origin): headers["Access-Control-Allow-Origin"] = origin headers.add_vary_header("Origin") await send(message)