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