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