Skip to content

Provide 'received headers' parameter for websocket server #107871

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Portponky
Copy link

This is a small change that allows http and websockets to be provided through one endpoint.

For example, if you run a tcp server acting as an http endpoint providing two paths:

/index.html -> serves a file through http get
/ws -> opens a websocket connection

At the moment this is not really possible, because you need to read the incoming headers from the tcp stream to find out what the connection is trying to get. However, turning a StreamPeer into a websocket server through accept_stream requires that the full http header is sitting in the stream peer. Once you get the bytes out of the stream to examine them, you can no longer use accept_stream to let the connection act as a websocket server.

This change allows the received headers to be fed into accept_stream, which are then used to populate the handshake buffer. So, if you have already pulled out the first line, or the entire header, you can supply it when upgrading the connection to a websocket and it will use that.

The code to complete the handshake buffer was rearranged to first check f the buffer is complete, before reading from the connection to fill it.

I have used this change in my game, to reduce it from requiring two separate endpoints (one for http and a separate one for websockets), to only one unified endpoint.

…at populates the handshake buffer

This means that investigation of the tcp stream, e.g. to find out what resource is being requested, does not prevent upgrading the connection to a WebSocket server.
@AThousandShips
Copy link
Member

Your commit seems not to be linked to your GitHub account. See: Why are my commits linked to the wrong user? for more info.

@Portponky
Copy link
Author

Thanks, I think it is now linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants