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