def get_response_for_static_content( request: Request, content_type: bytes, contents: bytes, last_modified_time: float, cache_time: int = 10800, ) -> Response: """ Returns a response object to serve static content. """ current_etag = str(last_modified_time).encode() previous_etag = request.if_none_match headers = [ (b"Last-Modified", formatdate(last_modified_time, usegmt=True).encode()), (b"ETag", current_etag), ] if cache_time > 0: headers.append( (b"Cache-Control", b"max-age=" + str(cache_time).encode())) if previous_etag and current_etag == previous_etag: # handle HTTP 304 Not Modified return Response(304, headers, None) if request.method == "HEAD": headers.append((b"Content-Type", content_type)) headers.append((b"Content-Length", str(len(contents)).encode())) return Response(200, headers, None) return Response(200, headers, Content(content_type, contents))
def produce_response(self, request: Request, exc: Exception) -> Response: tb = traceback.format_exception(exc.__class__, exc, exc.__traceback__) info = "" for item in tb: info += f"<li><pre>{html.escape(item)}</pre></li>" content = HtmlContent( self._error_page_template.format_map({ "info": info, "exctype": exc.__class__.__name__, "excmessage": str(exc), "method": request.method, "path": request.url.value.decode(), "full_url": get_request_url(request), })) return Response(500, content=content)
async def options_handler(request): return Response(404)