Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FastCGI mux-demux for connections |
| 3 | * |
| 4 | * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
| 15 | #include <common/fcgi.h> |
| 16 | #include <common/h1.h> |
| 17 | #include <common/htx.h> |
| 18 | #include <common/initcall.h> |
| 19 | #include <common/ist.h> |
| 20 | #include <common/mini-clist.h> |
| 21 | #include <common/net_helper.h> |
| 22 | |
| 23 | #include <types/proxy.h> |
| 24 | #include <types/session.h> |
| 25 | |
| 26 | #include <proto/connection.h> |
| 27 | #include <proto/fcgi-app.h> |
| 28 | #include <proto/h1_htx.h> |
| 29 | #include <proto/http_htx.h> |
| 30 | #include <proto/log.h> |
| 31 | #include <proto/session.h> |
| 32 | #include <proto/ssl_sock.h> |
| 33 | #include <proto/stream.h> |
| 34 | #include <proto/stream_interface.h> |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 35 | #include <proto/trace.h> |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 36 | |
| 37 | /* FCGI Connection flags (32 bits) */ |
| 38 | #define FCGI_CF_NONE 0x00000000 |
| 39 | |
| 40 | /* Flags indicating why writing to the mux is blockes */ |
| 41 | #define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */ |
| 42 | #define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */ |
| 43 | #define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */ |
| 44 | |
| 45 | /* Flags indicating why writing to the demux is blocked. |
| 46 | * The first two ones directly affect the ability for the mux to receive data |
| 47 | * from the connection. The other ones affect the mux's ability to demux |
| 48 | * received data. |
| 49 | */ |
| 50 | #define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */ |
| 51 | #define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */ |
| 52 | #define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */ |
| 53 | #define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */ |
| 54 | #define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */ |
| 55 | #define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */ |
| 56 | #define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */ |
| 57 | |
| 58 | /* Other flags */ |
| 59 | #define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */ |
| 60 | #define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */ |
| 61 | #define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */ |
| 62 | #define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */ |
| 63 | #define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */ |
| 64 | #define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */ |
| 65 | |
| 66 | /* FCGI connection state (fcgi_conn->state) */ |
| 67 | enum fcgi_conn_st { |
| 68 | FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */ |
| 69 | FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */ |
| 70 | FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */ |
| 71 | FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */ |
| 72 | FCGI_CS_RECORD_P, /* Record processed, remains the padding */ |
| 73 | FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */ |
| 74 | FCGI_CS_ENTRIES |
| 75 | } __attribute__((packed)); |
| 76 | |
| 77 | /* 32 buffers: one for the ring's root, rest for the mbuf itself */ |
| 78 | #define FCGI_C_MBUF_CNT 32 |
| 79 | |
| 80 | /* FCGI connection descriptor */ |
| 81 | struct fcgi_conn { |
| 82 | struct connection *conn; |
| 83 | |
| 84 | enum fcgi_conn_st state; /* FCGI connection state */ |
| 85 | int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */ |
| 86 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
| 87 | uint32_t flags; /* Connection flags: FCGI_CF_* */ |
| 88 | |
| 89 | int16_t dsi; /* dmux stream ID (<0 = idle ) */ |
| 90 | uint16_t drl; /* demux record length (if dsi >= 0) */ |
| 91 | uint8_t drt; /* demux record type (if dsi >= 0) */ |
| 92 | uint8_t drp; /* demux record padding (if dsi >= 0) */ |
| 93 | |
| 94 | struct buffer dbuf; /* demux buffer */ |
| 95 | struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */ |
| 96 | |
| 97 | int timeout; /* idle timeout duration in ticks */ |
| 98 | int shut_timeout; /* idle timeout duration in ticks after shutdown */ |
| 99 | unsigned int nb_streams; /* number of streams in the tree */ |
| 100 | unsigned int nb_cs; /* number of attached conn_streams */ |
| 101 | unsigned int nb_reserved; /* number of reserved streams */ |
| 102 | unsigned int stream_cnt; /* total number of streams seen */ |
| 103 | |
| 104 | struct proxy *proxy; /* the proxy this connection was created for */ |
| 105 | struct fcgi_app *app; /* FCGI application used by this mux */ |
| 106 | struct task *task; /* timeout management task */ |
| 107 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 108 | |
| 109 | struct list send_list; /* list of blocked streams requesting to send */ |
| 110 | struct list sending_list; /* list of fcgi_strm scheduled to send data */ |
| 111 | |
| 112 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 113 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | /* FCGI stream state, in fcgi_strm->state */ |
| 118 | enum fcgi_strm_st { |
| 119 | FCGI_SS_IDLE = 0, |
| 120 | FCGI_SS_OPEN, |
| 121 | FCGI_SS_HREM, // half-closed(remote) |
| 122 | FCGI_SS_HLOC, // half-closed(local) |
| 123 | FCGI_SS_ERROR, |
| 124 | FCGI_SS_CLOSED, |
| 125 | FCGI_SS_ENTRIES |
| 126 | } __attribute__((packed)); |
| 127 | |
| 128 | |
| 129 | /* FCGI stream flags (32 bits) */ |
| 130 | #define FCGI_SF_NONE 0x00000000 |
| 131 | #define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */ |
| 132 | #define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */ |
| 133 | #define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */ |
| 134 | |
| 135 | /* Stream flags indicating the reason the stream is blocked */ |
| 136 | #define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */ |
| 137 | #define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */ |
| 138 | #define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */ |
| 139 | |
| 140 | #define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */ |
| 141 | #define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */ |
| 142 | |
| 143 | #define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */ |
| 144 | #define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */ |
| 145 | #define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */ |
| 146 | |
| 147 | /* Other flags */ |
| 148 | #define FCGI_SF_HAVE_I_TLR 0x00010000 /* Set during input process to know the trailers were processed */ |
| 149 | |
| 150 | /* FCGI stream descriptor */ |
| 151 | struct fcgi_strm { |
| 152 | struct conn_stream *cs; |
| 153 | struct session *sess; |
| 154 | struct fcgi_conn *fconn; |
| 155 | |
| 156 | int32_t id; /* stream ID */ |
| 157 | |
| 158 | uint32_t flags; /* Connection flags: FCGI_SF_* */ |
| 159 | enum fcgi_strm_st state; /* FCGI stream state */ |
| 160 | int proto_status; /* FCGI_PS_* */ |
| 161 | |
| 162 | struct h1m h1m; /* response parser state for H1 */ |
| 163 | |
| 164 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
| 165 | |
| 166 | struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */ |
| 167 | struct wait_event wait_event; /* Wait list, when we're attempting to send an ABORT but we can't send */ |
| 168 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 169 | struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 170 | struct list send_list; /* To be used when adding in fcgi_conn->send_list */ |
| 171 | struct list sending_list; /* To be used when adding in fcgi_conn->sending_list */ |
| 172 | }; |
| 173 | |
| 174 | /* Flags representing all default FCGI parameters */ |
| 175 | #define FCGI_SP_CGI_GATEWAY 0x00000001 |
| 176 | #define FCGI_SP_DOC_ROOT 0x00000002 |
| 177 | #define FCGI_SP_SCRIPT_NAME 0x00000004 |
| 178 | #define FCGI_SP_PATH_INFO 0x00000008 |
| 179 | #define FCGI_SP_REQ_URI 0x00000010 |
| 180 | #define FCGI_SP_REQ_METH 0x00000020 |
| 181 | #define FCGI_SP_REQ_QS 0x00000040 |
| 182 | #define FCGI_SP_SRV_PORT 0x00000080 |
| 183 | #define FCGI_SP_SRV_PROTO 0x00000100 |
| 184 | #define FCGI_SP_SRV_NAME 0x00000200 |
| 185 | #define FCGI_SP_REM_ADDR 0x00000400 |
| 186 | #define FCGI_SP_REM_PORT 0x00000800 |
| 187 | #define FCGI_SP_SCRIPT_FILE 0x00001000 |
| 188 | #define FCGI_SP_PATH_TRANS 0x00002000 |
| 189 | #define FCGI_SP_CONT_LEN 0x00004000 |
| 190 | #define FCGI_SP_HTTPS 0x00008000 |
| 191 | #define FCGI_SP_MASK 0x0000FFFF |
| 192 | #define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS) |
| 193 | |
| 194 | /* FCGI parameters used when PARAMS record is sent */ |
| 195 | struct fcgi_strm_params { |
| 196 | uint32_t mask; |
| 197 | struct ist docroot; |
| 198 | struct ist scriptname; |
| 199 | struct ist pathinfo; |
| 200 | struct ist meth; |
| 201 | struct ist uri; |
| 202 | struct ist vsn; |
| 203 | struct ist qs; |
| 204 | struct ist srv_name; |
| 205 | struct ist srv_port; |
| 206 | struct ist rem_addr; |
| 207 | struct ist rem_port; |
| 208 | struct ist cont_len; |
| 209 | int https; |
| 210 | struct buffer *p; |
| 211 | }; |
| 212 | |
| 213 | /* Maximum amount of data we're OK with re-aligning for buffer optimizations */ |
| 214 | #define MAX_DATA_REALIGN 1024 |
| 215 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 216 | /* trace source and events */ |
| 217 | static void fcgi_trace(enum trace_level level, uint64_t mask, |
| 218 | const struct trace_source *src, |
| 219 | const struct ist where, const struct ist func, |
| 220 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 221 | |
| 222 | /* The event representation is split like this : |
| 223 | * fconn - internal FCGI connection |
| 224 | * fstrm - internal FCGI stream |
| 225 | * strm - application layer |
| 226 | * rx - data receipt |
| 227 | * tx - data transmission |
| 228 | * rsp - response parsing |
| 229 | */ |
| 230 | static const struct trace_event fcgi_trace_events[] = { |
| 231 | #define FCGI_EV_FCONN_NEW (1ULL << 0) |
| 232 | { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" }, |
| 233 | #define FCGI_EV_FCONN_RECV (1ULL << 1) |
| 234 | { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" }, |
| 235 | #define FCGI_EV_FCONN_SEND (1ULL << 2) |
| 236 | { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" }, |
| 237 | #define FCGI_EV_FCONN_BLK (1ULL << 3) |
| 238 | { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" }, |
| 239 | #define FCGI_EV_FCONN_WAKE (1ULL << 4) |
| 240 | { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" }, |
| 241 | #define FCGI_EV_FCONN_END (1ULL << 5) |
| 242 | { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" }, |
| 243 | #define FCGI_EV_FCONN_ERR (1ULL << 6) |
| 244 | { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" }, |
| 245 | |
| 246 | #define FCGI_EV_RX_FHDR (1ULL << 7) |
| 247 | { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" }, |
| 248 | #define FCGI_EV_RX_RECORD (1ULL << 8) |
| 249 | { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" }, |
| 250 | #define FCGI_EV_RX_EOI (1ULL << 9) |
| 251 | { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" }, |
| 252 | #define FCGI_EV_RX_GETVAL (1ULL << 10) |
| 253 | { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" }, |
| 254 | #define FCGI_EV_RX_STDOUT (1ULL << 11) |
| 255 | { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" }, |
| 256 | #define FCGI_EV_RX_STDERR (1ULL << 12) |
| 257 | { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" }, |
| 258 | #define FCGI_EV_RX_ENDREQ (1ULL << 13) |
| 259 | { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" }, |
| 260 | |
| 261 | #define FCGI_EV_TX_RECORD (1ULL << 14) |
| 262 | { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" }, |
| 263 | #define FCGI_EV_TX_EOI (1ULL << 15) |
| 264 | { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" }, |
| 265 | #define FCGI_EV_TX_BEGREQ (1ULL << 16) |
| 266 | { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" }, |
| 267 | #define FCGI_EV_TX_GETVAL (1ULL << 17) |
| 268 | { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" }, |
| 269 | #define FCGI_EV_TX_PARAMS (1ULL << 18) |
| 270 | { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" }, |
| 271 | #define FCGI_EV_TX_STDIN (1ULL << 19) |
| 272 | { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" }, |
| 273 | #define FCGI_EV_TX_ABORT (1ULL << 20) |
| 274 | { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" }, |
| 275 | |
| 276 | #define FCGI_EV_RSP_DATA (1ULL << 21) |
| 277 | { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" }, |
| 278 | #define FCGI_EV_RSP_EOM (1ULL << 22) |
| 279 | { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" }, |
| 280 | #define FCGI_EV_RSP_HDRS (1ULL << 23) |
| 281 | { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" }, |
| 282 | #define FCGI_EV_RSP_BODY (1ULL << 24) |
| 283 | { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" }, |
| 284 | #define FCGI_EV_RSP_TLRS (1ULL << 25) |
| 285 | { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" }, |
| 286 | |
| 287 | #define FCGI_EV_FSTRM_NEW (1ULL << 26) |
| 288 | { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" }, |
| 289 | #define FCGI_EV_FSTRM_BLK (1ULL << 27) |
| 290 | { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" }, |
| 291 | #define FCGI_EV_FSTRM_END (1ULL << 28) |
| 292 | { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" }, |
| 293 | #define FCGI_EV_FSTRM_ERR (1ULL << 29) |
| 294 | { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" }, |
| 295 | |
| 296 | #define FCGI_EV_STRM_NEW (1ULL << 30) |
| 297 | { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 298 | #define FCGI_EV_STRM_RECV (1ULL << 31) |
| 299 | { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 300 | #define FCGI_EV_STRM_SEND (1ULL << 32) |
| 301 | { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 302 | #define FCGI_EV_STRM_FULL (1ULL << 33) |
| 303 | { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" }, |
| 304 | #define FCGI_EV_STRM_WAKE (1ULL << 34) |
| 305 | { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" }, |
| 306 | #define FCGI_EV_STRM_SHUT (1ULL << 35) |
| 307 | { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 308 | #define FCGI_EV_STRM_END (1ULL << 36) |
| 309 | { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 310 | #define FCGI_EV_STRM_ERR (1ULL << 37) |
| 311 | { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 312 | |
| 313 | { } |
| 314 | }; |
| 315 | |
| 316 | static const struct name_desc fcgi_trace_lockon_args[4] = { |
| 317 | /* arg1 */ { /* already used by the connection */ }, |
| 318 | /* arg2 */ { .name="fstrm", .desc="FCGI stream" }, |
| 319 | /* arg3 */ { }, |
| 320 | /* arg4 */ { } |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | static const struct name_desc fcgi_trace_decoding[] = { |
| 325 | #define FCGI_VERB_CLEAN 1 |
| 326 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 327 | #define FCGI_VERB_MINIMAL 2 |
| 328 | { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" }, |
| 329 | #define FCGI_VERB_SIMPLE 3 |
| 330 | { .name="simple", .desc="add request/response status line or htx info when available" }, |
| 331 | #define FCGI_VERB_ADVANCED 4 |
| 332 | { .name="advanced", .desc="add header fields or record decoding when available" }, |
| 333 | #define FCGI_VERB_COMPLETE 5 |
| 334 | { .name="complete", .desc="add full data dump when available" }, |
| 335 | { /* end */ } |
| 336 | }; |
| 337 | |
| 338 | static struct trace_source trace_fcgi = { |
| 339 | .name = IST("fcgi"), |
| 340 | .desc = "FastCGI multiplexer", |
| 341 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 342 | .default_cb = fcgi_trace, |
| 343 | .known_events = fcgi_trace_events, |
| 344 | .lockon_args = fcgi_trace_lockon_args, |
| 345 | .decoding = fcgi_trace_decoding, |
| 346 | .report_events = ~0, // report everything by default |
| 347 | }; |
| 348 | |
| 349 | #define TRACE_SOURCE &trace_fcgi |
| 350 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 351 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 352 | /* FCGI connection and stream pools */ |
| 353 | DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn)); |
| 354 | DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm)); |
| 355 | |
| 356 | static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state); |
| 357 | static int fcgi_process(struct fcgi_conn *fconn); |
| 358 | static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state); |
| 359 | static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id); |
| 360 | static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state); |
| 361 | static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 362 | static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm); |
| 363 | static void fcgi_strm_notify_send(struct fcgi_strm *fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 364 | static void fcgi_strm_alert(struct fcgi_strm *fstrm); |
| 365 | static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm); |
| 366 | |
| 367 | /* a dmumy management stream */ |
| 368 | static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){ |
| 369 | .cs = NULL, |
| 370 | .fconn = NULL, |
| 371 | .state = FCGI_SS_CLOSED, |
| 372 | .flags = FCGI_SF_NONE, |
| 373 | .id = 0, |
| 374 | }; |
| 375 | |
| 376 | /* and a dummy idle stream for use with any unknown stream */ |
| 377 | static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){ |
| 378 | .cs = NULL, |
| 379 | .fconn = NULL, |
| 380 | .state = FCGI_SS_IDLE, |
| 381 | .flags = FCGI_SF_NONE, |
| 382 | .id = 0, |
| 383 | }; |
| 384 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 385 | /* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */ |
| 386 | static inline const char *fconn_st_to_str(enum fcgi_conn_st st) |
| 387 | { |
| 388 | switch (st) { |
| 389 | case FCGI_CS_INIT : return "INI"; |
| 390 | case FCGI_CS_SETTINGS : return "STG"; |
| 391 | case FCGI_CS_RECORD_H : return "RDH"; |
| 392 | case FCGI_CS_RECORD_D : return "RDD"; |
| 393 | case FCGI_CS_RECORD_P : return "RDP"; |
| 394 | case FCGI_CS_CLOSED : return "CLO"; |
| 395 | default : return "???"; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */ |
| 400 | static inline const char *fstrm_st_to_str(enum fcgi_strm_st st) |
| 401 | { |
| 402 | switch (st) { |
| 403 | case FCGI_SS_IDLE : return "IDL"; |
| 404 | case FCGI_SS_OPEN : return "OPN"; |
| 405 | case FCGI_SS_HREM : return "RCL"; |
| 406 | case FCGI_SS_HLOC : return "HCL"; |
| 407 | case FCGI_SS_ERROR : return "ERR"; |
| 408 | case FCGI_SS_CLOSED : return "CLO"; |
| 409 | default : return "???"; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
| 414 | /* the FCGI traces always expect that arg1, if non-null, is of type connection |
| 415 | * (from which we can derive fconn), that arg2, if non-null, is of type fstrm, |
| 416 | * and that arg3, if non-null, is a htx for rx/tx headers. |
| 417 | */ |
| 418 | static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 419 | const struct ist where, const struct ist func, |
| 420 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 421 | { |
| 422 | const struct connection *conn = a1; |
| 423 | const struct fcgi_conn *fconn = conn ? conn->ctx : NULL; |
| 424 | const struct fcgi_strm *fstrm = a2; |
| 425 | const struct htx *htx = a3; |
| 426 | const size_t *val = a4; |
| 427 | |
| 428 | if (!fconn) |
| 429 | fconn = (fstrm ? fstrm->fconn : NULL); |
| 430 | |
| 431 | if (!fconn || src->verbosity < FCGI_VERB_CLEAN) |
| 432 | return; |
| 433 | |
| 434 | /* Display the response state if fstrm is defined */ |
| 435 | if (fstrm) |
| 436 | chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state)); |
| 437 | |
| 438 | if (src->verbosity == FCGI_VERB_CLEAN) |
| 439 | return; |
| 440 | |
| 441 | /* Display the value to the 4th argument (level > STATE) */ |
| 442 | if (src->level > TRACE_LEVEL_STATE && val) |
| 443 | chunk_appendf(&trace_buf, " - VAL=%lu", *val); |
| 444 | |
| 445 | /* Display status-line if possible (verbosity > MINIMAL) */ |
| 446 | if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) { |
| 447 | const struct htx_blk *blk = htx_get_head_blk(htx); |
| 448 | const struct htx_sl *sl = htx_get_blk_ptr(htx, blk); |
| 449 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 450 | |
| 451 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) |
| 452 | chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"", |
| 453 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 454 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 455 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
| 456 | } |
| 457 | |
| 458 | /* Display fconn info and, if defined, fstrm info */ |
| 459 | chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags); |
| 460 | if (fstrm) |
| 461 | chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags); |
| 462 | |
| 463 | if (!fstrm || fstrm->id <= 0) |
| 464 | chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi); |
| 465 | if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR)) |
| 466 | chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt)); |
| 467 | |
| 468 | if (src->verbosity == FCGI_VERB_MINIMAL) |
| 469 | return; |
| 470 | |
| 471 | /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */ |
| 472 | if (src->level > TRACE_LEVEL_USER) { |
| 473 | if (src->verbosity == FCGI_VERB_COMPLETE || |
| 474 | (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD)))) |
| 475 | chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u", |
| 476 | (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf), |
| 477 | (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf)); |
| 478 | if (src->verbosity == FCGI_VERB_COMPLETE || |
| 479 | (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) { |
| 480 | struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf); |
| 481 | struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf); |
| 482 | |
| 483 | chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
| 484 | br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf), |
| 485 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 486 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
| 487 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 488 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
| 489 | } |
| 490 | |
| 491 | if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE || |
| 492 | (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA))))) |
| 493 | chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u", |
| 494 | (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf), |
| 495 | (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf)); |
| 496 | } |
| 497 | |
| 498 | /* Display htx info if defined (level > USER) */ |
| 499 | if (src->level > TRACE_LEVEL_USER && htx) { |
| 500 | int full = 0; |
| 501 | |
| 502 | /* Full htx info (level > STATE && verbosity > SIMPLE) */ |
| 503 | if (src->level > TRACE_LEVEL_STATE) { |
| 504 | if (src->verbosity == FCGI_VERB_COMPLETE) |
| 505 | full = 1; |
| 506 | else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS))) |
| 507 | full = 1; |
| 508 | } |
| 509 | |
| 510 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 511 | htx_dump(&trace_buf, htx, full); |
| 512 | } |
| 513 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 514 | |
| 515 | /*****************************************************/ |
| 516 | /* functions below are for dynamic buffer management */ |
| 517 | /*****************************************************/ |
| 518 | |
| 519 | /* Indicates whether or not the we may call the fcgi_recv() function to attempt |
| 520 | * to receive data into the buffer and/or demux pending data. The condition is |
| 521 | * a bit complex due to some API limits for now. The rules are the following : |
| 522 | * - if an error or a shutdown was detected on the connection and the buffer |
| 523 | * is empty, we must not attempt to receive |
| 524 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 525 | * we know there is nothing pending |
| 526 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 527 | * regardless of whether the demux buffer is full or not, so that only |
| 528 | * de demux part decides whether or not to block. This is needed because |
| 529 | * the connection API indeed prevents us from re-enabling receipt that is |
| 530 | * already enabled in a polled state, so we must always immediately stop |
| 531 | * as soon as the demux can't proceed so as never to hit an end of read |
| 532 | * with data pending in the buffers. |
| 533 | * - otherwise must may not attempt |
| 534 | */ |
| 535 | static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn) |
| 536 | { |
| 537 | if (b_data(&fconn->dbuf) == 0 && |
| 538 | (fconn->state == FCGI_CS_CLOSED || |
| 539 | fconn->conn->flags & CO_FL_ERROR || |
| 540 | conn_xprt_read0_pending(fconn->conn))) |
| 541 | return 0; |
| 542 | |
| 543 | if (!(fconn->flags & FCGI_CF_DEM_DALLOC) && |
| 544 | !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) |
| 545 | return 1; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* Restarts reading on the connection if it was not enabled */ |
| 551 | static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer) |
| 552 | { |
| 553 | if (!fcgi_recv_allowed(fconn)) |
| 554 | return; |
| 555 | if ((!consider_buffer || !b_data(&fconn->dbuf)) && |
| 556 | (fconn->wait_event.events & SUB_RETRY_RECV)) |
| 557 | return; |
| 558 | tasklet_wakeup(fconn->wait_event.tasklet); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The |
| 563 | * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if |
| 564 | * the allocation succeeds, in which case the connection is woken up, or 0 if |
| 565 | * it's impossible to wake up and we prefer to be woken up later. |
| 566 | */ |
| 567 | static int fcgi_buf_available(void *target) |
| 568 | { |
| 569 | struct fcgi_conn *fconn = target; |
| 570 | struct fcgi_strm *fstrm; |
| 571 | |
| 572 | if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 573 | TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 574 | fconn->flags &= ~FCGI_CF_DEM_DALLOC; |
| 575 | fcgi_conn_restart_reading(fconn, 1); |
| 576 | return 1; |
| 577 | } |
| 578 | |
| 579 | if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 580 | TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 581 | fconn->flags &= ~FCGI_CF_MUX_MALLOC; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 582 | if (fconn->flags & FCGI_CF_DEM_MROOM) { |
| 583 | fconn->flags &= ~FCGI_CF_DEM_MROOM; |
| 584 | fcgi_conn_restart_reading(fconn, 1); |
| 585 | } |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | if ((fconn->flags & FCGI_CF_DEM_SALLOC) && |
| 590 | (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs && |
| 591 | b_alloc_margin(&fstrm->rxbuf, 0)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 592 | TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 593 | fconn->flags &= ~FCGI_CF_DEM_SALLOC; |
| 594 | fcgi_conn_restart_reading(fconn, 1); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 595 | fcgi_strm_notify_recv(fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr) |
| 603 | { |
| 604 | struct buffer *buf = NULL; |
| 605 | |
| 606 | if (likely(!LIST_ADDED(&fconn->buf_wait.list)) && |
| 607 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 608 | fconn->buf_wait.target = fconn; |
| 609 | fconn->buf_wait.wakeup_cb = fcgi_buf_available; |
| 610 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 611 | LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list); |
| 612 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 613 | __conn_xprt_stop_recv(fconn->conn); |
| 614 | } |
| 615 | return buf; |
| 616 | } |
| 617 | |
| 618 | static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr) |
| 619 | { |
| 620 | if (bptr->size) { |
| 621 | b_free(bptr); |
| 622 | offer_buffers(NULL, tasks_run_queue); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static inline void fcgi_release_mbuf(struct fcgi_conn *fconn) |
| 627 | { |
| 628 | struct buffer *buf; |
| 629 | unsigned int count = 0; |
| 630 | |
| 631 | while (b_size(buf = br_head_pick(fconn->mbuf))) { |
| 632 | b_free(buf); |
| 633 | count++; |
| 634 | } |
| 635 | if (count) |
| 636 | offer_buffers(NULL, tasks_run_queue); |
| 637 | } |
| 638 | |
| 639 | /* Returns the number of allocatable outgoing streams for the connection taking |
| 640 | * the number reserved streams into account. |
| 641 | */ |
| 642 | static inline int fcgi_streams_left(const struct fcgi_conn *fconn) |
| 643 | { |
| 644 | int ret; |
| 645 | |
| 646 | ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1; |
| 647 | if (ret < 0) |
| 648 | ret = 0; |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | /* Returns the number of streams in use on a connection to figure if it's |
| 653 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 654 | * to know if it was the last one after a detach(). |
| 655 | */ |
| 656 | static int fcgi_used_streams(struct connection *conn) |
| 657 | { |
| 658 | struct fcgi_conn *fconn = conn->ctx; |
| 659 | |
| 660 | return fconn->nb_cs; |
| 661 | } |
| 662 | |
| 663 | /* Returns the number of concurrent streams available on the connection */ |
| 664 | static int fcgi_avail_streams(struct connection *conn) |
| 665 | { |
| 666 | struct server *srv = objt_server(conn->target); |
| 667 | struct fcgi_conn *fconn = conn->ctx; |
| 668 | int ret1, ret2; |
| 669 | |
| 670 | /* Don't open new stream if the connection is closed */ |
| 671 | if (fconn->state == FCGI_CS_CLOSED) |
| 672 | return 0; |
| 673 | |
| 674 | /* May be negative if this setting has changed */ |
| 675 | ret1 = (fconn->streams_limit - fconn->nb_streams); |
| 676 | |
| 677 | /* we must also consider the limit imposed by stream IDs */ |
| 678 | ret2 = fcgi_streams_left(fconn); |
| 679 | ret1 = MIN(ret1, ret2); |
| 680 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
| 681 | ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0); |
| 682 | ret1 = MIN(ret1, ret2); |
| 683 | } |
| 684 | return ret1; |
| 685 | } |
| 686 | |
| 687 | /*****************************************************************/ |
| 688 | /* functions below are dedicated to the mux setup and management */ |
| 689 | /*****************************************************************/ |
| 690 | |
| 691 | /* Initializes the mux once it's attached. Only outgoing connections are |
| 692 | * supported. So the context is already initialized before installing the |
| 693 | * mux. <input> is always used as Input buffer and may contain data. It is the |
| 694 | * caller responsibility to not reuse it anymore. Returns < 0 on error. |
| 695 | */ |
| 696 | static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess, |
| 697 | struct buffer *input) |
| 698 | { |
| 699 | struct fcgi_conn *fconn; |
| 700 | struct fcgi_strm *fstrm; |
| 701 | struct fcgi_app *app = get_px_fcgi_app(px); |
| 702 | struct task *t = NULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 703 | void *conn_ctx = conn->ctx; |
| 704 | |
| 705 | TRACE_ENTER(FCGI_EV_FSTRM_NEW); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 706 | |
| 707 | if (!app) |
| 708 | goto fail_conn; |
| 709 | |
| 710 | fconn = pool_alloc(pool_head_fcgi_conn); |
| 711 | if (!fconn) |
| 712 | goto fail_conn; |
| 713 | |
| 714 | fconn->shut_timeout = fconn->timeout = px->timeout.server; |
| 715 | if (tick_isset(px->timeout.serverfin)) |
| 716 | fconn->shut_timeout = px->timeout.serverfin; |
| 717 | |
| 718 | fconn->flags = FCGI_CF_NONE; |
| 719 | |
| 720 | /* Retrieve usefull info from the FCGI app */ |
| 721 | if (app->flags & FCGI_APP_FL_KEEP_CONN) |
| 722 | fconn->flags |= FCGI_CF_KEEP_CONN; |
| 723 | if (app->flags & FCGI_APP_FL_GET_VALUES) |
| 724 | fconn->flags |= FCGI_CF_GET_VALUES; |
| 725 | if (app->flags & FCGI_APP_FL_MPXS_CONNS) |
| 726 | fconn->flags |= FCGI_CF_MPXS_CONNS; |
| 727 | |
| 728 | fconn->proxy = px; |
| 729 | fconn->app = app; |
| 730 | fconn->task = NULL; |
| 731 | if (tick_isset(fconn->timeout)) { |
| 732 | t = task_new(tid_bit); |
| 733 | if (!t) |
| 734 | goto fail; |
| 735 | |
| 736 | fconn->task = t; |
| 737 | t->process = fcgi_timeout_task; |
| 738 | t->context = fconn; |
| 739 | t->expire = tick_add(now_ms, fconn->timeout); |
| 740 | } |
| 741 | |
| 742 | fconn->wait_event.tasklet = tasklet_new(); |
| 743 | if (!fconn->wait_event.tasklet) |
| 744 | goto fail; |
| 745 | fconn->wait_event.tasklet->process = fcgi_io_cb; |
| 746 | fconn->wait_event.tasklet->context = fconn; |
| 747 | fconn->wait_event.events = 0; |
| 748 | |
| 749 | /* Initialise the context. */ |
| 750 | fconn->state = FCGI_CS_INIT; |
| 751 | fconn->conn = conn; |
| 752 | fconn->streams_limit = app->maxreqs; |
| 753 | fconn->max_id = -1; |
| 754 | fconn->nb_streams = 0; |
| 755 | fconn->nb_cs = 0; |
| 756 | fconn->nb_reserved = 0; |
| 757 | fconn->stream_cnt = 0; |
| 758 | |
| 759 | fconn->dbuf = *input; |
| 760 | fconn->dsi = -1; |
| 761 | |
| 762 | br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0])); |
| 763 | fconn->streams_by_id = EB_ROOT; |
| 764 | LIST_INIT(&fconn->send_list); |
| 765 | LIST_INIT(&fconn->sending_list); |
| 766 | LIST_INIT(&fconn->buf_wait.list); |
| 767 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 768 | conn->ctx = fconn; |
| 769 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 770 | if (t) |
| 771 | task_queue(t); |
| 772 | |
| 773 | /* FIXME: this is temporary, for outgoing connections we need to |
| 774 | * immediately allocate a stream until the code is modified so that the |
| 775 | * caller calls ->attach(). For now the outgoing cs is stored as |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 776 | * conn->ctx by the caller and saved in conn_ctx. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 777 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 778 | fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 779 | if (!fstrm) |
| 780 | goto fail; |
| 781 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 782 | |
| 783 | /* Repare to read something */ |
| 784 | fcgi_conn_restart_reading(fconn, 1); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 785 | TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 786 | return 0; |
| 787 | |
| 788 | fail: |
| 789 | task_destroy(t); |
| 790 | if (fconn->wait_event.tasklet) |
| 791 | tasklet_free(fconn->wait_event.tasklet); |
| 792 | pool_free(pool_head_fcgi_conn, fconn); |
| 793 | fail_conn: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 794 | conn->ctx = conn_ctx; // restore saved ctx |
| 795 | TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 796 | return -1; |
| 797 | } |
| 798 | |
| 799 | /* Returns the next allocatable outgoing stream ID for the FCGI connection, or |
| 800 | * -1 if no more is allocatable. |
| 801 | */ |
| 802 | static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn) |
| 803 | { |
| 804 | int32_t id = (fconn->max_id + 1) | 1; |
| 805 | |
| 806 | if ((id & 0x80000000U)) |
| 807 | id = -1; |
| 808 | return id; |
| 809 | } |
| 810 | |
| 811 | /* Returns the stream associated with id <id> or NULL if not found */ |
| 812 | static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id) |
| 813 | { |
| 814 | struct eb32_node *node; |
| 815 | |
| 816 | if (id == 0) |
| 817 | return (struct fcgi_strm *)fcgi_mgmt_stream; |
| 818 | |
| 819 | if (id > fconn->max_id) |
| 820 | return (struct fcgi_strm *)fcgi_unknown_stream; |
| 821 | |
| 822 | node = eb32_lookup(&fconn->streams_by_id, id); |
| 823 | if (!node) |
| 824 | return (struct fcgi_strm *)fcgi_unknown_stream; |
| 825 | return container_of(node, struct fcgi_strm, by_id); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /* Release function. This one should be called to free all resources allocated |
| 830 | * to the mux. |
| 831 | */ |
| 832 | static void fcgi_release(struct fcgi_conn *fconn) |
| 833 | { |
| 834 | struct connection *conn = NULL;; |
| 835 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 836 | TRACE_POINT(FCGI_EV_FCONN_END); |
| 837 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 838 | if (fconn) { |
| 839 | /* The connection must be attached to this mux to be released */ |
| 840 | if (fconn->conn && fconn->conn->ctx == fconn) |
| 841 | conn = fconn->conn; |
| 842 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 843 | TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn); |
| 844 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 845 | if (LIST_ADDED(&fconn->buf_wait.list)) { |
| 846 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 847 | LIST_DEL(&fconn->buf_wait.list); |
| 848 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 849 | } |
| 850 | |
| 851 | fcgi_release_buf(fconn, &fconn->dbuf); |
| 852 | fcgi_release_mbuf(fconn); |
| 853 | |
| 854 | if (fconn->task) { |
| 855 | fconn->task->context = NULL; |
| 856 | task_wakeup(fconn->task, TASK_WOKEN_OTHER); |
| 857 | fconn->task = NULL; |
| 858 | } |
| 859 | if (fconn->wait_event.tasklet) |
| 860 | tasklet_free(fconn->wait_event.tasklet); |
Christopher Faulet | a99db93 | 2019-09-18 11:11:46 +0200 | [diff] [blame] | 861 | if (conn && fconn->wait_event.events != 0) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 862 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events, |
| 863 | &fconn->wait_event); |
| 864 | } |
| 865 | |
| 866 | if (conn) { |
| 867 | conn->mux = NULL; |
| 868 | conn->ctx = NULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 869 | TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 870 | |
| 871 | conn_stop_tracking(conn); |
| 872 | conn_full_close(conn); |
| 873 | if (conn->destroy_cb) |
| 874 | conn->destroy_cb(conn); |
| 875 | conn_free(conn); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /* Retruns true if the FCGI connection must be release */ |
| 881 | static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn) |
| 882 | { |
| 883 | if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */ |
| 884 | (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */ |
| 885 | (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 886 | (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */ |
| 887 | (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
| 888 | (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */ |
| 889 | conn_xprt_read0_pending(fconn->conn)))) |
| 890 | return 1; |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | /********************************************************/ |
| 896 | /* functions below are for the FCGI protocol processing */ |
| 897 | /********************************************************/ |
| 898 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 899 | /* Marks an error on the stream. */ |
| 900 | static inline void fcgi_strm_error(struct fcgi_strm *fstrm) |
| 901 | { |
| 902 | if (fstrm->id && fstrm->state != FCGI_SS_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 903 | TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm); |
| 904 | if (fstrm->state < FCGI_SS_ERROR) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 905 | fstrm->state = FCGI_SS_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 906 | TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm); |
| 907 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 908 | if (fstrm->cs) |
| 909 | cs_set_error(fstrm->cs); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /* Attempts to notify the data layer of recv availability */ |
| 914 | static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm) |
| 915 | { |
| 916 | struct wait_event *sw; |
| 917 | |
| 918 | if (fstrm->recv_wait) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 919 | TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 920 | sw = fstrm->recv_wait; |
| 921 | sw->events &= ~SUB_RETRY_RECV; |
| 922 | tasklet_wakeup(sw->tasklet); |
| 923 | fstrm->recv_wait = NULL; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /* Attempts to notify the data layer of send availability */ |
| 928 | static void fcgi_strm_notify_send(struct fcgi_strm *fstrm) |
| 929 | { |
| 930 | struct wait_event *sw; |
| 931 | |
| 932 | if (fstrm->send_wait && !LIST_ADDED(&fstrm->sending_list)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 933 | TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 934 | sw = fstrm->send_wait; |
| 935 | sw->events &= ~SUB_RETRY_SEND; |
| 936 | LIST_ADDQ(&fstrm->fconn->sending_list, &fstrm->sending_list); |
| 937 | tasklet_wakeup(sw->tasklet); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* Alerts the data layer, trying to wake it up by all means, following |
| 942 | * this sequence : |
| 943 | * - if the fcgi stream' data layer is subscribed to recv, then it's woken up |
| 944 | * for recv |
| 945 | * - if its subscribed to send, then it's woken up for send |
| 946 | * - if it was subscribed to neither, its ->wake() callback is called |
| 947 | * It is safe to call this function with a closed stream which doesn't have a |
| 948 | * conn_stream anymore. |
| 949 | */ |
| 950 | static void fcgi_strm_alert(struct fcgi_strm *fstrm) |
| 951 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 952 | TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 953 | if (fstrm->recv_wait || fstrm->send_wait) { |
| 954 | fcgi_strm_notify_recv(fstrm); |
| 955 | fcgi_strm_notify_send(fstrm); |
| 956 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 957 | else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) { |
| 958 | TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 959 | fstrm->cs->data_cb->wake(fstrm->cs); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 960 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* Writes the 16-bit record size <len> at address <record> */ |
| 964 | static inline void fcgi_set_record_size(void *record, uint16_t len) |
| 965 | { |
| 966 | uint8_t *out = (record + 4); |
| 967 | |
| 968 | *out = (len >> 8); |
| 969 | *(out + 1) = (len & 0xff); |
| 970 | } |
| 971 | |
| 972 | /* Writes the 16-bit stream id <id> at address <record> */ |
| 973 | static inline void fcgi_set_record_id(void *record, uint16_t id) |
| 974 | { |
| 975 | uint8_t *out = (record + 2); |
| 976 | |
| 977 | *out = (id >> 8); |
| 978 | *(out + 1) = (id & 0xff); |
| 979 | } |
| 980 | |
| 981 | /* Marks a FCGI stream as CLOSED and decrement the number of active streams for |
| 982 | * its connection if the stream was not yet closed. Please use this exclusively |
| 983 | * before closing a stream to ensure stream count is well maintained. |
| 984 | */ |
| 985 | static inline void fcgi_strm_close(struct fcgi_strm *fstrm) |
| 986 | { |
| 987 | if (fstrm->state != FCGI_SS_CLOSED) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 988 | TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 989 | fstrm->fconn->nb_streams--; |
| 990 | if (!fstrm->id) |
| 991 | fstrm->fconn->nb_reserved--; |
| 992 | if (fstrm->cs) { |
| 993 | if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf)) |
| 994 | fcgi_strm_notify_recv(fstrm); |
| 995 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 996 | fstrm->state = FCGI_SS_CLOSED; |
| 997 | TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm); |
| 998 | TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 999 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* Detaches a FCGI stream from its FCGI connection and releases it to the |
| 1003 | * fcgi_strm pool. |
| 1004 | */ |
| 1005 | static void fcgi_strm_destroy(struct fcgi_strm *fstrm) |
| 1006 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1007 | struct connection *conn = fstrm->fconn->conn; |
| 1008 | |
| 1009 | TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm); |
| 1010 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1011 | fcgi_strm_close(fstrm); |
| 1012 | eb32_delete(&fstrm->by_id); |
| 1013 | if (b_size(&fstrm->rxbuf)) { |
| 1014 | b_free(&fstrm->rxbuf); |
| 1015 | offer_buffers(NULL, tasks_run_queue); |
| 1016 | } |
| 1017 | if (fstrm->send_wait != NULL) |
| 1018 | fstrm->send_wait->events &= ~SUB_RETRY_SEND; |
| 1019 | if (fstrm->recv_wait != NULL) |
| 1020 | fstrm->recv_wait->events &= ~SUB_RETRY_RECV; |
| 1021 | /* There's no need to explicitly call unsubscribe here, the only |
| 1022 | * reference left would be in the fconn send_list/fctl_list, and if |
| 1023 | * we're in it, we're getting out anyway |
| 1024 | */ |
| 1025 | LIST_DEL_INIT(&fstrm->send_list); |
| 1026 | if (LIST_ADDED(&fstrm->sending_list)) { |
| 1027 | tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet); |
| 1028 | LIST_DEL_INIT(&fstrm->sending_list); |
| 1029 | } |
| 1030 | tasklet_free(fstrm->wait_event.tasklet); |
| 1031 | pool_free(pool_head_fcgi_strm, fstrm); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1032 | |
| 1033 | TRACE_LEAVE(FCGI_EV_FSTRM_END, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /* Allocates a new stream <id> for connection <fconn> and adds it into fconn's |
| 1037 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 1038 | * causes of errors can be any failed memory allocation. The caller is |
| 1039 | * responsible for checking if the connection may support an extra stream prior |
| 1040 | * to calling this function. |
| 1041 | */ |
| 1042 | static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id) |
| 1043 | { |
| 1044 | struct fcgi_strm *fstrm; |
| 1045 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1046 | TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn); |
| 1047 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1048 | fstrm = pool_alloc(pool_head_fcgi_strm); |
| 1049 | if (!fstrm) |
| 1050 | goto out; |
| 1051 | |
| 1052 | fstrm->wait_event.tasklet = tasklet_new(); |
| 1053 | if (!fstrm->wait_event.tasklet) { |
| 1054 | pool_free(pool_head_fcgi_strm, fstrm); |
| 1055 | goto out; |
| 1056 | } |
| 1057 | fstrm->send_wait = NULL; |
| 1058 | fstrm->recv_wait = NULL; |
| 1059 | fstrm->wait_event.tasklet->process = fcgi_deferred_shut; |
| 1060 | fstrm->wait_event.tasklet->context = fstrm; |
| 1061 | fstrm->wait_event.events = 0; |
| 1062 | LIST_INIT(&fstrm->send_list); |
| 1063 | LIST_INIT(&fstrm->sending_list); |
| 1064 | fstrm->fconn = fconn; |
| 1065 | fstrm->cs = NULL; |
| 1066 | fstrm->flags = FCGI_SF_NONE; |
| 1067 | fstrm->proto_status = 0; |
| 1068 | fstrm->state = FCGI_SS_IDLE; |
| 1069 | fstrm->rxbuf = BUF_NULL; |
| 1070 | |
| 1071 | h1m_init_res(&fstrm->h1m); |
| 1072 | fstrm->h1m.err_pos = -1; // don't care about errors on the request path |
| 1073 | fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1074 | |
| 1075 | fstrm->by_id.key = fstrm->id = id; |
| 1076 | if (id > 0) |
| 1077 | fconn->max_id = id; |
| 1078 | else |
| 1079 | fconn->nb_reserved++; |
| 1080 | |
| 1081 | eb32_insert(&fconn->streams_by_id, &fstrm->by_id); |
| 1082 | fconn->nb_streams++; |
| 1083 | fconn->stream_cnt++; |
| 1084 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1085 | TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1086 | return fstrm; |
| 1087 | |
| 1088 | out: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1089 | TRACE_DEVEL("leaving in error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1090 | return NULL; |
| 1091 | } |
| 1092 | |
| 1093 | /* Allocates a new stream associated to conn_stream <cs> on the FCGI connection |
| 1094 | * <fconn> and returns it, or NULL in case of memory allocation error or if the |
| 1095 | * highest possible stream ID was reached. |
| 1096 | */ |
| 1097 | static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, |
| 1098 | struct session *sess) |
| 1099 | { |
| 1100 | struct fcgi_strm *fstrm = NULL; |
| 1101 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1102 | TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn); |
| 1103 | if (fconn->nb_streams >= fconn->streams_limit) { |
| 1104 | TRACE_DEVEL("leaving on streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1105 | goto out; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1106 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1107 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1108 | if (fcgi_streams_left(fconn) < 1) { |
| 1109 | TRACE_DEVEL("leaving on !streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1110 | goto out; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1111 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1112 | |
| 1113 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1114 | fstrm = fcgi_strm_new(fconn, 0); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1115 | if (!fstrm) { |
| 1116 | TRACE_DEVEL("leaving on fstrm creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1117 | goto out; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1118 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1119 | |
| 1120 | fstrm->cs = cs; |
| 1121 | fstrm->sess = sess; |
| 1122 | cs->ctx = fstrm; |
| 1123 | fconn->nb_cs++; |
| 1124 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1125 | TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1126 | return fstrm; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1127 | |
| 1128 | out: |
| 1129 | return NULL; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among |
| 1133 | * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is |
| 1134 | * automatically updated accordingly. If the stream is orphaned, it is |
| 1135 | * destroyed. |
| 1136 | */ |
| 1137 | static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm) |
| 1138 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1139 | struct fcgi_conn *fconn = fstrm->fconn; |
| 1140 | |
| 1141 | TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
| 1142 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1143 | if (!fstrm->cs) { |
| 1144 | /* this stream was already orphaned */ |
| 1145 | fcgi_strm_destroy(fstrm); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1146 | TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1147 | return; |
| 1148 | } |
| 1149 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1150 | if (conn_xprt_read0_pending(fconn->conn)) { |
| 1151 | if (fstrm->state == FCGI_SS_OPEN) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1152 | fstrm->state = FCGI_SS_HREM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1153 | TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm); |
| 1154 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1155 | else if (fstrm->state == FCGI_SS_HLOC) |
| 1156 | fcgi_strm_close(fstrm); |
| 1157 | } |
| 1158 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1159 | if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1160 | fstrm->cs->flags |= CS_FL_ERR_PENDING; |
| 1161 | if (fstrm->cs->flags & CS_FL_EOS) |
| 1162 | fstrm->cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1163 | |
| 1164 | if (fstrm->state < FCGI_SS_ERROR) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1165 | fstrm->state = FCGI_SS_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1166 | TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm); |
| 1167 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | fcgi_strm_alert(fstrm); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1171 | |
| 1172 | TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* Wakes unassigned streams (ID == 0) attached to the connection. */ |
| 1176 | static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn) |
| 1177 | { |
| 1178 | struct eb32_node *node; |
| 1179 | struct fcgi_strm *fstrm; |
| 1180 | |
| 1181 | node = eb32_lookup(&fconn->streams_by_id, 0); |
| 1182 | while (node) { |
| 1183 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 1184 | if (fstrm->id > 0) |
| 1185 | break; |
| 1186 | node = eb32_next(node); |
| 1187 | fcgi_strm_wake_one_stream(fstrm); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /* Wakes the streams attached to the connection, whose id is greater than <last> |
| 1192 | * or unassigned. |
| 1193 | */ |
| 1194 | static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last) |
| 1195 | { |
| 1196 | struct eb32_node *node; |
| 1197 | struct fcgi_strm *fstrm; |
| 1198 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1199 | TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn); |
| 1200 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1201 | /* Wake all streams with ID > last */ |
| 1202 | node = eb32_lookup_ge(&fconn->streams_by_id, last + 1); |
| 1203 | while (node) { |
| 1204 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 1205 | node = eb32_next(node); |
| 1206 | fcgi_strm_wake_one_stream(fstrm); |
| 1207 | } |
| 1208 | fcgi_wake_unassigned_streams(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1209 | |
| 1210 | TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 1214 | struct htx *htx, struct htx_sl *sl, |
| 1215 | struct fcgi_strm_params *params) |
| 1216 | { |
| 1217 | struct connection *cli_conn = objt_conn(fstrm->sess->origin); |
| 1218 | struct ist p; |
| 1219 | |
| 1220 | if (!sl) |
| 1221 | goto error; |
| 1222 | |
| 1223 | if (!(params->mask & FCGI_SP_DOC_ROOT)) |
| 1224 | params->docroot = fconn->app->docroot; |
| 1225 | |
| 1226 | if (!(params->mask & FCGI_SP_REQ_METH)) { |
| 1227 | p = htx_sl_req_meth(sl); |
| 1228 | params->meth = ist2(b_tail(params->p), p.len); |
| 1229 | chunk_memcat(params->p, p.ptr, p.len); |
| 1230 | } |
| 1231 | if (!(params->mask & FCGI_SP_REQ_URI)) { |
| 1232 | p = htx_sl_req_uri(sl); |
| 1233 | params->uri = ist2(b_tail(params->p), p.len); |
| 1234 | chunk_memcat(params->p, p.ptr, p.len); |
| 1235 | } |
| 1236 | if (!(params->mask & FCGI_SP_SRV_PROTO)) { |
| 1237 | p = htx_sl_req_vsn(sl); |
| 1238 | params->vsn = ist2(b_tail(params->p), p.len); |
| 1239 | chunk_memcat(params->p, p.ptr, p.len); |
| 1240 | } |
| 1241 | if (!(params->mask & FCGI_SP_SRV_PORT)) { |
| 1242 | char *end; |
| 1243 | int port = 0; |
| 1244 | if (conn_get_dst(cli_conn)) |
| 1245 | port = get_host_port(cli_conn->dst); |
| 1246 | end = ultoa_o(port, b_tail(params->p), b_room(params->p)); |
| 1247 | if (!end) |
| 1248 | goto error; |
| 1249 | params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p)); |
| 1250 | params->p->data += params->srv_port.len; |
| 1251 | } |
| 1252 | if (!(params->mask & FCGI_SP_SRV_NAME)) { |
| 1253 | /* If no Host header found, use the server address to fill |
| 1254 | * srv_name */ |
| 1255 | if (!istlen(params->srv_name)) { |
| 1256 | char *ptr = NULL; |
| 1257 | |
| 1258 | if (conn_get_dst(cli_conn)) |
| 1259 | if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1) |
| 1260 | ptr = b_tail(params->p); |
| 1261 | if (ptr) { |
| 1262 | params->srv_name = ist2(ptr, strlen(ptr)); |
| 1263 | params->p->data += params->srv_name.len; |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | if (!(params->mask & FCGI_SP_REM_ADDR)) { |
| 1268 | char *ptr = NULL; |
| 1269 | |
| 1270 | if (conn_get_src(cli_conn)) |
| 1271 | if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1) |
| 1272 | ptr = b_tail(params->p); |
| 1273 | if (ptr) { |
| 1274 | params->rem_addr = ist2(ptr, strlen(ptr)); |
| 1275 | params->p->data += params->rem_addr.len; |
| 1276 | } |
| 1277 | } |
| 1278 | if (!(params->mask & FCGI_SP_REM_PORT)) { |
| 1279 | char *end; |
| 1280 | int port = 0; |
| 1281 | if (conn_get_src(cli_conn)) |
| 1282 | port = get_host_port(cli_conn->src); |
| 1283 | end = ultoa_o(port, b_tail(params->p), b_room(params->p)); |
| 1284 | if (!end) |
| 1285 | goto error; |
| 1286 | params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p)); |
| 1287 | params->p->data += params->rem_port.len; |
| 1288 | } |
| 1289 | if (!(params->mask & FCGI_SP_CONT_LEN)) { |
| 1290 | struct htx_blk *blk; |
| 1291 | enum htx_blk_type type; |
| 1292 | char *end; |
| 1293 | size_t len = 0; |
| 1294 | |
| 1295 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 1296 | type = htx_get_blk_type(blk); |
| 1297 | |
| 1298 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 1299 | break; |
| 1300 | if (type == HTX_BLK_DATA) |
| 1301 | len += htx_get_blksz(blk); |
| 1302 | } |
| 1303 | end = ultoa_o(len, b_tail(params->p), b_room(params->p)); |
| 1304 | if (!end) |
| 1305 | goto error; |
| 1306 | params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p)); |
| 1307 | params->p->data += params->cont_len.len; |
| 1308 | } |
Christopher Faulet | d66700a | 2019-09-17 13:46:47 +0200 | [diff] [blame] | 1309 | #ifdef USE_OPENSSL |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1310 | if (!(params->mask & FCGI_SP_HTTPS)) { |
| 1311 | params->https = ssl_sock_is_ssl(cli_conn); |
| 1312 | } |
Christopher Faulet | d66700a | 2019-09-17 13:46:47 +0200 | [diff] [blame] | 1313 | #endif |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1314 | if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) { |
| 1315 | /* one of scriptname, pathinfo or query_string is no set */ |
| 1316 | struct ist path = http_get_path(params->uri); |
| 1317 | int len; |
| 1318 | |
| 1319 | /* Decode the path. it must first be copied to keep the URI |
| 1320 | * untouched. |
| 1321 | */ |
| 1322 | chunk_memcat(params->p, path.ptr, path.len); |
| 1323 | path.ptr = b_tail(params->p) - path.len; |
| 1324 | path.ptr[path.len] = '\0'; |
| 1325 | len = url_decode(path.ptr); |
| 1326 | if (len < 0) |
| 1327 | goto error; |
| 1328 | path.len = len; |
| 1329 | |
| 1330 | /* No scrit_name set but no valid path ==> error */ |
| 1331 | if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path)) |
| 1332 | goto error; |
| 1333 | |
| 1334 | /* Find limit between the path and the query-string */ |
| 1335 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++); |
| 1336 | |
| 1337 | /* If there is a query-string, Set it if not already set */ |
| 1338 | if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len) |
| 1339 | params->qs = ist2(path.ptr+len+1, path.len-len-1); |
| 1340 | |
| 1341 | /* If the script_name is set, don't try to deduce the path_info |
| 1342 | * too. The opposite is not true. |
| 1343 | */ |
| 1344 | if (params->mask & FCGI_SP_SCRIPT_NAME) { |
| 1345 | params->mask |= FCGI_SP_PATH_INFO; |
| 1346 | goto end; |
| 1347 | } |
| 1348 | |
| 1349 | /* script_name not set, preset it with the path for now */ |
| 1350 | params->scriptname = ist2(path.ptr, len); |
| 1351 | |
| 1352 | /* If there is no regex to match the pathinfo, just to the last |
| 1353 | * part and see if the index must be used. |
| 1354 | */ |
| 1355 | if (!fconn->app->pathinfo_re) |
| 1356 | goto check_index; |
| 1357 | |
| 1358 | /* The regex does not match, just to the last part and see if |
| 1359 | * the index must be used. |
| 1360 | */ |
| 1361 | if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0)) |
| 1362 | goto check_index; |
| 1363 | |
| 1364 | /* We must have at least 2 captures, otherwise we do nothing and |
| 1365 | * jump to the last part. Only first 2 ones will be considered |
| 1366 | */ |
| 1367 | if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1 || |
| 1368 | pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1) |
| 1369 | goto check_index; |
| 1370 | |
| 1371 | /* Finally we can set the script_name and the path_info */ |
| 1372 | params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so); |
| 1373 | params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so); |
| 1374 | |
| 1375 | check_index: |
| 1376 | len = params->scriptname.len; |
| 1377 | /* the script_name if finished by a '/' so we can add the index |
| 1378 | * part, if any. |
| 1379 | */ |
| 1380 | if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') { |
| 1381 | struct ist sn = params->scriptname; |
| 1382 | |
| 1383 | params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len); |
| 1384 | chunk_memcat(params->p, sn.ptr, sn.len); |
| 1385 | chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len); |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | end: |
| 1390 | return 1; |
| 1391 | error: |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 1396 | struct fcgi_strm_params *params, struct buffer *outbuf, int flag) |
| 1397 | { |
| 1398 | struct fcgi_param p; |
| 1399 | |
| 1400 | if (params->mask & flag) |
| 1401 | return 1; |
| 1402 | |
| 1403 | chunk_reset(&trash); |
| 1404 | |
| 1405 | switch (flag) { |
| 1406 | case FCGI_SP_CGI_GATEWAY: |
| 1407 | p.n = ist("GATEWAY_INTERFACE"); |
| 1408 | p.v = ist("CGI/1.1"); |
| 1409 | goto encode; |
| 1410 | case FCGI_SP_DOC_ROOT: |
| 1411 | p.n = ist("DOCUMENT_ROOT"); |
| 1412 | p.v = params->docroot; |
| 1413 | goto encode; |
| 1414 | case FCGI_SP_SCRIPT_NAME: |
| 1415 | p.n = ist("SCRIPT_NAME"); |
| 1416 | p.v = params->scriptname; |
| 1417 | goto encode; |
| 1418 | case FCGI_SP_PATH_INFO: |
| 1419 | p.n = ist("PATH_INFO"); |
| 1420 | p.v = params->pathinfo; |
| 1421 | goto encode; |
| 1422 | case FCGI_SP_REQ_URI: |
| 1423 | p.n = ist("REQUEST_URI"); |
| 1424 | p.v = params->uri; |
| 1425 | goto encode; |
| 1426 | case FCGI_SP_REQ_METH: |
| 1427 | p.n = ist("REQUEST_METHOD"); |
| 1428 | p.v = params->meth; |
| 1429 | goto encode; |
| 1430 | case FCGI_SP_REQ_QS: |
| 1431 | p.n = ist("QUERY_STRING"); |
| 1432 | p.v = params->qs; |
| 1433 | goto encode; |
| 1434 | case FCGI_SP_SRV_NAME: |
| 1435 | p.n = ist("SERVER_NAME"); |
| 1436 | p.v = params->srv_name; |
| 1437 | goto encode; |
| 1438 | case FCGI_SP_SRV_PORT: |
| 1439 | p.n = ist("SERVER_PORT"); |
| 1440 | p.v = params->srv_port; |
| 1441 | goto encode; |
| 1442 | case FCGI_SP_SRV_PROTO: |
| 1443 | p.n = ist("SERVER_PROTOCOL"); |
| 1444 | p.v = params->vsn; |
| 1445 | goto encode; |
| 1446 | case FCGI_SP_REM_ADDR: |
| 1447 | p.n = ist("REMOTE_ADDR"); |
| 1448 | p.v = params->rem_addr; |
| 1449 | goto encode; |
| 1450 | case FCGI_SP_REM_PORT: |
| 1451 | p.n = ist("REMOTE_PORT"); |
| 1452 | p.v = params->rem_port; |
| 1453 | goto encode; |
| 1454 | case FCGI_SP_SCRIPT_FILE: |
| 1455 | p.n = ist("SCRIPT_FILENAME"); |
| 1456 | chunk_memcat(&trash, params->docroot.ptr, params->docroot.len); |
| 1457 | chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len); |
| 1458 | p.v = ist2(b_head(&trash), b_data(&trash)); |
| 1459 | goto encode; |
| 1460 | case FCGI_SP_PATH_TRANS: |
| 1461 | if (!istlen(params->pathinfo)) |
| 1462 | goto skip; |
| 1463 | p.n = ist("PATH_TRANSLATED"); |
| 1464 | chunk_memcat(&trash, params->docroot.ptr, params->docroot.len); |
| 1465 | chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len); |
| 1466 | p.v = ist2(b_head(&trash), b_data(&trash)); |
| 1467 | goto encode; |
| 1468 | case FCGI_SP_CONT_LEN: |
| 1469 | p.n = ist("CONTENT_LENGTH"); |
| 1470 | p.v = params->cont_len; |
| 1471 | goto encode; |
| 1472 | case FCGI_SP_HTTPS: |
| 1473 | if (!params->https) |
| 1474 | goto skip; |
| 1475 | p.n = ist("HTTPS"); |
| 1476 | p.v = ist("on"); |
| 1477 | goto encode; |
| 1478 | default: |
| 1479 | goto skip; |
| 1480 | } |
| 1481 | |
| 1482 | encode: |
| 1483 | if (!istlen(p.v)) |
| 1484 | goto skip; |
| 1485 | if (!fcgi_encode_param(outbuf, &p)) |
| 1486 | return 0; |
| 1487 | skip: |
| 1488 | params->mask |= flag; |
| 1489 | return 1; |
| 1490 | } |
| 1491 | |
| 1492 | /* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do |
| 1493 | * anything. It is highly unexpected, but if the record is larger than a buffer |
| 1494 | * and cannot be encoded in one time, an error is triggered and the connection is |
| 1495 | * closed. GET_VALUES record cannot be split. |
| 1496 | */ |
| 1497 | static int fcgi_conn_send_get_values(struct fcgi_conn *fconn) |
| 1498 | { |
| 1499 | struct buffer outbuf; |
| 1500 | struct buffer *mbuf; |
| 1501 | struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")}; |
| 1502 | struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")}; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1503 | int ret = 0; |
| 1504 | |
| 1505 | TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1506 | |
| 1507 | mbuf = br_tail(fconn->mbuf); |
| 1508 | retry: |
| 1509 | if (!fcgi_get_buf(fconn, mbuf)) { |
| 1510 | fconn->flags |= FCGI_CF_MUX_MALLOC; |
| 1511 | fconn->flags |= FCGI_CF_DEM_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1512 | TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn); |
| 1513 | ret = 0; |
| 1514 | goto end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | while (1) { |
| 1518 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 1519 | if (outbuf.size >= 8 || !b_space_wraps(mbuf)) |
| 1520 | break; |
| 1521 | realign_again: |
| 1522 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
| 1523 | } |
| 1524 | |
| 1525 | if (outbuf.size < 8) |
| 1526 | goto full; |
| 1527 | |
| 1528 | /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000, |
| 1529 | * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */ |
| 1530 | memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8); |
| 1531 | outbuf.data = 8; |
| 1532 | |
| 1533 | /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be |
| 1534 | * handled by HAProxy. |
| 1535 | */ |
| 1536 | if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns)) |
| 1537 | goto full; |
| 1538 | |
| 1539 | /* update the record's size now */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1540 | TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn,,, (size_t[]){outbuf.data-8}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1541 | fcgi_set_record_size(outbuf.area, outbuf.data - 8); |
| 1542 | b_add(mbuf, outbuf.data); |
| 1543 | ret = 1; |
| 1544 | |
| 1545 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1546 | TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1547 | return ret; |
| 1548 | full: |
| 1549 | /* Too large to be encoded. For GET_VALUES records, it is an error */ |
| 1550 | if (!b_data(mbuf)) |
| 1551 | goto fail; |
| 1552 | |
| 1553 | if ((mbuf = br_tail_add(fconn->mbuf)) != NULL) |
| 1554 | goto retry; |
| 1555 | fconn->flags |= FCGI_CF_MUX_MFULL; |
| 1556 | fconn->flags |= FCGI_CF_DEM_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1557 | TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1558 | ret = 0; |
| 1559 | goto end; |
| 1560 | fail: |
| 1561 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1562 | TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn); |
| 1563 | TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn); |
| 1564 | return 0; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it |
| 1568 | * couldn't do anything. It is highly unexpected, but if the record is larger |
| 1569 | * than a buffer and cannot be decoded in one time, an error is triggered and |
| 1570 | * the connection is closed. GET_VALUES_RESULT record cannot be split. |
| 1571 | */ |
| 1572 | static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn) |
| 1573 | { |
| 1574 | struct buffer inbuf; |
| 1575 | struct buffer *dbuf; |
| 1576 | size_t offset; |
| 1577 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1578 | TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
| 1579 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1580 | dbuf = &fconn->dbuf; |
| 1581 | |
| 1582 | /* Record too large to be fully decoded */ |
| 1583 | if (b_size(dbuf) < (fconn->drl + fconn->drp)) |
| 1584 | goto fail; |
| 1585 | |
| 1586 | /* process full record only */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1587 | if (b_data(dbuf) < (fconn->drl + fconn->drp)) { |
| 1588 | TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1589 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1590 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1591 | |
| 1592 | if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) { |
| 1593 | /* Realign the dmux buffer if the record wraps. It is unexpected |
| 1594 | * at this stage because it should be the first record received |
| 1595 | * from the FCGI application. |
| 1596 | */ |
| 1597 | b_slow_realign(dbuf, trash.area, 0); |
| 1598 | } |
| 1599 | |
| 1600 | inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl); |
| 1601 | |
| 1602 | for (offset = 0; offset < b_data(&inbuf); ) { |
| 1603 | struct fcgi_param p; |
| 1604 | size_t ret; |
| 1605 | |
| 1606 | ret = fcgi_aligned_decode_param(&inbuf, offset, &p); |
| 1607 | if (!ret) { |
| 1608 | /* name or value too large to be decoded at once */ |
| 1609 | goto fail; |
| 1610 | } |
| 1611 | offset += ret; |
| 1612 | |
| 1613 | if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1614 | if (isteq(p.v, ist("1"))) { |
Christopher Faulet | 08618a7 | 2019-10-08 11:59:47 +0200 | [diff] [blame] | 1615 | TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){1}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1616 | fconn->flags |= FCGI_CF_MPXS_CONNS; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1617 | } |
| 1618 | else { |
Christopher Faulet | 08618a7 | 2019-10-08 11:59:47 +0200 | [diff] [blame] | 1619 | TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1620 | fconn->flags &= ~FCGI_CF_MPXS_CONNS; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1621 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1622 | } |
| 1623 | else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) { |
| 1624 | fconn->streams_limit = strl2ui(p.v.ptr, p.v.len); |
Christopher Faulet | 08618a7 | 2019-10-08 11:59:47 +0200 | [diff] [blame] | 1625 | TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->streams_limit}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1626 | } |
| 1627 | /* |
| 1628 | * Ignore all other params |
| 1629 | */ |
| 1630 | } |
| 1631 | |
| 1632 | /* Reset the number of concurrent streams supported if the FCGI |
| 1633 | * application does not support connection multiplexing |
| 1634 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1635 | if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1636 | fconn->streams_limit = 1; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1637 | TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
| 1638 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1639 | |
| 1640 | /* We must be sure to have read exactly the announced record length, no |
| 1641 | * more no less |
| 1642 | */ |
| 1643 | if (offset != fconn->drl) |
| 1644 | goto fail; |
| 1645 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1646 | TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->drl}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1647 | b_del(&fconn->dbuf, fconn->drl + fconn->drp); |
| 1648 | fconn->drl = 0; |
| 1649 | fconn->drp = 0; |
| 1650 | fconn->state = FCGI_CS_RECORD_H; |
| 1651 | fcgi_wake_unassigned_streams(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1652 | TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
| 1653 | TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1654 | return 1; |
| 1655 | fail: |
| 1656 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1657 | TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
| 1658 | TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /* Sends an ABORT_REQUEST record for each active streams. Closed streams are |
| 1663 | * excluded, as the streams which already received the end-of-stream. It returns |
| 1664 | * > 0 if the record was sent tp all streams. Otherwise it returns 0. |
| 1665 | */ |
| 1666 | static int fcgi_conn_send_aborts(struct fcgi_conn *fconn) |
| 1667 | { |
| 1668 | struct eb32_node *node; |
| 1669 | struct fcgi_strm *fstrm; |
| 1670 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1671 | TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn); |
| 1672 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1673 | node = eb32_lookup_ge(&fconn->streams_by_id, 1); |
| 1674 | while (node) { |
| 1675 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 1676 | node = eb32_next(node); |
| 1677 | if (fstrm->state != FCGI_SS_CLOSED && |
| 1678 | !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) && |
| 1679 | !fcgi_strm_send_abort(fconn, fstrm)) |
| 1680 | return 0; |
| 1681 | } |
| 1682 | fconn->flags |= FCGI_CF_ABRTS_SENT; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1683 | TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn); |
| 1684 | TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1685 | return 1; |
| 1686 | } |
| 1687 | |
| 1688 | /* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do |
| 1689 | * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough |
| 1690 | * space to proceed. It is small enough to be encoded in an empty buffer. |
| 1691 | */ |
| 1692 | static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 1693 | { |
| 1694 | struct buffer outbuf; |
| 1695 | struct buffer *mbuf; |
| 1696 | struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0}; |
| 1697 | int ret; |
| 1698 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1699 | TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm); |
| 1700 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1701 | mbuf = br_tail(fconn->mbuf); |
| 1702 | retry: |
| 1703 | if (!fcgi_get_buf(fconn, mbuf)) { |
| 1704 | fconn->flags |= FCGI_CF_MUX_MALLOC; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1705 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
| 1706 | TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
| 1707 | ret = 0; |
| 1708 | goto end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | while (1) { |
| 1712 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 1713 | if (outbuf.size >= 8 || !b_space_wraps(mbuf)) |
| 1714 | break; |
| 1715 | realign_again: |
| 1716 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
| 1717 | } |
| 1718 | |
| 1719 | if (outbuf.size < 8) |
| 1720 | goto full; |
| 1721 | |
| 1722 | /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id, |
| 1723 | * len: 0x0008, padding: 0x00, rsv: 0x00 */ |
| 1724 | memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8); |
| 1725 | fcgi_set_record_id(outbuf.area, fstrm->id); |
| 1726 | outbuf.data = 8; |
| 1727 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1728 | if (fconn->flags & FCGI_CF_KEEP_CONN) { |
| 1729 | TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1730 | rec.flags |= FCGI_KEEP_CONN; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1731 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1732 | if (!fcgi_encode_begin_request(&outbuf, &rec)) |
| 1733 | goto full; |
| 1734 | |
| 1735 | /* commit the record */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1736 | TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm,, (size_t[]){0}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1737 | b_add(mbuf, outbuf.data); |
| 1738 | fstrm->flags |= FCGI_SF_BEGIN_SENT; |
| 1739 | fstrm->state = FCGI_SS_OPEN; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1740 | TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1741 | ret = 1; |
| 1742 | |
| 1743 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1744 | TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1745 | return ret; |
| 1746 | full: |
| 1747 | if ((mbuf = br_tail_add(fconn->mbuf)) != NULL) |
| 1748 | goto retry; |
| 1749 | fconn->flags |= FCGI_CF_MUX_MFULL; |
| 1750 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1751 | TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1752 | ret = 0; |
| 1753 | goto end; |
| 1754 | } |
| 1755 | |
| 1756 | /* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it |
| 1757 | * couldn't do anything. Empty record cannot be split. So we wait to have enough |
| 1758 | * space to proceed. It is small enough to be encoded in an empty buffer. |
| 1759 | */ |
| 1760 | static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 1761 | enum fcgi_record_type rtype) |
| 1762 | { |
| 1763 | struct buffer outbuf; |
| 1764 | struct buffer *mbuf; |
| 1765 | int ret; |
| 1766 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1767 | TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1768 | mbuf = br_tail(fconn->mbuf); |
| 1769 | retry: |
| 1770 | if (!fcgi_get_buf(fconn, mbuf)) { |
| 1771 | fconn->flags |= FCGI_CF_MUX_MALLOC; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1772 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
| 1773 | TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
| 1774 | ret = 0; |
| 1775 | goto end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | while (1) { |
| 1779 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 1780 | if (outbuf.size >= 8 || !b_space_wraps(mbuf)) |
| 1781 | break; |
| 1782 | realign_again: |
| 1783 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
| 1784 | } |
| 1785 | |
| 1786 | if (outbuf.size < 8) |
| 1787 | goto full; |
| 1788 | |
| 1789 | /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id, |
| 1790 | * len: 0x0000, padding: 0x00, rsv: 0x00 */ |
| 1791 | memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8); |
| 1792 | outbuf.area[1] = rtype; |
| 1793 | fcgi_set_record_id(outbuf.area, fstrm->id); |
| 1794 | outbuf.data = 8; |
| 1795 | |
| 1796 | /* commit the record */ |
| 1797 | b_add(mbuf, outbuf.data); |
| 1798 | ret = 1; |
| 1799 | |
| 1800 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1801 | TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1802 | return ret; |
| 1803 | full: |
| 1804 | if ((mbuf = br_tail_add(fconn->mbuf)) != NULL) |
| 1805 | goto retry; |
| 1806 | fconn->flags |= FCGI_CF_MUX_MFULL; |
| 1807 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1808 | TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1809 | ret = 0; |
| 1810 | goto end; |
| 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | /* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It |
| 1815 | * marks the end of params. |
| 1816 | */ |
| 1817 | static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 1818 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1819 | int ret; |
| 1820 | |
| 1821 | TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm); |
| 1822 | ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS); |
| 1823 | if (ret) |
| 1824 | TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0}); |
| 1825 | return ret; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It |
| 1829 | * marks the end of input. On success, all the request was successfully sent. |
| 1830 | */ |
| 1831 | static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 1832 | { |
| 1833 | int ret; |
| 1834 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1835 | TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1836 | ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1837 | if (ret) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1838 | fstrm->flags |= FCGI_SF_ES_SENT; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1839 | TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0}); |
| 1840 | TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm); |
| 1841 | TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm); |
| 1842 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1843 | return ret; |
| 1844 | } |
| 1845 | |
| 1846 | /* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It |
| 1847 | * stops the request processing. |
| 1848 | */ |
| 1849 | static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 1850 | { |
| 1851 | int ret; |
| 1852 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1853 | TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1854 | ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1855 | if (ret) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1856 | fstrm->flags |= FCGI_SF_ABRT_SENT; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1857 | TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0}); |
| 1858 | TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm); |
| 1859 | TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm); |
| 1860 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1861 | return ret; |
| 1862 | } |
| 1863 | |
| 1864 | /* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do |
| 1865 | * anything. If there are too much K/V params to be encoded in a PARAMS record, |
| 1866 | * several records are sent. However, a K/V param cannot be split between 2 |
| 1867 | * records. |
| 1868 | */ |
| 1869 | static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 1870 | struct htx *htx) |
| 1871 | { |
| 1872 | struct buffer outbuf; |
| 1873 | struct buffer *mbuf; |
| 1874 | struct htx_blk *blk; |
| 1875 | struct htx_sl *sl = NULL; |
| 1876 | struct fcgi_strm_params params; |
| 1877 | size_t total = 0; |
| 1878 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1879 | TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx); |
| 1880 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1881 | memset(¶ms, 0, sizeof(params)); |
| 1882 | params.p = get_trash_chunk(); |
| 1883 | |
| 1884 | mbuf = br_tail(fconn->mbuf); |
| 1885 | retry: |
| 1886 | if (!fcgi_get_buf(fconn, mbuf)) { |
| 1887 | fconn->flags |= FCGI_CF_MUX_MALLOC; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 1888 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
| 1889 | TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
| 1890 | goto end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | while (1) { |
| 1894 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 1895 | if (outbuf.size >= 8 || !b_space_wraps(mbuf)) |
| 1896 | break; |
| 1897 | realign_again: |
| 1898 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
| 1899 | } |
| 1900 | |
| 1901 | if (outbuf.size < 8) |
| 1902 | goto full; |
| 1903 | |
| 1904 | /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id, |
| 1905 | * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */ |
| 1906 | memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8); |
| 1907 | fcgi_set_record_id(outbuf.area, fstrm->id); |
| 1908 | outbuf.data = 8; |
| 1909 | |
| 1910 | blk = htx_get_head_blk(htx); |
| 1911 | while (blk) { |
| 1912 | enum htx_blk_type type; |
| 1913 | uint32_t size = htx_get_blksz(blk); |
| 1914 | struct fcgi_param p; |
| 1915 | |
| 1916 | type = htx_get_blk_type(blk); |
| 1917 | switch (type) { |
| 1918 | case HTX_BLK_REQ_SL: |
| 1919 | sl = htx_get_blk_ptr(htx, blk); |
| 1920 | if (sl->info.req.meth == HTTP_METH_HEAD) |
| 1921 | fstrm->h1m.flags |= H1_MF_METH_HEAD; |
| 1922 | if (sl->flags & HTX_SL_F_VER_11) |
| 1923 | fstrm->h1m.flags |= H1_MF_VER_11; |
| 1924 | break; |
| 1925 | |
| 1926 | case HTX_BLK_HDR: |
| 1927 | p.n = htx_get_blk_name(htx, blk); |
| 1928 | p.v = htx_get_blk_value(htx, blk); |
| 1929 | |
| 1930 | if (istmatch(p.n, ist(":fcgi-"))) { |
| 1931 | p.n.ptr += 6; |
| 1932 | p.n.len -= 6; |
| 1933 | if (isteq(p.n, ist("gateway_interface"))) |
| 1934 | params.mask |= FCGI_SP_CGI_GATEWAY; |
| 1935 | else if (isteq(p.n, ist("document_root"))) { |
| 1936 | params.mask |= FCGI_SP_DOC_ROOT; |
| 1937 | params.docroot = p.v; |
| 1938 | } |
| 1939 | else if (isteq(p.n, ist("script_name"))) { |
| 1940 | params.mask |= FCGI_SP_SCRIPT_NAME; |
| 1941 | params.scriptname = p.v; |
| 1942 | } |
| 1943 | else if (isteq(p.n, ist("path_info"))) { |
| 1944 | params.mask |= FCGI_SP_PATH_INFO; |
| 1945 | params.pathinfo = p.v; |
| 1946 | } |
| 1947 | else if (isteq(p.n, ist("request_uri"))) { |
| 1948 | params.mask |= FCGI_SP_REQ_URI; |
| 1949 | params.uri = p.v; |
| 1950 | } |
| 1951 | else if (isteq(p.n, ist("request_meth"))) |
| 1952 | params.mask |= FCGI_SP_REQ_METH; |
| 1953 | else if (isteq(p.n, ist("query_string"))) |
| 1954 | params.mask |= FCGI_SP_REQ_QS; |
| 1955 | else if (isteq(p.n, ist("server_name"))) |
| 1956 | params.mask |= FCGI_SP_SRV_NAME; |
| 1957 | else if (isteq(p.n, ist("server_port"))) |
| 1958 | params.mask |= FCGI_SP_SRV_PORT; |
| 1959 | else if (isteq(p.n, ist("server_protocol"))) |
| 1960 | params.mask |= FCGI_SP_SRV_PROTO; |
| 1961 | else if (isteq(p.n, ist("remote_addr"))) |
| 1962 | params.mask |= FCGI_SP_REM_ADDR; |
| 1963 | else if (isteq(p.n, ist("remote_port"))) |
| 1964 | params.mask |= FCGI_SP_REM_PORT; |
| 1965 | else if (isteq(p.n, ist("script_filename"))) |
| 1966 | params.mask |= FCGI_SP_SCRIPT_FILE; |
| 1967 | else if (isteq(p.n, ist("path_translated"))) |
| 1968 | params.mask |= FCGI_SP_PATH_TRANS; |
| 1969 | else if (isteq(p.n, ist("https"))) |
| 1970 | params.mask |= FCGI_SP_HTTPS; |
| 1971 | } |
| 1972 | else if (isteq(p.n, ist("content-length"))) { |
| 1973 | p.n = ist("CONTENT_LENGTH"); |
| 1974 | params.mask |= FCGI_SP_CONT_LEN; |
| 1975 | } |
| 1976 | else if (isteq(p.n, ist("content-type"))) |
| 1977 | p.n = ist("CONTENT_TYPE"); |
| 1978 | else { |
| 1979 | if (isteq(p.n, ist("host"))) |
| 1980 | params.srv_name = p.v; |
| 1981 | |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 1982 | /* Skip header if same name is used to add the server name */ |
| 1983 | if (fconn->proxy->server_id_hdr_name && |
| 1984 | isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len))) |
| 1985 | break; |
| 1986 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 1987 | memcpy(trash.area, "http_", 5); |
| 1988 | memcpy(trash.area+5, p.n.ptr, p.n.len); |
| 1989 | p.n = ist2(trash.area, p.n.len+5); |
| 1990 | } |
| 1991 | |
| 1992 | if (!fcgi_encode_param(&outbuf, &p)) { |
| 1993 | if (b_space_wraps(mbuf)) |
| 1994 | goto realign_again; |
| 1995 | if (outbuf.data == 8) |
| 1996 | goto full; |
| 1997 | goto done; |
| 1998 | } |
| 1999 | break; |
| 2000 | |
| 2001 | case HTX_BLK_EOH: |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2002 | if (fconn->proxy->server_id_hdr_name) { |
| 2003 | struct server *srv = objt_server(fconn->conn->target); |
| 2004 | |
| 2005 | if (!srv) |
| 2006 | goto done; |
| 2007 | |
| 2008 | memcpy(trash.area, "http_", 5); |
| 2009 | memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len); |
| 2010 | p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5); |
| 2011 | p.v = ist(srv->id); |
| 2012 | |
| 2013 | if (!fcgi_encode_param(&outbuf, &p)) { |
| 2014 | if (b_space_wraps(mbuf)) |
| 2015 | goto realign_again; |
| 2016 | if (outbuf.data == 8) |
| 2017 | goto full; |
| 2018 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2019 | TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm); |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2020 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2021 | goto done; |
| 2022 | |
| 2023 | default: |
| 2024 | break; |
| 2025 | } |
| 2026 | total += size; |
| 2027 | blk = htx_remove_blk(htx, blk); |
| 2028 | } |
| 2029 | |
| 2030 | done: |
| 2031 | if (!fcgi_set_default_param(fconn, fstrm, htx, sl, ¶ms)) |
| 2032 | goto error; |
| 2033 | |
| 2034 | if (!fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_CGI_GATEWAY) || |
| 2035 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_DOC_ROOT) || |
| 2036 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_SCRIPT_NAME) || |
| 2037 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_PATH_INFO) || |
| 2038 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_REQ_URI) || |
| 2039 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_REQ_METH) || |
| 2040 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_REQ_QS) || |
| 2041 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_SRV_NAME) || |
| 2042 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_SRV_PORT) || |
| 2043 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_SRV_PROTO) || |
| 2044 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_REM_ADDR) || |
| 2045 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_REM_PORT) || |
| 2046 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_SCRIPT_FILE) || |
| 2047 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_PATH_TRANS) || |
| 2048 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_CONT_LEN) || |
| 2049 | !fcgi_encode_default_param(fconn, fstrm, ¶ms, &outbuf, FCGI_SP_HTTPS)) |
| 2050 | goto error; |
| 2051 | |
| 2052 | /* update the record's size */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2053 | TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2054 | fcgi_set_record_size(outbuf.area, outbuf.data - 8); |
| 2055 | b_add(mbuf, outbuf.data); |
| 2056 | |
| 2057 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2058 | TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2059 | return total; |
| 2060 | full: |
| 2061 | if ((mbuf = br_tail_add(fconn->mbuf)) != NULL) |
| 2062 | goto retry; |
| 2063 | fconn->flags |= FCGI_CF_MUX_MFULL; |
| 2064 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2065 | TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2066 | if (total) |
| 2067 | goto error; |
| 2068 | goto end; |
| 2069 | |
| 2070 | error: |
| 2071 | htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2072 | TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2073 | fcgi_strm_error(fstrm); |
| 2074 | goto end; |
| 2075 | } |
| 2076 | |
| 2077 | /* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do |
| 2078 | * anything. STDIN records contain the request body. |
| 2079 | */ |
| 2080 | static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 2081 | struct htx *htx, size_t count, struct buffer *buf) |
| 2082 | { |
| 2083 | struct buffer outbuf; |
| 2084 | struct buffer *mbuf; |
| 2085 | struct htx_blk *blk; |
| 2086 | enum htx_blk_type type; |
| 2087 | uint32_t size; |
| 2088 | size_t total = 0; |
| 2089 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2090 | TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2091 | if (!count) |
| 2092 | goto end; |
| 2093 | |
| 2094 | mbuf = br_tail(fconn->mbuf); |
| 2095 | retry: |
| 2096 | if (!fcgi_get_buf(fconn, mbuf)) { |
| 2097 | fconn->flags |= FCGI_CF_MUX_MALLOC; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2098 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
| 2099 | TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
| 2100 | goto end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | /* Perform some optimizations to reduce the number of buffer copies. |
| 2104 | * First, if the mux's buffer is empty and the htx area contains exactly |
| 2105 | * one data block of the same size as the requested count, and this |
| 2106 | * count fits within the record size, then it's possible to simply swap |
| 2107 | * the caller's buffer with the mux's output buffer and adjust offsets |
| 2108 | * and length to match the entire DATA HTX block in the middle. In this |
| 2109 | * case we perform a true zero-copy operation from end-to-end. This is |
| 2110 | * the situation that happens all the time with large files. Second, if |
| 2111 | * this is not possible, but the mux's output buffer is empty, we still |
| 2112 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 2113 | * making the intermediary buffer's area point to the output buffer's |
| 2114 | * area. In this case we want to skip the HTX header to make sure that |
| 2115 | * copies remain aligned and that this operation remains possible all |
| 2116 | * the time. This goes for headers, data blocks and any data extracted |
| 2117 | * from the HTX blocks. |
| 2118 | */ |
| 2119 | blk = htx_get_head_blk(htx); |
| 2120 | if (!blk) |
| 2121 | goto end; |
| 2122 | type = htx_get_blk_type(blk); |
| 2123 | size = htx_get_blksz(blk); |
| 2124 | if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) { |
| 2125 | void *old_area = mbuf->area; |
| 2126 | |
| 2127 | if (b_data(mbuf)) { |
| 2128 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 2129 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 2130 | * record into a buffer containing few data if it needs to be realigned, |
| 2131 | * and that it's also OK to copy few data without realigning. Otherwise |
| 2132 | * we'll pretend the mbuf is full and wait for it to become empty. |
| 2133 | */ |
| 2134 | if (size + 8 <= b_room(mbuf) && |
| 2135 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 2136 | (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf)))) |
| 2137 | goto copy; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2138 | goto full; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2141 | TRACE_PROTO("sending stding data (zero-copy)", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2142 | /* map a FCGI record to the HTX block so that we can put the |
| 2143 | * record header there. |
| 2144 | */ |
| 2145 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8); |
| 2146 | outbuf.area = b_head(mbuf); |
| 2147 | |
| 2148 | /* prepend a FCGI record header just before the DATA block */ |
| 2149 | memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8); |
| 2150 | fcgi_set_record_id(outbuf.area, fstrm->id); |
| 2151 | fcgi_set_record_size(outbuf.area, size); |
| 2152 | |
| 2153 | /* and exchange with our old area */ |
| 2154 | buf->area = old_area; |
| 2155 | buf->data = buf->head = 0; |
| 2156 | total += size; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2157 | |
| 2158 | htx = (struct htx *)buf->area; |
| 2159 | htx_reset(htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2160 | goto end; |
| 2161 | } |
| 2162 | |
| 2163 | copy: |
| 2164 | while (1) { |
| 2165 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 2166 | if (outbuf.size >= 8 || !b_space_wraps(mbuf)) |
| 2167 | break; |
| 2168 | realign_again: |
| 2169 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
| 2170 | } |
| 2171 | |
| 2172 | if (outbuf.size < 8) |
| 2173 | goto full; |
| 2174 | |
| 2175 | /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id, |
| 2176 | * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */ |
| 2177 | memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8); |
| 2178 | fcgi_set_record_id(outbuf.area, fstrm->id); |
| 2179 | outbuf.data = 8; |
| 2180 | |
| 2181 | blk = htx_get_head_blk(htx); |
| 2182 | while (blk && count) { |
| 2183 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2184 | uint32_t size = htx_get_blksz(blk); |
| 2185 | struct ist v; |
| 2186 | |
| 2187 | switch (type) { |
| 2188 | case HTX_BLK_DATA: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2189 | TRACE_PROTO("sending stding data", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2190 | v = htx_get_blk_value(htx, blk); |
| 2191 | if (v.len > count) |
| 2192 | v.len = count; |
| 2193 | |
| 2194 | if (v.len > b_room(&outbuf)) { |
| 2195 | /* It doesn't fit at once. If it at least fits once split and |
| 2196 | * the amount of data to move is low, let's defragment the |
| 2197 | * buffer now. |
| 2198 | */ |
| 2199 | if (b_space_wraps(mbuf) && |
| 2200 | b_data(&outbuf) + v.len <= b_room(mbuf) && |
| 2201 | b_data(mbuf) <= MAX_DATA_REALIGN) |
| 2202 | goto realign_again; |
| 2203 | v.len = b_room(&outbuf); |
| 2204 | } |
| 2205 | if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) { |
| 2206 | if (outbuf.data == 8) |
| 2207 | goto full; |
| 2208 | goto done; |
| 2209 | } |
| 2210 | if (v.len != size) { |
| 2211 | total += v.len; |
| 2212 | count -= v.len; |
| 2213 | htx_cut_data_blk(htx, blk, v.len); |
| 2214 | goto done; |
| 2215 | } |
| 2216 | break; |
| 2217 | |
| 2218 | case HTX_BLK_EOM: |
| 2219 | goto done; |
| 2220 | |
| 2221 | default: |
| 2222 | break; |
| 2223 | } |
| 2224 | total += size; |
| 2225 | count -= size; |
| 2226 | blk = htx_remove_blk(htx, blk); |
| 2227 | } |
| 2228 | |
| 2229 | done: |
| 2230 | /* update the record's size */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2231 | TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2232 | fcgi_set_record_size(outbuf.area, outbuf.data - 8); |
| 2233 | b_add(mbuf, outbuf.data); |
| 2234 | |
| 2235 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2236 | TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2237 | return total; |
| 2238 | full: |
| 2239 | if ((mbuf = br_tail_add(fconn->mbuf)) != NULL) |
| 2240 | goto retry; |
| 2241 | fconn->flags |= FCGI_CF_MUX_MFULL; |
| 2242 | fstrm->flags |= FCGI_SF_BLK_MROOM; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2243 | TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2244 | goto end; |
| 2245 | } |
| 2246 | |
| 2247 | /* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do |
| 2248 | * anything. STDOUT records contain the entire response. All the content is |
| 2249 | * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf(). |
| 2250 | */ |
| 2251 | static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 2252 | { |
| 2253 | struct buffer *dbuf; |
| 2254 | size_t ret; |
| 2255 | size_t max; |
| 2256 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2257 | TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
| 2258 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2259 | dbuf = &fconn->dbuf; |
| 2260 | |
| 2261 | /* Only padding remains */ |
| 2262 | if (fconn->state == FCGI_CS_RECORD_P) |
| 2263 | goto end_transfer; |
| 2264 | |
| 2265 | if (b_data(dbuf) < (fconn->drl + fconn->drp) && |
| 2266 | b_size(dbuf) > (fconn->drl + fconn->drp) && |
| 2267 | buf_room_for_htx_data(dbuf)) |
| 2268 | goto fail; // incomplete record |
| 2269 | |
| 2270 | if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) { |
| 2271 | fconn->flags |= FCGI_CF_DEM_SALLOC; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2272 | TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
| 2273 | goto fail; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/ |
| 2277 | max = buf_room_for_htx_data(&fstrm->rxbuf); |
| 2278 | if (!b_data(&fstrm->rxbuf)) |
| 2279 | fstrm->rxbuf.head = sizeof(struct htx); |
| 2280 | if (max > fconn->drl) |
| 2281 | max = fconn->drl; |
| 2282 | |
| 2283 | ret = b_xfer(&fstrm->rxbuf, dbuf, max); |
| 2284 | if (!ret) |
| 2285 | goto fail; |
| 2286 | fconn->drl -= ret; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2287 | TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret}); |
| 2288 | TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2289 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2290 | if (!buf_room_for_htx_data(&fstrm->rxbuf)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2291 | fconn->flags |= FCGI_CF_DEM_SFULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2292 | TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
| 2293 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2294 | |
| 2295 | if (fconn->drl) |
| 2296 | goto fail; |
| 2297 | |
| 2298 | end_transfer: |
| 2299 | fconn->drl += fconn->drp; |
| 2300 | fconn->drp = 0; |
| 2301 | ret = MIN(b_data(&fconn->dbuf), fconn->drl); |
| 2302 | b_del(&fconn->dbuf, ret); |
| 2303 | fconn->drl -= ret; |
| 2304 | if (fconn->drl) |
| 2305 | goto fail; |
| 2306 | |
| 2307 | fconn->state = FCGI_CS_RECORD_H; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2308 | TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm); |
| 2309 | TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2310 | return 1; |
| 2311 | fail: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2312 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | |
| 2317 | /* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do |
| 2318 | * anything. It only skip the padding in fact, there is no payload for such |
| 2319 | * records. It makrs the end of the response. |
| 2320 | */ |
| 2321 | static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 2322 | { |
| 2323 | int ret; |
| 2324 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2325 | TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
| 2326 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2327 | fconn->state = FCGI_CS_RECORD_P; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2328 | TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2329 | fconn->drl += fconn->drp; |
| 2330 | fconn->drp = 0; |
| 2331 | ret = MIN(b_data(&fconn->dbuf), fconn->drl); |
| 2332 | b_del(&fconn->dbuf, ret); |
| 2333 | fconn->drl -= ret; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2334 | if (fconn->drl) { |
| 2335 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2336 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2337 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2338 | fconn->state = FCGI_CS_RECORD_H; |
| 2339 | fstrm->state |= FCGI_SF_ES_RCVD; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2340 | TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0}); |
| 2341 | TRACE_STATE("stdout data fully send, switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_EOI, fconn->conn, fstrm); |
| 2342 | TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2343 | return 1; |
| 2344 | } |
| 2345 | |
| 2346 | /* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do |
| 2347 | * anything. |
| 2348 | */ |
| 2349 | static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 2350 | { |
| 2351 | struct buffer *dbuf; |
| 2352 | struct buffer tag; |
| 2353 | size_t ret; |
| 2354 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2355 | TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2356 | dbuf = &fconn->dbuf; |
| 2357 | |
| 2358 | /* Only padding remains */ |
| 2359 | if (fconn->state == FCGI_CS_RECORD_P) |
| 2360 | goto end_transfer; |
| 2361 | |
| 2362 | if (b_data(dbuf) < (fconn->drl + fconn->drp) && |
| 2363 | b_size(dbuf) > (fconn->drl + fconn->drp) && |
| 2364 | buf_room_for_htx_data(dbuf)) |
| 2365 | goto fail; // incomplete record |
| 2366 | |
| 2367 | chunk_reset(&trash); |
| 2368 | ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl)); |
| 2369 | if (!ret) |
| 2370 | goto fail; |
| 2371 | fconn->drl -= ret; |
Christopher Faulet | 08618a7 | 2019-10-08 11:59:47 +0200 | [diff] [blame] | 2372 | TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2373 | |
| 2374 | trash.area[ret] = '\n'; |
| 2375 | trash.area[ret+1] = '\0'; |
| 2376 | tag.area = fconn->app->name; tag.data = strlen(fconn->app->name); |
Christopher Faulet | c45791a | 2019-09-24 14:30:46 +0200 | [diff] [blame] | 2377 | app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2378 | |
| 2379 | if (fconn->drl) |
| 2380 | goto fail; |
| 2381 | |
| 2382 | end_transfer: |
| 2383 | fconn->drl += fconn->drp; |
| 2384 | fconn->drp = 0; |
| 2385 | ret = MIN(b_data(&fconn->dbuf), fconn->drl); |
| 2386 | b_del(&fconn->dbuf, ret); |
| 2387 | fconn->drl -= ret; |
| 2388 | if (fconn->drl) |
| 2389 | goto fail; |
| 2390 | fconn->state = FCGI_CS_RECORD_H; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2391 | TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm); |
| 2392 | TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2393 | return 1; |
| 2394 | fail: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2395 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2396 | return 0; |
| 2397 | } |
| 2398 | |
| 2399 | /* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do |
| 2400 | * anything. If the empty STDOUT record is not already received, this one marks |
| 2401 | * the end of the response. It is highly unexpected, but if the record is larger |
| 2402 | * than a buffer and cannot be decoded in one time, an error is triggered and |
| 2403 | * the connection is closed. END_REQUEST record cannot be split. |
| 2404 | */ |
| 2405 | static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm) |
| 2406 | { |
| 2407 | struct buffer inbuf; |
| 2408 | struct buffer *dbuf; |
| 2409 | struct fcgi_end_request endreq; |
| 2410 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2411 | TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2412 | dbuf = &fconn->dbuf; |
| 2413 | |
| 2414 | /* Record too large to be fully decoded */ |
| 2415 | if (b_size(dbuf) < (fconn->drl + fconn->drp)) |
| 2416 | goto fail; |
| 2417 | |
| 2418 | /* process full record only */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2419 | if (b_data(dbuf) < (fconn->drl + fconn->drp)) { |
| 2420 | TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2421 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2422 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2423 | |
| 2424 | if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) { |
| 2425 | /* Realign the dmux buffer if the record wraps. It is unexpected |
| 2426 | * at this stage because it should be the first record received |
| 2427 | * from the FCGI application. |
| 2428 | */ |
| 2429 | b_slow_realign(dbuf, trash.area, 0); |
| 2430 | } |
| 2431 | |
| 2432 | inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl); |
| 2433 | |
| 2434 | if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) |
| 2435 | goto fail; |
| 2436 | |
| 2437 | fstrm->flags |= FCGI_SF_ES_RCVD; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2438 | TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm); |
| 2439 | TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm,, (size_t[]){fconn->drl}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2440 | fstrm->proto_status = endreq.errcode; |
| 2441 | fcgi_strm_close(fstrm); |
| 2442 | |
| 2443 | b_del(&fconn->dbuf, fconn->drl + fconn->drp); |
| 2444 | fconn->drl = 0; |
| 2445 | fconn->drp = 0; |
| 2446 | fconn->state = FCGI_CS_RECORD_H; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2447 | TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm); |
| 2448 | TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2449 | return 1; |
| 2450 | |
| 2451 | fail: |
| 2452 | fcgi_strm_error(fstrm); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2453 | TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2454 | return 0; |
| 2455 | } |
| 2456 | |
| 2457 | /* process Rx records to be demultiplexed */ |
| 2458 | static void fcgi_process_demux(struct fcgi_conn *fconn) |
| 2459 | { |
| 2460 | struct fcgi_strm *fstrm = NULL, *tmp_fstrm; |
| 2461 | struct fcgi_header hdr; |
| 2462 | int ret; |
| 2463 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2464 | TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn); |
| 2465 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2466 | if (fconn->state == FCGI_CS_CLOSED) |
| 2467 | return; |
| 2468 | |
| 2469 | if (unlikely(fconn->state < FCGI_CS_RECORD_H)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2470 | if (fconn->state == FCGI_CS_INIT) { |
| 2471 | TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn); |
| 2472 | return; |
| 2473 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2474 | if (fconn->state == FCGI_CS_SETTINGS) { |
| 2475 | /* ensure that what is pending is a valid GET_VALUES_RESULT record. */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2476 | TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2477 | ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr); |
| 2478 | if (!ret) |
| 2479 | goto fail; |
| 2480 | b_del(&fconn->dbuf, ret); |
| 2481 | |
| 2482 | if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) { |
| 2483 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2484 | TRACE_PROTO("unexpected record type or flags", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn); |
| 2485 | TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2486 | goto fail; |
| 2487 | } |
| 2488 | goto new_record; |
| 2489 | } |
| 2490 | } |
| 2491 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2492 | /* process as many incoming records as possible below */ |
| 2493 | while (1) { |
| 2494 | if (!b_data(&fconn->dbuf)) { |
| 2495 | TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn); |
| 2496 | break; |
| 2497 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2498 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2499 | if (fconn->state == FCGI_CS_CLOSED) { |
| 2500 | TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2501 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2502 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2503 | |
| 2504 | if (fconn->state == FCGI_CS_RECORD_H) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2505 | TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2506 | ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr); |
| 2507 | if (!ret) |
| 2508 | break; |
| 2509 | b_del(&fconn->dbuf, ret); |
| 2510 | |
| 2511 | new_record: |
| 2512 | fconn->dsi = hdr.id; |
| 2513 | fconn->drt = hdr.type; |
| 2514 | fconn->drl = hdr.len; |
| 2515 | fconn->drp = hdr.padding; |
| 2516 | fconn->state = FCGI_CS_RECORD_D; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2517 | TRACE_STATE("FCGI record header rcvd, switching to RECORD_D", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */ |
| 2521 | tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi); |
| 2522 | |
| 2523 | if (tmp_fstrm != fstrm && fstrm && fstrm->cs && |
| 2524 | (b_data(&fstrm->rxbuf) || |
| 2525 | conn_xprt_read0_pending(fconn->conn) || |
| 2526 | fstrm->state == FCGI_SS_CLOSED || |
| 2527 | (fstrm->flags & FCGI_SF_ES_RCVD) || |
| 2528 | (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
| 2529 | /* we may have to signal the upper layers */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2530 | TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2531 | fstrm->cs->flags |= CS_FL_RCV_MORE; |
| 2532 | fcgi_strm_notify_recv(fstrm); |
| 2533 | } |
| 2534 | fstrm = tmp_fstrm; |
| 2535 | |
| 2536 | if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) { |
| 2537 | /* ignore all record for closed streams */ |
| 2538 | goto ignore_record; |
| 2539 | } |
| 2540 | if (fstrm->state == FCGI_SS_IDLE) { |
| 2541 | /* ignore all record for unknown streams */ |
| 2542 | goto ignore_record; |
| 2543 | } |
| 2544 | |
| 2545 | switch (fconn->drt) { |
| 2546 | case FCGI_GET_VALUES_RESULT: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2547 | TRACE_PROTO("receiving FCGI GET_VALUES_RESULT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2548 | ret = fcgi_conn_handle_values_result(fconn); |
| 2549 | break; |
| 2550 | |
| 2551 | case FCGI_STDOUT: |
| 2552 | if (fstrm->flags & FCGI_SF_ES_RCVD) |
| 2553 | goto ignore_record; |
| 2554 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2555 | TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2556 | if (fconn->drl) |
| 2557 | ret = fcgi_strm_handle_stdout(fconn, fstrm); |
| 2558 | else |
| 2559 | ret = fcgi_strm_handle_empty_stdout(fconn, fstrm); |
| 2560 | break; |
| 2561 | |
| 2562 | case FCGI_STDERR: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2563 | TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2564 | ret = fcgi_strm_handle_stderr(fconn, fstrm); |
| 2565 | break; |
| 2566 | |
| 2567 | case FCGI_END_REQUEST: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2568 | TRACE_PROTO("receiving FCGI END_REQUEST record", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2569 | ret = fcgi_strm_handle_end_request(fconn, fstrm); |
| 2570 | break; |
| 2571 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2572 | /* implement all extra record types here */ |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2573 | default: |
| 2574 | ignore_record: |
| 2575 | /* drop records that we ignore. They may be |
| 2576 | * larger than the buffer so we drain all of |
| 2577 | * their contents until we reach the end. |
| 2578 | */ |
| 2579 | fconn->state = FCGI_CS_RECORD_P; |
| 2580 | fconn->drl += fconn->drp; |
| 2581 | fconn->drp = 0; |
| 2582 | ret = MIN(b_data(&fconn->dbuf), fconn->drl); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2583 | TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret}); |
| 2584 | TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2585 | b_del(&fconn->dbuf, ret); |
| 2586 | fconn->drl -= ret; |
| 2587 | ret = (fconn->drl == 0); |
| 2588 | } |
| 2589 | |
| 2590 | /* error or missing data condition met above ? */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2591 | if (ret <= 0) { |
| 2592 | TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2593 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2594 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2595 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2596 | if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2597 | fconn->state = FCGI_CS_RECORD_H; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2598 | TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
| 2599 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | fail: |
| 2603 | /* we can go here on missing data, blocked response or error */ |
| 2604 | if (fstrm && fstrm->cs && |
| 2605 | (b_data(&fstrm->rxbuf) || |
| 2606 | conn_xprt_read0_pending(fconn->conn) || |
| 2607 | fstrm->state == FCGI_SS_CLOSED || |
| 2608 | (fstrm->flags & FCGI_SF_ES_RCVD) || |
| 2609 | (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
| 2610 | /* we may have to signal the upper layers */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2611 | TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2612 | fstrm->cs->flags |= CS_FL_RCV_MORE; |
| 2613 | fcgi_strm_notify_recv(fstrm); |
| 2614 | } |
| 2615 | |
| 2616 | fcgi_conn_restart_reading(fconn, 0); |
| 2617 | } |
| 2618 | |
| 2619 | /* process Tx records from streams to be multiplexed. Returns > 0 if it reached |
| 2620 | * the end. |
| 2621 | */ |
| 2622 | static int fcgi_process_mux(struct fcgi_conn *fconn) |
| 2623 | { |
| 2624 | struct fcgi_strm *fstrm, *fstrm_back; |
| 2625 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2626 | TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn); |
| 2627 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2628 | if (unlikely(fconn->state < FCGI_CS_RECORD_H)) { |
| 2629 | if (unlikely(fconn->state == FCGI_CS_INIT)) { |
| 2630 | if (!(fconn->flags & FCGI_CF_GET_VALUES)) { |
| 2631 | fconn->state = FCGI_CS_RECORD_H; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2632 | TRACE_STATE("switching to RECORD_H", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2633 | fcgi_wake_unassigned_streams(fconn); |
| 2634 | goto mux; |
| 2635 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2636 | TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2637 | if (unlikely(!fcgi_conn_send_get_values(fconn))) |
| 2638 | goto fail; |
| 2639 | fconn->state = FCGI_CS_SETTINGS; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2640 | TRACE_STATE("switching to SETTINGS", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2641 | } |
| 2642 | /* need to wait for the other side */ |
| 2643 | if (fconn->state < FCGI_CS_RECORD_H) |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2644 | goto done; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | mux: |
| 2648 | list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) { |
| 2649 | if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY) |
| 2650 | break; |
| 2651 | |
| 2652 | if (LIST_ADDED(&fstrm->sending_list)) |
| 2653 | continue; |
| 2654 | |
| 2655 | /* For some reason, the upper layer failed to subsribe again, |
| 2656 | * so remove it from the send_list |
| 2657 | */ |
| 2658 | if (!fstrm->send_wait) { |
| 2659 | LIST_DEL_INIT(&fstrm->send_list); |
| 2660 | continue; |
| 2661 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2662 | TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2663 | fstrm->flags &= ~FCGI_SF_BLK_ANY; |
| 2664 | fstrm->send_wait->events &= ~SUB_RETRY_SEND; |
| 2665 | LIST_ADDQ(&fconn->sending_list, &fstrm->sending_list); |
| 2666 | tasklet_wakeup(fstrm->send_wait->tasklet); |
| 2667 | } |
| 2668 | |
| 2669 | fail: |
| 2670 | if (fconn->state == FCGI_CS_CLOSED) { |
| 2671 | if (fconn->stream_cnt - fconn->nb_reserved > 0) { |
| 2672 | fcgi_conn_send_aborts(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2673 | if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) { |
| 2674 | TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2675 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2676 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2677 | } |
| 2678 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2679 | |
| 2680 | done: |
| 2681 | TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2682 | return 1; |
| 2683 | } |
| 2684 | |
| 2685 | |
| 2686 | /* Attempt to read data, and subscribe if none available. |
| 2687 | * The function returns 1 if data has been received, otherwise zero. |
| 2688 | */ |
| 2689 | static int fcgi_recv(struct fcgi_conn *fconn) |
| 2690 | { |
| 2691 | struct connection *conn = fconn->conn; |
| 2692 | struct buffer *buf; |
| 2693 | int max; |
| 2694 | size_t ret; |
| 2695 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2696 | TRACE_ENTER(FCGI_EV_FCONN_RECV, conn); |
| 2697 | |
| 2698 | if (fconn->wait_event.events & SUB_RETRY_RECV) { |
| 2699 | TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2700 | return (b_data(&fconn->dbuf)); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2701 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2702 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2703 | if (!fcgi_recv_allowed(fconn)) { |
| 2704 | TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2705 | return 1; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2706 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2707 | |
| 2708 | buf = fcgi_get_buf(fconn, &fconn->dbuf); |
| 2709 | if (!buf) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2710 | TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2711 | fconn->flags |= FCGI_CF_DEM_DALLOC; |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
| 2715 | b_realign_if_empty(buf); |
| 2716 | if (!b_data(buf)) { |
| 2717 | /* try to pre-align the buffer like the |
| 2718 | * rxbufs will be to optimize memory copies. We'll make |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2719 | * sure that the record header lands at the end of the |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2720 | * HTX block to alias it upon recv. We cannot use the |
| 2721 | * head because rcv_buf() will realign the buffer if |
| 2722 | * it's empty. Thus we cheat and pretend we already |
| 2723 | * have a few bytes there. |
| 2724 | */ |
| 2725 | max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0); |
| 2726 | buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0); |
| 2727 | } |
| 2728 | else |
| 2729 | max = buf_room_for_htx_data(buf); |
| 2730 | |
| 2731 | ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0; |
| 2732 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2733 | if (max && !ret && fcgi_recv_allowed(fconn)) { |
| 2734 | TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2735 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2736 | } |
| 2737 | else |
| 2738 | TRACE_DATA("send data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2739 | |
| 2740 | if (!b_data(buf)) { |
| 2741 | fcgi_release_buf(fconn, &fconn->dbuf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2742 | TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2743 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
| 2744 | } |
| 2745 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2746 | if (ret == max) { |
| 2747 | TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2748 | fconn->flags |= FCGI_CF_DEM_DFULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2749 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2750 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2751 | TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2752 | return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn); |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | /* Try to send data if possible. |
| 2757 | * The function returns 1 if data have been sent, otherwise zero. |
| 2758 | */ |
| 2759 | static int fcgi_send(struct fcgi_conn *fconn) |
| 2760 | { |
| 2761 | struct connection *conn = fconn->conn; |
| 2762 | int done; |
| 2763 | int sent = 0; |
| 2764 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2765 | TRACE_ENTER(FCGI_EV_FCONN_SEND, conn); |
| 2766 | |
| 2767 | if (conn->flags & CO_FL_ERROR) { |
| 2768 | TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2769 | return 1; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2770 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2771 | |
| 2772 | |
| 2773 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2774 | /* a handshake was requested */ |
| 2775 | goto schedule; |
| 2776 | } |
| 2777 | |
| 2778 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2779 | * pending streams into the existing buffer until it's reportedly full |
| 2780 | * or the end of send requests is reached. Then it tries to send this |
| 2781 | * buffer's contents out, marks it not full if at least one byte could |
| 2782 | * be sent, and tries again. |
| 2783 | * |
| 2784 | * The snd_buf() function normally takes a "flags" argument which may |
| 2785 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2786 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2787 | * connection is streaming lots of data (used to increase TLS record |
| 2788 | * size at the expense of latency). The former can be sent any time |
| 2789 | * there's a buffer full flag, as it indicates at least one stream |
| 2790 | * attempted to send and failed so there are pending data. An |
| 2791 | * alternative would be to set it as long as there's an active stream |
| 2792 | * but that would be problematic for ACKs until we have an absolute |
| 2793 | * guarantee that all waiters have at least one byte to send. The |
| 2794 | * latter should possibly not be set for now. |
| 2795 | */ |
| 2796 | |
| 2797 | done = 0; |
| 2798 | while (!done) { |
| 2799 | unsigned int flags = 0; |
| 2800 | unsigned int released = 0; |
| 2801 | struct buffer *buf; |
| 2802 | |
| 2803 | /* fill as much as we can into the current buffer */ |
| 2804 | while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done) |
| 2805 | done = fcgi_process_mux(fconn); |
| 2806 | |
| 2807 | if (fconn->flags & FCGI_CF_MUX_MALLOC) |
| 2808 | done = 1; // we won't go further without extra buffers |
| 2809 | |
| 2810 | if (conn->flags & CO_FL_ERROR) |
| 2811 | break; |
| 2812 | |
| 2813 | if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) |
| 2814 | flags |= CO_SFL_MSG_MORE; |
| 2815 | |
| 2816 | for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) { |
| 2817 | if (b_data(buf)) { |
| 2818 | int ret; |
| 2819 | |
| 2820 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
| 2821 | if (!ret) { |
| 2822 | done = 1; |
| 2823 | break; |
| 2824 | } |
| 2825 | sent = 1; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2826 | TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2827 | b_del(buf, ret); |
| 2828 | if (b_data(buf)) { |
| 2829 | done = 1; |
| 2830 | break; |
| 2831 | } |
| 2832 | } |
| 2833 | b_free(buf); |
| 2834 | released++; |
| 2835 | } |
| 2836 | |
| 2837 | if (released) |
| 2838 | offer_buffers(NULL, tasks_run_queue); |
| 2839 | |
| 2840 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2841 | if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) |
| 2842 | TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2843 | fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM); |
| 2844 | } |
| 2845 | |
| 2846 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2847 | /* output closed, nothing to send, clear the buffer to release it */ |
| 2848 | b_reset(br_tail(fconn->mbuf)); |
| 2849 | } |
| 2850 | /* We're not full anymore, so we can wake any task that are waiting |
| 2851 | * for us. |
| 2852 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2853 | if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2854 | struct fcgi_strm *fstrm; |
| 2855 | |
| 2856 | list_for_each_entry(fstrm, &fconn->send_list, send_list) { |
| 2857 | if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY) |
| 2858 | break; |
| 2859 | |
| 2860 | if (LIST_ADDED(&fstrm->sending_list)) |
| 2861 | continue; |
| 2862 | |
| 2863 | /* For some reason, the upper layer failed to subsribe again, |
| 2864 | * so remove it from the send_list |
| 2865 | */ |
| 2866 | if (!fstrm->send_wait) { |
| 2867 | LIST_DEL_INIT(&fstrm->send_list); |
| 2868 | continue; |
| 2869 | } |
| 2870 | fstrm->flags &= ~FCGI_SF_BLK_ANY; |
| 2871 | fstrm->send_wait->events &= ~SUB_RETRY_SEND; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2872 | TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2873 | tasklet_wakeup(fstrm->send_wait->tasklet); |
| 2874 | LIST_ADDQ(&fconn->sending_list, &fstrm->sending_list); |
| 2875 | } |
| 2876 | } |
| 2877 | /* We're done, no more to send */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2878 | if (!br_data(fconn->mbuf)) { |
| 2879 | TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2880 | return sent; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2881 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2882 | schedule: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2883 | if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) { |
| 2884 | TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2885 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2886 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2887 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2888 | TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2889 | return sent; |
| 2890 | } |
| 2891 | |
| 2892 | /* this is the tasklet referenced in fconn->wait_event.tasklet */ |
| 2893 | static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2894 | { |
| 2895 | struct fcgi_conn *fconn = ctx; |
| 2896 | int ret = 0; |
| 2897 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2898 | TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn); |
| 2899 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2900 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 2901 | ret = fcgi_send(fconn); |
| 2902 | if (!(fconn->wait_event.events & SUB_RETRY_RECV)) |
| 2903 | ret |= fcgi_recv(fconn); |
| 2904 | if (ret || b_data(&fconn->dbuf)) |
| 2905 | fcgi_process(fconn); |
| 2906 | return NULL; |
| 2907 | } |
| 2908 | |
| 2909 | /* callback called on any event by the connection handler. |
| 2910 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2911 | * destruction of the connection (which normally doesn not happen in FCGI). |
| 2912 | */ |
| 2913 | static int fcgi_process(struct fcgi_conn *fconn) |
| 2914 | { |
| 2915 | struct connection *conn = fconn->conn; |
| 2916 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2917 | TRACE_POINT(FCGI_EV_FCONN_WAKE, conn); |
| 2918 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2919 | if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) { |
| 2920 | fcgi_process_demux(fconn); |
| 2921 | |
| 2922 | if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR) |
| 2923 | b_reset(&fconn->dbuf); |
| 2924 | |
| 2925 | if (buf_room_for_htx_data(&fconn->dbuf)) |
| 2926 | fconn->flags &= ~FCGI_CF_DEM_DFULL; |
| 2927 | } |
| 2928 | fcgi_send(fconn); |
| 2929 | |
| 2930 | if (unlikely(fconn->proxy->state == PR_STSTOPPED)) { |
| 2931 | /* frontend is stopping, reload likely in progress, let's try |
| 2932 | * to announce a graceful shutdown if not yet done. We don't |
| 2933 | * care if it fails, it will be tried again later. |
| 2934 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2935 | TRACE_STATE("proxy stopped, sending ABORT to all streams", FCGI_EV_FCONN_WAKE|FCGI_EV_TX_RECORD, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2936 | if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 2937 | if (fconn->stream_cnt - fconn->nb_reserved > 0) |
| 2938 | fcgi_conn_send_aborts(fconn); |
| 2939 | } |
| 2940 | } |
| 2941 | |
| 2942 | /* |
| 2943 | * If we received early data, and the handshake is done, wake |
| 2944 | * any stream that was waiting for it. |
| 2945 | */ |
| 2946 | if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) && |
| 2947 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2948 | struct eb32_node *node; |
| 2949 | struct fcgi_strm *fstrm; |
| 2950 | |
| 2951 | fconn->flags |= FCGI_CF_WAIT_FOR_HS; |
| 2952 | node = eb32_lookup_ge(&fconn->streams_by_id, 1); |
| 2953 | |
| 2954 | while (node) { |
| 2955 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 2956 | if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS) |
| 2957 | fcgi_strm_notify_recv(fstrm); |
| 2958 | node = eb32_next(node); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || |
| 2963 | fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) || |
| 2964 | eb_is_empty(&fconn->streams_by_id)) { |
| 2965 | fcgi_wake_some_streams(fconn, 0); |
| 2966 | |
| 2967 | if (eb_is_empty(&fconn->streams_by_id)) { |
| 2968 | /* no more stream, kill the connection now */ |
| 2969 | fcgi_release(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2970 | TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2971 | return -1; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | if (!b_data(&fconn->dbuf)) |
| 2976 | fcgi_release_buf(fconn, &fconn->dbuf); |
| 2977 | |
| 2978 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2979 | fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) || |
| 2980 | (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list)))) |
| 2981 | fcgi_release_mbuf(fconn); |
| 2982 | |
| 2983 | if (fconn->task) { |
| 2984 | fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout)); |
| 2985 | task_queue(fconn->task); |
| 2986 | } |
| 2987 | |
| 2988 | fcgi_send(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2989 | TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2990 | return 0; |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
| 2995 | static int fcgi_wake(struct connection *conn) |
| 2996 | { |
| 2997 | struct fcgi_conn *fconn = conn->ctx; |
| 2998 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2999 | TRACE_POINT(FCGI_EV_FCONN_WAKE, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3000 | return (fcgi_process(fconn)); |
| 3001 | } |
| 3002 | |
| 3003 | /* Connection timeout management. The principle is that if there's no receipt |
| 3004 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3005 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3006 | * immediately killed. If it's allocatable and empty, we attempt to send a |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3007 | * ABORT records. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3008 | */ |
| 3009 | static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state) |
| 3010 | { |
| 3011 | struct fcgi_conn *fconn = context; |
| 3012 | int expired = tick_is_expired(t->expire, now_ms); |
| 3013 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3014 | TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL)); |
| 3015 | |
| 3016 | if (!expired && fconn) { |
| 3017 | TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3018 | return t; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3019 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3020 | |
| 3021 | task_destroy(t); |
| 3022 | |
| 3023 | if (!fconn) { |
| 3024 | /* resources were already deleted */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3025 | TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3026 | return NULL; |
| 3027 | } |
| 3028 | |
| 3029 | fconn->task = NULL; |
| 3030 | fconn->state = FCGI_CS_CLOSED; |
| 3031 | fcgi_wake_some_streams(fconn, 0); |
| 3032 | |
| 3033 | if (br_data(fconn->mbuf)) { |
| 3034 | /* don't even try to send aborts, the buffer is stuck */ |
| 3035 | fconn->flags |= FCGI_CF_ABRTS_FAILED; |
| 3036 | goto end; |
| 3037 | } |
| 3038 | |
| 3039 | /* try to send but no need to insist */ |
| 3040 | if (!fcgi_conn_send_aborts(fconn)) |
| 3041 | fconn->flags |= FCGI_CF_ABRTS_FAILED; |
| 3042 | |
| 3043 | if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) && |
| 3044 | conn_xprt_ready(fconn->conn)) { |
| 3045 | unsigned int released = 0; |
| 3046 | struct buffer *buf; |
| 3047 | |
| 3048 | for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) { |
| 3049 | if (b_data(buf)) { |
| 3050 | int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx, |
| 3051 | buf, b_data(buf), 0); |
| 3052 | if (!ret) |
| 3053 | break; |
| 3054 | b_del(buf, ret); |
| 3055 | if (b_data(buf)) |
| 3056 | break; |
| 3057 | b_free(buf); |
| 3058 | released++; |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | if (released) |
| 3063 | offer_buffers(NULL, tasks_run_queue); |
| 3064 | } |
| 3065 | |
| 3066 | end: |
| 3067 | /* either we can release everything now or it will be done later once |
| 3068 | * the last stream closes. |
| 3069 | */ |
| 3070 | if (eb_is_empty(&fconn->streams_by_id)) |
| 3071 | fcgi_release(fconn); |
| 3072 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3073 | TRACE_LEAVE(FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3074 | return NULL; |
| 3075 | } |
| 3076 | |
| 3077 | |
| 3078 | /*******************************************/ |
| 3079 | /* functions below are used by the streams */ |
| 3080 | /*******************************************/ |
| 3081 | |
| 3082 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 3083 | * The description must be small enough to always fit in a buffer. The output |
| 3084 | * buffer may be the trash so the trash must not be used inside this function. |
| 3085 | */ |
| 3086 | static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 3087 | { |
| 3088 | chunk_appendf(out, |
| 3089 | " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n" |
| 3090 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 3091 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 3092 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 3093 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 3094 | es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen); |
| 3095 | } |
| 3096 | /* |
| 3097 | * Capture a bad response and archive it in the proxy's structure. By default |
| 3098 | * it tries to report the error position as h1m->err_pos. However if this one is |
| 3099 | * not set, it will then report h1m->next, which is the last known parsing |
| 3100 | * point. The function is able to deal with wrapping buffers. It always displays |
| 3101 | * buffers as a contiguous area starting at buf->p. The direction is determined |
| 3102 | * thanks to the h1m's flags. |
| 3103 | */ |
| 3104 | static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 3105 | struct h1m *h1m, struct buffer *buf) |
| 3106 | { |
| 3107 | struct session *sess = fstrm->sess; |
| 3108 | struct proxy *proxy = fconn->proxy; |
| 3109 | struct proxy *other_end = sess->fe; |
| 3110 | union error_snapshot_ctx ctx; |
| 3111 | |
| 3112 | /* http-specific part now */ |
| 3113 | ctx.h1.state = h1m->state; |
| 3114 | ctx.h1.c_flags = fconn->flags; |
| 3115 | ctx.h1.s_flags = fstrm->flags; |
| 3116 | ctx.h1.m_flags = h1m->flags; |
| 3117 | ctx.h1.m_clen = h1m->curr_len; |
| 3118 | ctx.h1.m_blen = h1m->body_len; |
| 3119 | |
| 3120 | proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0, |
| 3121 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 3122 | &ctx, fcgi_show_error_snapshot); |
| 3123 | } |
| 3124 | |
| 3125 | static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, |
| 3126 | struct buffer *buf, size_t *ofs, size_t max) |
| 3127 | { |
| 3128 | int ret; |
| 3129 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3130 | TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3131 | ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max); |
| 3132 | if (!ret) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3133 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3134 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3135 | TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3136 | fcgi_strm_error(fstrm); |
| 3137 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3138 | } |
| 3139 | goto end; |
| 3140 | } |
| 3141 | |
| 3142 | *ofs += ret; |
| 3143 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3144 | TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3145 | return ret; |
| 3146 | |
| 3147 | } |
| 3148 | |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 3149 | static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx, |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3150 | struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf) |
| 3151 | { |
| 3152 | int ret; |
| 3153 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3154 | TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3155 | ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); |
| 3156 | if (ret <= 0) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3157 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm); |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 3158 | if ((*htx)->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3159 | TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3160 | fcgi_strm_error(fstrm); |
| 3161 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3162 | } |
| 3163 | goto end; |
| 3164 | } |
| 3165 | *ofs += ret; |
| 3166 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3167 | TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3168 | return ret; |
| 3169 | } |
| 3170 | |
| 3171 | static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, |
| 3172 | struct buffer *buf, size_t *ofs, size_t max) |
| 3173 | { |
| 3174 | int ret; |
| 3175 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3176 | TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3177 | ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); |
| 3178 | if (ret <= 0) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3179 | TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3180 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3181 | TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3182 | fcgi_strm_error(fstrm); |
| 3183 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3184 | } |
| 3185 | goto end; |
| 3186 | } |
| 3187 | *ofs += ret; |
| 3188 | fstrm->flags |= FCGI_SF_HAVE_I_TLR; |
| 3189 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3190 | TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3191 | return ret; |
| 3192 | } |
| 3193 | |
| 3194 | static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, |
| 3195 | size_t max) |
| 3196 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3197 | TRACE_ENTER(FCGI_EV_RSP_DATA, fstrm->fconn->conn, fstrm,, (size_t[]){max}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3198 | if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) |
| 3199 | return 0; |
| 3200 | |
| 3201 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3202 | TRACE_STATE("end of response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm); |
| 3203 | TRACE_LEAVE(FCGI_EV_RSP_DATA, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3204 | return (sizeof(struct htx_blk) + 1); |
| 3205 | } |
| 3206 | |
| 3207 | static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count) |
| 3208 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3209 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3210 | struct htx *htx; |
| 3211 | struct h1m *h1m = &fstrm->h1m; |
| 3212 | size_t ret, data, total = 0; |
| 3213 | |
| 3214 | htx = htx_from_buf(buf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3215 | TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count}); |
| 3216 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3217 | data = htx->data; |
| 3218 | if (fstrm->state == FCGI_SS_ERROR) |
| 3219 | goto end; |
| 3220 | |
| 3221 | do { |
| 3222 | size_t used = htx_used_space(htx); |
| 3223 | |
| 3224 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3225 | TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3226 | ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count); |
| 3227 | if (!ret) |
| 3228 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3229 | |
| 3230 | TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx); |
| 3231 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3232 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) { |
| 3233 | struct htx_blk *blk = htx_get_head_blk(htx); |
| 3234 | struct htx_sl *sl; |
| 3235 | |
| 3236 | if (!blk) |
| 3237 | break; |
| 3238 | sl = htx_get_blk_ptr(htx, blk); |
| 3239 | sl->flags |= HTX_SL_F_XFER_LEN; |
| 3240 | htx->extra = 0; |
| 3241 | } |
| 3242 | } |
| 3243 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3244 | TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm); |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 3245 | ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3246 | if (!ret) |
| 3247 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3248 | |
| 3249 | TRACE_PROTO("rcvd response payload data", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm, htx); |
| 3250 | |
| 3251 | if (h1m->state == H1_MSG_DONE) |
| 3252 | TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3253 | } |
| 3254 | else if (h1m->state == H1_MSG_TRAILERS) { |
| 3255 | if (!(fstrm->flags & FCGI_SF_HAVE_I_TLR)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3256 | TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3257 | ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count); |
| 3258 | if (!ret) |
| 3259 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3260 | |
| 3261 | TRACE_PROTO("rcvd H1 response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3262 | } |
| 3263 | else if (!fcgi_strm_add_eom(fstrm, h1m, htx, count)) |
| 3264 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3265 | |
| 3266 | if (h1m->state == H1_MSG_DONE) |
| 3267 | TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3268 | } |
| 3269 | else if (h1m->state == H1_MSG_DONE) { |
| 3270 | if (b_data(&fstrm->rxbuf) > total) { |
| 3271 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3272 | TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3273 | fcgi_strm_error(fstrm); |
| 3274 | } |
| 3275 | break; |
| 3276 | } |
| 3277 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3278 | TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm); |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 3279 | ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3280 | if (fstrm->state != FCGI_SS_ERROR && |
| 3281 | (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3282 | TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3283 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) { |
| 3284 | if (!fcgi_strm_add_eom(fstrm, h1m, htx, count)) |
| 3285 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3286 | TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3287 | } |
| 3288 | else { |
| 3289 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3290 | TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | if (!ret) |
| 3295 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3296 | |
| 3297 | TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3298 | } |
| 3299 | else { |
| 3300 | htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3301 | TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3302 | fcgi_strm_error(fstrm); |
| 3303 | break; |
| 3304 | } |
| 3305 | |
| 3306 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3307 | } while (fstrm->state != FCGI_SS_ERROR); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3308 | |
| 3309 | if (fstrm->state == FCGI_SS_ERROR) { |
| 3310 | b_reset(&fstrm->rxbuf); |
| 3311 | htx_to_buf(htx, buf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3312 | TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3313 | return 0; |
| 3314 | } |
| 3315 | |
| 3316 | b_del(&fstrm->rxbuf, total); |
| 3317 | |
| 3318 | end: |
| 3319 | htx_to_buf(htx, buf); |
| 3320 | ret = htx->data - data; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3321 | TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3322 | return ret; |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * Attach a new stream to a connection |
| 3327 | * (Used for outgoing connections) |
| 3328 | */ |
| 3329 | static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess) |
| 3330 | { |
| 3331 | struct conn_stream *cs; |
| 3332 | struct fcgi_strm *fstrm; |
| 3333 | struct fcgi_conn *fconn = conn->ctx; |
| 3334 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3335 | TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3336 | cs = cs_new(conn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3337 | if (!cs) { |
| 3338 | TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3339 | return NULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3340 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3341 | fstrm = fcgi_conn_stream_new(fconn, cs, sess); |
| 3342 | if (!fstrm) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3343 | TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3344 | cs_free(cs); |
| 3345 | return NULL; |
| 3346 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3347 | TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3348 | return cs; |
| 3349 | } |
| 3350 | |
| 3351 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3352 | * We have to scan because we may have some orphan streams. It might be |
| 3353 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3354 | * orphans. |
| 3355 | */ |
| 3356 | static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn) |
| 3357 | { |
| 3358 | struct fcgi_conn *fconn = conn->ctx; |
| 3359 | struct fcgi_strm *fstrm; |
| 3360 | struct eb32_node *node; |
| 3361 | |
| 3362 | node = eb32_first(&fconn->streams_by_id); |
| 3363 | while (node) { |
| 3364 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 3365 | if (fstrm->cs) |
| 3366 | return fstrm->cs; |
| 3367 | node = eb32_next(node); |
| 3368 | } |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * Destroy the mux and the associated connection, if it is no longer used |
| 3374 | */ |
| 3375 | static void fcgi_destroy(void *ctx) |
| 3376 | { |
| 3377 | struct fcgi_conn *fconn = ctx; |
| 3378 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3379 | TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3380 | if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn) |
| 3381 | fcgi_release(fconn); |
| 3382 | } |
| 3383 | |
| 3384 | /* |
| 3385 | * Detach the stream from the connection and possibly release the connection. |
| 3386 | */ |
| 3387 | static void fcgi_detach(struct conn_stream *cs) |
| 3388 | { |
| 3389 | struct fcgi_strm *fstrm = cs->ctx; |
| 3390 | struct fcgi_conn *fconn; |
| 3391 | struct session *sess; |
| 3392 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3393 | TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm); |
| 3394 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3395 | cs->ctx = NULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3396 | if (!fstrm) { |
| 3397 | TRACE_LEAVE(FCGI_EV_STRM_END); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3398 | return; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3399 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3400 | |
| 3401 | /* The stream is about to die, so no need to attempt to run its task */ |
| 3402 | if (LIST_ADDED(&fstrm->sending_list) && |
| 3403 | fstrm->send_wait != &fstrm->wait_event) { |
| 3404 | tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet); |
| 3405 | LIST_DEL_INIT(&fstrm->sending_list); |
| 3406 | /* |
| 3407 | * At this point, the stream_interface is supposed to have called |
| 3408 | * fcgi_unsubscribe(), so the only way there's still a |
| 3409 | * subscription that came from the stream_interface (as we |
| 3410 | * can subscribe ourself, in fcgi_do_shutw() and fcgi_do_shutr(), |
| 3411 | * without the stream_interface involved) is that we subscribed |
| 3412 | * for sending, we woke the tasklet up and removed the |
| 3413 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3414 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3415 | * run yet. Make sure to handle that by explicitely setting |
| 3416 | * send_wait to NULL, as nothing else will do it for us. |
| 3417 | */ |
| 3418 | fstrm->send_wait = NULL; |
| 3419 | } |
| 3420 | |
| 3421 | sess = fstrm->sess; |
| 3422 | fconn = fstrm->fconn; |
| 3423 | fstrm->cs = NULL; |
| 3424 | fconn->nb_cs--; |
| 3425 | |
| 3426 | if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) { |
| 3427 | fconn->flags &= ~FCGI_CF_MPXS_CONNS; |
| 3428 | fconn->streams_limit = 1; |
| 3429 | } |
| 3430 | else if (fstrm->proto_status == FCGI_PS_OVERLOADED || |
| 3431 | fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) { |
| 3432 | fconn->flags &= ~FCGI_CF_KEEP_CONN; |
| 3433 | fconn->state = FCGI_CS_CLOSED; |
| 3434 | } |
| 3435 | |
| 3436 | /* this stream may be blocked waiting for some data to leave, so orphan |
| 3437 | * it in this case. |
| 3438 | */ |
| 3439 | if (!(cs->conn->flags & CO_FL_ERROR) && |
| 3440 | (fconn->state != FCGI_CS_CLOSED) && |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3441 | (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) && (fstrm->send_wait || fstrm->recv_wait)) { |
| 3442 | TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3443 | return; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3444 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3445 | |
| 3446 | if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) { |
| 3447 | /* unblock the connection if it was blocked on this stream. */ |
| 3448 | fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY; |
| 3449 | fcgi_conn_restart_reading(fconn, 1); |
| 3450 | } |
| 3451 | |
| 3452 | fcgi_strm_destroy(fstrm); |
| 3453 | |
| 3454 | if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && |
| 3455 | !(fconn->flags & FCGI_CF_KEEP_CONN)) { |
| 3456 | if (!fconn->conn->owner) { |
| 3457 | fconn->conn->owner = sess; |
| 3458 | if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) { |
| 3459 | fconn->conn->owner = NULL; |
| 3460 | if (eb_is_empty(&fconn->streams_by_id)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3461 | if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3462 | /* The server doesn't want it, let's kill the connection right away */ |
| 3463 | fconn->conn->mux->destroy(fconn->conn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3464 | TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR); |
| 3465 | } |
| 3466 | TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3467 | return; |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | if (eb_is_empty(&fconn->streams_by_id)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3472 | int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn); |
| 3473 | if (ret == -1) { |
| 3474 | /* The connection is destroyed, let's leave */ |
| 3475 | TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3476 | return; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3477 | } |
| 3478 | else if (ret == 1) { |
| 3479 | /* The connection was added to the server idle list, just stop */ |
| 3480 | TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn); |
| 3481 | return; |
| 3482 | } |
| 3483 | TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3484 | } |
| 3485 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3486 | if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3487 | fconn->conn->flags |= CO_FL_PRIVATE; |
| 3488 | if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) { |
| 3489 | struct server *srv = objt_server(fconn->conn->target); |
| 3490 | |
| 3491 | if (srv) { |
| 3492 | if (fconn->conn->flags & CO_FL_PRIVATE) |
| 3493 | LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list); |
| 3494 | else |
| 3495 | LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list); |
| 3496 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3497 | TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | /* We don't want to close right now unless we're removing the last |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3502 | * stream and the connection is in error. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3503 | */ |
| 3504 | if (fcgi_conn_is_dead(fconn)) { |
| 3505 | /* no more stream will come, kill it now */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3506 | TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3507 | fcgi_release(fconn); |
| 3508 | } |
| 3509 | else if (fconn->task) { |
| 3510 | fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout)); |
| 3511 | task_queue(fconn->task); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3512 | TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3513 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3514 | else |
| 3515 | TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | |
| 3519 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3520 | static void fcgi_do_shutr(struct fcgi_strm *fstrm) |
| 3521 | { |
| 3522 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3523 | struct wait_event *sw = &fstrm->wait_event; |
| 3524 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3525 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3526 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3527 | if (fstrm->state == FCGI_SS_CLOSED) |
| 3528 | goto done; |
| 3529 | |
| 3530 | /* a connstream may require us to immediately kill the whole connection |
| 3531 | * for example because of a "tcp-request content reject" rule that is |
| 3532 | * normally used to limit abuse. |
| 3533 | */ |
| 3534 | if ((fstrm->flags & FCGI_SF_KILL_CONN) && |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3535 | !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 3536 | TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3537 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3538 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3539 | else if (fstrm->flags & FCGI_SF_BEGIN_SENT) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3540 | TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3541 | if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) && |
| 3542 | !fcgi_strm_send_abort(fconn, fstrm)) |
| 3543 | goto add_to_list; |
| 3544 | } |
| 3545 | |
| 3546 | fcgi_strm_close(fstrm); |
| 3547 | |
| 3548 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 3549 | tasklet_wakeup(fconn->wait_event.tasklet); |
| 3550 | done: |
| 3551 | fstrm->flags &= ~FCGI_SF_WANT_SHUTR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3552 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3553 | return; |
| 3554 | |
| 3555 | add_to_list: |
| 3556 | if (!LIST_ADDED(&fstrm->send_list)) { |
| 3557 | sw->events |= SUB_RETRY_SEND; |
| 3558 | if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) { |
| 3559 | fstrm->send_wait = sw; |
| 3560 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
| 3561 | } |
| 3562 | } |
| 3563 | /* Let the handler know we want shutr */ |
| 3564 | fstrm->flags |= FCGI_SF_WANT_SHUTR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3565 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3570 | static void fcgi_do_shutw(struct fcgi_strm *fstrm) |
| 3571 | { |
| 3572 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3573 | struct wait_event *sw = &fstrm->wait_event; |
| 3574 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3575 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3576 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3577 | if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED) |
| 3578 | goto done; |
| 3579 | |
| 3580 | if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) { |
| 3581 | if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) && |
| 3582 | !fcgi_strm_send_abort(fconn, fstrm)) |
| 3583 | goto add_to_list; |
| 3584 | |
| 3585 | if (fstrm->state == FCGI_SS_HREM) |
| 3586 | fcgi_strm_close(fstrm); |
| 3587 | else |
| 3588 | fstrm->state = FCGI_SS_HLOC; |
| 3589 | } else { |
| 3590 | /* a connstream may require us to immediately kill the whole connection |
| 3591 | * for example because of a "tcp-request content reject" rule that is |
| 3592 | * normally used to limit abuse. |
| 3593 | */ |
| 3594 | if ((fstrm->flags & FCGI_SF_KILL_CONN) && |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3595 | !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 3596 | TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3597 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3598 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3599 | |
| 3600 | fcgi_strm_close(fstrm); |
| 3601 | } |
| 3602 | |
| 3603 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 3604 | tasklet_wakeup(fconn->wait_event.tasklet); |
| 3605 | done: |
| 3606 | fstrm->flags &= ~FCGI_SF_WANT_SHUTW; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3607 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3608 | return; |
| 3609 | |
| 3610 | add_to_list: |
| 3611 | if (!LIST_ADDED(&fstrm->send_list)) { |
| 3612 | sw->events |= SUB_RETRY_SEND; |
| 3613 | if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) { |
| 3614 | fstrm->send_wait = sw; |
| 3615 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
| 3616 | } |
| 3617 | } |
| 3618 | /* let the handler know we want to shutw */ |
| 3619 | fstrm->flags |= FCGI_SF_WANT_SHUTW; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3620 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3621 | return; |
| 3622 | } |
| 3623 | |
| 3624 | /* This is the tasklet referenced in fstrm->wait_event.tasklet, it is used for |
| 3625 | * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3626 | * and prevented the last record from being emitted. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3627 | */ |
| 3628 | static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3629 | { |
| 3630 | struct fcgi_strm *fstrm = ctx; |
| 3631 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3632 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3633 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3634 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3635 | LIST_DEL_INIT(&fstrm->sending_list); |
| 3636 | if (fstrm->flags & FCGI_SF_WANT_SHUTW) |
| 3637 | fcgi_do_shutw(fstrm); |
| 3638 | |
| 3639 | if (fstrm->flags & FCGI_SF_WANT_SHUTR) |
| 3640 | fcgi_do_shutr(fstrm); |
| 3641 | |
| 3642 | if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) { |
| 3643 | /* We're done trying to send, remove ourself from the send_list */ |
| 3644 | LIST_DEL_INIT(&fstrm->send_list); |
| 3645 | |
| 3646 | if (!fstrm->cs) { |
| 3647 | fcgi_strm_destroy(fstrm); |
| 3648 | if (fcgi_conn_is_dead(fconn)) |
| 3649 | fcgi_release(fconn); |
| 3650 | } |
| 3651 | } |
| 3652 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3653 | TRACE_LEAVE(FCGI_EV_STRM_SHUT); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3654 | return NULL; |
| 3655 | } |
| 3656 | |
| 3657 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
| 3658 | static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3659 | { |
| 3660 | struct fcgi_strm *fstrm = cs->ctx; |
| 3661 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3662 | TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3663 | if (cs->flags & CS_FL_KILL_CONN) |
| 3664 | fstrm->flags |= FCGI_SF_KILL_CONN; |
| 3665 | |
| 3666 | if (!mode) |
| 3667 | return; |
| 3668 | |
| 3669 | fcgi_do_shutr(fstrm); |
| 3670 | } |
| 3671 | |
| 3672 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
| 3673 | static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3674 | { |
| 3675 | struct fcgi_strm *fstrm = cs->ctx; |
| 3676 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3677 | TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3678 | if (cs->flags & CS_FL_KILL_CONN) |
| 3679 | fstrm->flags |= FCGI_SF_KILL_CONN; |
| 3680 | |
| 3681 | fcgi_do_shutw(fstrm); |
| 3682 | } |
| 3683 | |
| 3684 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 3685 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 3686 | * which will be passed to fstrm->recv_wait or fstrm->send_wait depending on the |
| 3687 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 3688 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 3689 | * returns 0 except for the error above. |
| 3690 | */ |
| 3691 | static int fcgi_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 3692 | { |
| 3693 | struct wait_event *sw; |
| 3694 | struct fcgi_strm *fstrm = cs->ctx; |
| 3695 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3696 | |
| 3697 | if (event_type & SUB_RETRY_RECV) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3698 | TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3699 | sw = param; |
| 3700 | BUG_ON(fstrm->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 3701 | sw->events |= SUB_RETRY_RECV; |
| 3702 | fstrm->recv_wait = sw; |
| 3703 | event_type &= ~SUB_RETRY_RECV; |
| 3704 | } |
| 3705 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3706 | TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3707 | sw = param; |
| 3708 | BUG_ON(fstrm->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 3709 | sw->events |= SUB_RETRY_SEND; |
| 3710 | fstrm->send_wait = sw; |
| 3711 | if (!LIST_ADDED(&fstrm->send_list)) |
| 3712 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
| 3713 | event_type &= ~SUB_RETRY_SEND; |
| 3714 | } |
| 3715 | if (event_type != 0) |
| 3716 | return -1; |
| 3717 | return 0; |
| 3718 | } |
| 3719 | |
| 3720 | /* Called from the upper layer, to unsubscribe some events (undo fcgi_subscribe). |
| 3721 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 3722 | * struct that was passed to fcgi_subscribe() otherwise nothing will be changed. |
| 3723 | * It always returns zero. |
| 3724 | */ |
| 3725 | static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 3726 | { |
| 3727 | struct wait_event *sw; |
| 3728 | struct fcgi_strm *fstrm = cs->ctx; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3729 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3730 | |
| 3731 | if (event_type & SUB_RETRY_RECV) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3732 | TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3733 | sw = param; |
| 3734 | BUG_ON(fstrm->recv_wait != sw); |
| 3735 | sw->events &= ~SUB_RETRY_RECV; |
| 3736 | fstrm->recv_wait = NULL; |
| 3737 | } |
| 3738 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3739 | TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3740 | sw = param; |
| 3741 | BUG_ON(fstrm->send_wait != sw); |
| 3742 | LIST_DEL(&fstrm->send_list); |
| 3743 | LIST_INIT(&fstrm->send_list); |
| 3744 | sw->events &= ~SUB_RETRY_SEND; |
| 3745 | /* We were about to send, make sure it does not happen */ |
| 3746 | if (LIST_ADDED(&fstrm->sending_list) && fstrm->send_wait != &fstrm->wait_event) { |
| 3747 | tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet); |
| 3748 | LIST_DEL_INIT(&fstrm->sending_list); |
| 3749 | } |
| 3750 | fstrm->send_wait = NULL; |
| 3751 | } |
| 3752 | return 0; |
| 3753 | } |
| 3754 | |
| 3755 | /* Called from the upper layer, to receive data */ |
| 3756 | static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 3757 | { |
| 3758 | struct fcgi_strm *fstrm = cs->ctx; |
| 3759 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3760 | size_t ret = 0; |
| 3761 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3762 | TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
| 3763 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3764 | if (!(fconn->flags & FCGI_CF_DEM_SALLOC)) |
| 3765 | ret = fcgi_strm_parse_response(fstrm, buf, count); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3766 | else |
| 3767 | TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3768 | |
| 3769 | if (b_data(&fstrm->rxbuf)) |
| 3770 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 3771 | else { |
| 3772 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 3773 | if (fstrm->state == FCGI_SS_ERROR || fstrm->h1m.state == H1_MSG_DONE) { |
| 3774 | cs->flags |= CS_FL_EOI; |
| 3775 | if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN))) |
| 3776 | cs->flags |= CS_FL_EOS; |
| 3777 | } |
| 3778 | if (conn_xprt_read0_pending(fconn->conn)) |
| 3779 | cs->flags |= CS_FL_EOS; |
| 3780 | if (cs->flags & CS_FL_ERR_PENDING) |
| 3781 | cs->flags |= CS_FL_ERROR; |
| 3782 | fcgi_release_buf(fconn, &fstrm->rxbuf); |
| 3783 | } |
| 3784 | |
| 3785 | if (ret && fconn->dsi == fstrm->id) { |
| 3786 | /* demux is blocking on this stream's buffer */ |
| 3787 | fconn->flags &= ~FCGI_CF_DEM_SFULL; |
| 3788 | fcgi_conn_restart_reading(fconn, 1); |
| 3789 | } |
| 3790 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3791 | TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3792 | return ret; |
| 3793 | } |
| 3794 | |
| 3795 | |
| 3796 | /* stops all senders of this connection for example when the mux buffer is full. |
| 3797 | * They are moved from the sending_list to send_list. |
| 3798 | */ |
| 3799 | static void fcgi_stop_senders(struct fcgi_conn *fconn) |
| 3800 | { |
| 3801 | struct fcgi_strm *fstrm, *fstrm_back; |
| 3802 | |
| 3803 | list_for_each_entry_safe(fstrm, fstrm_back, &fconn->sending_list, sending_list) { |
| 3804 | LIST_DEL_INIT(&fstrm->sending_list); |
| 3805 | tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet); |
| 3806 | fstrm->send_wait->events |= SUB_RETRY_SEND; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | |
| 3811 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 3812 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 3813 | * flags may be updated on the conn_stream. |
| 3814 | */ |
| 3815 | static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 3816 | { |
| 3817 | struct fcgi_strm *fstrm = cs->ctx; |
| 3818 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3819 | size_t total = 0; |
| 3820 | size_t ret; |
| 3821 | struct htx *htx = NULL; |
| 3822 | struct htx_sl *sl; |
| 3823 | struct htx_blk *blk; |
| 3824 | uint32_t bsize; |
| 3825 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3826 | TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count}); |
| 3827 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3828 | /* If we were not just woken because we wanted to send but couldn't, |
| 3829 | * and there's somebody else that is waiting to send, do nothing, |
| 3830 | * we will subscribe later and be put at the end of the list |
| 3831 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3832 | if (!LIST_ADDED(&fstrm->sending_list) && !LIST_ISEMPTY(&fconn->send_list)) { |
| 3833 | TRACE_STATE("other streams already waiting, going to the queue and leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3834 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3835 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3836 | LIST_DEL_INIT(&fstrm->sending_list); |
| 3837 | |
| 3838 | /* We couldn't set it to NULL before, because we needed it in case |
| 3839 | * we had to cancel the tasklet |
| 3840 | */ |
| 3841 | fstrm->send_wait = NULL; |
| 3842 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3843 | if (fconn->state < FCGI_CS_RECORD_H) { |
| 3844 | TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3845 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3846 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3847 | |
| 3848 | htx = htxbuf(buf); |
| 3849 | if (fstrm->id == 0) { |
| 3850 | int32_t id = fcgi_conn_get_next_sid(fconn); |
| 3851 | |
| 3852 | if (id < 0) { |
| 3853 | fcgi_strm_close(fstrm); |
| 3854 | cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3855 | TRACE_DEVEL("couldn't get a stream ID, leaving in error", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_ERR|FCGI_EV_STRM_ERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3856 | return 0; |
| 3857 | } |
| 3858 | |
| 3859 | eb32_delete(&fstrm->by_id); |
| 3860 | fstrm->by_id.key = fstrm->id = id; |
| 3861 | fconn->max_id = id; |
| 3862 | fconn->nb_reserved--; |
| 3863 | eb32_insert(&fconn->streams_by_id, &fstrm->by_id); |
| 3864 | |
| 3865 | |
| 3866 | /* Check if length of the body is known or if the message is |
| 3867 | * full. Otherwise, the request is invalid. |
| 3868 | */ |
| 3869 | sl = http_get_stline(htx); |
| 3870 | if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) { |
| 3871 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 3872 | fcgi_strm_error(fstrm); |
| 3873 | goto done; |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3878 | TRACE_PROTO("sending FCGI BEGIN_REQUEST record", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3879 | if (!fcgi_strm_send_begin_request(fconn, fstrm)) |
| 3880 | goto done; |
| 3881 | } |
| 3882 | |
| 3883 | if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count) |
| 3884 | fstrm->flags |= FCGI_SF_OUTGOING_DATA; |
| 3885 | |
| 3886 | while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) && |
| 3887 | count && !htx_is_empty(htx)) { |
| 3888 | blk = htx_get_head_blk(htx); |
William Lallemand | 13ed9fa | 2019-09-25 21:21:57 +0200 | [diff] [blame] | 3889 | ALREADY_CHECKED(blk); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3890 | bsize = htx_get_blksz(blk); |
| 3891 | |
| 3892 | switch (htx_get_blk_type(blk)) { |
| 3893 | case HTX_BLK_REQ_SL: |
| 3894 | case HTX_BLK_HDR: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3895 | TRACE_USER("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3896 | ret = fcgi_strm_send_params(fconn, fstrm, htx); |
| 3897 | if (!ret) { |
| 3898 | goto done; |
| 3899 | } |
| 3900 | total += ret; |
| 3901 | count -= ret; |
| 3902 | break; |
| 3903 | |
| 3904 | case HTX_BLK_EOH: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3905 | TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3906 | ret = fcgi_strm_send_empty_params(fconn, fstrm); |
| 3907 | if (!ret) |
| 3908 | goto done; |
| 3909 | goto remove_blk; |
| 3910 | |
| 3911 | case HTX_BLK_DATA: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3912 | TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3913 | ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf); |
| 3914 | if (ret > 0) { |
| 3915 | htx = htx_from_buf(buf); |
| 3916 | total += ret; |
| 3917 | count -= ret; |
| 3918 | if (ret < bsize) |
| 3919 | goto done; |
| 3920 | } |
| 3921 | break; |
| 3922 | |
| 3923 | case HTX_BLK_EOM: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3924 | TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3925 | ret = fcgi_strm_send_empty_stdin(fconn, fstrm); |
| 3926 | if (!ret) |
| 3927 | goto done; |
| 3928 | goto remove_blk; |
| 3929 | |
| 3930 | default: |
| 3931 | remove_blk: |
| 3932 | htx_remove_blk(htx, blk); |
| 3933 | total += bsize; |
| 3934 | count -= bsize; |
| 3935 | break; |
| 3936 | } |
| 3937 | } |
| 3938 | |
| 3939 | done: |
| 3940 | if (fstrm->state >= FCGI_SS_HLOC) { |
| 3941 | /* trim any possibly pending data after we close (extra CR-LF, |
| 3942 | * unprocessed trailers, abnormal extra data, ...) |
| 3943 | */ |
| 3944 | total += count; |
| 3945 | count = 0; |
| 3946 | } |
| 3947 | |
| 3948 | if (fstrm->state == FCGI_SS_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3949 | TRACE_DEVEL("reporting error to the app-layer stream", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_ERR|FCGI_EV_STRM_ERR, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3950 | cs_set_error(cs); |
| 3951 | if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm)) |
| 3952 | fcgi_strm_close(fstrm); |
| 3953 | } |
| 3954 | |
| 3955 | if (htx) |
| 3956 | htx_to_buf(htx, buf); |
| 3957 | |
| 3958 | /* The mux is full, cancel the pending tasks */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3959 | if ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || (fstrm->flags & FCGI_SF_BLK_MBUSY)) { |
| 3960 | TRACE_DEVEL("mux full, stopping senders", FCGI_EV_STRM_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3961 | fcgi_stop_senders(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3962 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3963 | |
| 3964 | if (total > 0) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3965 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) { |
| 3966 | TRACE_DEVEL("data queued, waking up fconn sender", FCGI_EV_STRM_SEND|FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_WAKE, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3967 | tasklet_wakeup(fconn->wait_event.tasklet); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3968 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3969 | |
| 3970 | /* Ok we managed to send something, leave the send_list */ |
| 3971 | LIST_DEL_INIT(&fstrm->send_list); |
| 3972 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3973 | |
| 3974 | TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total}); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3975 | return total; |
| 3976 | } |
| 3977 | |
| 3978 | /* for debugging with CLI's "show fd" command */ |
| 3979 | static void fcgi_show_fd(struct buffer *msg, struct connection *conn) |
| 3980 | { |
| 3981 | struct fcgi_conn *fconn = conn->ctx; |
| 3982 | struct fcgi_strm *fstrm = NULL; |
| 3983 | struct eb32_node *node; |
| 3984 | int send_cnt = 0; |
| 3985 | int tree_cnt = 0; |
| 3986 | int orph_cnt = 0; |
| 3987 | struct buffer *hmbuf, *tmbuf; |
| 3988 | |
| 3989 | if (!fconn) |
| 3990 | return; |
| 3991 | |
| 3992 | list_for_each_entry(fstrm, &fconn->send_list, send_list) |
| 3993 | send_cnt++; |
| 3994 | |
| 3995 | fstrm = NULL; |
| 3996 | node = eb32_first(&fconn->streams_by_id); |
| 3997 | while (node) { |
| 3998 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 3999 | tree_cnt++; |
| 4000 | if (!fstrm->cs) |
| 4001 | orph_cnt++; |
| 4002 | node = eb32_next(node); |
| 4003 | } |
| 4004 | |
| 4005 | hmbuf = br_head(fconn->mbuf); |
| 4006 | tmbuf = br_tail(fconn->mbuf); |
| 4007 | chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u" |
| 4008 | " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d " |
| 4009 | ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
| 4010 | fconn->state, fconn->max_id, fconn->flags, |
| 4011 | fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt, |
| 4012 | fconn->wait_event.events, fconn->dsi, |
| 4013 | (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf), |
| 4014 | (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf), |
| 4015 | br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf), |
| 4016 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 4017 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
| 4018 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 4019 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
| 4020 | |
| 4021 | if (fstrm) { |
| 4022 | chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 4023 | fstrm, fstrm->id, fstrm->flags, |
| 4024 | (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf), |
| 4025 | (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf), |
| 4026 | fstrm->cs); |
| 4027 | if (fstrm->cs) |
| 4028 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 4029 | fstrm->cs->flags, fstrm->cs->data); |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /****************************************/ |
| 4034 | /* MUX initialization and instanciation */ |
| 4035 | /****************************************/ |
| 4036 | |
| 4037 | /* The mux operations */ |
| 4038 | static const struct mux_ops mux_fcgi_ops = { |
| 4039 | .init = fcgi_init, |
| 4040 | .wake = fcgi_wake, |
| 4041 | .attach = fcgi_attach, |
| 4042 | .get_first_cs = fcgi_get_first_cs, |
| 4043 | .detach = fcgi_detach, |
| 4044 | .destroy = fcgi_destroy, |
| 4045 | .avail_streams = fcgi_avail_streams, |
| 4046 | .used_streams = fcgi_used_streams, |
| 4047 | .rcv_buf = fcgi_rcv_buf, |
| 4048 | .snd_buf = fcgi_snd_buf, |
| 4049 | .subscribe = fcgi_subscribe, |
| 4050 | .unsubscribe = fcgi_unsubscribe, |
| 4051 | .shutr = fcgi_shutr, |
| 4052 | .shutw = fcgi_shutw, |
| 4053 | .show_fd = fcgi_show_fd, |
| 4054 | .flags = MX_FL_HTX, |
| 4055 | .name = "FCGI", |
| 4056 | }; |
| 4057 | |
| 4058 | |
| 4059 | /* this mux registers FCGI proto */ |
| 4060 | static struct mux_proto_list mux_proto_fcgi = |
| 4061 | { .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops }; |
| 4062 | |
| 4063 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi); |
| 4064 | |
| 4065 | /* |
| 4066 | * Local variables: |
| 4067 | * c-indent-level: 8 |
| 4068 | * c-basic-offset: 8 |
| 4069 | * End: |
| 4070 | */ |