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