Provide 'received headers' parameter for websocket server #107871
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 useaccept_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.