3131#include "log/log.h"
3232#include "recv.h"
3333
34- bool
35- event_recv (struct fh_server * server , const xevent_t * event )
34+ static bool
35+ event_recv_h2 (struct fh_server * server , struct fh_conn * conn ,
36+ char * proto_det_buf , size_t proto_det_off )
3637{
37- struct fh_conn * conn = itable_get (server -> connections , event -> data .fd );
38+ (void ) proto_det_buf ;
39+ (void ) proto_det_off ;
3840
39- if (!conn )
40- {
41- fh_pr_err ("Socket %d does not have an associated connection object" , event -> data .fd );
42- xpoll_del (server -> xpoll_fd , event -> data .fd , XPOLLIN );
43- close (event -> data .fd );
44- return false;
45- }
41+ fh_pr_err ("Not implemented: h2" );
42+ fh_server_close_conn (server , conn );
43+ return false;
44+ }
4645
47- fh_pr_info ("connection %lu: recv called" , conn -> id );
46+ static bool
47+ event_recv_http1 (struct fh_server * server , struct fh_conn * conn ,
48+ char * proto_det_buf , size_t proto_det_off )
49+ {
4850 pool_t * child_pool = NULL ;
4951
5052 if (!conn -> io_ctx .h1 .req_ctx )
@@ -59,24 +61,40 @@ event_recv (struct fh_server *server, const xevent_t *event)
5961 }
6062
6163 fh_stream_init (conn -> stream , child_pool );
64+
65+ if (fh_stream_add_buf_data (conn -> stream , (uint8_t * ) proto_det_buf ,
66+ proto_det_off , proto_det_off )
67+ && conn -> stream -> head )
68+ {
69+ conn -> stream -> head -> buf -> attrs .mem .rd_only = true;
70+ }
6271 }
6372
64- struct fh_http1_req_ctx * ctx = conn -> io_ctx .h1 .req_ctx ? conn -> io_ctx .h1 .req_ctx : fh_http1_ctx_create (server , conn , conn -> stream );
73+ struct fh_http1_req_ctx * ctx
74+ = conn -> io_ctx .h1 .req_ctx
75+ ? conn -> io_ctx .h1 .req_ctx
76+ : fh_http1_ctx_create (server , conn , conn -> stream );
6577
6678 if (!conn -> io_ctx .h1 .req_ctx )
79+ {
6780 conn -> io_ctx .h1 .req_ctx = ctx ;
81+ ctx -> cur .link = ctx -> arg_cur .link = conn -> stream -> head ;
82+ ctx -> cur .off = ctx -> arg_cur .off = 0 ;
83+ conn -> stream -> head -> is_start = true;
84+ }
6885
6986 if (!fh_http1_parse (ctx , conn ))
7087 {
7188 if (ctx -> state == H1_REQ_STATE_ERROR )
7289 {
7390 fh_pr_err ("HTTP/1.x parsing failed" );
74- fh_conn_send_err_response (conn , ctx -> suggested_code == 0 ? 500 : ctx -> suggested_code );
91+ fh_conn_send_err_response (
92+ conn , ctx -> suggested_code == 0 ? 500 : ctx -> suggested_code );
7593 fh_server_close_conn (server , conn );
7694
7795 if (child_pool )
7896 fh_pool_destroy (child_pool );
79-
97+
8098 return true;
8199 }
82100
@@ -91,8 +109,10 @@ event_recv (struct fh_server *server, const xevent_t *event)
91109 fh_conn_push_request (conn -> requests , request );
92110
93111 fh_pr_info ("Method: |%s|" , fh_method_to_string (ctx -> request .method ));
94- fh_pr_info ("URI: |%.*s|" , (int ) ctx -> request .uri_len , ctx -> request .uri );
95- fh_pr_info ("Protocol: %s" , fh_protocol_to_string (ctx -> request .protocol ));
112+ fh_pr_info ("URI: |%.*s|" , (int ) ctx -> request .uri_len ,
113+ ctx -> request .uri );
114+ fh_pr_info ("Protocol: %s" ,
115+ fh_protocol_to_string (ctx -> request .protocol ));
96116
97117 if (!xpoll_mod (server -> xpoll_fd , conn -> client_sockfd , XPOLLOUT ))
98118 {
@@ -107,3 +127,67 @@ event_recv (struct fh_server *server, const xevent_t *event)
107127
108128 return true;
109129}
130+
131+ bool
132+ event_recv (struct fh_server * server , const xevent_t * event )
133+ {
134+ struct fh_conn * conn = itable_get (server -> connections , event -> data .fd );
135+
136+ if (!conn )
137+ {
138+ fh_pr_err ("Socket %d does not have an associated connection object" ,
139+ event -> data .fd );
140+ xpoll_del (server -> xpoll_fd , event -> data .fd , XPOLLIN );
141+ close (event -> data .fd );
142+ return false;
143+ }
144+
145+ fh_pr_info ("connection %lu: recv called" , conn -> id );
146+
147+ char * proto_det_buf = NULL ;
148+ size_t proto_det_off = 0 ;
149+
150+ if (conn -> protocol == FH_PROTOCOL_UNKNOWN )
151+ {
152+ /* Detect the connection protocol */
153+
154+ int rc = fh_conn_detect_protocol (conn );
155+
156+ switch (rc )
157+ {
158+ case 0 :
159+ fh_pr_debug ("EAGAIN while detecting protocol" );
160+ return true;
161+
162+ case -1 :
163+ fh_pr_debug ("I/O error while detecting protocol" );
164+ fh_server_close_conn (server , conn );
165+ return true;
166+ }
167+
168+ proto_det_buf = conn -> io_ctx .proto_det_buf .buf ;
169+ proto_det_off = conn -> io_ctx .proto_det_buf .off ;
170+
171+ conn -> io_ctx .h1 .req_ctx = NULL ;
172+ conn -> io_ctx .h1 .res_ctx = NULL ;
173+
174+ fh_pr_debug ("Detected protocol: %s" ,
175+ fh_protocol_to_string (conn -> protocol ));
176+ }
177+
178+ switch (conn -> protocol )
179+ {
180+ case FH_PROTOCOL_HTTP_1_0 :
181+ case FH_PROTOCOL_HTTP_1_1 :
182+ return event_recv_http1 (server , conn , proto_det_buf ,
183+ proto_det_off );
184+
185+ case FH_PROTOCOL_H2 :
186+ return event_recv_h2 (server , conn , proto_det_buf , proto_det_off );
187+
188+ default :
189+ fh_pr_debug ("Unsupported protocol" );
190+ fh_server_close_conn (server , conn );
191+ return true;
192+ }
193+ }
0 commit comments