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