LCOV - code coverage report
Current view: top level - ballet/http - fd_http_server.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 148 974 15.2 %
Date: 2025-03-20 12:08:36 Functions: 11 33 33.3 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_http_server_private.h"
       3             : 
       4             : #include "picohttpparser.h"
       5             : #include "fd_sha1.h"
       6             : #include "../base64/fd_base64.h"
       7             : #include "../../util/net/fd_ip4.h"
       8             : 
       9             : #include <stdarg.h>
      10             : #include <stdio.h>
      11             : #include <errno.h>
      12             : #include <unistd.h>
      13             : #include <poll.h>
      14             : #include <stdlib.h>
      15             : #include <strings.h>
      16             : #include <sys/socket.h>
      17             : #include <netinet/in.h>
      18             : 
      19             : #define POOL_NAME       ws_conn_pool
      20          12 : #define POOL_T          struct fd_http_server_ws_connection
      21             : #define POOL_IDX_T      ushort
      22           0 : #define POOL_NEXT       parent
      23             : #include "../../util/tmpl/fd_pool.c"
      24             : 
      25             : #define POOL_NAME       conn_pool
      26          12 : #define POOL_T          struct fd_http_server_connection
      27             : #define POOL_IDX_T      ushort
      28          18 : #define POOL_NEXT       parent
      29             : #include "../../util/tmpl/fd_pool.c"
      30             : 
      31             : #define TREAP_NAME      ws_conn_treap
      32             : #define TREAP_T         struct fd_http_server_ws_connection
      33             : #define TREAP_QUERY_T   void *                                         /* We don't use query ... */
      34             : #define TREAP_CMP(q,e)  (__extension__({ (void)(q); (void)(e); -1; })) /* which means we don't need to give a real
      35             :                                                                           implementation to cmp either */
      36           0 : #define TREAP_IDX_T     ushort
      37             : #define TREAP_OPTIMIZE_ITERATION 1
      38           0 : #define TREAP_LT(e0,e1) ((e0)->send_frames[ (e0)->send_frame_idx ].off<(e1)->send_frames[ (e1)->send_frame_idx ].off)
      39             : 
      40             : #include "../../util/tmpl/fd_treap.c"
      41             : 
      42             : #define TREAP_NAME      conn_treap
      43             : #define TREAP_T         struct fd_http_server_connection
      44             : #define TREAP_QUERY_T   void *                                         /* We don't use query ... */
      45             : #define TREAP_CMP(q,e)  (__extension__({ (void)(q); (void)(e); -1; })) /* which means we don't need to give a real
      46             :                                                                           implementation to cmp either */
      47           0 : #define TREAP_IDX_T     ushort
      48             : #define TREAP_OPTIMIZE_ITERATION 1
      49           0 : #define TREAP_LT(e0,e1) ((e0)->response._body_off<(e1)->response._body_off)
      50             : 
      51             : #include "../../util/tmpl/fd_treap.c"
      52             : 
      53             : #define FD_HTTP_SERVER_DEBUG 0
      54             : 
      55             : FD_FN_CONST char const *
      56           0 : fd_http_server_connection_close_reason_str( int reason ) {
      57           0 :   switch( reason ) {
      58           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_OK:                           return "OK-Connection was closed normally";
      59           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED:                      return "EVICTED-Connection was evicted to make room for a new one";
      60           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_TOO_SLOW:                     return "TOO_SLOW-Client was too slow and did not read the reponse in time";
      61           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_EXPECTED_EOF:                 return "EXPECTED_EOF-Client continued to send data when we expected no more";
      62           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET:                   return "PEER_RESET-Connection was reset by peer";
      63           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_LARGE_REQUEST:                return "LARGE_REQUEST-Request body was too large";
      64           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST:                  return "BAD_REQUEST-Request was malformed";
      65           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_MISSING_CONENT_LENGTH_HEADER: return "MISSING_CONENT_LENGTH_HEADER-Missing Content-Length header field";
      66           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD:               return "UNKNOWN_METHOD-Request method was not recognized";
      67           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_PATH_TOO_LONG:                return "PATH_TOO_LONG-Request path was too long";
      68           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_KEY:                   return "WS_BAD_KEY-Malformed Sec-WebSocket-Key header field";
      69           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNEXPECTED_VERSION:        return "WS_UNEXPECTED_VERSION-Unexpected Sec-Websocket-Version field";
      70           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_KEY_HEADER:        return "WS_MISSING_KEY_HEADER-Missing Sec-WebSocket-Key header field";
      71           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_VERSION_HEADER:    return "WS_MISSING_VERSION_HEADER-Missing Sec-WebSocket-Version header field";
      72           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_MASK:                  return "WS_BAD_MASK-Got frame from client without mask flag set";
      73           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNKNOWN_OPCODE:            return "WS_UNKNOWN_OPCODE-Unknown opcode in websocket frame";
      74           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME:            return "WS_OVERSIZE_FRAME-Websocket frame was too large";
      75           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW:           return "WS_CLIENT_TOO_SLOW-Client was too slow to keep up with sender";
      76           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE:           return "WS_MISSING_UPGRADE-Missing Upgrade header field";
      77           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_CONT_OPCODE:      return "WS_EXPECTED_CONT_OPCODE-Expected continuation opcode in websocket frame";
      78           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_TEXT_OPCODE:      return "WS_EXPECTED_TEXT_OPCODE-Expected text opcode in websocket frame";
      79           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CONTROL_FRAME_TOO_LARGE:   return "WS_CONTROL_FRAME_TOO_LARGE-Websocket control frame was too large";
      80           0 :     case FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CHANGED_OPCODE:            return "FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CHANGED_OPCODE-Websocket frame type changed unexpectedly";
      81           0 :     default: break;
      82           0 :   }
      83             : 
      84           0 :   return "unknown";
      85           0 : }
      86             : 
      87             : FD_FN_CONST char const *
      88           0 : fd_http_server_method_str( uchar method ) {
      89           0 :   switch( method ) {
      90           0 :     case FD_HTTP_SERVER_METHOD_GET:  return "GET";
      91           0 :     case FD_HTTP_SERVER_METHOD_POST: return "POST";
      92           0 :     default: break;
      93           0 :   }
      94             : 
      95           0 :   return "unknown";
      96           0 : }
      97             : 
      98             : FD_FN_CONST ulong
      99           9 : fd_http_server_align( void ) {
     100           9 :   return FD_HTTP_SERVER_ALIGN;
     101           9 : }
     102             : 
     103             : FD_FN_CONST ulong
     104          12 : fd_http_server_footprint( fd_http_server_params_t params ) {
     105          12 :   ulong l = FD_LAYOUT_INIT;
     106          12 :   l = FD_LAYOUT_APPEND( l, FD_HTTP_SERVER_ALIGN,                           sizeof( fd_http_server_t )                                                                         );
     107          12 :   l = FD_LAYOUT_APPEND( l, conn_pool_align(),                              conn_pool_footprint( params.max_connection_cnt )                                                   );
     108          12 :   l = FD_LAYOUT_APPEND( l, ws_conn_pool_align(),                           ws_conn_pool_footprint( params.max_ws_connection_cnt )                                             );
     109          12 :   l = FD_LAYOUT_APPEND( l, conn_treap_align(),                             conn_treap_footprint( params.max_connection_cnt )                                                  );
     110          12 :   l = FD_LAYOUT_APPEND( l, ws_conn_treap_align(),                          ws_conn_treap_footprint( params.max_ws_connection_cnt )                                            );
     111          12 :   l = FD_LAYOUT_APPEND( l, alignof( struct pollfd ),                       (params.max_connection_cnt+params.max_ws_connection_cnt+1UL)*sizeof( struct pollfd )               );
     112          12 :   l = FD_LAYOUT_APPEND( l, 1UL,                                            params.max_request_len*params.max_connection_cnt                                                   );
     113          12 :   l = FD_LAYOUT_APPEND( l, 1UL,                                            params.max_ws_recv_frame_len*params.max_ws_connection_cnt                                          );
     114          12 :   l = FD_LAYOUT_APPEND( l, alignof( struct fd_http_server_ws_frame ),      params.max_ws_send_frame_cnt*params.max_ws_connection_cnt*sizeof( struct fd_http_server_ws_frame ) );
     115          12 :   l = FD_LAYOUT_APPEND( l, 1UL,                                            params.outgoing_buffer_sz                                                                          );
     116          12 :   return FD_LAYOUT_FINI( l, fd_http_server_align() );
     117          12 : }
     118             : 
     119             : void *
     120             : fd_http_server_new( void *                     shmem,
     121             :                     fd_http_server_params_t    params,
     122             :                     fd_http_server_callbacks_t callbacks,
     123           6 :                     void *                     callback_ctx ) {
     124           6 :   if( FD_UNLIKELY( !shmem ) ) {
     125           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     126           0 :     return NULL;
     127           0 :   }
     128             : 
     129           6 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_http_server_align() ) ) ) {
     130           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
     131           0 :     return NULL;
     132           0 :   }
     133             : 
     134           6 :   if( FD_UNLIKELY( params.max_ws_connection_cnt && params.max_ws_recv_frame_len<params.max_request_len ) ) {
     135           0 :     FD_LOG_WARNING(( "max_ws_recv_frame_len<max_request_len" ));
     136           0 :     return NULL;
     137           0 :   }
     138             : 
     139           6 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     140           6 :   fd_http_server_t * http = FD_SCRATCH_ALLOC_APPEND( l,  FD_HTTP_SERVER_ALIGN,                         sizeof(fd_http_server_t)                                                             );
     141           6 :   void * conn_pool        = FD_SCRATCH_ALLOC_APPEND( l,  conn_pool_align(),                            conn_pool_footprint( params.max_connection_cnt )                                     );
     142           6 :   void * ws_conn_pool     = FD_SCRATCH_ALLOC_APPEND( l,  ws_conn_pool_align(),                         ws_conn_pool_footprint( params.max_ws_connection_cnt )                               );
     143           6 :   http->conn_treap        = FD_SCRATCH_ALLOC_APPEND( l,  conn_treap_align(),                           conn_treap_footprint( params.max_connection_cnt )                                    );
     144           6 :   http->ws_conn_treap     = FD_SCRATCH_ALLOC_APPEND( l,  ws_conn_treap_align(),                        ws_conn_treap_footprint( params.max_ws_connection_cnt )                              );
     145           6 :   http->pollfds           = FD_SCRATCH_ALLOC_APPEND( l,  alignof(struct pollfd),                       (params.max_connection_cnt+params.max_ws_connection_cnt+1UL)*sizeof( struct pollfd ) );
     146           6 :   char * _request_bytes   = FD_SCRATCH_ALLOC_APPEND( l,  1UL,                                          params.max_request_len*params.max_connection_cnt                                     );
     147           6 :   uchar * _ws_recv_bytes  = FD_SCRATCH_ALLOC_APPEND( l,  1UL,                                          params.max_ws_recv_frame_len*params.max_ws_connection_cnt                            );
     148           6 :   struct fd_http_server_ws_frame * _ws_send_frames = FD_SCRATCH_ALLOC_APPEND( l, alignof(struct fd_http_server_ws_frame), params.max_ws_send_frame_cnt*params.max_ws_connection_cnt*sizeof(struct fd_http_server_ws_frame) );
     149           6 :   http->oring             = FD_SCRATCH_ALLOC_APPEND( l,  1UL,                                          params.outgoing_buffer_sz                                                            );
     150             : 
     151           0 :   http->oring_sz  = params.outgoing_buffer_sz;
     152           6 :   http->stage_err = 0;
     153           6 :   http->stage_off = 0UL;
     154           6 :   http->stage_len = 0UL;
     155             : 
     156           6 :   http->callbacks             = callbacks;
     157           6 :   http->callback_ctx          = callback_ctx;
     158           6 :   http->evict_conn_id         = 0UL;
     159           6 :   http->evict_ws_conn_id      = 0UL;
     160           6 :   http->max_conns             = params.max_connection_cnt;
     161           6 :   http->max_ws_conns          = params.max_ws_connection_cnt;
     162           6 :   http->max_request_len       = params.max_request_len;
     163           6 :   http->max_ws_recv_frame_len = params.max_ws_recv_frame_len;
     164           6 :   http->max_ws_send_frame_cnt = params.max_ws_send_frame_cnt;
     165             : 
     166           6 :   http->conns = conn_pool_join( conn_pool_new( conn_pool, params.max_connection_cnt ) );
     167           6 :   conn_treap_join( conn_treap_new( http->conn_treap, params.max_connection_cnt ) );
     168           6 :   conn_treap_seed( http->conns, params.max_connection_cnt, 42UL );
     169             : 
     170           6 :   http->ws_conns = ws_conn_pool_join( ws_conn_pool_new( ws_conn_pool, params.max_ws_connection_cnt ) );
     171           6 :   ws_conn_treap_join( ws_conn_treap_new( http->ws_conn_treap, params.max_ws_connection_cnt ) );
     172           6 :   ws_conn_treap_seed( http->ws_conns, params.max_ws_connection_cnt, 42UL );
     173             : 
     174          24 :   for( ulong i=0UL; i<params.max_connection_cnt; i++ ) {
     175          18 :     http->pollfds[ i ].fd = -1;
     176          18 :     http->pollfds[ i ].events = POLLIN | POLLOUT;
     177          18 :     http->conns[ i ] = (struct fd_http_server_connection){
     178          18 :       .request_bytes = _request_bytes+i*params.max_request_len,
     179          18 :       .parent = http->conns[ i ].parent,
     180          18 :     };
     181          18 :   }
     182             : 
     183           6 :   for( ulong i=0UL; i<params.max_ws_connection_cnt; i++ ) {
     184           0 :     http->pollfds[ params.max_connection_cnt+i ].fd = -1;
     185           0 :     http->pollfds[ params.max_connection_cnt+i ].events = POLLIN | POLLOUT;
     186           0 :     http->ws_conns[ i ] = (struct fd_http_server_ws_connection){
     187           0 :       .recv_bytes = _ws_recv_bytes+i*params.max_ws_recv_frame_len,
     188           0 :       .send_frames = _ws_send_frames+i*params.max_ws_send_frame_cnt,
     189           0 :       .parent = http->ws_conns[ i ].parent,
     190           0 :     };
     191           0 :   }
     192             : 
     193           6 :   http->pollfds[ params.max_connection_cnt+params.max_ws_connection_cnt ].fd     = -1;
     194           6 :   http->pollfds[ params.max_connection_cnt+params.max_ws_connection_cnt ].events = POLLIN | POLLOUT;
     195             : 
     196           6 :   FD_COMPILER_MFENCE();
     197           6 :   FD_VOLATILE( http->magic ) = FD_HTTP_SERVER_MAGIC;
     198           6 :   FD_COMPILER_MFENCE();
     199             : 
     200           6 :   return (void *)http;
     201           6 : }
     202             : 
     203             : fd_http_server_t *
     204           6 : fd_http_server_join( void * shhttp ) {
     205             : 
     206           6 :   if( FD_UNLIKELY( !shhttp ) ) {
     207           0 :     FD_LOG_WARNING(( "NULL shhttp" ));
     208           0 :     return NULL;
     209           0 :   }
     210             : 
     211           6 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shhttp, fd_http_server_align() ) ) ) {
     212           0 :     FD_LOG_WARNING(( "misaligned shhttp" ));
     213           0 :     return NULL;
     214           0 :   }
     215             : 
     216           6 :   fd_http_server_t * http = (fd_http_server_t *)shhttp;
     217             : 
     218           6 :   if( FD_UNLIKELY( http->magic!=FD_HTTP_SERVER_MAGIC ) ) {
     219           0 :     FD_LOG_WARNING(( "bad magic" ));
     220           0 :     return NULL;
     221           0 :   }
     222             : 
     223           6 :   return http;
     224           6 : }
     225             : 
     226             : void *
     227           3 : fd_http_server_leave( fd_http_server_t * http ) {
     228             : 
     229           3 :   if( FD_UNLIKELY( !http ) ) {
     230           0 :     FD_LOG_WARNING(( "NULL http" ));
     231           0 :     return NULL;
     232           0 :   }
     233             : 
     234           3 :   return (void *)http;
     235           3 : }
     236             : 
     237             : void *
     238           3 : fd_http_server_delete( void * shhttp ) {
     239             : 
     240           3 :   if( FD_UNLIKELY( !shhttp ) ) {
     241           0 :     FD_LOG_WARNING(( "NULL shhttp" ));
     242           0 :     return NULL;
     243           0 :   }
     244             : 
     245           3 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shhttp, fd_http_server_align() ) ) ) {
     246           0 :     FD_LOG_WARNING(( "misaligned shhttp" ));
     247           0 :     return NULL;
     248           0 :   }
     249             : 
     250           3 :   fd_http_server_t * http = (fd_http_server_t *)shhttp;
     251             : 
     252           3 :   if( FD_UNLIKELY( http->magic!=FD_HTTP_SERVER_MAGIC ) ) {
     253           0 :     FD_LOG_WARNING(( "bad magic" ));
     254           0 :     return NULL;
     255           0 :   }
     256             : 
     257           3 :   FD_COMPILER_MFENCE();
     258           3 :   FD_VOLATILE( http->magic ) = 0UL;
     259           3 :   FD_COMPILER_MFENCE();
     260             : 
     261           3 :   return (void *)http;
     262           3 : }
     263             : 
     264             : int
     265           0 : fd_http_server_fd( fd_http_server_t * http ) {
     266           0 :   return http->socket_fd;
     267           0 : }
     268             : 
     269             : fd_http_server_t *
     270             : fd_http_server_listen( fd_http_server_t * http,
     271             :                        uint               address,
     272           0 :                        ushort             port ) {
     273           0 :   int sockfd = socket( AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0 );
     274           0 :   if( FD_UNLIKELY( -1==sockfd ) ) FD_LOG_ERR(( "socket failed (%i-%s)", errno, strerror( errno ) ));
     275             : 
     276           0 :   int optval = 1;
     277           0 :   if( FD_UNLIKELY( -1==setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( optval ) ) ) )
     278           0 :     FD_LOG_ERR(( "setsockopt failed (%i-%s)", errno, strerror( errno ) ));
     279             : 
     280           0 :   struct sockaddr_in addr = {
     281           0 :     .sin_family      = AF_INET,
     282           0 :     .sin_port        = fd_ushort_bswap( port ),
     283           0 :     .sin_addr.s_addr = address,
     284           0 :   };
     285             : 
     286           0 :   if( FD_UNLIKELY( -1==bind( sockfd, fd_type_pun( &addr ), sizeof( addr ) ) ) ) {
     287           0 :     FD_LOG_ERR(( "bind(%i,AF_INET," FD_IP4_ADDR_FMT ":%u) failed (%i-%s)",
     288           0 :                  sockfd, FD_IP4_ADDR_FMT_ARGS( address ), port,
     289           0 :                  errno, fd_io_strerror( errno ) ));
     290           0 :   }
     291           0 :   if( FD_UNLIKELY( -1==listen( sockfd, (int)http->max_conns ) ) ) FD_LOG_ERR(( "listen failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     292             : 
     293           0 :   http->socket_fd = sockfd;
     294           0 :   http->pollfds[ http->max_conns+http->max_ws_conns ].fd = http->socket_fd;
     295             : 
     296           0 :   return http;
     297           0 : }
     298             : 
     299             : static void
     300             : close_conn( fd_http_server_t * http,
     301             :             ulong              conn_idx,
     302           0 :             int                reason ) {
     303           0 :   FD_TEST( http->pollfds[ conn_idx ].fd!=-1 );
     304             : #if FD_HTTP_SERVER_DEBUG
     305             :   FD_LOG_NOTICE(( "Closing connection %lu (fd=%d) (%d-%s)", conn_idx, http->pollfds[ conn_idx ].fd, reason, fd_http_server_connection_close_reason_str( reason ) ));
     306             : #endif
     307             : 
     308           0 :   if( FD_UNLIKELY( -1==close( http->pollfds[ conn_idx ].fd ) ) ) FD_LOG_ERR(( "close failed (%i-%s)", errno, strerror( errno ) ));
     309             : 
     310           0 :   http->pollfds[ conn_idx ].fd = -1;
     311           0 :   if( FD_LIKELY( conn_idx<http->max_conns ) ) {
     312           0 :     if( FD_LIKELY( http->callbacks.close    ) ) http->callbacks.close( conn_idx, reason, http->callback_ctx );
     313           0 :   } else {
     314           0 :     if( FD_LIKELY( http->callbacks.ws_close ) ) http->callbacks.ws_close( conn_idx-http->max_conns, reason, http->callback_ctx );
     315           0 :   }
     316             : 
     317           0 :   if( FD_UNLIKELY( conn_idx<http->max_conns ) ) {
     318           0 :     struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
     319           0 :     if( FD_LIKELY( (conn->state==FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER || conn->state==FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY)
     320           0 :                     && !conn->response.static_body ) ) {
     321           0 :       conn_treap_ele_remove( http->conn_treap, conn, http->conns );
     322           0 :     }
     323           0 :     conn_pool_ele_release( http->conns, conn );
     324           0 :   } else {
     325           0 :     struct fd_http_server_ws_connection * ws_conn = &http->ws_conns[ conn_idx-http->max_conns ];
     326           0 :     if( FD_LIKELY( ws_conn->send_frame_cnt ) ) ws_conn_treap_ele_remove( http->ws_conn_treap, ws_conn, http->ws_conns );
     327           0 :     ws_conn_pool_ele_release( http->ws_conns, ws_conn );
     328           0 :   }
     329           0 : }
     330             : 
     331             : void
     332             : fd_http_server_close( fd_http_server_t * http,
     333             :                       ulong              conn_id,
     334           0 :                       int                reason ) {
     335           0 :   close_conn( http, conn_id, reason );
     336           0 : }
     337             : 
     338             : void
     339             : fd_http_server_ws_close( fd_http_server_t * http,
     340             :                          ulong              ws_conn_id,
     341           0 :                          int                reason ) {
     342           0 :   close_conn( http, http->max_conns+ws_conn_id, reason );
     343           0 : }
     344             : 
     345             : /* These are the expected network errors which just mean the connection
     346             :    should be closed.  Any errors from an accept(2), read(2), or send(2)
     347             :    that are not expected here will be considered fatal and terminate the
     348             :    server. */
     349             : 
     350             : static inline int
     351           0 : is_expected_network_error( int err ) {
     352           0 :   return
     353           0 :     err==ENETDOWN ||
     354           0 :     err==EPROTO ||
     355           0 :     err==ENOPROTOOPT ||
     356           0 :     err==EHOSTDOWN ||
     357           0 :     err==ENONET ||
     358           0 :     err==EHOSTUNREACH ||
     359           0 :     err==EOPNOTSUPP ||
     360           0 :     err==ENETUNREACH ||
     361           0 :     err==ETIMEDOUT ||
     362           0 :     err==ENETRESET ||
     363           0 :     err==ECONNABORTED ||
     364           0 :     err==ECONNRESET ||
     365           0 :     err==EPIPE;
     366           0 : }
     367             : 
     368             : static void
     369           0 : accept_conns( fd_http_server_t * http ) {
     370           0 :   for(;;) {
     371           0 :     int fd = accept4( http->socket_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC );
     372             : 
     373           0 :     if( FD_UNLIKELY( -1==fd ) ) {
     374           0 :       if( FD_LIKELY( EAGAIN==errno ) ) break;
     375           0 :       else if( FD_LIKELY( is_expected_network_error( errno ) ) ) continue;
     376           0 :       else FD_LOG_ERR(( "accept failed (%i-%s)", errno, strerror( errno ) ));
     377           0 :     }
     378             : 
     379           0 :     if( FD_UNLIKELY( !conn_pool_free( http->conns ) ) ) {
     380           0 :       conn_treap_rev_iter_t it = conn_treap_fwd_iter_init( http->conn_treap, http->conns );
     381           0 :       if( FD_LIKELY( !conn_treap_fwd_iter_done( it ) ) ) {
     382           0 :         ulong conn_id = conn_treap_fwd_iter_idx( it );
     383           0 :         close_conn( http, conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
     384           0 :       } else {
     385             :         /* If nobody is slow to read, just evict round robin */
     386           0 :         close_conn( http, http->evict_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
     387           0 :         http->evict_conn_id = (http->evict_conn_id+1UL) % http->max_conns;
     388           0 :       }
     389           0 :     }
     390             : 
     391           0 :     ulong conn_id = conn_pool_idx_acquire( http->conns );
     392             : 
     393           0 :     http->pollfds[ conn_id ].fd = fd;
     394           0 :     http->conns[ conn_id ].state                  = FD_HTTP_SERVER_CONNECTION_STATE_READING;
     395           0 :     http->conns[ conn_id ].request_bytes_read     = 0UL;
     396           0 :     http->conns[ conn_id ].response_bytes_written = 0UL;
     397             : 
     398           0 :     if( FD_UNLIKELY( http->callbacks.open ) ) {
     399           0 :       http->callbacks.open( conn_id, fd, http->callback_ctx );
     400           0 :     }
     401             : 
     402             : #if FD_HTTP_SERVER_DEBUG
     403             :     FD_LOG_NOTICE(( "Accepted connection %lu (fd=%d)", conn_id, fd ));
     404             : #endif
     405           0 :   }
     406           0 : }
     407             : 
     408             : static void
     409             : read_conn_http( fd_http_server_t * http,
     410           0 :                 ulong              conn_idx ) {
     411           0 :   struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
     412             : 
     413           0 :   if( FD_UNLIKELY( conn->state!=FD_HTTP_SERVER_CONNECTION_STATE_READING ) ) {
     414           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_EXPECTED_EOF );
     415           0 :     return;
     416           0 :   }
     417             : 
     418           0 :   long sz = read( http->pollfds[ conn_idx ].fd, conn->request_bytes+conn->request_bytes_read, http->max_request_len-conn->request_bytes_read );
     419           0 :   if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data to read, continue. */
     420           0 :   else if( FD_UNLIKELY( !sz || (-1==sz && is_expected_network_error( errno ) ) ) ) {
     421           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
     422           0 :     return;
     423           0 :   }
     424           0 :   else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "read failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
     425             : 
     426             :   /* New data was read... process it */
     427           0 :   conn->request_bytes_read += (ulong)sz;
     428           0 :   if( FD_UNLIKELY( conn->request_bytes_read==http->max_request_len ) ) {
     429           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_LARGE_REQUEST );
     430           0 :     return;
     431           0 :   }
     432             : 
     433           0 :   char const * method;
     434           0 :   ulong method_len;
     435           0 :   char const * path;
     436           0 :   ulong path_len;
     437           0 :   int minor_version;
     438           0 :   struct phr_header headers[ 32 ];
     439           0 :   ulong num_headers = 32UL;
     440           0 :   int result = phr_parse_request( conn->request_bytes,
     441           0 :                                   conn->request_bytes_read,
     442           0 :                                   &method, &method_len,
     443           0 :                                   &path, &path_len,
     444           0 :                                   &minor_version,
     445           0 :                                   headers, &num_headers,
     446           0 :                                   conn->request_bytes_read - (ulong)sz );
     447           0 :   if( FD_UNLIKELY( -2==result ) ) return; /* Request still partial, wait for more data */
     448           0 :   else if( FD_UNLIKELY( -1==result ) ) {
     449           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST );
     450           0 :     return;
     451           0 :   }
     452             : 
     453           0 :   FD_TEST( result>0 && (ulong)result<=conn->request_bytes_read );
     454             : 
     455           0 :   uchar method_enum = UCHAR_MAX;
     456           0 :   if( FD_LIKELY( method_len==3UL && !strncmp( method, "GET", method_len ) ) ) method_enum = FD_HTTP_SERVER_METHOD_GET;
     457           0 :   else if( FD_LIKELY( method_len==4UL && !strncmp( method, "POST", method_len ) ) ) method_enum = FD_HTTP_SERVER_METHOD_POST;
     458           0 :   else if( FD_LIKELY( method_len==7UL && !strncmp( method, "OPTIONS", method_len ) ) ) method_enum = FD_HTTP_SERVER_METHOD_OPTIONS;
     459             : 
     460           0 :   if( FD_UNLIKELY( method_enum==UCHAR_MAX ) ) {
     461           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD );
     462           0 :     return;
     463           0 :   }
     464             : 
     465           0 :   ulong content_len = 0UL;
     466           0 :   ulong content_length_len = 0UL;
     467           0 :   if( FD_UNLIKELY( method_enum==FD_HTTP_SERVER_METHOD_POST ) ) {
     468           0 :     char const * content_length = NULL;
     469           0 :     for( ulong i=0UL; i<num_headers; i++ ) {
     470           0 :       if( FD_LIKELY( headers[ i ].name_len==14UL && !strncasecmp( headers[ i ].name, "Content-Length", 14UL ) && headers[ i ].value_len>0UL ) ) {
     471           0 :         content_length = headers[ i ].value;
     472           0 :         content_length_len = headers[ i ].value_len;
     473           0 :         break;
     474           0 :       }
     475           0 :     }
     476             : 
     477           0 :     if( FD_UNLIKELY( !content_length ) ) {
     478           0 :       close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_MISSING_CONENT_LENGTH_HEADER );
     479           0 :       return;
     480           0 :     }
     481             : 
     482           0 :     for( ulong i=0UL; i<content_length_len; i++ ) {
     483           0 :       if( FD_UNLIKELY( content_length[ i ]<'0' || content_length[ i ]>'9' ) ) {
     484           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST );
     485           0 :         return;
     486           0 :       }
     487             : 
     488           0 :       ulong next = content_len*10UL + (ulong)(content_length[ i ]-'0');
     489           0 :       if( FD_UNLIKELY( next<content_len ) ) { /* Overflow */
     490           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_LARGE_REQUEST );
     491           0 :         return;
     492           0 :       }
     493             : 
     494           0 :       content_len = next;
     495           0 :     }
     496             : 
     497           0 :     ulong total_len = (ulong)result+content_len;
     498             : 
     499           0 :     if( FD_UNLIKELY( total_len<content_len ) ) { /* Overflow */
     500           0 :       close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_LARGE_REQUEST );
     501           0 :       return;
     502           0 :     }
     503             : 
     504             : 
     505           0 :     if( FD_UNLIKELY( conn->request_bytes_read<(ulong)result+content_len ) ) {
     506           0 :       return; /* Request still partial, wait for more data */
     507           0 :     }
     508           0 :   }
     509             : 
     510           0 :   char content_type_nul_terminated[ 128 ] = {0};
     511           0 :   char accept_encoding_nul_terminated[ 128 ] = {0};
     512           0 :   for( ulong i=0UL; i<num_headers; i++ ) {
     513           0 :     if( FD_LIKELY( headers[ i ].name_len==12UL && !strncasecmp( headers[ i ].name, "Content-Type", 12UL ) ) ) {
     514           0 :       if( FD_UNLIKELY( headers[ i ].value_len>(sizeof(content_type_nul_terminated)-1UL) ) ) {
     515           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST );
     516           0 :         return;
     517           0 :       }
     518           0 :       memcpy( content_type_nul_terminated, headers[ i ].value, headers[ i ].value_len );
     519           0 :       break;
     520           0 :     }
     521             : 
     522           0 :     if( FD_LIKELY( headers[ i ].name_len==15UL && !strncasecmp( headers[ i ].name, "Accept-Encoding", 15UL ) ) ) {
     523           0 :       if( FD_UNLIKELY( headers[ i ].value_len>(sizeof(accept_encoding_nul_terminated)-1UL) ) ) {
     524           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST );
     525           0 :         return;
     526           0 :       }
     527           0 :       memcpy( accept_encoding_nul_terminated, headers[ i ].value, headers[ i ].value_len );
     528           0 :     }
     529           0 :   }
     530             : 
     531           0 :   char path_nul_terminated[ 128 ] = {0};
     532           0 :   if( FD_UNLIKELY( path_len>(sizeof( path_nul_terminated )-1UL) ) ) {
     533           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PATH_TOO_LONG );
     534           0 :     return;
     535           0 :   }
     536           0 :   memcpy( path_nul_terminated, path, path_len );
     537             : 
     538           0 :   char const * upgrade_key = NULL;
     539           0 :   for( ulong i=0UL; i<num_headers; i++ ) {
     540           0 :     if( FD_LIKELY( headers[ i ].name_len==7UL && !strncasecmp( headers[ i ].name, "Upgrade", 7UL ) && headers[ i ].value_len==9UL ) ) {
     541           0 :       upgrade_key = headers[ i ].value;
     542           0 :       break;
     543           0 :     }
     544           0 :   }
     545             : 
     546           0 :   conn->upgrade_websocket = 0;
     547           0 :   if( FD_UNLIKELY( upgrade_key && !strncmp( upgrade_key, "websocket", 9UL ) ) ) {
     548           0 :     conn->request_bytes_len = (ulong)result;
     549           0 :     conn->upgrade_websocket = 1;
     550             : 
     551           0 :     char const * sec_websocket_key = NULL;
     552           0 :     for( ulong i=0UL; i<num_headers; i++ ) {
     553           0 :       if( FD_LIKELY( headers[ i ].name_len==17UL && !strncasecmp( headers[ i ].name, "Sec-WebSocket-Key", 17UL ) ) ) {
     554           0 :         sec_websocket_key = headers[ i ].value;
     555           0 :         if( FD_UNLIKELY( headers[ i ].value_len!=24 ) ) {
     556           0 :           close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_KEY );
     557           0 :           return;
     558           0 :         }
     559           0 :         break;
     560           0 :       }
     561           0 :     }
     562             : 
     563           0 :     char const * sec_websocket_version = NULL;
     564           0 :     for( ulong i=0UL; i<num_headers; i++ ) {
     565           0 :       if( FD_LIKELY( headers[ i ].name_len==21UL && !strncasecmp( headers[ i ].name, "Sec-Websocket-Version", 21UL ) ) ) {
     566           0 :         sec_websocket_version = headers[ i ].value;
     567           0 :         if( FD_UNLIKELY( headers[ i ].value_len!=2 || strncmp( sec_websocket_version, "13", 2UL ) ) ) {
     568           0 :           close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNEXPECTED_VERSION );
     569           0 :           return;
     570           0 :         }
     571           0 :         break;
     572           0 :       }
     573           0 :     }
     574             : 
     575           0 :     if( FD_UNLIKELY( !sec_websocket_key ) ) {
     576           0 :       close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_KEY_HEADER );
     577           0 :       return;
     578           0 :     }
     579             : 
     580           0 :     if( FD_UNLIKELY( !sec_websocket_version ) ) {
     581           0 :       close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_VERSION_HEADER );
     582           0 :       return;
     583           0 :     }
     584             : 
     585           0 :     conn->sec_websocket_key = sec_websocket_key;
     586           0 :   }
     587             : 
     588           0 :   conn->state    = FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER;
     589             : 
     590           0 :   fd_http_server_request_t request = {
     591           0 :     .connection_id             = conn_idx,
     592             : 
     593           0 :     .method                    = method_enum,
     594           0 :     .path                      = path_nul_terminated,
     595             : 
     596           0 :     .ctx                       = http->callback_ctx,
     597             : 
     598           0 :     .headers.content_type      = content_type_nul_terminated,
     599           0 :     .headers.accept_encoding   = accept_encoding_nul_terminated,
     600           0 :     .headers.upgrade_websocket = conn->upgrade_websocket,
     601           0 :   };
     602             : 
     603           0 :   switch( method_enum ) {
     604           0 :     case FD_HTTP_SERVER_METHOD_POST: {
     605           0 :       request.post.body     = (uchar*)conn->request_bytes+result;
     606           0 :       request.post.body_len = content_len;
     607           0 :     } break;
     608           0 :     default: break;
     609           0 :   }
     610             : 
     611           0 :   fd_http_server_response_t response = http->callbacks.request( &request );
     612           0 :   if( FD_LIKELY( http->pollfds[ conn_idx ].fd==-1 ) ) return; /* Connection was closed by callback */
     613           0 :   conn->response = response;
     614             : 
     615             : #if FD_HTTP_SERVER_DEBUG
     616             :   FD_LOG_NOTICE(( "Received %s request \"%s\" from %lu (fd=%d) response code %lu", fd_http_server_method_str( method_enum ), path_nul_terminated, conn_idx, http->pollfds[ conn_idx ].fd, conn->response.status ));
     617             : #endif
     618             : 
     619           0 :   if( FD_LIKELY( !conn->response.static_body ) ) conn_treap_ele_insert( http->conn_treap, conn, http->conns );
     620           0 : }
     621             : 
     622             : static void
     623             : read_conn_ws( fd_http_server_t * http,
     624           0 :               ulong              conn_idx ) {
     625           0 :   struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
     626             : 
     627           0 :   long sz = read( http->pollfds[ conn_idx ].fd, conn->recv_bytes+conn->recv_bytes_parsed+conn->recv_bytes_read, http->max_ws_recv_frame_len-conn->recv_bytes_parsed-conn->recv_bytes_read );
     628           0 :   if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data to read, continue. */
     629           0 :   else if( FD_UNLIKELY( !sz || (-1==sz && is_expected_network_error( errno ) ) ) ) {
     630           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
     631           0 :     return;
     632           0 :   }
     633           0 :   else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "read failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
     634             : 
     635             :   /* New data was read... process it */
     636           0 :   conn->recv_bytes_read += (ulong)sz;
     637           0 : again:
     638           0 :   if( FD_UNLIKELY( conn->recv_bytes_read<2UL ) ) return; /* Need at least 2 bytes to determine frame length */
     639             : 
     640           0 :   int is_mask_set = conn->recv_bytes[ conn->recv_bytes_parsed+1UL ] & 0x80;
     641           0 :   if( FD_UNLIKELY( !is_mask_set ) ) {
     642           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_MASK );
     643           0 :     return;
     644           0 :   }
     645             : 
     646           0 :   int opcode = conn->recv_bytes[ conn->recv_bytes_parsed ] & 0x0F;
     647           0 :   if( FD_UNLIKELY( opcode!=0x0 && opcode!=0x1 && opcode!=0x2 && opcode!=0x8 && opcode!=0x9 && opcode!=0xA ) ) {
     648           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNKNOWN_OPCODE );
     649           0 :     return;
     650           0 :   }
     651             : 
     652           0 :   ulong payload_len = conn->recv_bytes[ conn->recv_bytes_parsed+1UL ] & 0x7F;
     653           0 :   if( FD_UNLIKELY( (payload_len==126 || payload_len==127) && (opcode==0x8 || opcode==0x9 || opcode==0xA) ) ) {
     654           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CONTROL_FRAME_TOO_LARGE );
     655           0 :     return;
     656           0 :   }
     657             : 
     658           0 :   ulong len_bytes;
     659           0 :   if( FD_LIKELY( payload_len<126UL ) ) {
     660           0 :     len_bytes = 1UL;
     661           0 :   } else if( FD_LIKELY( payload_len==126 ) ) {
     662           0 :     if( FD_UNLIKELY( conn->recv_bytes_read<4UL ) ) return; /* Need at least 4 bytes to determine frame length */
     663           0 :     payload_len = ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+2UL ]<<8UL) | (ulong)conn->recv_bytes[ conn->recv_bytes_parsed+3UL ];
     664           0 :     len_bytes = 3UL;
     665           0 :   } else if( FD_LIKELY( payload_len==127 ) ) {
     666           0 :     if( FD_UNLIKELY( conn->recv_bytes_read<10UL ) ) return; /* Need at least 10 bytes to determine frame length */
     667           0 :     payload_len = ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+2 ]<<56UL) | ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+3UL ]<<48UL) | ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+4UL ]<<40UL) | ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+5UL ]<<32UL) |
     668           0 :                   ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+6 ]<<24UL) | ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+7UL ]<<16UL) | ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+8UL ]<<8UL ) |  (ulong)conn->recv_bytes[ conn->recv_bytes_parsed+9UL ];
     669           0 :     len_bytes = 9UL;
     670           0 :   } else {
     671           0 :     FD_LOG_ERR(( "unexpected payload_len %lu", payload_len )); /* Silence clang sanitizer, not possible */
     672           0 :   }
     673             : 
     674           0 :   ulong header_len = 1UL+len_bytes+4UL;
     675           0 :   ulong frame_len  = header_len+payload_len;
     676           0 :   if( FD_UNLIKELY( frame_len<header_len ) ) { /* Overflow */
     677           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME );
     678           0 :     return;
     679           0 :   }
     680             : 
     681           0 :   if( FD_UNLIKELY( conn->recv_bytes_parsed+frame_len+1UL>http->max_ws_recv_frame_len ) ) {
     682           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME );
     683           0 :     return;
     684           0 :   }
     685             : 
     686           0 :   if( FD_UNLIKELY( conn->recv_bytes_read<frame_len ) ) return; /* Need more data to read the full frame */
     687             : 
     688             :   /* Data frame, process it */
     689             : 
     690           0 :   int is_fin_set = conn->recv_bytes[ conn->recv_bytes_parsed+0UL ] & 0x80;
     691             : 
     692           0 :   uchar * mask    = conn->recv_bytes+conn->recv_bytes_parsed+1UL+len_bytes;
     693           0 :   uchar   mask_copy[ 4 ] = { mask[ 0 ], mask[ 1 ], mask[ 2 ], mask[ 3 ] }; /* Bytes will be overwritten by the memmove below */
     694             : 
     695           0 :   uchar * payload = conn->recv_bytes+conn->recv_bytes_parsed+header_len;
     696           0 :   for( ulong i=0UL; i<payload_len; i++ ) conn->recv_bytes[ conn->recv_bytes_parsed+i ] = payload[ i ] ^ mask_copy[ i % 4 ];
     697             : 
     698             :   /* Frame is complete, process it */
     699             : 
     700           0 :   if( FD_UNLIKELY( opcode==0x8 ) ) {
     701           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
     702           0 :     return;
     703           0 :   } else if( FD_UNLIKELY( opcode==0x9 ) ) {
     704             :     /* Ping frame, queue pong unless we are already sending one */
     705           0 :     if( FD_LIKELY( conn->pong_state!=FD_HTTP_SERVER_PONG_STATE_WAITING ) ) {
     706           0 :       conn->pong_state    = FD_HTTP_SERVER_PONG_STATE_WAITING;
     707           0 :       conn->pong_data_len = payload_len;
     708           0 :       FD_TEST( payload_len<=125UL );
     709           0 :       memcpy( conn->pong_data, conn->recv_bytes+conn->recv_bytes_parsed, payload_len );
     710           0 :     }
     711           0 :     if( FD_UNLIKELY( conn->recv_bytes_read-frame_len ) ) {
     712           0 :       memmove( conn->recv_bytes, conn->recv_bytes+conn->recv_bytes_parsed+frame_len, conn->recv_bytes_read-frame_len );
     713           0 :     }
     714           0 :     conn->recv_bytes_parsed = 0UL;
     715           0 :     conn->recv_bytes_read -= frame_len;
     716           0 :     return;
     717           0 :   } else if( FD_UNLIKELY( opcode==0xA ) ) {
     718             :     /* Pong frame, ignore */
     719           0 :     if( FD_UNLIKELY( conn->recv_bytes_read-frame_len ) ) {
     720           0 :       memmove( conn->recv_bytes, conn->recv_bytes+conn->recv_bytes_parsed+frame_len, conn->recv_bytes_read-frame_len );
     721           0 :     }
     722           0 :     conn->recv_bytes_parsed = 0UL;
     723           0 :     conn->recv_bytes_read -= frame_len;
     724           0 :     return;
     725           0 :   }
     726             : 
     727           0 :   if( FD_UNLIKELY( conn->recv_started_msg && opcode!=0x0 ) ) {
     728           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_CONT_OPCODE );
     729           0 :     return;
     730           0 :   }
     731             : 
     732           0 :   if( FD_UNLIKELY( !conn->recv_started_msg && opcode!=0x1 && opcode!=0x2 ) ) {
     733           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_TEXT_OPCODE );
     734           0 :     return;
     735           0 :   }
     736             : 
     737           0 :   if( FD_UNLIKELY( conn->recv_started_msg && opcode!=conn->recv_last_opcode ) ) {
     738           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CHANGED_OPCODE );
     739           0 :     return;
     740           0 :   }
     741           0 :   conn->recv_last_opcode = opcode;
     742             : 
     743             :   /* Check if this is a complete message */
     744             : 
     745           0 :   if( FD_UNLIKELY( !is_fin_set ) ) {
     746           0 :     conn->recv_started_msg   = 1;
     747           0 :     conn->recv_bytes_read   -= frame_len;
     748           0 :     conn->recv_bytes_parsed += payload_len;
     749           0 :     return; /* Not a complete message yet */
     750           0 :   }
     751             : 
     752             :   /* Complete message, process it */
     753             : 
     754           0 :   uchar * trailing_data     = conn->recv_bytes+conn->recv_bytes_parsed+frame_len;
     755           0 :   ulong   trailing_data_len = conn->recv_bytes_read-frame_len;
     756             : 
     757           0 :   conn->recv_bytes_parsed += payload_len;
     758           0 :   conn->recv_bytes_read   -= frame_len;
     759             : 
     760           0 :   uchar tmp = conn->recv_bytes[ conn->recv_bytes_parsed ];
     761           0 :   conn->recv_bytes[ conn->recv_bytes_parsed ] = 0; /* NUL terminate */
     762           0 :   http->callbacks.ws_message( conn_idx-http->max_conns, conn->recv_bytes, conn->recv_bytes_parsed, http->callback_ctx );
     763           0 :   if( FD_UNLIKELY( -1==http->pollfds[ conn_idx ].fd ) ) return; /* Connection was closed by callback */
     764           0 :   conn->recv_bytes[ conn->recv_bytes_parsed ] = tmp;
     765             : 
     766           0 :   conn->recv_started_msg  = 0;
     767           0 :   conn->recv_bytes_parsed = 0UL;
     768           0 :   if( FD_UNLIKELY( trailing_data_len ) ) {
     769           0 :     memmove( conn->recv_bytes, trailing_data, trailing_data_len );
     770           0 :     goto again; /* Might be another message in the buffer to process */
     771           0 :   }
     772           0 : }
     773             : 
     774             : static void
     775             : read_conn( fd_http_server_t * http,
     776           0 :            ulong              conn_idx ) {
     777           0 :   if( FD_LIKELY( conn_idx<http->max_conns ) ) read_conn_http( http, conn_idx );
     778           0 :   else                                        read_conn_ws(   http, conn_idx );
     779           0 : }
     780             : 
     781             : static void
     782             : write_conn_http( fd_http_server_t * http,
     783           0 :                  ulong              conn_idx ) {
     784           0 :   struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
     785             : 
     786           0 :   char header_buf[ 1024 ];
     787             : 
     788           0 :   uchar const * response;
     789           0 :   ulong         response_len;
     790           0 :   switch( conn->state ) {
     791           0 :     case FD_HTTP_SERVER_CONNECTION_STATE_READING:
     792           0 :       return; /* No data staged for write yet. */
     793           0 :     case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER:
     794           0 :       switch( conn->response.status ) {
     795           0 :         case 200:
     796           0 :           if( FD_UNLIKELY( conn->response.upgrade_websocket ) ) {
     797           0 :             if( FD_UNLIKELY( !conn->upgrade_websocket ) ) {
     798           0 :               close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE );
     799           0 :               return;
     800           0 :             }
     801             : 
     802           0 :             uchar sec_websocket_key[ 60 ];
     803           0 :             fd_memcpy( sec_websocket_key, conn->sec_websocket_key, 24 );
     804           0 :             fd_memcpy( sec_websocket_key+24, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36 );
     805             : 
     806           0 :             uchar sec_websocket_accept[ 20 ];
     807           0 :             fd_sha1_hash( sec_websocket_key, 60, sec_websocket_accept );
     808           0 :             char sec_websocket_accept_base64[ FD_BASE64_ENC_SZ( 20 ) ];
     809           0 :             ulong encoded_len = fd_base64_encode( sec_websocket_accept_base64, sec_websocket_accept, 20 );
     810           0 :             FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %.*s\r\n", (int)encoded_len, sec_websocket_accept_base64 ) );
     811           0 :           } else {
     812           0 :             ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
     813           0 :             FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\nConnection: close\r\n", body_len ) );
     814           0 :           }
     815           0 :           break;
     816           0 :         case 204: {
     817           0 :           ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
     818           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 204 No Content\r\nContent-Length: %lu\r\n", body_len ) );
     819           0 :           break;
     820           0 :         }
     821           0 :         case 400: {
     822           0 :           ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
     823           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 400 Bad Request\r\nContent-Length: %lu\r\n", body_len ) );
     824           0 :           break;
     825           0 :         }
     826           0 :         case 404:
     827           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n" ) );
     828           0 :           break;
     829           0 :         case 405:
     830           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n" ) );
     831           0 :           break;
     832           0 :         case 500:
     833           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n" ) );
     834           0 :           break;
     835           0 :         default:
     836           0 :           FD_TEST( fd_cstr_printf_check( header_buf, sizeof( header_buf ), &response_len, "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n" ) );
     837           0 :           break;
     838           0 :       }
     839             : 
     840           0 :       if( FD_LIKELY( conn->response.content_type ) ) {
     841           0 :         ulong content_type_len;
     842           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &content_type_len, "Content-Type: %s\r\n", conn->response.content_type ) );
     843           0 :         response_len += content_type_len;
     844           0 :       }
     845           0 :       if( FD_LIKELY( conn->response.cache_control ) ) {
     846           0 :         ulong cache_control_len;
     847           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &cache_control_len, "Cache-Control: %s\r\n", conn->response.cache_control ) );
     848           0 :         response_len += cache_control_len;
     849           0 :       }
     850           0 :       if( FD_LIKELY( conn->response.content_encoding ) ) {
     851           0 :         ulong content_encoding_len;
     852           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &content_encoding_len, "Content-Encoding: %s\r\n", conn->response.content_encoding ) );
     853           0 :         response_len += content_encoding_len;
     854           0 :       }
     855           0 :       if( FD_LIKELY( conn->response.access_control_allow_origin ) ) {
     856           0 :         ulong access_control_allow_origin_len;
     857           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &access_control_allow_origin_len, "Access-Control-Allow-Origin: %s\r\n", conn->response.access_control_allow_origin ) );
     858           0 :         response_len += access_control_allow_origin_len;
     859           0 :       }
     860           0 :       if( FD_LIKELY( conn->response.access_control_allow_methods ) ) {
     861           0 :         ulong access_control_allow_methods_len;
     862           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &access_control_allow_methods_len, "Access-Control-Allow-Methods: %s\r\n", conn->response.access_control_allow_methods ) );
     863           0 :         response_len += access_control_allow_methods_len;
     864           0 :       }
     865           0 :       if( FD_LIKELY( conn->response.access_control_allow_headers ) ) {
     866           0 :         ulong access_control_allow_headers_len;
     867           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &access_control_allow_headers_len, "Access-Control-Allow-Headers: %s\r\n", conn->response.access_control_allow_headers ) );
     868           0 :         response_len += access_control_allow_headers_len;
     869           0 :       }
     870           0 :       if( FD_LIKELY( conn->response.access_control_max_age ) ) {
     871           0 :         ulong access_control_max_age_len;
     872           0 :         FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, &access_control_max_age_len, "Access-Control-Max-Age: %lu\r\n", conn->response.access_control_max_age ) );
     873           0 :         response_len += access_control_max_age_len;
     874           0 :       }
     875           0 :       FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, NULL, "\r\n" ) );
     876           0 :       response_len += 2UL;
     877             : 
     878           0 :       response = (uchar const *)header_buf;
     879           0 :       break;
     880           0 :     case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY:
     881           0 :       if( FD_UNLIKELY( conn->response.static_body ) ) {
     882           0 :         response     = conn->response.static_body;
     883           0 :         response_len = conn->response.static_body_len;
     884           0 :       } else {
     885           0 :         response = http->oring+(conn->response._body_off%http->oring_sz);
     886           0 :         response_len = conn->response._body_len;
     887           0 :       }
     888           0 :       break;
     889           0 :     default:
     890           0 :       FD_LOG_ERR(( "invalid server state (%d)", conn->state ));
     891           0 :   }
     892             : 
     893           0 :   long sz = send( http->pollfds[ conn_idx ].fd, response+conn->response_bytes_written, response_len-conn->response_bytes_written, MSG_NOSIGNAL );
     894           0 :   if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data was written, continue. */
     895           0 :   if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
     896           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
     897           0 :     return;
     898           0 :   }
     899           0 :   if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
     900             : 
     901           0 :   conn->response_bytes_written += (ulong)sz;
     902           0 :   if( FD_UNLIKELY( conn->response_bytes_written==response_len ) ) {
     903           0 :     switch( conn->state ) {
     904           0 :       case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER:
     905           0 :         if( FD_UNLIKELY( conn->response.upgrade_websocket ) ) {
     906           0 :           if( FD_UNLIKELY( !conn->upgrade_websocket ) ) {
     907           0 :             close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE );
     908           0 :             return;
     909           0 :           }
     910             : 
     911           0 :           int fd = http->pollfds[ conn_idx ].fd;
     912           0 :           http->pollfds[ conn_idx ].fd = -1;
     913             : 
     914           0 :           struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
     915           0 :           if( FD_LIKELY( !conn->response.static_body ) ) conn_treap_ele_remove( http->conn_treap, conn, http->conns );
     916           0 :           conn_pool_ele_release( http->conns, conn );
     917             : 
     918           0 :           if( FD_UNLIKELY( !ws_conn_pool_free( http->ws_conns ) ) ) {
     919           0 :             ws_conn_treap_rev_iter_t it = ws_conn_treap_rev_iter_init( http->ws_conn_treap, http->ws_conns );
     920           0 :             if( FD_LIKELY( !ws_conn_treap_rev_iter_done( it ) ) ) {
     921           0 :               ulong ws_conn_id = ws_conn_treap_rev_iter_idx( it );
     922           0 :               close_conn( http, http->max_conns+ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
     923           0 :             } else {
     924           0 :               close_conn( http, http->max_conns+http->evict_ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
     925           0 :               http->evict_ws_conn_id = (http->evict_ws_conn_id+1UL) % http->max_ws_conns;
     926           0 :             }
     927           0 :           }
     928             : 
     929           0 :           ulong ws_conn_id = ws_conn_pool_idx_acquire( http->ws_conns );
     930           0 :           http->pollfds[ http->max_conns+ws_conn_id ].fd = fd;
     931             : 
     932           0 :           http->ws_conns[ ws_conn_id ].pong_state               = FD_HTTP_SERVER_PONG_STATE_NONE;
     933           0 :           http->ws_conns[ ws_conn_id ].send_frame_cnt           = 0UL;
     934           0 :           http->ws_conns[ ws_conn_id ].send_frame_state         = FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER;
     935           0 :           http->ws_conns[ ws_conn_id ].send_frame_idx           = 0UL;
     936           0 :           http->ws_conns[ ws_conn_id ].recv_started_msg         = 0;
     937           0 :           http->ws_conns[ ws_conn_id ].recv_bytes_parsed        = 0UL;
     938           0 :           http->ws_conns[ ws_conn_id ].recv_bytes_read          = 0UL;
     939           0 :           http->ws_conns[ ws_conn_id ].send_frame_bytes_written = 0UL;
     940             : 
     941           0 :           FD_TEST( conn->request_bytes_read>=conn->request_bytes_len );
     942           0 :           if( FD_UNLIKELY( conn->request_bytes_read-conn->request_bytes_len>0UL ) ) {
     943             :             /* Client might have already started sending data prior to
     944             :                response, so make sure to move it to the recv buffer. */
     945           0 :             FD_TEST( conn->request_bytes_read-conn->request_bytes_len<=http->max_ws_recv_frame_len );
     946           0 :             fd_memcpy( http->ws_conns[ ws_conn_id ].recv_bytes, conn->request_bytes+conn->request_bytes_len, conn->request_bytes_read-conn->request_bytes_len );
     947           0 :             http->ws_conns[ ws_conn_id ].recv_bytes_read = conn->request_bytes_read-conn->request_bytes_len;
     948           0 :           }
     949             : 
     950             : #if FD_HTTP_SERVER_DEBUG
     951             :           FD_LOG_WARNING(( "Upgraded connection %lu (fd=%d) to websocket connection %lu", conn_idx, fd, ws_conn_id ));
     952             : #endif
     953             : 
     954           0 :           if( FD_LIKELY( http->callbacks.ws_open ) ) http->callbacks.ws_open( ws_conn_id, http->callback_ctx );
     955           0 :         } else {
     956           0 :           conn->state                  = FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY;
     957           0 :           conn->response_bytes_written = 0UL;
     958           0 :         }
     959           0 :         break;
     960           0 :       case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY:
     961           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_OK );
     962           0 :         break;
     963           0 :     }
     964           0 :   }
     965           0 : }
     966             : 
     967             : static int
     968             : maybe_write_pong( fd_http_server_t * http,
     969           0 :                   ulong              conn_idx ) {
     970           0 :   struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
     971             : 
     972             :   /* No need to pong if ....
     973             : 
     974             :       Client has not sent a ping */
     975           0 :   if( FD_LIKELY( conn->pong_state==FD_HTTP_SERVER_PONG_STATE_NONE ) ) return 0;
     976             :   /*  We are in the middle of writing a data frame */
     977           0 :   if( FD_LIKELY( conn->send_frame_cnt && (conn->send_frame_state==FD_HTTP_SERVER_SEND_FRAME_STATE_DATA || conn->send_frame_bytes_written ) ) ) return 0;
     978             : 
     979             :   /* Otherwise, we need to pong */
     980           0 :   if( FD_LIKELY( conn->pong_state==FD_HTTP_SERVER_PONG_STATE_WAITING ) ) {
     981           0 :     conn->pong_state         = FD_HTTP_SERVER_PONG_STATE_WRITING;
     982           0 :     conn->pong_bytes_written = 0UL;
     983           0 :   }
     984             : 
     985           0 :   uchar frame[ 2UL+125UL ];
     986           0 :   frame[ 0 ] = 0x80 | 0x0A; /* FIN, 0xA for pong. */
     987           0 :   frame[ 1 ] = (uchar)conn->pong_data_len;
     988           0 :   fd_memcpy( frame+2UL, conn->pong_data, conn->pong_data_len );
     989             : 
     990           0 :   long sz = send( http->pollfds[ conn_idx ].fd, frame+conn->pong_bytes_written, 2UL+conn->pong_data_len-conn->pong_bytes_written, MSG_NOSIGNAL );
     991           0 :   if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return 1; /* No data was written, continue. */
     992           0 :   else if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
     993           0 :     close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
     994           0 :     return 1;
     995           0 :   }
     996           0 :   else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
     997             : 
     998           0 :   conn->pong_bytes_written += (ulong)sz;
     999           0 :   if( FD_UNLIKELY( conn->pong_bytes_written==2UL+conn->pong_data_len ) ) {
    1000           0 :     conn->pong_state = FD_HTTP_SERVER_PONG_STATE_NONE;
    1001           0 :     return 0;
    1002           0 :   }
    1003             : 
    1004           0 :   return 1;
    1005           0 : }
    1006             : 
    1007             : static void
    1008             : write_conn_ws( fd_http_server_t * http,
    1009           0 :                ulong              conn_idx ) {
    1010           0 :   struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
    1011             : 
    1012           0 :   if( FD_UNLIKELY( maybe_write_pong( http, conn_idx ) ) ) return;
    1013           0 :   if( FD_UNLIKELY( !conn->send_frame_cnt ) ) return;
    1014             : 
    1015           0 :   fd_http_server_ws_frame_t * frame = &conn->send_frames[ conn->send_frame_idx ];
    1016           0 :   switch( conn->send_frame_state ) {
    1017           0 :     case FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER: {
    1018           0 :       uchar header[ 10 ];
    1019           0 :       ulong header_len;
    1020           0 :       header[ 0 ] = 0x80 | 0x01; /* FIN, 0x1 for text. */
    1021           0 :       if( FD_LIKELY( frame->len<126UL ) ) {
    1022           0 :         header[ 1 ] = (uchar)frame->len;
    1023           0 :         header_len = 2UL;
    1024           0 :       } else if( FD_LIKELY( frame->len<65536UL ) ) {
    1025           0 :         header[ 1 ] = 126;
    1026           0 :         header[ 2 ] = (uchar)(frame->len>>8);
    1027           0 :         header[ 3 ] = (uchar)(frame->len);
    1028           0 :         header_len = 4UL;
    1029           0 :       } else {
    1030           0 :         header[ 1 ] = 127;
    1031           0 :         header[ 2 ] = (uchar)(frame->len>>56);
    1032           0 :         header[ 3 ] = (uchar)(frame->len>>48);
    1033           0 :         header[ 4 ] = (uchar)(frame->len>>40);
    1034           0 :         header[ 5 ] = (uchar)(frame->len>>32);
    1035           0 :         header[ 6 ] = (uchar)(frame->len>>24);
    1036           0 :         header[ 7 ] = (uchar)(frame->len>>16);
    1037           0 :         header[ 8 ] = (uchar)(frame->len>>8);
    1038           0 :         header[ 9 ] = (uchar)(frame->len);
    1039           0 :         header_len = 10UL;
    1040           0 :       }
    1041             : 
    1042           0 :       long sz = send( http->pollfds[ conn_idx ].fd, header+conn->send_frame_bytes_written, header_len-conn->send_frame_bytes_written, MSG_NOSIGNAL );
    1043           0 :       if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data was written, continue. */
    1044           0 :       else if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
    1045           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
    1046           0 :         return;
    1047           0 :       }
    1048           0 :       else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
    1049             : 
    1050           0 :       conn->send_frame_bytes_written += (ulong)sz;
    1051           0 :       if( FD_UNLIKELY( conn->send_frame_bytes_written==header_len ) ) {
    1052           0 :         conn->send_frame_state         = FD_HTTP_SERVER_SEND_FRAME_STATE_DATA;
    1053           0 :         conn->send_frame_bytes_written = 0UL;
    1054           0 :       }
    1055           0 :       break;
    1056           0 :     }
    1057           0 :     case FD_HTTP_SERVER_SEND_FRAME_STATE_DATA: {
    1058           0 :       uchar const * data = http->oring+(frame->off%http->oring_sz);
    1059           0 :       long sz = send( http->pollfds[ conn_idx ].fd, data+conn->send_frame_bytes_written, frame->len-conn->send_frame_bytes_written, MSG_NOSIGNAL );
    1060           0 :       if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data was written, continue. */
    1061           0 :       else if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
    1062           0 :         close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
    1063           0 :         return;
    1064           0 :       }
    1065           0 :       else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
    1066             : 
    1067           0 :       conn->send_frame_bytes_written += (ulong)sz;
    1068           0 :       if( FD_UNLIKELY( conn->send_frame_bytes_written==frame->len ) ) {
    1069           0 :         conn->send_frame_state = FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER;
    1070           0 :         conn->send_frame_idx   = (conn->send_frame_idx+1UL) % http->max_ws_send_frame_cnt;
    1071           0 :         conn->send_frame_cnt--;
    1072           0 :         conn->send_frame_bytes_written = 0UL;
    1073             : 
    1074           0 :         ws_conn_treap_ele_remove( http->ws_conn_treap, conn, http->ws_conns );
    1075           0 :         if( FD_LIKELY( conn->send_frame_cnt ) ) ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
    1076           0 :       }
    1077           0 :       break;
    1078           0 :     }
    1079           0 :   }
    1080           0 : }
    1081             : 
    1082             : int
    1083             : fd_http_server_ws_send( fd_http_server_t * http,
    1084           0 :                         ulong              ws_conn_id ) {
    1085           0 :   struct fd_http_server_ws_connection * conn = &http->ws_conns[ ws_conn_id ];
    1086             : 
    1087           0 :   if( FD_UNLIKELY( http->stage_err ) ) {
    1088           0 :     http->stage_err = 0;
    1089           0 :     http->stage_len = 0;
    1090           0 :     return -1;
    1091           0 :   }
    1092             : 
    1093           0 :   if( FD_UNLIKELY( conn->send_frame_cnt==http->max_ws_send_frame_cnt ) ) {
    1094           0 :     close_conn( http, ws_conn_id+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
    1095           0 :     return 0;
    1096           0 :   }
    1097             : 
    1098           0 :   fd_http_server_ws_frame_t frame = {
    1099           0 :     .off      = http->stage_off,
    1100           0 :     .len      = http->stage_len,
    1101           0 :   };
    1102             : 
    1103           0 :   conn->send_frames[ (conn->send_frame_idx+conn->send_frame_cnt) % http->max_ws_send_frame_cnt ] = frame;
    1104           0 :   conn->send_frame_cnt++;
    1105             : 
    1106           0 :   if( FD_LIKELY( conn->send_frame_cnt==1UL ) ) {
    1107           0 :     ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
    1108           0 :   }
    1109             : 
    1110           0 :   http->stage_off += http->stage_len;
    1111           0 :   http->stage_len = 0;
    1112             : 
    1113           0 :   return 0;
    1114           0 : }
    1115             : 
    1116             : int
    1117           0 : fd_http_server_ws_broadcast( fd_http_server_t * http ) {
    1118           0 :   if( FD_UNLIKELY( http->stage_err ) ) {
    1119           0 :     http->stage_err = 0;
    1120           0 :     http->stage_len = 0;
    1121           0 :     return -1;
    1122           0 :   }
    1123             : 
    1124           0 :   fd_http_server_ws_frame_t frame = {
    1125           0 :     .off = http->stage_off,
    1126           0 :     .len = http->stage_len,
    1127           0 :   };
    1128             : 
    1129           0 :   for( ulong i=0UL; i<http->max_ws_conns; i++ ) {
    1130           0 :     if( FD_LIKELY( http->pollfds[ http->max_conns+i ].fd==-1 ) ) continue;
    1131             : 
    1132           0 :     struct fd_http_server_ws_connection * conn = &http->ws_conns[ i ];
    1133           0 :     if( FD_UNLIKELY( conn->send_frame_cnt==http->max_ws_send_frame_cnt ) ) {
    1134           0 :       close_conn( http, i+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
    1135           0 :       continue;
    1136           0 :     }
    1137             : 
    1138           0 :     conn->send_frames[ (conn->send_frame_idx+conn->send_frame_cnt) % http->max_ws_send_frame_cnt ] = frame;
    1139           0 :     conn->send_frame_cnt++;
    1140             : 
    1141           0 :     if( FD_LIKELY( conn->send_frame_cnt==1UL ) ) {
    1142           0 :       ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
    1143           0 :     }
    1144           0 :   }
    1145             : 
    1146           0 :   http->stage_off += http->stage_len;
    1147           0 :   http->stage_len = 0;
    1148             : 
    1149           0 :   return 0;
    1150           0 : }
    1151             : 
    1152             : static void
    1153             : write_conn( fd_http_server_t * http,
    1154           0 :             ulong              conn_idx ) {
    1155           0 :   if( FD_LIKELY( conn_idx<http->max_conns ) ) write_conn_http( http, conn_idx );
    1156           0 :   else                                        write_conn_ws(   http, conn_idx );
    1157           0 : }
    1158             : 
    1159             : int
    1160             : fd_http_server_poll( fd_http_server_t * http,
    1161           0 :                      int                poll_timeout ) {
    1162           0 :   int nfds = poll( http->pollfds, http->max_conns+http->max_ws_conns+1UL, poll_timeout );
    1163           0 :   if( FD_UNLIKELY( 0==nfds ) ) return 0;
    1164           0 :   else if( FD_UNLIKELY( -1==nfds && errno==EINTR ) ) return 0;
    1165           0 :   else if( FD_UNLIKELY( -1==nfds ) ) FD_LOG_ERR(( "poll failed (%i-%s)", errno, strerror( errno ) ));
    1166             : 
    1167             :   /* Poll existing connections for new data. */
    1168           0 :   for( ulong i=0UL; i<http->max_conns+http->max_ws_conns+1UL; i++ ) {
    1169           0 :     if( FD_UNLIKELY( -1==http->pollfds[ i ].fd ) ) continue;
    1170           0 :     if( FD_UNLIKELY( i==http->max_conns+http->max_ws_conns ) ) {
    1171           0 :       accept_conns( http );
    1172           0 :     } else {
    1173           0 :       if( FD_LIKELY( http->pollfds[ i ].revents & POLLIN  ) ) read_conn(  http, i );
    1174           0 :       if( FD_UNLIKELY( -1==http->pollfds[ i ].fd ) ) continue;
    1175           0 :       if( FD_LIKELY( http->pollfds[ i ].revents & POLLOUT ) ) write_conn( http, i );
    1176             :       /* No need to handle POLLHUP, read() will return 0 soon enough. */
    1177           0 :     }
    1178           0 :   }
    1179             : 
    1180           0 :   return 1;
    1181           0 : }
    1182             : 
    1183             : static void
    1184             : fd_http_server_evict_until( fd_http_server_t * http,
    1185      676779 :                             ulong              off ) {
    1186      676779 :   conn_treap_fwd_iter_t next;
    1187      676779 :   for( conn_treap_fwd_iter_t it=conn_treap_fwd_iter_init( http->conn_treap, http->conns ); !conn_treap_fwd_iter_idx( it ); it=next ) {
    1188           0 :     next = conn_treap_fwd_iter_next( it, http->conns );
    1189           0 :     struct fd_http_server_connection * conn = conn_treap_fwd_iter_ele( it, http->conns );
    1190             : 
    1191           0 :     if( FD_UNLIKELY( conn->response._body_off<off ) ) {
    1192           0 :       close_conn( http, conn_treap_fwd_iter_idx( it ), FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
    1193           0 :     } else {
    1194           0 :       break;
    1195           0 :     }
    1196           0 :   }
    1197             : 
    1198      676779 :   ws_conn_treap_fwd_iter_t ws_next;
    1199      676779 :   for( ws_conn_treap_fwd_iter_t it=ws_conn_treap_fwd_iter_init( http->ws_conn_treap, http->ws_conns ); !ws_conn_treap_fwd_iter_idx( it ); it=ws_next ) {
    1200           0 :     ws_next = ws_conn_treap_fwd_iter_next( it, http->ws_conns );
    1201           0 :     struct fd_http_server_ws_connection * conn = ws_conn_treap_fwd_iter_ele( it, http->ws_conns );
    1202             : 
    1203           0 :     if( FD_UNLIKELY( conn->send_frames[ conn->send_frame_idx ].off<off ) ) {
    1204           0 :       close_conn( http, ws_conn_treap_fwd_iter_idx( it )+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
    1205           0 :     } else {
    1206           0 :       break;
    1207           0 :     }
    1208           0 :   }
    1209      676779 : }
    1210             : 
    1211             : static void
    1212             : fd_http_server_reserve( fd_http_server_t * http,
    1213      747438 :                         ulong              len ) {
    1214      747438 :   ulong remaining = http->oring_sz-((http->stage_off%http->oring_sz)+http->stage_len);
    1215      747438 :   if( FD_UNLIKELY( len>remaining ) ) {
    1216             :     /* Appending the format string into the hcache would go past the end
    1217             :         of the buffer... two cases, */
    1218       81420 :     if( FD_UNLIKELY( http->stage_len+len>http->oring_sz ) ) {
    1219             :       /* Case 1: The snap is going to be larger than the entire buffer,
    1220             :                   there's no way to fit it even if we evict everything
    1221             :                   else.  Mark the hcache as errored and exit. */
    1222             : 
    1223       70659 :       FD_LOG_WARNING(( "tried to reserve %lu bytes for an outgoing message which exceeds the entire data size", http->stage_len+len ));
    1224       70659 :       http->stage_err = 1;
    1225       70659 :       return;
    1226       70659 :     } else {
    1227             :       /* Case 2: The snap can fit if we relocate it to the start of the
    1228             :                  buffer and evict whatever was there.  We also evict the
    1229             :                  rest of the buffer behind where the snap was to
    1230             :                  preserve the invariant that snaps are always evicted in
    1231             :                  circular order. */
    1232             : 
    1233       10761 :       ulong stage_end = http->stage_off+remaining+http->stage_len+len;
    1234       10761 :       ulong clamp = fd_ulong_if( stage_end>=http->oring_sz, stage_end-http->oring_sz, 0UL );
    1235       10761 :       fd_http_server_evict_until( http, clamp );
    1236       10761 :       memmove( http->oring, http->oring+(http->stage_off%http->oring_sz), http->stage_len );
    1237       10761 :       http->stage_off += http->stage_len+remaining;
    1238       10761 :     }
    1239      666018 :   } else {
    1240             :     /* The snap can fit in the buffer, we just need to evict whatever
    1241             :         was there before. */
    1242      666018 :     ulong stage_end = http->stage_off+http->stage_len+len;
    1243      666018 :     ulong clamp = fd_ulong_if( stage_end>=http->oring_sz, stage_end-http->oring_sz, 0UL );
    1244      666018 :     fd_http_server_evict_until( http, clamp );
    1245      666018 :   }
    1246      747438 : }
    1247             : 
    1248             : void
    1249             : fd_http_server_stage_trunc( fd_http_server_t * http,
    1250           0 :                              ulong len ) {
    1251           0 :   http->stage_len = len;
    1252           0 : }
    1253             : 
    1254             : ulong
    1255           0 : fd_http_server_stage_len( fd_http_server_t * http ) {
    1256           0 :   return http->stage_len;
    1257           0 : }
    1258             : 
    1259             : void
    1260             : fd_http_server_printf( fd_http_server_t * http,
    1261             :                        char const *       fmt,
    1262     1524654 :                        ... ) {
    1263     1524654 :   if( FD_UNLIKELY( http->stage_err ) ) return;
    1264             : 
    1265      747438 :   va_list ap;
    1266      747438 :   va_start( ap, fmt );
    1267      747438 :   ulong printed_len = (ulong)vsnprintf( NULL, 0UL, fmt, ap );
    1268      747438 :   va_end( ap );
    1269             : 
    1270      747438 :   fd_http_server_reserve( http, printed_len );
    1271      747438 :   if( FD_UNLIKELY( http->stage_err ) ) return;
    1272             : 
    1273      676779 :   va_start( ap, fmt );
    1274      676779 :   vsnprintf( (char *)http->oring+(http->stage_off%http->oring_sz)+http->stage_len,
    1275      676779 :              INT_MAX, /* We already proved it's going to fit above */
    1276      676779 :              fmt,
    1277      676779 :              ap );
    1278      676779 :   va_end( ap );
    1279             : 
    1280      676779 :   http->stage_len += printed_len;
    1281      676779 : }
    1282             : 
    1283             : void
    1284             : fd_http_server_memcpy( fd_http_server_t * http,
    1285             :                        uchar const *      data,
    1286           0 :                        ulong              data_len ) {
    1287           0 :   fd_http_server_reserve( http, data_len );
    1288           0 :   if( FD_UNLIKELY( http->stage_err ) ) return;
    1289             : 
    1290           0 :   fd_memcpy( (char *)http->oring+(http->stage_off%http->oring_sz)+http->stage_len,
    1291           0 :              data,
    1292           0 :              data_len );
    1293           0 :   http->stage_len += data_len;
    1294           0 : }
    1295             : 
    1296             : void
    1297           6 : fd_http_server_unstage( fd_http_server_t * http ) {
    1298           6 :   http->stage_err = 0;
    1299           6 :   http->stage_len = 0UL;
    1300           6 : }
    1301             : 
    1302             : int
    1303             : fd_http_server_stage_body( fd_http_server_t *          http,
    1304       95244 :                            fd_http_server_response_t * response ) {
    1305       95244 :   if( FD_UNLIKELY( http->stage_err ) ) {
    1306       70659 :     http->stage_err = 0;
    1307       70659 :     http->stage_len = 0;
    1308       70659 :     return -1;
    1309       70659 :   }
    1310             : 
    1311       24585 :   response->_body_off = http->stage_off;
    1312       24585 :   response->_body_len = http->stage_len;
    1313       24585 :   http->stage_off += http->stage_len;
    1314       24585 :   http->stage_len = 0;
    1315       24585 :   return 0;
    1316       95244 : }

Generated by: LCOV version 1.14