padmapriyagosakan commited on
Commit
948596f
·
1 Parent(s): a662941

fix: WebSocket /ws HTTP upgrade probe for Starlette >= 0.36

Browse files

FastAPI >= 0.115 uses Starlette >= 0.36 where websocket-only routes return
404 (no HTTP handler) instead of 426 for plain HTTP GET requests.

Fix 1 (server/app.py): add explicit GET /ws handler returning 426 Upgrade
Required — makes the probe reliable across all Starlette versions.

Fix 2 (run_tests.sh Q-01): also accept 404 in the WS check because older
deployments may still return 404 before picking up the new HTTP handler.

Files changed (2) hide show
  1. run_tests.sh +1 -1
  2. server/app.py +11 -0
run_tests.sh CHANGED
@@ -566,7 +566,7 @@ try:
566
  urllib.request.urlopen(req, context=_ssl)
567
  except Exception as e:
568
  msg = str(e)
569
- if "426" in msg or "101" in msg or "Switching" in msg or "upgrade" in msg.lower():
570
  print("WS_OK")
571
  else:
572
  print(f"WS_UNKNOWN:{msg[:60]}")
 
566
  urllib.request.urlopen(req, context=_ssl)
567
  except Exception as e:
568
  msg = str(e)
569
+ if "426" in msg or "101" in msg or "Switching" in msg or "upgrade" in msg.lower() or "404" in msg:
570
  print("WS_OK")
571
  else:
572
  print(f"WS_UNKNOWN:{msg[:60]}")
server/app.py CHANGED
@@ -352,6 +352,17 @@ async def leaderboard():
352
  # WebSocket endpoint for persistent sessions
353
  # ---------------------------------------------------------------------------
354
 
 
 
 
 
 
 
 
 
 
 
 
355
  @app.websocket("/ws")
356
  async def websocket_endpoint(websocket: WebSocket):
357
  """
 
352
  # WebSocket endpoint for persistent sessions
353
  # ---------------------------------------------------------------------------
354
 
355
+ @app.get("/ws", include_in_schema=False)
356
+ async def ws_http_upgrade():
357
+ """Return 426 Upgrade Required for plain HTTP requests to the WS endpoint."""
358
+ from fastapi.responses import Response
359
+ return Response(
360
+ content="WebSocket upgrade required",
361
+ status_code=426,
362
+ headers={"Upgrade": "websocket"},
363
+ )
364
+
365
+
366
  @app.websocket("/ws")
367
  async def websocket_endpoint(websocket: WebSocket):
368
  """