Preparations for WebSockets #2040
Closed
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 PR lays the groundwork for an upcoming implementation of WebSockets. It makes the following changes:
StreamHandler. If set, it replaces all other content-serving mechanisms and – in conjunction with a response handler – provides a minimal interface to implement a WebSocket client. TheResponsestruct gained aset_stream_handler()function to accomplish the same server-side.nonblocking_read_size()which returns a size that is guaranteed to not blockread()calls.read()may return fewer bytes including none at all.Add timeout setters, to allow other protocol implementations to set more appropriate timeouts in stream handlers.random_bytes(): Refactorrandom_string()intorandom_bytes(). This will be used to generate the masking key for WebSockets.select_read(): Add a two FD version ofselect_read()with support for infinite timeouts. This change is critical to implement bidirectional communication. The WebSockets implementation will use this to interrupt reads when a message is added to the send queue. Branching on the non-type template parameter ensures the common, single FD case isn't pessimized.httplib.{h,cc}.I'm aware of #1103 and while I liked it initially, IMHO, it falls short of its goal of supporting protocol switching generically. The stream handler mechanism doesn't bill itself as anything but another way to handle the body of an HTTP response. It's meant as a low-level, advanced feature that doesn't have to be exposed in the high-level API. (Protocol switching isn't limited to
GETrequests.)I've happily taken some inspiration from it, though. (Thanks, @PixlRainbow!)
Checklist:
std::function<bool(Stream&)>withStreamHandler?CPPHTTPLIB_USE_POLLwhere applicable.