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 && !strncmp( 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 ) && strstr( headers[ i ].value, "compress-zstd" ) ) ) {
585 0 : compress_websocket = 1;
586 0 : }
587 0 : }
588 0 : #endif
589 :
590 0 : char const * sec_websocket_key = NULL;
591 0 : for( ulong i=0UL; i<num_headers; i++ ) {
592 0 : if( FD_LIKELY( headers[ i ].name_len==17UL && !strncasecmp( headers[ i ].name, "Sec-WebSocket-Key", 17UL ) ) ) {
593 0 : sec_websocket_key = headers[ i ].value;
594 0 : if( FD_UNLIKELY( headers[ i ].value_len!=24 ) ) {
595 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_KEY );
596 0 : return;
597 0 : }
598 0 : break;
599 0 : }
600 0 : }
601 :
602 0 : char const * sec_websocket_version = NULL;
603 0 : for( ulong i=0UL; i<num_headers; i++ ) {
604 0 : if( FD_LIKELY( headers[ i ].name_len==21UL && !strncasecmp( headers[ i ].name, "Sec-Websocket-Version", 21UL ) ) ) {
605 0 : sec_websocket_version = headers[ i ].value;
606 0 : if( FD_UNLIKELY( headers[ i ].value_len!=2 || strncmp( sec_websocket_version, "13", 2UL ) ) ) {
607 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNEXPECTED_VERSION );
608 0 : return;
609 0 : }
610 0 : break;
611 0 : }
612 0 : }
613 :
614 0 : if( FD_UNLIKELY( !sec_websocket_key ) ) {
615 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_KEY_HEADER );
616 0 : return;
617 0 : }
618 :
619 0 : if( FD_UNLIKELY( !sec_websocket_version ) ) {
620 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_VERSION_HEADER );
621 0 : return;
622 0 : }
623 :
624 0 : conn->sec_websocket_key = sec_websocket_key;
625 0 : }
626 :
627 0 : conn->state = FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER;
628 :
629 0 : fd_http_server_request_t request = {
630 0 : .connection_id = conn_idx,
631 :
632 0 : .method = method_enum,
633 0 : .path = path_nul_terminated,
634 :
635 0 : .ctx = http->callback_ctx,
636 :
637 0 : .headers.content_type = content_type_nul_terminated,
638 0 : .headers.accept_encoding = accept_encoding_nul_terminated,
639 0 : .headers.compress_websocket = compress_websocket,
640 0 : .headers.upgrade_websocket = conn->upgrade_websocket,
641 0 : };
642 :
643 0 : switch( method_enum ) {
644 0 : case FD_HTTP_SERVER_METHOD_POST:
645 0 : case FD_HTTP_SERVER_METHOD_PUT: {
646 0 : request.post.body = (uchar*)conn->request_bytes+result;
647 0 : request.post.body_len = content_len;
648 0 : } break;
649 0 : default: break;
650 0 : }
651 :
652 0 : fd_http_server_response_t response = http->callbacks.request( &request );
653 0 : if( FD_LIKELY( http->pollfds[ conn_idx ].fd==-1 ) ) return; /* Connection was closed by callback */
654 0 : conn->response = response;
655 :
656 : #if FD_HTTP_SERVER_DEBUG
657 : 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 ));
658 : #endif
659 :
660 0 : if( FD_LIKELY( !conn->response.static_body ) ) conn_treap_ele_insert( http->conn_treap, conn, http->conns );
661 0 : }
662 :
663 : static void
664 : read_conn_ws( fd_http_server_t * http,
665 0 : ulong conn_idx ) {
666 0 : struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
667 :
668 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 );
669 0 : if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data to read, continue. */
670 0 : else if( FD_UNLIKELY( !sz || (-1==sz && is_expected_network_error( errno ) ) ) ) {
671 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
672 0 : return;
673 0 : }
674 0 : else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "read failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
675 :
676 : /* New data was read... process it */
677 0 : conn->recv_bytes_read += (ulong)sz;
678 0 : http->metrics.bytes_read += (ulong)sz;
679 0 : again:
680 0 : if( FD_UNLIKELY( conn->recv_bytes_read<2UL ) ) return; /* Need at least 2 bytes to determine frame length */
681 :
682 0 : int is_mask_set = conn->recv_bytes[ conn->recv_bytes_parsed+1UL ] & 0x80;
683 0 : if( FD_UNLIKELY( !is_mask_set ) ) {
684 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_BAD_MASK );
685 0 : return;
686 0 : }
687 :
688 0 : int opcode = conn->recv_bytes[ conn->recv_bytes_parsed ] & 0x0F;
689 0 : if( FD_UNLIKELY( opcode!=0x0 && opcode!=0x1 && opcode!=0x2 && opcode!=0x8 && opcode!=0x9 && opcode!=0xA ) ) {
690 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_UNKNOWN_OPCODE );
691 0 : return;
692 0 : }
693 :
694 0 : ulong payload_len = conn->recv_bytes[ conn->recv_bytes_parsed+1UL ] & 0x7F;
695 0 : if( FD_UNLIKELY( (payload_len==126 || payload_len==127) && (opcode==0x8 || opcode==0x9 || opcode==0xA) ) ) {
696 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CONTROL_FRAME_TOO_LARGE );
697 0 : return;
698 0 : }
699 :
700 0 : ulong len_bytes;
701 0 : if( FD_LIKELY( payload_len<126UL ) ) {
702 0 : len_bytes = 1UL;
703 0 : } else if( FD_LIKELY( payload_len==126 ) ) {
704 0 : if( FD_UNLIKELY( conn->recv_bytes_read<4UL ) ) return; /* Need at least 4 bytes to determine frame length */
705 0 : payload_len = ((ulong)conn->recv_bytes[ conn->recv_bytes_parsed+2UL ]<<8UL) | (ulong)conn->recv_bytes[ conn->recv_bytes_parsed+3UL ];
706 0 : len_bytes = 3UL;
707 0 : } else if( FD_LIKELY( payload_len==127 ) ) {
708 0 : if( FD_UNLIKELY( conn->recv_bytes_read<10UL ) ) return; /* Need at least 10 bytes to determine frame length */
709 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) |
710 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 ];
711 0 : len_bytes = 9UL;
712 0 : } else {
713 0 : FD_LOG_ERR(( "unexpected payload_len %lu", payload_len )); /* Silence clang sanitizer, not possible */
714 0 : }
715 :
716 0 : ulong header_len = 1UL+len_bytes+4UL;
717 0 : ulong frame_len = header_len+payload_len;
718 0 : if( FD_UNLIKELY( frame_len<header_len ) ) { /* Overflow */
719 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME );
720 0 : return;
721 0 : }
722 :
723 0 : if( FD_UNLIKELY( conn->recv_bytes_parsed+frame_len+1UL>http->max_ws_recv_frame_len ) ) {
724 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_OVERSIZE_FRAME );
725 0 : return;
726 0 : }
727 :
728 0 : if( FD_UNLIKELY( conn->recv_bytes_read<frame_len ) ) return; /* Need more data to read the full frame */
729 :
730 : /* Data frame, process it */
731 :
732 0 : int is_fin_set = conn->recv_bytes[ conn->recv_bytes_parsed+0UL ] & 0x80;
733 :
734 0 : uchar * mask = conn->recv_bytes+conn->recv_bytes_parsed+1UL+len_bytes;
735 0 : uchar mask_copy[ 4 ] = { mask[ 0 ], mask[ 1 ], mask[ 2 ], mask[ 3 ] }; /* Bytes will be overwritten by the memmove below */
736 :
737 0 : uchar * payload = conn->recv_bytes+conn->recv_bytes_parsed+header_len;
738 0 : for( ulong i=0UL; i<payload_len; i++ ) conn->recv_bytes[ conn->recv_bytes_parsed+i ] = payload[ i ] ^ mask_copy[ i % 4 ];
739 :
740 0 : http->metrics.frames_read++;
741 :
742 : /* Frame is complete, process it */
743 :
744 0 : if( FD_UNLIKELY( opcode==0x8 ) ) {
745 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
746 0 : return;
747 0 : } else if( FD_UNLIKELY( opcode==0x9 ) ) {
748 : /* Ping frame, queue pong unless we are already sending one */
749 0 : if( FD_LIKELY( conn->pong_state!=FD_HTTP_SERVER_PONG_STATE_WAITING ) ) {
750 0 : conn->pong_state = FD_HTTP_SERVER_PONG_STATE_WAITING;
751 0 : conn->pong_data_len = payload_len;
752 0 : FD_TEST( payload_len<=125UL );
753 0 : memcpy( conn->pong_data, conn->recv_bytes+conn->recv_bytes_parsed, payload_len );
754 0 : }
755 0 : if( FD_UNLIKELY( conn->recv_bytes_read-frame_len ) ) {
756 0 : memmove( conn->recv_bytes, conn->recv_bytes+conn->recv_bytes_parsed+frame_len, conn->recv_bytes_read-frame_len );
757 0 : }
758 0 : conn->recv_bytes_parsed = 0UL;
759 0 : conn->recv_bytes_read -= frame_len;
760 0 : return;
761 0 : } else if( FD_UNLIKELY( opcode==0xA ) ) {
762 : /* Pong frame, ignore */
763 0 : if( FD_UNLIKELY( conn->recv_bytes_read-frame_len ) ) {
764 0 : memmove( conn->recv_bytes, conn->recv_bytes+conn->recv_bytes_parsed+frame_len, conn->recv_bytes_read-frame_len );
765 0 : }
766 0 : conn->recv_bytes_parsed = 0UL;
767 0 : conn->recv_bytes_read -= frame_len;
768 0 : return;
769 0 : }
770 :
771 0 : if( FD_UNLIKELY( conn->recv_started_msg && opcode!=0x0 ) ) {
772 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_CONT_OPCODE );
773 0 : return;
774 0 : }
775 :
776 0 : if( FD_UNLIKELY( !conn->recv_started_msg && opcode!=0x1 && opcode!=0x2 ) ) {
777 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_EXPECTED_TEXT_OPCODE );
778 0 : return;
779 0 : }
780 :
781 0 : if( FD_UNLIKELY( conn->recv_started_msg && opcode!=conn->recv_last_opcode ) ) {
782 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CHANGED_OPCODE );
783 0 : return;
784 0 : }
785 0 : conn->recv_last_opcode = opcode;
786 :
787 : /* Check if this is a complete message */
788 :
789 0 : if( FD_UNLIKELY( !is_fin_set ) ) {
790 0 : conn->recv_started_msg = 1;
791 0 : conn->recv_bytes_read -= frame_len;
792 0 : conn->recv_bytes_parsed += payload_len;
793 0 : return; /* Not a complete message yet */
794 0 : }
795 :
796 : /* Complete message, process it */
797 :
798 0 : uchar * trailing_data = conn->recv_bytes+conn->recv_bytes_parsed+frame_len;
799 0 : ulong trailing_data_len = conn->recv_bytes_read-frame_len;
800 :
801 0 : conn->recv_bytes_parsed += payload_len;
802 0 : conn->recv_bytes_read -= frame_len;
803 :
804 0 : uchar tmp = conn->recv_bytes[ conn->recv_bytes_parsed ];
805 0 : conn->recv_bytes[ conn->recv_bytes_parsed ] = 0; /* NUL terminate */
806 0 : http->callbacks.ws_message( conn_idx-http->max_conns, conn->recv_bytes, conn->recv_bytes_parsed, http->callback_ctx );
807 0 : if( FD_UNLIKELY( -1==http->pollfds[ conn_idx ].fd ) ) return; /* Connection was closed by callback */
808 0 : conn->recv_bytes[ conn->recv_bytes_parsed ] = tmp;
809 :
810 0 : conn->recv_started_msg = 0;
811 0 : conn->recv_bytes_parsed = 0UL;
812 0 : if( FD_UNLIKELY( trailing_data_len ) ) {
813 0 : memmove( conn->recv_bytes, trailing_data, trailing_data_len );
814 0 : goto again; /* Might be another message in the buffer to process */
815 0 : }
816 0 : }
817 :
818 : static void
819 : read_conn( fd_http_server_t * http,
820 0 : ulong conn_idx ) {
821 0 : if( FD_LIKELY( conn_idx<http->max_conns ) ) read_conn_http( http, conn_idx );
822 0 : else read_conn_ws( http, conn_idx );
823 0 : }
824 :
825 : static void
826 : write_conn_http( fd_http_server_t * http,
827 0 : ulong conn_idx ) {
828 0 : struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
829 :
830 0 : char header_buf[ 1024 ];
831 :
832 0 : uchar const * response;
833 0 : ulong response_len;
834 0 : switch( conn->state ) {
835 0 : case FD_HTTP_SERVER_CONNECTION_STATE_READING:
836 0 : return; /* No data staged for write yet. */
837 0 : case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER:
838 0 : switch( conn->response.status ) {
839 0 : case 200:
840 0 : if( FD_UNLIKELY( conn->response.upgrade_websocket ) ) {
841 0 : if( FD_UNLIKELY( !conn->upgrade_websocket ) ) {
842 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE );
843 0 : return;
844 0 : }
845 :
846 0 : uchar sec_websocket_key[ 60 ];
847 0 : fd_memcpy( sec_websocket_key, conn->sec_websocket_key, 24 );
848 0 : fd_memcpy( sec_websocket_key+24, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36 );
849 :
850 0 : uchar sec_websocket_accept[ 20 ];
851 0 : fd_sha1_hash( sec_websocket_key, 60, sec_websocket_accept );
852 0 : char sec_websocket_accept_base64[ FD_BASE64_ENC_SZ( 20 ) ];
853 0 : ulong encoded_len = fd_base64_encode( sec_websocket_accept_base64, sec_websocket_accept, 20 );
854 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 ) );
855 0 : } else {
856 0 : ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
857 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 ) );
858 0 : }
859 0 : break;
860 0 : case 204: {
861 0 : ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
862 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 ) );
863 0 : break;
864 0 : }
865 0 : case 400: {
866 0 : ulong body_len = conn->response.static_body ? conn->response.static_body_len : conn->response._body_len;
867 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 ) );
868 0 : break;
869 0 : }
870 0 : case 404:
871 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" ) );
872 0 : break;
873 0 : case 405:
874 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" ) );
875 0 : break;
876 0 : case 500:
877 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" ) );
878 0 : break;
879 0 : default:
880 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" ) );
881 0 : break;
882 0 : }
883 :
884 0 : if( FD_LIKELY( conn->response.compress_websocket ) ) {
885 0 : ulong compress_websocket_len;
886 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" ) );
887 0 : response_len += compress_websocket_len;
888 0 : }
889 0 : if( FD_LIKELY( conn->response.content_type ) ) {
890 0 : ulong content_type_len;
891 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 ) );
892 0 : response_len += content_type_len;
893 0 : }
894 0 : if( FD_LIKELY( conn->response.cache_control ) ) {
895 0 : ulong cache_control_len;
896 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 ) );
897 0 : response_len += cache_control_len;
898 0 : }
899 0 : if( FD_LIKELY( conn->response.content_encoding ) ) {
900 0 : ulong content_encoding_len;
901 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 ) );
902 0 : response_len += content_encoding_len;
903 0 : }
904 0 : if( FD_LIKELY( conn->response.access_control_allow_origin ) ) {
905 0 : ulong access_control_allow_origin_len;
906 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 ) );
907 0 : response_len += access_control_allow_origin_len;
908 0 : }
909 0 : if( FD_LIKELY( conn->response.access_control_allow_methods ) ) {
910 0 : ulong access_control_allow_methods_len;
911 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 ) );
912 0 : response_len += access_control_allow_methods_len;
913 0 : }
914 0 : if( FD_LIKELY( conn->response.access_control_allow_headers ) ) {
915 0 : ulong access_control_allow_headers_len;
916 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 ) );
917 0 : response_len += access_control_allow_headers_len;
918 0 : }
919 0 : if( FD_LIKELY( conn->response.access_control_max_age ) ) {
920 0 : ulong access_control_max_age_len;
921 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 ) );
922 0 : response_len += access_control_max_age_len;
923 0 : }
924 0 : FD_TEST( fd_cstr_printf_check( header_buf+response_len, sizeof( header_buf )-response_len, NULL, "\r\n" ) );
925 0 : response_len += 2UL;
926 :
927 0 : response = (uchar const *)header_buf;
928 0 : break;
929 0 : case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY:
930 0 : if( FD_UNLIKELY( conn->response.static_body ) ) {
931 0 : response = conn->response.static_body;
932 0 : response_len = conn->response.static_body_len;
933 0 : } else {
934 0 : response = http->oring+(conn->response._body_off%http->oring_sz);
935 0 : response_len = conn->response._body_len;
936 0 : }
937 0 : break;
938 0 : default:
939 0 : FD_LOG_ERR(( "invalid server state (%d)", conn->state ));
940 0 : }
941 :
942 0 : long sz = send( http->pollfds[ conn_idx ].fd, response+conn->response_bytes_written, response_len-conn->response_bytes_written, MSG_NOSIGNAL );
943 0 : if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return; /* No data was written, continue. */
944 0 : if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
945 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
946 0 : return;
947 0 : }
948 0 : if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
949 :
950 0 : http->metrics.bytes_written += (ulong)sz;
951 0 : conn->response_bytes_written += (ulong)sz;
952 0 : if( FD_UNLIKELY( conn->response_bytes_written==response_len ) ) {
953 0 : switch( conn->state ) {
954 0 : case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_HEADER:
955 0 : if( FD_UNLIKELY( conn->response.upgrade_websocket ) ) {
956 0 : if( FD_UNLIKELY( !conn->upgrade_websocket ) ) {
957 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_MISSING_UPGRADE );
958 0 : return;
959 0 : }
960 :
961 0 : int fd = http->pollfds[ conn_idx ].fd;
962 0 : http->pollfds[ conn_idx ].fd = -1;
963 :
964 0 : struct fd_http_server_connection * conn = &http->conns[ conn_idx ];
965 0 : if( FD_LIKELY( !conn->response.static_body ) ) conn_treap_ele_remove( http->conn_treap, conn, http->conns );
966 0 : conn_pool_ele_release( http->conns, conn );
967 :
968 0 : if( FD_UNLIKELY( !ws_conn_pool_free( http->ws_conns ) ) ) {
969 0 : ws_conn_treap_rev_iter_t it = ws_conn_treap_rev_iter_init( http->ws_conn_treap, http->ws_conns );
970 0 : if( FD_LIKELY( !ws_conn_treap_rev_iter_done( it ) ) ) {
971 0 : ulong ws_conn_id = ws_conn_treap_rev_iter_idx( it );
972 0 : close_conn( http, http->max_conns+ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
973 0 : } else {
974 0 : close_conn( http, http->max_conns+http->evict_ws_conn_id, FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
975 0 : http->evict_ws_conn_id = (http->evict_ws_conn_id+1UL) % http->max_ws_conns;
976 0 : }
977 0 : }
978 :
979 0 : ulong ws_conn_id = ws_conn_pool_idx_acquire( http->ws_conns );
980 0 : http->pollfds[ http->max_conns+ws_conn_id ].fd = fd;
981 :
982 0 : http->ws_conns[ ws_conn_id ].pong_state = FD_HTTP_SERVER_PONG_STATE_NONE;
983 0 : http->ws_conns[ ws_conn_id ].send_frame_cnt = 0UL;
984 0 : http->ws_conns[ ws_conn_id ].send_frame_state = FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER;
985 0 : http->ws_conns[ ws_conn_id ].send_frame_idx = 0UL;
986 0 : http->ws_conns[ ws_conn_id ].recv_started_msg = 0;
987 0 : http->ws_conns[ ws_conn_id ].recv_bytes_parsed = 0UL;
988 0 : http->ws_conns[ ws_conn_id ].recv_bytes_read = 0UL;
989 0 : http->ws_conns[ ws_conn_id ].send_frame_bytes_written = 0UL;
990 0 : http->ws_conns[ ws_conn_id ].compress_websocket = conn->response.compress_websocket;
991 :
992 0 : http->metrics.connection_cnt--;
993 0 : http->metrics.ws_connection_cnt++;
994 :
995 0 : FD_TEST( conn->request_bytes_read>=conn->request_bytes_len );
996 0 : if( FD_UNLIKELY( conn->request_bytes_read-conn->request_bytes_len>0UL ) ) {
997 : /* Client might have already started sending data prior to
998 : response, so make sure to move it to the recv buffer. */
999 0 : FD_TEST( conn->request_bytes_read-conn->request_bytes_len<=http->max_ws_recv_frame_len );
1000 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 );
1001 0 : http->ws_conns[ ws_conn_id ].recv_bytes_read = conn->request_bytes_read-conn->request_bytes_len;
1002 0 : }
1003 :
1004 : #if FD_HTTP_SERVER_DEBUG
1005 : FD_LOG_WARNING(( "Upgraded connection %lu (fd=%d) to websocket connection %lu", conn_idx, fd, ws_conn_id ));
1006 : #endif
1007 :
1008 0 : if( FD_LIKELY( http->callbacks.ws_open ) ) http->callbacks.ws_open( ws_conn_id, http->callback_ctx );
1009 0 : } else {
1010 0 : conn->state = FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY;
1011 0 : conn->response_bytes_written = 0UL;
1012 0 : }
1013 0 : break;
1014 0 : case FD_HTTP_SERVER_CONNECTION_STATE_WRITING_BODY:
1015 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_OK );
1016 0 : break;
1017 0 : }
1018 0 : }
1019 0 : }
1020 :
1021 : static int
1022 : maybe_write_pong( fd_http_server_t * http,
1023 0 : ulong conn_idx ) {
1024 0 : struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
1025 :
1026 : /* No need to pong if ....
1027 :
1028 : Client has not sent a ping */
1029 0 : if( FD_LIKELY( conn->pong_state==FD_HTTP_SERVER_PONG_STATE_NONE ) ) return 0;
1030 : /* We are in the middle of writing a data frame */
1031 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;
1032 :
1033 : /* Otherwise, we need to pong */
1034 0 : if( FD_LIKELY( conn->pong_state==FD_HTTP_SERVER_PONG_STATE_WAITING ) ) {
1035 0 : conn->pong_state = FD_HTTP_SERVER_PONG_STATE_WRITING;
1036 0 : conn->pong_bytes_written = 0UL;
1037 0 : }
1038 :
1039 0 : uchar frame[ 2UL+125UL ];
1040 0 : frame[ 0 ] = 0x80 | 0x0A; /* FIN, 0xA for pong. */
1041 0 : frame[ 1 ] = (uchar)conn->pong_data_len;
1042 0 : fd_memcpy( frame+2UL, conn->pong_data, conn->pong_data_len );
1043 :
1044 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 );
1045 0 : if( FD_UNLIKELY( -1==sz && errno==EAGAIN ) ) return 1; /* No data was written, continue. */
1046 0 : else if( FD_UNLIKELY( -1==sz && is_expected_network_error( errno ) ) ) {
1047 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
1048 0 : return 1;
1049 0 : }
1050 0 : else if( FD_UNLIKELY( -1==sz ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, strerror( errno ) )); /* Unexpected programmer error, abort */
1051 :
1052 0 : http->metrics.bytes_written += (ulong)sz;
1053 0 : conn->pong_bytes_written += (ulong)sz;
1054 0 : if( FD_UNLIKELY( conn->pong_bytes_written==2UL+conn->pong_data_len ) ) {
1055 0 : conn->pong_state = FD_HTTP_SERVER_PONG_STATE_NONE;
1056 0 : return 0;
1057 0 : }
1058 :
1059 0 : return 1;
1060 0 : }
1061 :
1062 : static void
1063 : write_conn_ws( fd_http_server_t * http,
1064 0 : ulong conn_idx ) {
1065 0 : struct fd_http_server_ws_connection * conn = &http->ws_conns[ conn_idx-http->max_conns ];
1066 :
1067 0 : if( FD_UNLIKELY( maybe_write_pong( http, conn_idx ) ) ) return;
1068 0 : if( FD_UNLIKELY( !conn->send_frame_cnt ) ) return;
1069 :
1070 0 : struct iovec iovecs[ 512UL*2UL ];
1071 0 : uchar headers[ 512UL ][ 10UL ];
1072 :
1073 0 : ulong batch_cnt = fd_ulong_min( conn->send_frame_cnt, 512UL );
1074 0 : ulong out_idx = 0UL;
1075 0 : for( ulong i=0UL; i<batch_cnt; i++ ) {
1076 0 : fd_http_server_ws_frame_t * frame = &conn->send_frames[ (conn->send_frame_idx+i) % http->max_ws_send_frame_cnt ];
1077 0 : if( FD_UNLIKELY( i || conn->send_frame_state==FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER ) ) {
1078 0 : ulong header_len;
1079 0 : headers[ i ][ 0 ] = 0x80 | fd_uchar_if(frame->compressed, 0x02, 0x01); /* FIN, 0x1 for text, 0x2 for binary */
1080 0 : if( FD_LIKELY( frame->len<126UL ) ) {
1081 0 : headers[ i ][ 1 ] = (uchar)frame->len;
1082 0 : header_len = 2UL;
1083 0 : } else if( FD_LIKELY( frame->len<65536UL ) ) {
1084 0 : headers[ i ][ 1 ] = 126;
1085 0 : headers[ i ][ 2 ] = (uchar)(frame->len>>8);
1086 0 : headers[ i ][ 3 ] = (uchar)(frame->len);
1087 0 : header_len = 4UL;
1088 0 : } else {
1089 0 : headers[ i ][ 1 ] = 127;
1090 0 : headers[ i ][ 2 ] = (uchar)(frame->len>>56);
1091 0 : headers[ i ][ 3 ] = (uchar)(frame->len>>48);
1092 0 : headers[ i ][ 4 ] = (uchar)(frame->len>>40);
1093 0 : headers[ i ][ 5 ] = (uchar)(frame->len>>32);
1094 0 : headers[ i ][ 6 ] = (uchar)(frame->len>>24);
1095 0 : headers[ i ][ 7 ] = (uchar)(frame->len>>16);
1096 0 : headers[ i ][ 8 ] = (uchar)(frame->len>>8);
1097 0 : headers[ i ][ 9 ] = (uchar)(frame->len);
1098 0 : header_len = 10UL;
1099 0 : }
1100 :
1101 0 : ulong header_bytes_written = fd_ulong_if( i==0UL, conn->send_frame_bytes_written, 0UL );
1102 :
1103 0 : iovecs[ out_idx ].iov_base = headers[ i ]+header_bytes_written;
1104 0 : iovecs[ out_idx ].iov_len = header_len-header_bytes_written;
1105 0 : out_idx++;
1106 0 : }
1107 :
1108 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 );
1109 0 : iovecs[ out_idx ].iov_base = http->oring+(frame->off%http->oring_sz)+data_bytes_written;
1110 0 : iovecs[ out_idx ].iov_len = frame->len-data_bytes_written;
1111 0 : out_idx++;
1112 0 : }
1113 :
1114 0 : struct mmsghdr msg = {0};
1115 0 : msg.msg_hdr.msg_iov = iovecs;
1116 0 : msg.msg_hdr.msg_iovlen = out_idx;
1117 :
1118 0 : int result = sendmmsg( http->pollfds[ conn_idx ].fd, &msg, 1U, MSG_NOSIGNAL );
1119 0 : if( FD_UNLIKELY( -1==result && errno==EAGAIN ) ) return; /* No data was written, continue. */
1120 0 : else if( FD_UNLIKELY( -1==result && is_expected_network_error( errno ) ) ) {
1121 0 : close_conn( http, conn_idx, FD_HTTP_SERVER_CONNECTION_CLOSE_PEER_RESET );
1122 0 : return;
1123 0 : }
1124 0 : else if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "write failed (%i-%s)", errno, fd_io_strerror( errno ) )); /* Unexpected programmer error, abort */
1125 :
1126 0 : FD_TEST( result==1 );
1127 :
1128 0 : ulong sent = (ulong)msg.msg_len;
1129 0 : http->metrics.bytes_written += sent;
1130 :
1131 0 : for( ulong i=0UL; i<out_idx; i++ ) {
1132 0 : ulong iov_len = iovecs[ i ].iov_len;
1133 0 : if( FD_LIKELY( sent>=iov_len ) ) {
1134 0 : conn->send_frame_bytes_written = 0UL;
1135 :
1136 0 : if( FD_LIKELY( conn->send_frame_state==FD_HTTP_SERVER_SEND_FRAME_STATE_DATA ) ) {
1137 0 : conn->send_frame_state = FD_HTTP_SERVER_SEND_FRAME_STATE_HEADER;
1138 0 : conn->send_frame_idx = (conn->send_frame_idx+1UL) % http->max_ws_send_frame_cnt;
1139 0 : conn->send_frame_cnt--;
1140 :
1141 0 : ws_conn_treap_ele_remove( http->ws_conn_treap, conn, http->ws_conns );
1142 0 : if( FD_LIKELY( conn->send_frame_cnt ) ) ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
1143 :
1144 0 : http->metrics.frames_written++;
1145 0 : } else {
1146 0 : conn->send_frame_state = FD_HTTP_SERVER_SEND_FRAME_STATE_DATA;
1147 0 : }
1148 :
1149 0 : sent -= iov_len;
1150 0 : } else {
1151 0 : conn->send_frame_bytes_written += sent;
1152 0 : break;
1153 0 : }
1154 0 : }
1155 0 : }
1156 :
1157 : static void
1158 : write_conn( fd_http_server_t * http,
1159 0 : ulong conn_idx ) {
1160 0 : if( FD_LIKELY( conn_idx<http->max_conns ) ) write_conn_http( http, conn_idx );
1161 0 : else write_conn_ws( http, conn_idx );
1162 0 : }
1163 :
1164 : int
1165 : fd_http_server_poll( fd_http_server_t * http,
1166 0 : int poll_timeout ) {
1167 0 : int nfds = fd_syscall_poll( http->pollfds, (uint)( http->max_conns+http->max_ws_conns+1UL ), poll_timeout );
1168 0 : if( FD_UNLIKELY( 0==nfds ) ) return 0;
1169 0 : else if( FD_UNLIKELY( -1==nfds && errno==EINTR ) ) return 0;
1170 0 : else if( FD_UNLIKELY( -1==nfds ) ) FD_LOG_ERR(( "poll failed (%i-%s)", errno, strerror( errno ) ));
1171 :
1172 : /* Poll existing connections for new data. */
1173 0 : for( ulong i=0UL; i<http->max_conns+http->max_ws_conns+1UL; i++ ) {
1174 0 : if( FD_UNLIKELY( -1==http->pollfds[ i ].fd ) ) continue;
1175 0 : if( FD_UNLIKELY( i==http->max_conns+http->max_ws_conns ) ) {
1176 0 : if( FD_LIKELY( http->pollfds[ i ].revents & POLLIN ) ) accept_conns( http );
1177 0 : } else {
1178 0 : if( FD_LIKELY( http->pollfds[ i ].revents & POLLIN ) ) read_conn( http, i );
1179 0 : if( FD_UNLIKELY( -1==http->pollfds[ i ].fd ) ) continue;
1180 0 : if( FD_LIKELY( http->pollfds[ i ].revents & POLLOUT ) ) write_conn( http, i );
1181 : /* No need to handle POLLHUP, read() will return 0 soon enough. */
1182 0 : }
1183 0 : }
1184 :
1185 0 : return 1;
1186 0 : }
1187 :
1188 : static void
1189 : fd_http_server_evict_until( fd_http_server_t * http,
1190 676995 : ulong off ) {
1191 676995 : conn_treap_fwd_iter_t next;
1192 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 ) {
1193 0 : next = conn_treap_fwd_iter_next( it, http->conns );
1194 0 : struct fd_http_server_connection * conn = conn_treap_fwd_iter_ele( it, http->conns );
1195 :
1196 0 : if( FD_UNLIKELY( conn->response._body_off<off ) ) {
1197 0 : close_conn( http, conn_treap_fwd_iter_idx( it ), FD_HTTP_SERVER_CONNECTION_CLOSE_EVICTED );
1198 0 : } else {
1199 0 : break;
1200 0 : }
1201 0 : }
1202 :
1203 676995 : ws_conn_treap_fwd_iter_t ws_next;
1204 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 ) {
1205 0 : ws_next = ws_conn_treap_fwd_iter_next( it, http->ws_conns );
1206 0 : struct fd_http_server_ws_connection * conn = ws_conn_treap_fwd_iter_ele( it, http->ws_conns );
1207 :
1208 0 : if( FD_UNLIKELY( conn->send_frames[ conn->send_frame_idx ].off<off ) ) {
1209 0 : close_conn( http, ws_conn_treap_fwd_iter_idx( it )+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
1210 0 : } else {
1211 0 : break;
1212 0 : }
1213 0 : }
1214 676995 : }
1215 :
1216 : static void
1217 : fd_http_server_reserve( fd_http_server_t * http,
1218 747654 : ulong len ) {
1219 : /* fd_http_server_reserve should not be called after
1220 : fd_http_server_compress */
1221 747654 : FD_TEST( http->stage_comp_len == 0 );
1222 :
1223 747654 : ulong remaining = http->oring_sz-((http->stage_off%http->oring_sz)+http->stage_len);
1224 747654 : if( FD_UNLIKELY( len>remaining ) ) {
1225 : /* Appending the format string into the hcache would go past the end
1226 : of the buffer... two cases, */
1227 81420 : if( FD_UNLIKELY( http->stage_len+len>http->oring_sz ) ) {
1228 : /* Case 1: The snap is going to be larger than the entire buffer,
1229 : there's no way to fit it even if we evict everything
1230 : else. Mark the hcache as errored and exit. */
1231 :
1232 70659 : FD_LOG_WARNING(( "tried to reserve %lu bytes for an outgoing message which exceeds the entire data size", http->stage_len+len ));
1233 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 ) ));
1234 70659 : FD_LOG_HEXDUMP_WARNING(( "start of buffer:\n%.*s", http->oring, fd_ulong_min( 500UL, http->oring_sz ) ));
1235 70659 : http->stage_err = 1;
1236 70659 : return;
1237 70659 : } else {
1238 : /* Case 2: The snap can fit if we relocate it to the start of the
1239 : buffer and evict whatever was there. We also evict the
1240 : rest of the buffer behind where the snap was to
1241 : preserve the invariant that snaps are always evicted in
1242 : circular order. */
1243 :
1244 10761 : ulong stage_end = http->stage_off+remaining+http->stage_len+len;
1245 10761 : ulong clamp = fd_ulong_if( stage_end>=http->oring_sz, stage_end-http->oring_sz, 0UL );
1246 10761 : fd_http_server_evict_until( http, clamp );
1247 10761 : memmove( http->oring, http->oring+(http->stage_off%http->oring_sz), http->stage_len );
1248 10761 : http->stage_off += http->stage_len+remaining;
1249 10761 : }
1250 666234 : } else {
1251 : /* The snap can fit in the buffer, we just need to evict whatever
1252 : was there before. */
1253 666234 : ulong stage_end = http->stage_off+http->stage_len+len;
1254 666234 : ulong clamp = fd_ulong_if( stage_end>=http->oring_sz, stage_end-http->oring_sz, 0UL );
1255 666234 : fd_http_server_evict_until( http, clamp );
1256 666234 : }
1257 747654 : }
1258 :
1259 : static int
1260 0 : fd_http_ws_compress_maybe( fd_http_server_t * http ) {
1261 : /* we don't compress if the message is small, or if compression is
1262 : disabled in the config */
1263 0 : if( FD_LIKELY( !http->compress_websocket || http->stage_len <= 200 || http->stage_err ) ) return 0;
1264 :
1265 0 : #if FD_HAS_ZSTD
1266 0 : ulong worst_case_compressed_sz = ZSTD_compressBound( http->stage_len );
1267 0 : fd_http_server_reserve( http, worst_case_compressed_sz );
1268 :
1269 0 : if( FD_UNLIKELY( http->stage_err ) ) return 0;
1270 :
1271 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 );
1272 0 : if( FD_UNLIKELY( ZSTD_isError( compressed_sz ) ) ) {
1273 0 : FD_LOG_WARNING(( "ZSTD_compress2 failed (%s)", ZSTD_getErrorName( compressed_sz ) ) );
1274 0 : http->stage_err = 1;
1275 0 : return 0;
1276 0 : }
1277 0 : FD_TEST( compressed_sz <= worst_case_compressed_sz );
1278 :
1279 0 : http->stage_comp_len = compressed_sz;
1280 :
1281 0 : return 1;
1282 : #else
1283 : return 0;
1284 : #endif
1285 0 : }
1286 :
1287 : int
1288 : fd_http_server_ws_send( fd_http_server_t * http,
1289 0 : ulong ws_conn_id ) {
1290 0 : struct fd_http_server_ws_connection * conn = &http->ws_conns[ ws_conn_id ];
1291 0 : int compressed = conn->compress_websocket;
1292 0 : if( FD_LIKELY( compressed ) ) compressed = fd_http_ws_compress_maybe( http );
1293 :
1294 :
1295 0 : if( FD_UNLIKELY( http->stage_err ) ) {
1296 0 : http->stage_err = 0;
1297 0 : http->stage_len = 0;
1298 0 : http->stage_comp_len = 0;
1299 0 : return -1;
1300 0 : }
1301 :
1302 : /* It is possible that ws_conn_id has already been closed by
1303 : fd_http_server_reserve during staging. If the staging buffer is
1304 : full, the incoming frame is added to the beginning of the buffer,
1305 : and any connections that were previously using that allotted space
1306 : are closed. There is a small chance that ws_conn_id is one of
1307 : those connections, and has therefore already been closed. */
1308 0 : if( FD_LIKELY( http->pollfds[ http->max_conns+ws_conn_id ].fd==-1 ) ) {
1309 0 : http->stage_len = 0;
1310 0 : return 0;
1311 0 : }
1312 :
1313 0 : if( FD_UNLIKELY( conn->send_frame_cnt==http->max_ws_send_frame_cnt ) ) {
1314 0 : close_conn( http, ws_conn_id+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
1315 0 : http->stage_len = 0;
1316 0 : return 0;
1317 0 : }
1318 :
1319 : /* A frame is compressed only if the connection is configured to do
1320 : so, and if the compression step wasn't skipped (i.e. stage_len>200) */
1321 0 : fd_http_server_ws_frame_t frame = {
1322 0 : .off = fd_ulong_if(compressed, http->stage_off+http->stage_len, http->stage_off),
1323 0 : .len = fd_ulong_if(compressed, http->stage_comp_len, http->stage_len),
1324 0 : .compressed = compressed,
1325 0 : };
1326 :
1327 0 : conn->send_frames[ (conn->send_frame_idx+conn->send_frame_cnt) % http->max_ws_send_frame_cnt ] = frame;
1328 0 : conn->send_frame_cnt++;
1329 :
1330 0 : if( FD_LIKELY( conn->send_frame_cnt==1UL ) ) {
1331 0 : ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
1332 0 : }
1333 :
1334 0 : http->stage_off += http->stage_len+http->stage_comp_len;
1335 0 : http->stage_len = 0;
1336 0 : http->stage_comp_len = 0;
1337 :
1338 0 : return 0;
1339 0 : }
1340 :
1341 : int
1342 0 : fd_http_server_ws_broadcast( fd_http_server_t * http ) {
1343 0 : int compressed = fd_http_ws_compress_maybe( http );
1344 :
1345 0 : if( FD_UNLIKELY( http->stage_err ) ) {
1346 0 : http->stage_err = 0;
1347 0 : http->stage_len = 0;
1348 0 : http->stage_comp_len = 0;
1349 0 : return -1;
1350 0 : }
1351 :
1352 0 : for( ulong i=0UL; i<http->max_ws_conns; i++ ) {
1353 0 : if( FD_LIKELY( http->pollfds[ http->max_conns+i ].fd==-1 ) ) continue;
1354 :
1355 0 : struct fd_http_server_ws_connection * conn = &http->ws_conns[ i ];
1356 0 : if( FD_UNLIKELY( conn->send_frame_cnt==http->max_ws_send_frame_cnt ) ) {
1357 0 : close_conn( http, i+http->max_conns, FD_HTTP_SERVER_CONNECTION_CLOSE_WS_CLIENT_TOO_SLOW );
1358 0 : continue;
1359 0 : }
1360 :
1361 0 : fd_http_server_ws_frame_t frame = {
1362 0 : .off = fd_ulong_if(conn->compress_websocket && compressed, http->stage_off+http->stage_len, http->stage_off),
1363 0 : .len = fd_ulong_if(conn->compress_websocket && compressed, http->stage_comp_len, http->stage_len),
1364 0 : .compressed = conn->compress_websocket && compressed,
1365 0 : };
1366 :
1367 0 : conn->send_frames[ (conn->send_frame_idx+conn->send_frame_cnt) % http->max_ws_send_frame_cnt ] = frame;
1368 0 : conn->send_frame_cnt++;
1369 :
1370 0 : if( FD_LIKELY( conn->send_frame_cnt==1UL ) ) {
1371 0 : ws_conn_treap_ele_insert( http->ws_conn_treap, conn, http->ws_conns );
1372 0 : }
1373 0 : }
1374 :
1375 0 : http->stage_off += http->stage_len+http->stage_comp_len;
1376 0 : http->stage_len = 0;
1377 0 : http->stage_comp_len = 0;
1378 :
1379 0 : return 0;
1380 0 : }
1381 :
1382 : void
1383 : fd_http_server_stage_trunc( fd_http_server_t * http,
1384 0 : ulong len ) {
1385 0 : http->stage_comp_len = 0;
1386 0 : http->stage_len = len;
1387 0 : }
1388 :
1389 : ulong
1390 0 : fd_http_server_stage_len( fd_http_server_t * http ) {
1391 0 : return http->stage_len;
1392 0 : }
1393 :
1394 : void
1395 : fd_http_server_printf( fd_http_server_t * http,
1396 : char const * fmt,
1397 1524870 : ... ) {
1398 1524870 : if( FD_UNLIKELY( http->stage_err ) ) return;
1399 :
1400 747654 : va_list ap;
1401 747654 : va_start( ap, fmt );
1402 747654 : ulong printed_len = (ulong)vsnprintf( NULL, 0UL, fmt, ap );
1403 747654 : va_end( ap );
1404 :
1405 747654 : fd_http_server_reserve( http, printed_len );
1406 747654 : if( FD_UNLIKELY( http->stage_err ) ) return;
1407 :
1408 747654 : va_start( ap, fmt );
1409 676995 : vsnprintf( (char *)http->oring+(http->stage_off%http->oring_sz)+http->stage_len,
1410 676995 : INT_MAX, /* We already proved it's going to fit above */
1411 676995 : fmt,
1412 676995 : ap );
1413 676995 : va_end( ap );
1414 :
1415 676995 : http->stage_len += printed_len;
1416 676995 : }
1417 :
1418 : void
1419 : fd_http_server_memcpy( fd_http_server_t * http,
1420 : uchar const * data,
1421 0 : ulong data_len ) {
1422 0 : fd_http_server_reserve( http, data_len );
1423 0 : if( FD_UNLIKELY( http->stage_err ) ) return;
1424 :
1425 0 : fd_memcpy( (char *)http->oring+(http->stage_off%http->oring_sz)+http->stage_len,
1426 0 : data,
1427 0 : data_len );
1428 0 : http->stage_len += data_len;
1429 0 : }
1430 :
1431 : void
1432 6 : fd_http_server_unstage( fd_http_server_t * http ) {
1433 6 : http->stage_err = 0;
1434 6 : http->stage_len = 0UL;
1435 6 : http->stage_comp_len = 0UL;
1436 6 : }
1437 :
1438 : int
1439 : fd_http_server_stage_body( fd_http_server_t * http,
1440 95244 : fd_http_server_response_t * response ) {
1441 95244 : if( FD_UNLIKELY( http->stage_err ) ) {
1442 70659 : http->stage_err = 0;
1443 70659 : http->stage_len = 0;
1444 70659 : return -1;
1445 70659 : }
1446 :
1447 24585 : response->_body_off = http->stage_off;
1448 24585 : response->_body_len = http->stage_len;
1449 24585 : http->stage_off += http->stage_len;
1450 24585 : http->stage_len = 0;
1451 24585 : return 0;
1452 95244 : }
|