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