LCOV - code coverage report
Current view: top level - waltz/http - fd_http_server.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 1 28 3.6 %
Date: 2025-07-01 05:00:49 Functions: 0 0 -

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

Generated by: LCOV version 1.14