-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
Denying WebSocket connection in WebSocketEndpoint.on_connect leads to Exception #1560
Comments
Can someone confirm this? |
I can't reproduce this, a minimal example would be very helpful. |
While creating a minimal example I noticed that the problem only exists when using wsproto instead of websockets. Minimal example: from starlette.applications import Starlette
from starlette.endpoints import WebSocketEndpoint, HTTPEndpoint
from starlette.responses import HTMLResponse
from starlette.routing import Route, WebSocketRoute
html = """
<!DOCTYPE html>
<script>
var ws = new WebSocket("ws://localhost:8000/ws");
</script>
"""
class Homepage(HTTPEndpoint):
async def get(self, request):
return HTMLResponse(html)
class Echo(WebSocketEndpoint):
async def on_connect(self, websocket):
await websocket.close()
routes = [
Route("/", Homepage),
WebSocketRoute("/ws", Echo)
]
app = Starlette(routes=routes) Running it with |
As per https://asgi.readthedocs.io/en/latest/specs/www.html#disconnect-receive-event-ws (image below), the application should receive the "code" from the server. There's no default on the specs, meaning that the server MUST send it. The interpretation of "if the server" should default to a value, or if the application should be the one to default to, is confusing... That being said, I'll create a fix for Starlette as well. |
Discussed in #1555
Originally posted by dingensundso March 27, 2022
I created a WebSocketEndpoint class in which I want to deny the connection in certain conditions.
When the WebSocket is closed in on_connect I receive the following exception:
So code is None when the server denies the connection instead of non-existant.
starlette/starlette/endpoints.py
Lines 72 to 87 in e086fc2
Changing line 83 to the following should fix the issue:
The text was updated successfully, but these errors were encountered: