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

Use surrogateescape to encode headers on websockets implementation #1005

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ async def websocket_connect(self, message):
headers = self.scope.get("headers")
headers = dict(headers)
assert headers[b"host"].startswith(b"127.0.0.1")
assert headers[b"username"] == bytes("abraão", "utf-8")
await self.send({"type": "websocket.accept"})

async def open_connection(url):
async with websockets.connect(url) as websocket:
async with websockets.connect(
url, extra_headers=[("username", "abraão")]
) as websocket:
return websocket.open

config = Config(app=App, ws=protocol_cls, lifespan="off")
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def process_request(self, path, headers):
subprotocols.extend([token.strip() for token in header.split(",")])

asgi_headers = [
(name.encode("ascii"), value.encode("ascii"))
(name.encode("ascii"), value.encode("ascii", errors="surrogateescape"))
for name, value in headers.raw_items()
]

Expand Down