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