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