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

Conversation

sephioh
Copy link
Contributor

@sephioh sephioh commented Mar 11, 2021

Currently, any non-ascii header value sent to uvicorn running with websockets implementation will raise a UnicodeEncodeError.
As websockets package represents non-ascii characters in headers using surrogate escapes, this PR makes websockets implementation to follow the same pattern when encoding them.

Edit by @Kludex:

@sephioh sephioh mentioned this pull request Mar 11, 2021
2 tasks
@Kludex Kludex mentioned this pull request Dec 7, 2021
@euri10
Copy link
Member

euri10 commented Dec 8, 2021

closing this until we agree on a fix, let's discuss that in #968

@euri10 euri10 closed this Dec 8, 2021
@Kludex Kludex reopened this Dec 11, 2022
Copy link
Sponsor Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the behavior with wsproto.

You can confirm it with the following client code:

#!/usr/bin/env python

import asyncio

import websockets


async def hello():
    async with websockets.connect(
        "ws://localhost:8000", extra_headers={"haha": "Abraão"}
    ) as websocket:
        await websocket.send("Hello world!")
        await websocket.recv()


asyncio.run(hello())

And on the uvicorn side, you can run the following using wsproto:

uvicorn main:app --port 8000 --reload --ws wsproto

And... Replace wsproto by websockets above to check the matching behavior.

@tomchristie can you please confirm that we are fine with this?

@tomchristie
Copy link
Member

I don't have a problem with this.

@Kludex Kludex changed the title use surrogateescape to encode headers with non-ascii characters Use surrogateescape to encode headers on websockets implementation Dec 12, 2022
@Kludex Kludex merged commit ef16e9c into encode:master Dec 12, 2022
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.

WebSocket headers encoding problem
4 participants