Websocket Routes overides uvicorns default Server header #1574
-
This is a continuation of a discussion happened in FastAPI Discord Channel after an issue was reported by When a Websocket route is created, there is a Sample:
On tracing it back, it was found that the websocket library adds this header. Interesting part is, for normal routes (http), the default headers added by Uvicorn looks like this:
This behavior exits even if One way I found to remove the Server header is to replace it by doing this: @app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept(headers=[(b"Server", b"")])
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}") Test Scenario:Webserver: uvicorn 0.18.2 with CPython 3.10.4 on Windows Code Snippetfrom starlette.websockets import WebSocket
from fastapi import FastAPI, Form
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}") |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 23 replies
-
IMHO, it should be considered a bug (minor) in Uvicorn simply for the fact that Uvicorn behaves differently for HTTP routes. I went through the code. Not exactly sure where we can set the appropriate server header before passing on to Web Sockets. As I mentioned in the question, setting the header (rather replacing header - as pointed by @Kludex in discord) during |
Beta Was this translation helpful? Give feedback.
-
This is fixed in #1606 |
Beta Was this translation helpful? Give feedback.
This is fixed in #1606