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