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