Line data Source code
1 : #ifndef HEADER_fd_src_waltz_http_fd_http_server_h
2 : #define HEADER_fd_src_waltz_http_fd_http_server_h
3 :
4 : /* An fd_http_server is a WebSocket capable HTTP server designed to
5 : stream output messages quickly to many connected clients, where each
6 : output message can go to many (and in some cases all) clients.
7 :
8 : The primary use case is for serving ongoing RPC data to RPC
9 : subscribers, but it also serves a WebSocket stream for browser
10 : clients to show the GUI.
11 :
12 : The server does not allocate and has a built in allocation strategy
13 : and memory region for outgoing messages which the caller should use.
14 : HTTP response bodies and WebSocket frames are placed into an outgoing
15 : ring buffer, wrapping around when reaching the end, and the server
16 : will automatically evict slow clients that do not read their messages
17 : in time and would be overwritten when the buffer has wrapped fully
18 : around.
19 :
20 : Using the outgoing ring has two steps,
21 :
22 : (1) Stage data into the ring with fd_http_server_printf and
23 : fd_http_server_memcpy functions.
24 : (2) Send the staged data to clients with fd_http_server_send and
25 : fd_http_server_oring_broadcast.
26 :
27 : The server is designed to be used in a single threaded event loop and
28 : run within a tile. The caller should call fd_http_server_poll as
29 : frequently as possible to service connections and make forward
30 : progress. */
31 :
32 : #include "../../util/fd_util_base.h"
33 :
34 111 : #define FD_HTTP_SERVER_ALIGN (128UL)
35 :
36 6 : #define FD_HTTP_SERVER_METHOD_GET (0)
37 9 : #define FD_HTTP_SERVER_METHOD_POST (1)
38 0 : #define FD_HTTP_SERVER_METHOD_OPTIONS (2)
39 0 : #define FD_HTTP_SERVER_METHOD_PUT (3)
40 :
41 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_OK ( -1)
42 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED ( -2)
43 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_TOO_SLOW ( -3)
44 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_EXPECTED_EOF ( -4)
45 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET ( -5)
46 3 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_LARGE_REQUEST ( -6)
47 15 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST ( -7)
48 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_MISSING_CONTENT_LENGTH_HEADER ( -8)
49 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD ( -9)
50 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_PATH_TOO_LONG (-10)
51 6 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_KEY (-11)
52 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNEXPECTED_VERSION (-12)
53 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_KEY_HEADER (-13)
54 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_VERSION_HEADER (-14)
55 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_MASK (-15)
56 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNKNOWN_OPCODE (-16)
57 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME (-17)
58 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW (-18)
59 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE (-19)
60 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_CONT_OPCODE (-20)
61 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_TEXT_OPCODE (-21)
62 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CONTROL_FRAME_TOO_LARGE (-22)
63 0 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CHANGED_OPCODE (-23)
64 6 : #define FD_HTTP_SERVER_CONNECTION_CLOSE_UNSUPPORTED_TRANSFER_ENCODING (-24)
65 :
66 : /* Given a FD_HTTP_SERVER_CONNECTION_CLOSE_* reason code, a reason that
67 : a HTTP connection a client was closed, produce a human readable
68 : string describing the reason. */
69 :
70 : FD_FN_CONST char const *
71 : fd_http_server_connection_close_reason_str( int reason );
72 :
73 : /* Given a FD_HTTP_SERVER_METHOD_* code, produce the string for that
74 : method. */
75 :
76 : FD_FN_CONST char const *
77 : fd_http_server_method_str( uchar method );
78 :
79 : /* Parameters needed for constructing an HTTP server. */
80 :
81 : struct fd_http_server_params {
82 : ulong max_connection_cnt; /* Maximum number of concurrent HTTP/1.1 connections open. Connections are not persistent and will be closed after one request is served */
83 : ulong max_ws_connection_cnt; /* Maximum number of concurrent websocket connections open */
84 : ulong max_request_len; /* Maximum total length of an HTTP request, including the terminating \r\n\r\n and any body in the case of a POST */
85 : ulong max_ws_recv_frame_len; /* Maximum size of an incoming websocket frame from the client. Must be >= max_request_len */
86 : ulong max_ws_send_frame_cnt; /* Maximum number of outgoing websocket frames that can be queued before the client is disconnected */
87 : ulong outgoing_buffer_sz; /* Size of the outgoing data ring, which is used to stage outgoing HTTP response bodies and WebSocket frames */
88 : int compress_websocket; /* True if large websocket messages are compressed and sent as binary websocket frames */
89 : };
90 :
91 : typedef struct fd_http_server_params fd_http_server_params_t;
92 :
93 : struct fd_http_server_request {
94 : ulong connection_id; /* Unique identifier for the connection. In [0, max_connection_cnt). The connection ID is a unique identifier for the lifetime of the connection, and will be
95 : provided to close to indicate that the connection is closed. After a connection is closed the ID may be recycled */
96 :
97 : uchar method; /* One of FD_HTTP_SERVER_METHOD_* indicating what the method of the request is */
98 : char const * path; /* The NUL termoinated path component of the request. Not sanitized and may contain arbitrary content. Path is the full HTTP path of the request, for example
99 : "/img/monkeys/gorilla.jpg" */
100 :
101 : void * ctx; /* The user provided context pointer passed when constructing the HTTP server */
102 :
103 : struct {
104 : char const * content_type; /* The NUL terminated value of the Content-Type header of the request. Not sanitized and may contain arbitrary content. May be NULL if the header was not present */
105 : char const * accept_encoding; /* The NUL terminated value of the Accept-Encoding header of the request. Not sanitized and may contain arbitrary content. May be NULL if the header was not present */
106 : int compress_websocket; /* True if the client has provided an `Sec-WebSocket-Protocol: compress-zstd` header indicating that the responder can choose to compress WebSocket frames with ZStandard. Only large (>200 bytes) Server -> Client messages are compressed */
107 : int upgrade_websocket; /* True if the client has provided an `Upgrade: websocket` header, valid `Sec-WebSocket-Key` and supported `Sec-Websocket-Version`, indicating that the
108 : responder should upgrade the connection to a WebSocket by setting `upgrade_websocket` to 1 in the response */
109 : } headers;
110 :
111 : union {
112 : struct {
113 : uchar const * body; /* The body of the HTTP request. The body is byte data, might have internal NUL characters, and may not be NUL terminated */
114 : ulong body_len; /* The length of the body of the HTTP request */
115 : } post;
116 : };
117 : };
118 :
119 : typedef struct fd_http_server_request fd_http_server_request_t;
120 :
121 : /* A response issued by the server handler function to an HTTP request.
122 :
123 : The handler should typically create response bodies via. the HTTP
124 : server functions like fd_http_server_printf. This allows the server
125 : to manage buffer lifetimes and ensure high performance. If using
126 : the server buffers, the handler should not set a static_body or
127 : static_body_len, and should instead use fd_http_server_stage_body
128 : to snap off the staging buffer contents into the body.
129 :
130 : In certain cases, it is desirable to send static content where the
131 : lifetime of the buffer is known to outlive the HTTP server. In
132 : that case, you can set body_static to non-NULL and body_len_static
133 : to the length of the body payload, and the server will send this
134 : data instead of the staged data instead.
135 :
136 : status is an HTTP status code. If status is not 200, the response
137 : body is ignored and the server will send an empty response.
138 :
139 : If upgrade_websocket is true, the connection will be upgraded to a
140 : websocket, after which the handler will begin receiving websocket
141 : frames. */
142 :
143 : struct fd_http_server_response {
144 : ulong status; /* Status code of the HTTP response */
145 : int upgrade_websocket; /* 1 if we should send a websocket upgrade response */
146 : int compress_websocket; /* 1 if we should add a `Sec-WebSocket-Protocol: compress-zstd` header to the response */
147 :
148 : char const * content_type; /* Content-Type to set in the HTTP response */
149 : char const * cache_control; /* Cache-Control to set in the HTTP response */
150 : char const * content_encoding; /* Content-Encoding to set in the HTTP response */
151 :
152 : char const * access_control_allow_origin;
153 : char const * access_control_allow_methods;
154 : char const * access_control_allow_headers;
155 : ulong access_control_max_age;
156 :
157 : uchar const * static_body; /* Response body to send. Lifetime of response data must outlive the entire HTTP server. */
158 : ulong static_body_len; /* Length of the response body */
159 :
160 : ulong _body_off; /* Internal use only. Offset into the outgoing buffer where the body starts */
161 : ulong _body_len; /* Internal use only. Length of the body in the outgoing buffer */
162 : };
163 :
164 : typedef struct fd_http_server_response fd_http_server_response_t;
165 :
166 : struct fd_http_server_callbacks {
167 : /* Handle an incoming HTTP request. The callback must be provided
168 : and is assumed to be non-NULL. request is a representation of
169 : the incoming HTTP request. The callback should return a response
170 : which will be sent to the client. */
171 :
172 : fd_http_server_response_t ( * request )( fd_http_server_request_t const * request );
173 :
174 : /* Called when a regular HTTP connection is established. Called
175 : immediately after the connection is accepted. sockfd is the file
176 : descriptor of the socket. ctx is the user provided context pointer
177 : provided when constructing the HTTP server. The open callback can
178 : be NULL in which case the callback will not be invoked. */
179 :
180 : void ( * open )( ulong conn_id, int sockfd, void * ctx );
181 :
182 : /* Close an HTTP request. This is called back once all the data has
183 : been sent to the HTTP client, or an error condition occurs, or the
184 : caller force closes the connection by calling close. If a
185 : connection is upgraded to a WebSocket connection, a close event is
186 : first sent once the HTTP upgrade response is sent, before a ws_open
187 : event is sent. Close is not called when a WebSocket connection is
188 : closed, instead ws_close is called. reason is one of
189 : FD_HTTP_SERVER_CONNECTION_CLOSE_* indicating why the connection is
190 : being closed. ctx is the user provided context pointer provided
191 : when constructing the HTTP server. The close callback can be NULL
192 : in which case the callback will not be invoked. */
193 :
194 : void ( * close )( ulong conn_id, int reason, void * ctx );
195 :
196 : /* Called when a WebSocket is opened. ws_conn_id in [0,
197 : max_ws_connection_cnt). Connection IDs are recycled as WebSocket
198 : connections are closed. Connection IDs overlap with regular
199 : (non-WebSocket) connection IDs, but are in a distinct namespace,
200 : and WebSocket connection 0 is different from regular connection 0.
201 : ctx is the user provided context pointer provided when constructing
202 : the HTTP server. */
203 :
204 : void ( * ws_open )( ulong ws_conn_id, void * ctx );
205 :
206 : /* Called when a WebSocket message is received on the connection.
207 : data is the message data, and data_len is the length of the message
208 : data. ctx is the user provided context pointer provided when
209 : constructing the HTTP server. The data provided is valid only
210 : until the callback returns, and the buffer will be recycled again
211 : immediately. data_len is in [0, max_ws_recv_frame_len). */
212 :
213 : void ( * ws_message )( ulong ws_conn_id, uchar const * data, ulong data_len, void * ctx );
214 :
215 : /* Called when a WebSocket connection is closed. reason is one of
216 : FD_HTTP_SERVER_CONNECTION_CLOSE_* indicating why the connection was
217 : closed. ctx is the user provided context pointer provided when
218 : constructing the HTTP server. Typical reasons for closing the
219 : WebSocket include the peer disconnecting or timing out, or being
220 : evicted to make space for a new incoming connection. Also called
221 : back when the user of the API forcibly closes a connection by
222 : calling ws_close. */
223 :
224 : void ( * ws_close )( ulong ws_conn_id, int reason, void * ctx );
225 : };
226 :
227 : typedef struct fd_http_server_callbacks fd_http_server_callbacks_t;
228 :
229 : struct fd_http_server_private;
230 : typedef struct fd_http_server_private fd_http_server_t;
231 :
232 : FD_PROTOTYPES_BEGIN
233 :
234 : /* fd_http_server_{align,footprint} give the needed alignment and
235 : footprint of a memory region suitable to hold an http server.
236 :
237 : fd_http_server_new formats memory region with suitable alignment and
238 : footprint suitable for holding a http server state. Assumes shmem
239 : points on the caller to the first byte of the memory region owned by
240 : the caller to use. Returns shmem on success and NULL on failure
241 : (logs details). The memory region will be owned by the state on
242 : successful return. The caller is not joined on return.
243 :
244 : fd_http_server_join joins the caller to a http server state. Assumes
245 : shhttp points to the first byte of the memory region holding the
246 : state. Returns a local handle to the join on success (this is
247 : not necessarily a simple cast of the address) and NULL on failure
248 : (logs details).
249 :
250 : fd_http_server_leave leaves the caller's current local join to a http
251 : server state. Returns a pointer to the memory region holding the
252 : state on success (this is not necessarily a simple cast of the
253 : address) and NULL on failure (logs details). The caller is not
254 : joined on successful return.
255 :
256 : fd_http_server_delete unformats a memory region that holds a http
257 : server state. Assumes shhttp points on the caller to the first
258 : byte of the memory region holding the state and that nobody is
259 : joined. Returns a pointer to the memory region on success and NULL
260 : on failure (logs details). The caller has ownership of the memory
261 : region on successful return. */
262 :
263 : FD_FN_CONST ulong
264 : fd_http_server_align( void );
265 :
266 : FD_FN_CONST ulong
267 : fd_http_server_footprint( fd_http_server_params_t params );
268 :
269 : void *
270 : fd_http_server_new( void * shmem,
271 : fd_http_server_params_t params,
272 : fd_http_server_callbacks_t callbacks,
273 : void * callback_ctx );
274 :
275 : fd_http_server_t *
276 : fd_http_server_join( void * shhttp );
277 :
278 : void *
279 : fd_http_server_leave( fd_http_server_t * http );
280 :
281 : void *
282 : fd_http_server_delete( void * shhttp );
283 :
284 : /* fd_http_server_fd returns the file descriptor of the server. The
285 : file descriptor is used to poll for incoming connections and data
286 : on the server. */
287 :
288 : int
289 : fd_http_server_fd( fd_http_server_t * http );
290 :
291 : fd_http_server_t *
292 : fd_http_server_listen( fd_http_server_t * http,
293 : uint address,
294 : ushort port );
295 :
296 : /* Close an active connection. The connection ID must be an open
297 : open connection in [0, max_connection_cnt). The connection will
298 : be forcibly (ungracefully) terminated. The connection ID is released
299 : and should not be used again, as it may be recycled for a future
300 : connection. If a close callback has been provided to the http
301 : server, it will be invoked with the reason provided. */
302 :
303 : void
304 : fd_http_server_close( fd_http_server_t * http,
305 : ulong conn_id,
306 : int reason );
307 :
308 : /* Close an active WebSocket connection. The connection ID must be an
309 : open WebSocket connection ID in [0, max_ws_connection_cnt). The
310 : connection will be forcibly (ungracefully) terminated. The
311 : connection ID is released and should not be used again, as it may be
312 : recycled for a future WebSocket connection. If a ws_close callback
313 : has been provided to the http server, it will be invoked with the
314 : reason provided. */
315 :
316 : void
317 : fd_http_server_ws_close( fd_http_server_t * http,
318 : ulong ws_conn_id,
319 : int reason );
320 :
321 : /* fd_http_server_buffer_trunc truncates the pending message to the given length. */
322 :
323 : void
324 : fd_http_server_stage_trunc( fd_http_server_t * http,
325 : ulong len );
326 :
327 : /* fd_http_server_buffer_len returns the length of the pending message. */
328 :
329 : ulong
330 : fd_http_server_stage_len( fd_http_server_t * http );
331 :
332 : /* fd_http_server_printf appends the rendered format string fmt into the
333 : staging area of the outgoing ring buffer. Assumes http is a current
334 : local join.
335 :
336 : If appending to the ring buffer causes it to wrap around and
337 : overwrite existing data from a prior message, any connections which
338 : are still using data from the prior message will be evicted, as they
339 : cannot keep up.
340 :
341 : Once the full message has been appended into the outgoing ring buffer,
342 : the staged contents can be sent to all connected WebSocket clients of
343 : the HTTP server using fd_http_server_broadcast. This will end the
344 : current staged message so future prints go into a new message.
345 :
346 : Printing is not error-free, it is assumed that the format string is
347 : valid but the entire outgoing buffer may not be large enough to hold
348 : the printed string. In that case, the staging buffer is marked as
349 : being in an error state internally. The next call to send or
350 : broadcast will fail, returning the error, and the error state will be
351 : cleared. */
352 :
353 : void
354 : fd_http_server_printf( fd_http_server_t * http,
355 : char const * fmt,
356 : ... ) __attribute__((format (printf, 2, 3)));
357 :
358 : /* fd_http_server_memcpy appends the data provided to the end of the
359 : staging area of the outgoing ring buffer. Assumes http is a current
360 : local join.
361 :
362 : If appending to the ring buffer causes it to wrap around and
363 : overwrite existing data from a prior message, any connections which
364 : are still using data from the prior message will be evicted, as they
365 : cannot keep up.
366 :
367 : Once the full message has been appended into the outgoing ring buffer,
368 : the staged contents can be sent to all connected WebSocket clients of
369 : the HTTP server using fd_http_server_broadcast. This will end the
370 : current staged message so future prints go into a new message.
371 :
372 : Appending is not error-free, it is assumed that the data provided is
373 : valid but the entire outgoing buffer may not be large enough to hold
374 : data_len bytes. In that case, the staging buffer is marked as
375 : being in an error state internally. The next call to send or
376 : broadcast will fail, returning the error, and the error state will be
377 : cleared. */
378 :
379 : void
380 : fd_http_server_memcpy( fd_http_server_t * http,
381 : uchar const * data,
382 : ulong data_len );
383 :
384 : /* fd_http_server_append_start starts an in-place append operation.
385 : len is the amount of buffer space to reserve. Returns a pointer to
386 : len bytes to which the user should write the message to, on success.
387 : On failure (insufficient buffer space), returns NULL. */
388 :
389 : uchar *
390 : fd_http_server_append_start( fd_http_server_t * http,
391 : ulong len );
392 :
393 : /* fd_http_server_append_end finishes an earlier started in-place
394 : append. len is the number of bytes that were actually written. */
395 :
396 : void
397 : fd_http_server_append_end( fd_http_server_t * http,
398 : ulong len );
399 :
400 : /* fd_http_server_unstage unstages any data written into the staging
401 : buffer, clearing its contents. It does not advance the ring buffer
402 : usage, and no clients will be evicted. */
403 :
404 : void
405 : fd_http_server_unstage( fd_http_server_t * http );
406 :
407 : /* fd_http_server_stage_body marks the current contents of the staging
408 : buffer as the body of the response. The response is then ready to be
409 : sent to the client. Returns 0 on success and -1 on failure if the
410 : ring buffer is in an error state, and then clears the error state. */
411 :
412 : int
413 : fd_http_server_stage_body( fd_http_server_t * http,
414 : fd_http_server_response_t * response );
415 :
416 : /* Send the contents of the staging buffer as a a WebSocket message to a
417 : single client. The staging buffer is then cleared. Returns -1 on
418 : failure if the ring buffer is an error state, and then clears the
419 : error state.
420 :
421 : The contents are marked as needing to be sent to the client, but this
422 : does not block or wait for them to send, which happens async as the
423 : client is able to read. If the client reads too slow, and the
424 : staging buffer wraps around and is eventually overwritten by another
425 : printer, this client will be force disconnected as being too slow. */
426 :
427 : int
428 : fd_http_server_ws_send( fd_http_server_t * http,
429 : ulong ws_conn_id ); /* An existing, open connection. In [0, max_ws_connection_cnt) */
430 :
431 : /* Broadcast the contents of the staging buffer as a WebSocket message
432 : to all connected clients. The staging buffer is then cleared.
433 : Returns -1 on failure if the ring buffer is an error state, and then
434 : clears the error state.
435 :
436 : The contents are marked as needing to be sent to the client, but this
437 : does not block or wait for them to send, which happens async as the
438 : client is able to read. If the client reads too slow, and the
439 : staging buffer wraps around and is eventually overwritten by another
440 : printer, this client will be force disconnected as being too slow. */
441 :
442 : int
443 : fd_http_server_ws_broadcast( fd_http_server_t * http );
444 :
445 : /* fd_http_server_poll needs to be continuously called in a spin loop to
446 : drive the HTTP server forward. Setting poll_timeout==0 makes it a
447 : non-blocking socket ppoll(2). Returns 1 if there was any work to do
448 : on the HTTP server, or 0 otherwise. */
449 :
450 : int
451 : fd_http_server_poll( fd_http_server_t * http,
452 : int poll_timeout );
453 :
454 : FD_PROTOTYPES_END
455 :
456 : #endif /* HEADER_fd_src_waltz_http_fd_http_server_h */
|