Add support for server-side websocket upgrade #500
+240
−12
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.
The main idea here is a new
aws_websocket_upgrade
function that allows creating anaws_websocket
instance from a server-side stream. In order for the stream to correctly be marked as having switched protocols ins_stream_complete
, I added ais_switching_protocols
bool field to theaws_h1_encoder_message
struct that is checked ins_stream_complete
.Usage would then allow calling
aws_websocket_upgrade
from the server-sideon_complete
function ofaws_http_request_handler_options
and using theaws_websocket
instance as desired as a server websocket.I recognize that there may be a cleaner or more desirable way to track the
is_switching_protocols
, but after playing around with a few things, this approach seemed pretty simple. If we'd prefer to track this on theaws_h1_stream
instead, or in some other way, I'm open to ideas.I'm also open to other approaches on how to handle/allow the overall
aws_websocket_upgrade
. The other thought I had was perhaps you could pass something like the proposedaws_websocket_server_upgrade_options
to the server configuration options itself and thenaws_websocket_upgrade
would be called automatically if an incoming websocket upgrade request was detected? i.e. teh websocket response would automatically be sent, the websocket upgraded, and then twoaws_websocket_on_connect
andaws_websocket_on_connect_shutdown
callback functions would be required? That seemed a little more involved and I wasn't sure if it would even work dependency wise with server bringing in websocket definitions.I also realize a discussion/issue could have been opened around this, but I've also just been personally curious to dive in and try something out in the project and this has been a fun way to get to know all the http (h1 at least) lifecycle code. Happy to restart if there are ideas on how to do something like this cleaner/simpler starting from scratch.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.