Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom head method in HTTPEndpoint #1222

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions starlette/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ def __await__(self) -> typing.Generator:

async def dispatch(self) -> None:
request = Request(self.scope, receive=self.receive)
handler_name = "get" if request.method == "HEAD" else request.method.lower()

if request.method == "HEAD":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safer to check request.method.lower() == "head"

if hasattr(self, "head"):
handler_name = "head"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have test coverage for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In which file would the relevant tests be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure, maybe tests/test_endpoints.py. Or any other places using HTTPEndpoint

else:
handler_name = "get"
else:
handler_name = request.method.lower()

Comment on lines +26 to +34
Copy link
Sponsor Member

@Kludex Kludex Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need to add the not hasattr(self, "head") logic.

Suggested change
if request.method == "HEAD":
if hasattr(self, "head"):
handler_name = "head"
else:
handler_name = "get"
else:
handler_name = request.method.lower()
handler_name = (
"get"
if request.method == "HEAD" and not hasattr(self, "head")
else request.method.lower()
)

handler = getattr(self, handler_name, self.method_not_allowed)
is_async = asyncio.iscoroutinefunction(handler)
if is_async:
Expand Down Expand Up @@ -101,9 +109,7 @@ async def decode(self, websocket: WebSocket, message: Message) -> typing.Any:
await websocket.close(code=status.WS_1003_UNSUPPORTED_DATA)
raise RuntimeError("Malformed JSON data received.")

assert (
self.encoding is None
), f"Unsupported 'encoding' attribute {self.encoding}"
assert self.encoding is None, f"Unsupported 'encoding' attribute {self.encoding}"
return message["text"] if message.get("text") else message["bytes"]

async def on_connect(self, websocket: WebSocket) -> None:
Expand Down