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