Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions starlette/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ def __init__(self, scope: Scope, receive: Receive, send: Send) -> None:
self.scope = scope
self.receive = receive
self.send = send
# Precompute allowed methods string for optimal reuse
self._allowed_methods = [
method
for method in ("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
if getattr(self, method.lower(), None) is not None
]
self._allowed_methods_str = ", ".join(self._allowed_methods)
self._method_not_allowed_headers = {"Allow": self._allowed_methods_str}

def __await__(self) -> Generator[Any, None, None]:
return self.dispatch().__await__()
Expand All @@ -45,10 +48,10 @@ async def method_not_allowed(self, request: Request) -> Response:
# If we're running inside a starlette application then raise an
# exception, so that the configurable exception handler can deal with
# returning the response. For plain ASGI apps, just return the response.
headers = {"Allow": ", ".join(self._allowed_methods)}
if "app" in self.scope:
raise HTTPException(status_code=405, headers=headers)
return PlainTextResponse("Method Not Allowed", status_code=405, headers=headers)
raise HTTPException(status_code=405, headers=self._method_not_allowed_headers)
# Reuse precomputed headers dictionary for efficiency
return PlainTextResponse("Method Not Allowed", status_code=405, headers=self._method_not_allowed_headers)


class WebSocketEndpoint:
Expand Down