Skip to content

Conversation

@majiayu000
Copy link

Summary

  • Return HTTP 405 Method Not Allowed for non-GET requests (HEAD, POST, etc.) instead of raising an exception
  • This reduces log noise from bots and crawlers hitting WebSocket endpoints

Changes

  • Added InvalidMethod exception to websockets.exceptions
  • Modified Request.parse() in http11.py to raise InvalidMethod instead of ValueError for unsupported methods
  • Updated ServerProtocol.parse() in server.py to catch InvalidMethod and return a proper 405 response with Allow: GET header
  • Updated tests to verify the new behavior

Test plan

  • Added tests for HEAD and POST requests returning 405
  • Updated existing tests to expect InvalidMethod instead of ValueError
  • All tests pass

Fixes #1677

Instead of raising an exception when receiving HEAD, POST, or other
non-GET HTTP methods, the server now properly returns a 405 Method
Not Allowed response with an Allow: GET header.

This change:
- Adds InvalidMethod exception for unsupported HTTP methods
- Modifies Request.parse() to raise InvalidMethod instead of ValueError
- Handles InvalidMethod in ServerProtocol.parse() to return 405 response
- Updates tests accordingly

Fixes python-websockets#1677
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HEAD raises an exception

1 participant