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