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