Skip to content

Commit

Permalink
Remove vestigial patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Sep 4, 2024
1 parent 7f7329d commit 7e62034
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 42 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ all = [
"rich",
"sparse",
"sqlalchemy[asyncio] >=2",
"starlette",
"starlette >=0.38.0",
"tifffile",
"typer",
"uvicorn[standard]",
Expand Down Expand Up @@ -217,6 +217,7 @@ minimal-server = [
"python-multipart",
"pyyaml",
"sqlalchemy[asyncio] >=2",
"starlette >=0.38.0",
"starlette",
"typer",
"uvicorn[standard]",
Expand Down Expand Up @@ -266,7 +267,7 @@ server = [
"pyyaml",
"sparse",
"sqlalchemy[asyncio] >=2",
"starlette",
"starlette >=0.38.0",
"tifffile",
"typer",
"uvicorn[standard]",
Expand Down
7 changes: 0 additions & 7 deletions tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from . import schemas
from .authentication import get_current_principal
from .compression import CompressionMiddleware
from .core import PatchedStreamingResponse
from .dependencies import (
get_query_registry,
get_root_tree,
Expand Down Expand Up @@ -714,7 +713,6 @@ async def double_submit_cookie_csrf_protection(request: Request, call_next):
)

response = await call_next(request)
response.__class__ = PatchedStreamingResponse # tolerate memoryview
if not csrf_cookie:
response.set_cookie(
key=CSRF_COOKIE_NAME,
Expand Down Expand Up @@ -754,7 +752,6 @@ async def client_compatibility_check(request: Request, call_next):
},
)
response = await call_next(request)
response.__class__ = PatchedStreamingResponse # tolerate memoryview
return response

@app.middleware("http")
Expand All @@ -763,7 +760,6 @@ async def set_cookies(request: Request, call_next):
# Create some Request state, to be (possibly) populated by dependencies.
request.state.cookies_to_set = []
response = await call_next(request)
response.__class__ = PatchedStreamingResponse # tolerate memoryview
for params in request.state.cookies_to_set:
params.setdefault("httponly", True)
params.setdefault("samesite", "lax")
Expand Down Expand Up @@ -818,7 +814,6 @@ async def capture_metrics(request: Request, call_next):
# Record the overall application time.
with record_timing(metrics, "app"):
response = await call_next(request)
response.__class__ = PatchedStreamingResponse # tolerate memoryview
# Server-Timing specifies times should be in milliseconds.
# Prometheus specifies times should be in seconds.
# Therefore, we store as seconds and convert to ms for Server-Timing here.
Expand Down Expand Up @@ -873,7 +868,6 @@ async def capture_metrics_prometheus(request: Request, call_next):
# send an appropriate response to the client.
raise
finally:
response.__class__ = PatchedStreamingResponse # tolerate memoryview
metrics.capture_request_metrics(request, response)

# This is a *real* response (i.e., not the 'only_for_metrics' response).
Expand All @@ -884,7 +878,6 @@ async def capture_metrics_prometheus(request: Request, call_next):
async def current_principal_logging_filter(request: Request, call_next):
request.state.principal = SpecialUsers.public
response = await call_next(request)
response.__class__ = PatchedStreamingResponse # tolerate memoryview
current_principal.set(request.state.principal)
return response

Expand Down
36 changes: 3 additions & 33 deletions tiled/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import jmespath
import msgpack
from fastapi import HTTPException, Response
from starlette.responses import JSONResponse, Send, StreamingResponse
from starlette.responses import JSONResponse, StreamingResponse
from starlette.status import HTTP_200_OK, HTTP_304_NOT_MODIFIED, HTTP_400_BAD_REQUEST

# Some are not directly used, but they register things on import.
Expand Down Expand Up @@ -386,9 +386,9 @@ async def construct_data_response(
f"This type is supported in general but there was an error packing this specific data: {err.args[0]}",
)
if isinstance(content, types.GeneratorType):
response_class = PatchedStreamingResponse
response_class = StreamingResponse
else:
response_class = PatchedResponse
response_class = Response
return response_class(
content,
media_type=media_type,
Expand Down Expand Up @@ -547,36 +547,6 @@ async def construct_resource(
return resource


class PatchedResponse(Response):
"Patch the render method to accept memoryview."

def render(self, content: Any) -> bytes:
if isinstance(content, memoryview):
return content.cast("B")
return super().render(content)


class PatchedStreamingResponse(StreamingResponse):
"Patch the stream_response method to accept memoryview."

async def stream_response(self, send: Send) -> None:
await send(
{
"type": "http.response.start",
"status": self.status_code,
"headers": self.raw_headers,
}
)
async for chunk in self.body_iterator:
# BEGIN ALTERATION
if not isinstance(chunk, (bytes, memoryview)):
# END ALTERATION
chunk = chunk.encode(self.charset)
await send({"type": "http.response.body", "body": chunk, "more_body": True})

await send({"type": "http.response.body", "body": b"", "more_body": False})


class NumpySafeJSONResponse(JSONResponse):
def __init__(self, *args, metrics, **kwargs):
self.__metrics = metrics
Expand Down

0 comments on commit 7e62034

Please sign in to comment.