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 | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 607 | if (likely(!MT_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 | 8689127 | 2020-07-10 08:22:26 +0200 | [diff] [blame] | 611 | MT_LIST_ADDQ(&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); |
| 620 | offer_buffers(NULL, tasks_run_queue); |
| 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) |
| 634 | offer_buffers(NULL, tasks_run_queue); |
| 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 | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 769 | MT_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 | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 848 | if (MT_LIST_ADDED(&fconn->buf_wait.list)) |
| 849 | MT_LIST_DEL(&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); |
| 1030 | offer_buffers(NULL, tasks_run_queue); |
| 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) { |
| 1272 | params->srv_name = ist2(ptr, strlen(ptr)); |
| 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) { |
| 1284 | params->rem_addr = ist2(ptr, strlen(ptr)); |
| 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 | |
| 2791 | b_realign_if_empty(buf); |
| 2792 | if (!b_data(buf)) { |
| 2793 | /* try to pre-align the buffer like the |
| 2794 | * rxbufs will be to optimize memory copies. We'll make |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2795 | * sure that the record header lands at the end of the |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2796 | * HTX block to alias it upon recv. We cannot use the |
| 2797 | * head because rcv_buf() will realign the buffer if |
| 2798 | * it's empty. Thus we cheat and pretend we already |
| 2799 | * have a few bytes there. |
| 2800 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2801 | max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0); |
| 2802 | 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] | 2803 | } |
| 2804 | else |
| 2805 | max = buf_room_for_htx_data(buf); |
| 2806 | |
| 2807 | ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0; |
| 2808 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2809 | if (max && !ret && fcgi_recv_allowed(fconn)) { |
| 2810 | TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2811 | 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] | 2812 | } |
| 2813 | else |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2814 | 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] | 2815 | |
| 2816 | if (!b_data(buf)) { |
| 2817 | fcgi_release_buf(fconn, &fconn->dbuf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2818 | TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2819 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
| 2820 | } |
| 2821 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2822 | if (ret == max) { |
| 2823 | 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] | 2824 | fconn->flags |= FCGI_CF_DEM_DFULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2825 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2826 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2827 | TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2828 | return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn); |
| 2829 | } |
| 2830 | |
| 2831 | |
| 2832 | /* Try to send data if possible. |
| 2833 | * The function returns 1 if data have been sent, otherwise zero. |
| 2834 | */ |
| 2835 | static int fcgi_send(struct fcgi_conn *fconn) |
| 2836 | { |
| 2837 | struct connection *conn = fconn->conn; |
| 2838 | int done; |
| 2839 | int sent = 0; |
| 2840 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2841 | TRACE_ENTER(FCGI_EV_FCONN_SEND, conn); |
| 2842 | |
| 2843 | if (conn->flags & CO_FL_ERROR) { |
| 2844 | TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2845 | return 1; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2846 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2847 | |
| 2848 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 2849 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2850 | /* a handshake was requested */ |
| 2851 | goto schedule; |
| 2852 | } |
| 2853 | |
| 2854 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2855 | * pending streams into the existing buffer until it's reportedly full |
| 2856 | * or the end of send requests is reached. Then it tries to send this |
| 2857 | * buffer's contents out, marks it not full if at least one byte could |
| 2858 | * be sent, and tries again. |
| 2859 | * |
| 2860 | * The snd_buf() function normally takes a "flags" argument which may |
| 2861 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2862 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2863 | * connection is streaming lots of data (used to increase TLS record |
| 2864 | * size at the expense of latency). The former can be sent any time |
| 2865 | * there's a buffer full flag, as it indicates at least one stream |
| 2866 | * attempted to send and failed so there are pending data. An |
| 2867 | * alternative would be to set it as long as there's an active stream |
| 2868 | * but that would be problematic for ACKs until we have an absolute |
| 2869 | * guarantee that all waiters have at least one byte to send. The |
| 2870 | * latter should possibly not be set for now. |
| 2871 | */ |
| 2872 | |
| 2873 | done = 0; |
| 2874 | while (!done) { |
| 2875 | unsigned int flags = 0; |
| 2876 | unsigned int released = 0; |
| 2877 | struct buffer *buf; |
| 2878 | |
| 2879 | /* fill as much as we can into the current buffer */ |
| 2880 | while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done) |
| 2881 | done = fcgi_process_mux(fconn); |
| 2882 | |
| 2883 | if (fconn->flags & FCGI_CF_MUX_MALLOC) |
| 2884 | done = 1; // we won't go further without extra buffers |
| 2885 | |
| 2886 | if (conn->flags & CO_FL_ERROR) |
| 2887 | break; |
| 2888 | |
| 2889 | if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) |
| 2890 | flags |= CO_SFL_MSG_MORE; |
| 2891 | |
| 2892 | for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) { |
| 2893 | if (b_data(buf)) { |
| 2894 | int ret; |
| 2895 | |
| 2896 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
| 2897 | if (!ret) { |
| 2898 | done = 1; |
| 2899 | break; |
| 2900 | } |
| 2901 | sent = 1; |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2902 | 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] | 2903 | b_del(buf, ret); |
| 2904 | if (b_data(buf)) { |
| 2905 | done = 1; |
| 2906 | break; |
| 2907 | } |
| 2908 | } |
| 2909 | b_free(buf); |
| 2910 | released++; |
| 2911 | } |
| 2912 | |
| 2913 | if (released) |
| 2914 | offer_buffers(NULL, tasks_run_queue); |
| 2915 | |
| 2916 | /* wrote at least one byte, the buffer is not full anymore */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2917 | if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) |
| 2918 | 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] | 2919 | fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM); |
| 2920 | } |
| 2921 | |
| 2922 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2923 | /* output closed, nothing to send, clear the buffer to release it */ |
| 2924 | b_reset(br_tail(fconn->mbuf)); |
| 2925 | } |
| 2926 | /* We're not full anymore, so we can wake any task that are waiting |
| 2927 | * for us. |
| 2928 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2929 | 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] | 2930 | struct fcgi_strm *fstrm; |
| 2931 | |
| 2932 | list_for_each_entry(fstrm, &fconn->send_list, send_list) { |
| 2933 | if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY) |
| 2934 | break; |
| 2935 | |
Willy Tarreau | f11be0e | 2020-01-16 16:59:45 +0100 | [diff] [blame] | 2936 | if (fstrm->flags & FCGI_SF_NOTIFIED) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2937 | continue; |
| 2938 | |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 2939 | /* If the sender changed his mind and unsubscribed, let's just |
| 2940 | * remove the stream from the send_list. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2941 | */ |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 2942 | if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) && |
| 2943 | (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2944 | LIST_DEL_INIT(&fstrm->send_list); |
| 2945 | continue; |
| 2946 | } |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 2947 | |
| 2948 | if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) { |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 2949 | 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] | 2950 | fstrm->flags &= ~FCGI_SF_BLK_ANY; |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 2951 | fstrm->flags |= FCGI_SF_NOTIFIED; |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 2952 | tasklet_wakeup(fstrm->subs->tasklet); |
| 2953 | fstrm->subs->events &= ~SUB_RETRY_SEND; |
| 2954 | if (!fstrm->subs->events) |
| 2955 | fstrm->subs = NULL; |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 2956 | } else { |
| 2957 | /* it's the shut request that was queued */ |
| 2958 | TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm); |
| 2959 | tasklet_wakeup(fstrm->shut_tl); |
| 2960 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2961 | } |
| 2962 | } |
| 2963 | /* We're done, no more to send */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2964 | if (!br_data(fconn->mbuf)) { |
| 2965 | TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2966 | return sent; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2967 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2968 | schedule: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2969 | if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) { |
| 2970 | TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2971 | 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] | 2972 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2973 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 2974 | 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] | 2975 | return sent; |
| 2976 | } |
| 2977 | |
| 2978 | /* this is the tasklet referenced in fconn->wait_event.tasklet */ |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 2979 | 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] | 2980 | { |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 2981 | struct connection *conn; |
| 2982 | struct fcgi_conn *fconn; |
| 2983 | struct tasklet *tl = (struct tasklet *)t; |
| 2984 | int conn_in_list; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 2985 | int ret = 0; |
| 2986 | |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 2987 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2988 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 2989 | if (tl->context == NULL) { |
| 2990 | /* The connection has been taken over by another thread, |
| 2991 | * we're no longer responsible for it, so just free the |
| 2992 | * tasklet, and do nothing. |
| 2993 | */ |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2994 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 2995 | tasklet_free(tl); |
| 2996 | return NULL; |
| 2997 | |
| 2998 | } |
| 2999 | fconn = ctx; |
| 3000 | conn = fconn->conn; |
| 3001 | |
| 3002 | TRACE_POINT(FCGI_EV_FCONN_WAKE, conn); |
| 3003 | |
| 3004 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 3005 | if (conn_in_list) |
| 3006 | MT_LIST_DEL(&conn->list); |
| 3007 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 3008 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3009 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3010 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 3011 | ret = fcgi_send(fconn); |
| 3012 | if (!(fconn->wait_event.events & SUB_RETRY_RECV)) |
| 3013 | ret |= fcgi_recv(fconn); |
| 3014 | if (ret || b_data(&fconn->dbuf)) |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 3015 | ret = fcgi_process(fconn); |
| 3016 | |
| 3017 | /* If we were in an idle list, we want to add it back into it, |
| 3018 | * unless fcgi_process() returned -1, which mean it has destroyed |
| 3019 | * the connection (testing !ret is enough, if fcgi_process() wasn't |
| 3020 | * called then ret will be 0 anyway. |
| 3021 | */ |
| 3022 | if (!ret && conn_in_list) { |
| 3023 | struct server *srv = objt_server(conn->target); |
| 3024 | |
| 3025 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 3026 | MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 3027 | else |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 3028 | MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 3029 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3030 | return NULL; |
| 3031 | } |
| 3032 | |
| 3033 | /* callback called on any event by the connection handler. |
| 3034 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 3035 | * destruction of the connection (which normally doesn not happen in FCGI). |
| 3036 | */ |
| 3037 | static int fcgi_process(struct fcgi_conn *fconn) |
| 3038 | { |
| 3039 | struct connection *conn = fconn->conn; |
| 3040 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3041 | TRACE_POINT(FCGI_EV_FCONN_WAKE, conn); |
| 3042 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3043 | if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) { |
| 3044 | fcgi_process_demux(fconn); |
| 3045 | |
| 3046 | if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR) |
| 3047 | b_reset(&fconn->dbuf); |
| 3048 | |
| 3049 | if (buf_room_for_htx_data(&fconn->dbuf)) |
| 3050 | fconn->flags &= ~FCGI_CF_DEM_DFULL; |
| 3051 | } |
| 3052 | fcgi_send(fconn); |
| 3053 | |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 3054 | if (unlikely(fconn->proxy->disabled)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3055 | /* frontend is stopping, reload likely in progress, let's try |
| 3056 | * to announce a graceful shutdown if not yet done. We don't |
| 3057 | * care if it fails, it will be tried again later. |
| 3058 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3059 | 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] | 3060 | if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 3061 | if (fconn->stream_cnt - fconn->nb_reserved > 0) |
| 3062 | fcgi_conn_send_aborts(fconn); |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * If we received early data, and the handshake is done, wake |
| 3068 | * any stream that was waiting for it. |
| 3069 | */ |
| 3070 | if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) && |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3071 | (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] | 3072 | struct eb32_node *node; |
| 3073 | struct fcgi_strm *fstrm; |
| 3074 | |
| 3075 | fconn->flags |= FCGI_CF_WAIT_FOR_HS; |
| 3076 | node = eb32_lookup_ge(&fconn->streams_by_id, 1); |
| 3077 | |
| 3078 | while (node) { |
| 3079 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 3080 | if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS) |
| 3081 | fcgi_strm_notify_recv(fstrm); |
| 3082 | node = eb32_next(node); |
| 3083 | } |
| 3084 | } |
| 3085 | |
Christopher Faulet | 6670e3e | 2020-10-08 15:26:33 +0200 | [diff] [blame] | 3086 | if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) || |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3087 | fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) || |
| 3088 | eb_is_empty(&fconn->streams_by_id)) { |
| 3089 | fcgi_wake_some_streams(fconn, 0); |
| 3090 | |
| 3091 | if (eb_is_empty(&fconn->streams_by_id)) { |
| 3092 | /* no more stream, kill the connection now */ |
| 3093 | fcgi_release(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3094 | TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3095 | return -1; |
| 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | if (!b_data(&fconn->dbuf)) |
| 3100 | fcgi_release_buf(fconn, &fconn->dbuf); |
| 3101 | |
| 3102 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3103 | fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) || |
| 3104 | (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list)))) |
| 3105 | fcgi_release_mbuf(fconn); |
| 3106 | |
| 3107 | if (fconn->task) { |
| 3108 | fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout)); |
| 3109 | task_queue(fconn->task); |
| 3110 | } |
| 3111 | |
| 3112 | fcgi_send(fconn); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3113 | TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3114 | return 0; |
| 3115 | } |
| 3116 | |
| 3117 | |
| 3118 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
| 3119 | static int fcgi_wake(struct connection *conn) |
| 3120 | { |
| 3121 | struct fcgi_conn *fconn = conn->ctx; |
| 3122 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3123 | TRACE_POINT(FCGI_EV_FCONN_WAKE, conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3124 | return (fcgi_process(fconn)); |
| 3125 | } |
| 3126 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3127 | |
| 3128 | static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3129 | { |
| 3130 | int ret = 0; |
| 3131 | switch (mux_ctl) { |
| 3132 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3133 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3134 | ret |= MUX_STATUS_READY; |
| 3135 | return ret; |
Christopher Faulet | 4c8ad84 | 2020-10-06 14:59:17 +0200 | [diff] [blame] | 3136 | case MUX_EXIT_STATUS: |
| 3137 | return MUX_ES_UNKNOWN; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3138 | default: |
| 3139 | return -1; |
| 3140 | } |
| 3141 | } |
| 3142 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3143 | /* Connection timeout management. The principle is that if there's no receipt |
| 3144 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3145 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3146 | * 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] | 3147 | * ABORT records. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3148 | */ |
Willy Tarreau | ac6322d | 2021-01-29 12:33:46 +0100 | [diff] [blame] | 3149 | 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] | 3150 | { |
| 3151 | struct fcgi_conn *fconn = context; |
| 3152 | int expired = tick_is_expired(t->expire, now_ms); |
| 3153 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3154 | TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL)); |
| 3155 | |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3156 | if (fconn) { |
Olivier Houchard | 48ce6a3 | 2020-07-02 11:58:05 +0200 | [diff] [blame] | 3157 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 3158 | |
| 3159 | /* Somebody already stole the connection from us, so we should not |
| 3160 | * free it, we just have to free the task. |
| 3161 | */ |
| 3162 | if (!t->context) { |
| 3163 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 3164 | fconn = NULL; |
| 3165 | goto do_leave; |
| 3166 | } |
| 3167 | |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3168 | if (!expired) { |
Olivier Houchard | 48ce6a3 | 2020-07-02 11:58:05 +0200 | [diff] [blame] | 3169 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3170 | TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn); |
| 3171 | return t; |
| 3172 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3173 | |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3174 | /* We're about to destroy the connection, so make sure nobody attempts |
| 3175 | * to steal it from us. |
| 3176 | */ |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3177 | if (fconn->conn->flags & CO_FL_LIST_MASK) |
| 3178 | MT_LIST_DEL(&fconn->conn->list); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 3179 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 3180 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Willy Tarreau | 60814ff | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3181 | } |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 3182 | |
Olivier Houchard | 48ce6a3 | 2020-07-02 11:58:05 +0200 | [diff] [blame] | 3183 | do_leave: |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3184 | task_destroy(t); |
| 3185 | |
| 3186 | if (!fconn) { |
| 3187 | /* resources were already deleted */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3188 | TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3189 | return NULL; |
| 3190 | } |
| 3191 | |
| 3192 | fconn->task = NULL; |
| 3193 | fconn->state = FCGI_CS_CLOSED; |
| 3194 | fcgi_wake_some_streams(fconn, 0); |
| 3195 | |
| 3196 | if (br_data(fconn->mbuf)) { |
| 3197 | /* don't even try to send aborts, the buffer is stuck */ |
| 3198 | fconn->flags |= FCGI_CF_ABRTS_FAILED; |
| 3199 | goto end; |
| 3200 | } |
| 3201 | |
| 3202 | /* try to send but no need to insist */ |
| 3203 | if (!fcgi_conn_send_aborts(fconn)) |
| 3204 | fconn->flags |= FCGI_CF_ABRTS_FAILED; |
| 3205 | |
| 3206 | if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) && |
| 3207 | conn_xprt_ready(fconn->conn)) { |
| 3208 | unsigned int released = 0; |
| 3209 | struct buffer *buf; |
| 3210 | |
| 3211 | for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) { |
| 3212 | if (b_data(buf)) { |
| 3213 | int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx, |
| 3214 | buf, b_data(buf), 0); |
| 3215 | if (!ret) |
| 3216 | break; |
| 3217 | b_del(buf, ret); |
| 3218 | if (b_data(buf)) |
| 3219 | break; |
| 3220 | b_free(buf); |
| 3221 | released++; |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | if (released) |
| 3226 | offer_buffers(NULL, tasks_run_queue); |
| 3227 | } |
| 3228 | |
| 3229 | end: |
| 3230 | /* either we can release everything now or it will be done later once |
| 3231 | * the last stream closes. |
| 3232 | */ |
| 3233 | if (eb_is_empty(&fconn->streams_by_id)) |
| 3234 | fcgi_release(fconn); |
| 3235 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3236 | TRACE_LEAVE(FCGI_EV_FCONN_WAKE); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3237 | return NULL; |
| 3238 | } |
| 3239 | |
| 3240 | |
| 3241 | /*******************************************/ |
| 3242 | /* functions below are used by the streams */ |
| 3243 | /*******************************************/ |
| 3244 | |
| 3245 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 3246 | * The description must be small enough to always fit in a buffer. The output |
| 3247 | * buffer may be the trash so the trash must not be used inside this function. |
| 3248 | */ |
| 3249 | static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 3250 | { |
| 3251 | chunk_appendf(out, |
| 3252 | " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n" |
| 3253 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 3254 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 3255 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 3256 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 3257 | es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen); |
| 3258 | } |
| 3259 | /* |
| 3260 | * Capture a bad response and archive it in the proxy's structure. By default |
| 3261 | * it tries to report the error position as h1m->err_pos. However if this one is |
| 3262 | * not set, it will then report h1m->next, which is the last known parsing |
| 3263 | * point. The function is able to deal with wrapping buffers. It always displays |
| 3264 | * buffers as a contiguous area starting at buf->p. The direction is determined |
| 3265 | * thanks to the h1m's flags. |
| 3266 | */ |
| 3267 | static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm, |
| 3268 | struct h1m *h1m, struct buffer *buf) |
| 3269 | { |
| 3270 | struct session *sess = fstrm->sess; |
| 3271 | struct proxy *proxy = fconn->proxy; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 3272 | struct proxy *other_end; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3273 | union error_snapshot_ctx ctx; |
| 3274 | |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 3275 | if (fstrm->cs && fstrm->cs->data) { |
| 3276 | if (sess == NULL) |
| 3277 | sess = si_strm(fstrm->cs->data)->sess; |
| 3278 | if (!(h1m->flags & H1_MF_RESP)) |
| 3279 | other_end = si_strm(fstrm->cs->data)->be; |
| 3280 | else |
| 3281 | other_end = sess->fe; |
| 3282 | } else |
| 3283 | other_end = NULL; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3284 | /* http-specific part now */ |
| 3285 | ctx.h1.state = h1m->state; |
| 3286 | ctx.h1.c_flags = fconn->flags; |
| 3287 | ctx.h1.s_flags = fstrm->flags; |
| 3288 | ctx.h1.m_flags = h1m->flags; |
| 3289 | ctx.h1.m_clen = h1m->curr_len; |
| 3290 | ctx.h1.m_blen = h1m->body_len; |
| 3291 | |
| 3292 | proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0, |
| 3293 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 3294 | &ctx, fcgi_show_error_snapshot); |
| 3295 | } |
| 3296 | |
| 3297 | static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, |
| 3298 | struct buffer *buf, size_t *ofs, size_t max) |
| 3299 | { |
| 3300 | int ret; |
| 3301 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3302 | 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] | 3303 | ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max); |
| 3304 | if (!ret) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3305 | 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] | 3306 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3307 | 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] | 3308 | fcgi_strm_error(fstrm); |
| 3309 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3310 | } |
| 3311 | goto end; |
| 3312 | } |
| 3313 | |
| 3314 | *ofs += ret; |
| 3315 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3316 | 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] | 3317 | return ret; |
| 3318 | |
| 3319 | } |
| 3320 | |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 3321 | 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] | 3322 | struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf) |
| 3323 | { |
| 3324 | int ret; |
| 3325 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3326 | 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] | 3327 | ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 3328 | if (!ret) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3329 | 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] | 3330 | if ((*htx)->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3331 | 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] | 3332 | fcgi_strm_error(fstrm); |
| 3333 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3334 | } |
| 3335 | goto end; |
| 3336 | } |
| 3337 | *ofs += ret; |
| 3338 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3339 | 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] | 3340 | return ret; |
| 3341 | } |
| 3342 | |
| 3343 | static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx, |
| 3344 | struct buffer *buf, size_t *ofs, size_t max) |
| 3345 | { |
| 3346 | int ret; |
| 3347 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3348 | 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] | 3349 | ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 3350 | if (!ret) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3351 | 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] | 3352 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3353 | 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] | 3354 | fcgi_strm_error(fstrm); |
| 3355 | fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf); |
| 3356 | } |
| 3357 | goto end; |
| 3358 | } |
| 3359 | *ofs += ret; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3360 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3361 | 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] | 3362 | return ret; |
| 3363 | } |
| 3364 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3365 | static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count) |
| 3366 | { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3367 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3368 | struct htx *htx; |
| 3369 | struct h1m *h1m = &fstrm->h1m; |
| 3370 | size_t ret, data, total = 0; |
| 3371 | |
| 3372 | htx = htx_from_buf(buf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3373 | TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count}); |
| 3374 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3375 | data = htx->data; |
| 3376 | if (fstrm->state == FCGI_SS_ERROR) |
| 3377 | goto end; |
| 3378 | |
| 3379 | do { |
| 3380 | size_t used = htx_used_space(htx); |
| 3381 | |
| 3382 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3383 | 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] | 3384 | ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count); |
| 3385 | if (!ret) |
| 3386 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3387 | |
| 3388 | TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx); |
| 3389 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3390 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) { |
| 3391 | struct htx_blk *blk = htx_get_head_blk(htx); |
| 3392 | struct htx_sl *sl; |
| 3393 | |
| 3394 | if (!blk) |
| 3395 | break; |
| 3396 | sl = htx_get_blk_ptr(htx, blk); |
| 3397 | sl->flags |= HTX_SL_F_XFER_LEN; |
| 3398 | htx->extra = 0; |
| 3399 | } |
| 3400 | } |
| 3401 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3402 | 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] | 3403 | 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] | 3404 | |
| 3405 | if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR && |
| 3406 | (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) { |
| 3407 | TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 3408 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 1e85778 | 2020-12-08 10:38:22 +0100 | [diff] [blame] | 3409 | h1m->state = H1_MSG_DONE; |
| 3410 | TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx); |
| 3411 | } |
| 3412 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 3413 | if (!ret && h1m->state != H1_MSG_DONE) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3414 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3415 | |
| 3416 | 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] | 3417 | } |
| 3418 | else if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 3419 | TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm); |
| 3420 | ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count); |
| 3421 | if (!ret && h1m->state != H1_MSG_DONE) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3422 | break; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3423 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 3424 | 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] | 3425 | } |
| 3426 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 3427 | 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] | 3428 | if (b_data(&fstrm->rxbuf) > total) { |
| 3429 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3430 | 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] | 3431 | fcgi_strm_error(fstrm); |
| 3432 | } |
| 3433 | break; |
| 3434 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3435 | else { |
| 3436 | htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3437 | 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] | 3438 | fcgi_strm_error(fstrm); |
| 3439 | break; |
| 3440 | } |
| 3441 | |
| 3442 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3443 | } while (fstrm->state != FCGI_SS_ERROR); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3444 | |
| 3445 | if (fstrm->state == FCGI_SS_ERROR) { |
| 3446 | b_reset(&fstrm->rxbuf); |
| 3447 | htx_to_buf(htx, buf); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3448 | 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] | 3449 | return 0; |
| 3450 | } |
| 3451 | |
| 3452 | b_del(&fstrm->rxbuf, total); |
| 3453 | |
| 3454 | end: |
| 3455 | htx_to_buf(htx, buf); |
| 3456 | ret = htx->data - data; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3457 | 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] | 3458 | return ret; |
| 3459 | } |
| 3460 | |
| 3461 | /* |
| 3462 | * Attach a new stream to a connection |
| 3463 | * (Used for outgoing connections) |
| 3464 | */ |
| 3465 | static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess) |
| 3466 | { |
| 3467 | struct conn_stream *cs; |
| 3468 | struct fcgi_strm *fstrm; |
| 3469 | struct fcgi_conn *fconn = conn->ctx; |
| 3470 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3471 | TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn); |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 3472 | cs = cs_new(conn, conn->target); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3473 | if (!cs) { |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3474 | TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn); |
| 3475 | goto err; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3476 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3477 | fstrm = fcgi_conn_stream_new(fconn, cs, sess); |
| 3478 | if (!fstrm) { |
| 3479 | cs_free(cs); |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3480 | goto err; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3481 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3482 | TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3483 | return cs; |
Christopher Faulet | 73518be | 2021-01-27 12:06:54 +0100 | [diff] [blame] | 3484 | |
| 3485 | err: |
| 3486 | TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn); |
| 3487 | return NULL; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3491 | * We have to scan because we may have some orphan streams. It might be |
| 3492 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3493 | * orphans. |
| 3494 | */ |
| 3495 | static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn) |
| 3496 | { |
| 3497 | struct fcgi_conn *fconn = conn->ctx; |
| 3498 | struct fcgi_strm *fstrm; |
| 3499 | struct eb32_node *node; |
| 3500 | |
| 3501 | node = eb32_first(&fconn->streams_by_id); |
| 3502 | while (node) { |
| 3503 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 3504 | if (fstrm->cs) |
| 3505 | return fstrm->cs; |
| 3506 | node = eb32_next(node); |
| 3507 | } |
| 3508 | return NULL; |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * Destroy the mux and the associated connection, if it is no longer used |
| 3513 | */ |
| 3514 | static void fcgi_destroy(void *ctx) |
| 3515 | { |
| 3516 | struct fcgi_conn *fconn = ctx; |
| 3517 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3518 | TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3519 | if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn) |
| 3520 | fcgi_release(fconn); |
| 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * Detach the stream from the connection and possibly release the connection. |
| 3525 | */ |
| 3526 | static void fcgi_detach(struct conn_stream *cs) |
| 3527 | { |
| 3528 | struct fcgi_strm *fstrm = cs->ctx; |
| 3529 | struct fcgi_conn *fconn; |
| 3530 | struct session *sess; |
| 3531 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3532 | TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm); |
| 3533 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3534 | cs->ctx = NULL; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3535 | if (!fstrm) { |
| 3536 | TRACE_LEAVE(FCGI_EV_STRM_END); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3537 | return; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3538 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3539 | |
Willy Tarreau | f11be0e | 2020-01-16 16:59:45 +0100 | [diff] [blame] | 3540 | /* there's no txbuf so we're certain no to be able to send anything */ |
| 3541 | fstrm->flags &= ~FCGI_SF_NOTIFIED; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3542 | |
| 3543 | sess = fstrm->sess; |
| 3544 | fconn = fstrm->fconn; |
| 3545 | fstrm->cs = NULL; |
| 3546 | fconn->nb_cs--; |
| 3547 | |
| 3548 | if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) { |
| 3549 | fconn->flags &= ~FCGI_CF_MPXS_CONNS; |
| 3550 | fconn->streams_limit = 1; |
| 3551 | } |
| 3552 | else if (fstrm->proto_status == FCGI_PS_OVERLOADED || |
| 3553 | fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) { |
| 3554 | fconn->flags &= ~FCGI_CF_KEEP_CONN; |
| 3555 | fconn->state = FCGI_CS_CLOSED; |
| 3556 | } |
| 3557 | |
| 3558 | /* this stream may be blocked waiting for some data to leave, so orphan |
| 3559 | * it in this case. |
| 3560 | */ |
| 3561 | if (!(cs->conn->flags & CO_FL_ERROR) && |
| 3562 | (fconn->state != FCGI_CS_CLOSED) && |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3563 | (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) && |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3564 | (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3565 | 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] | 3566 | return; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3567 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3568 | |
| 3569 | if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) { |
| 3570 | /* unblock the connection if it was blocked on this stream. */ |
| 3571 | fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY; |
| 3572 | fcgi_conn_restart_reading(fconn, 1); |
| 3573 | } |
| 3574 | |
| 3575 | fcgi_strm_destroy(fstrm); |
| 3576 | |
| 3577 | 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] | 3578 | (fconn->flags & FCGI_CF_KEEP_CONN)) { |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3579 | if (fconn->conn->flags & CO_FL_PRIVATE) { |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3580 | /* Add the connection in the session serverlist, if not already done */ |
| 3581 | if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) { |
| 3582 | fconn->conn->owner = NULL; |
| 3583 | if (eb_is_empty(&fconn->streams_by_id)) { |
| 3584 | /* let's kill the connection right away */ |
| 3585 | fconn->conn->mux->destroy(fconn); |
| 3586 | TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR); |
| 3587 | return; |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3588 | } |
| 3589 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3590 | if (eb_is_empty(&fconn->streams_by_id)) { |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3591 | if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) { |
| 3592 | /* The connection is destroyed, let's leave */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3593 | 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] | 3594 | return; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3595 | } |
| 3596 | } |
| 3597 | } |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3598 | else { |
| 3599 | if (eb_is_empty(&fconn->streams_by_id)) { |
Amaury Denoyelle | 46f041d | 2020-10-14 18:17:11 +0200 | [diff] [blame] | 3600 | /* If the connection is owned by the session, first remove it |
| 3601 | * from its list |
| 3602 | */ |
| 3603 | if (fconn->conn->owner) { |
| 3604 | session_unown_conn(fconn->conn->owner, fconn->conn); |
| 3605 | fconn->conn->owner = NULL; |
| 3606 | } |
| 3607 | |
Olivier Houchard | dc2f275 | 2020-02-13 19:12:07 +0100 | [diff] [blame] | 3608 | 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] | 3609 | /* The server doesn't want it, let's kill the connection right away */ |
| 3610 | fconn->conn->mux->destroy(fconn); |
| 3611 | TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR); |
| 3612 | return; |
| 3613 | } |
Olivier Houchard | 199d4fa | 2020-03-22 23:25:51 +0100 | [diff] [blame] | 3614 | /* At this point, the connection has been added to the |
| 3615 | * server idle list, so another thread may already have |
| 3616 | * hijacked it, so we can't do anything with it. |
| 3617 | */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3618 | TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn); |
| 3619 | return; |
| 3620 | } |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3621 | else if (MT_LIST_ISEMPTY(&fconn->conn->list) && |
Amaury Denoyelle | 46f041d | 2020-10-14 18:17:11 +0200 | [diff] [blame] | 3622 | fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) && |
| 3623 | !LIST_ADDED(&fconn->conn->session_list)) { |
Olivier Houchard | f0d4dff | 2020-03-06 18:12:03 +0100 | [diff] [blame] | 3624 | LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list)); |
Olivier Houchard | dc2f275 | 2020-02-13 19:12:07 +0100 | [diff] [blame] | 3625 | } |
Christopher Faulet | 29ae7ff | 2020-07-01 15:51:46 +0200 | [diff] [blame] | 3626 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | /* 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] | 3630 | * stream and the connection is in error. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3631 | */ |
| 3632 | if (fcgi_conn_is_dead(fconn)) { |
| 3633 | /* no more stream will come, kill it now */ |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3634 | TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3635 | fcgi_release(fconn); |
| 3636 | } |
| 3637 | else if (fconn->task) { |
| 3638 | fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout)); |
| 3639 | task_queue(fconn->task); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3640 | 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] | 3641 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3642 | else |
| 3643 | TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | |
| 3647 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3648 | static void fcgi_do_shutr(struct fcgi_strm *fstrm) |
| 3649 | { |
| 3650 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3651 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3652 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3653 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3654 | if (fstrm->state == FCGI_SS_CLOSED) |
| 3655 | goto done; |
| 3656 | |
| 3657 | /* a connstream may require us to immediately kill the whole connection |
| 3658 | * for example because of a "tcp-request content reject" rule that is |
| 3659 | * normally used to limit abuse. |
| 3660 | */ |
| 3661 | if ((fstrm->flags & FCGI_SF_KILL_CONN) && |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3662 | !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 3663 | 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] | 3664 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3665 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3666 | else if (fstrm->flags & FCGI_SF_BEGIN_SENT) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3667 | 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] | 3668 | if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) && |
| 3669 | !fcgi_strm_send_abort(fconn, fstrm)) |
| 3670 | goto add_to_list; |
| 3671 | } |
| 3672 | |
| 3673 | fcgi_strm_close(fstrm); |
| 3674 | |
| 3675 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 3676 | tasklet_wakeup(fconn->wait_event.tasklet); |
| 3677 | done: |
| 3678 | fstrm->flags &= ~FCGI_SF_WANT_SHUTR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3679 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3680 | return; |
| 3681 | |
| 3682 | add_to_list: |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3683 | /* Let the handler know we want to shutr, and add ourselves to the |
| 3684 | * send list if not yet done. fcgi_deferred_shut() will be |
| 3685 | * automatically called via the shut_tl tasklet when there's room |
| 3686 | * again. |
| 3687 | */ |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3688 | if (!LIST_ADDED(&fstrm->send_list)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3689 | if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3690 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
| 3691 | } |
| 3692 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3693 | fstrm->flags |= FCGI_SF_WANT_SHUTR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3694 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3699 | static void fcgi_do_shutw(struct fcgi_strm *fstrm) |
| 3700 | { |
| 3701 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3702 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3703 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3704 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3705 | if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED) |
| 3706 | goto done; |
| 3707 | |
| 3708 | if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) { |
| 3709 | if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) && |
| 3710 | !fcgi_strm_send_abort(fconn, fstrm)) |
| 3711 | goto add_to_list; |
| 3712 | |
| 3713 | if (fstrm->state == FCGI_SS_HREM) |
| 3714 | fcgi_strm_close(fstrm); |
| 3715 | else |
| 3716 | fstrm->state = FCGI_SS_HLOC; |
| 3717 | } else { |
| 3718 | /* a connstream may require us to immediately kill the whole connection |
| 3719 | * for example because of a "tcp-request content reject" rule that is |
| 3720 | * normally used to limit abuse. |
| 3721 | */ |
| 3722 | if ((fstrm->flags & FCGI_SF_KILL_CONN) && |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3723 | !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) { |
| 3724 | 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] | 3725 | fconn->state = FCGI_CS_CLOSED; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3726 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3727 | |
| 3728 | fcgi_strm_close(fstrm); |
| 3729 | } |
| 3730 | |
| 3731 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) |
| 3732 | tasklet_wakeup(fconn->wait_event.tasklet); |
| 3733 | done: |
| 3734 | fstrm->flags &= ~FCGI_SF_WANT_SHUTW; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3735 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3736 | return; |
| 3737 | |
| 3738 | add_to_list: |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3739 | /* Let the handler know we want to shutr, and add ourselves to the |
| 3740 | * send list if not yet done. fcgi_deferred_shut() will be |
| 3741 | * automatically called via the shut_tl tasklet when there's room |
| 3742 | * again. |
| 3743 | */ |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3744 | if (!LIST_ADDED(&fstrm->send_list)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3745 | if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3746 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
| 3747 | } |
| 3748 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3749 | fstrm->flags |= FCGI_SF_WANT_SHUTW; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3750 | TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3751 | return; |
| 3752 | } |
| 3753 | |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3754 | /* 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] | 3755 | * 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] | 3756 | * and prevented the last record from being emitted. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3757 | */ |
Willy Tarreau | ac6322d | 2021-01-29 12:33:46 +0100 | [diff] [blame] | 3758 | 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] | 3759 | { |
| 3760 | struct fcgi_strm *fstrm = ctx; |
| 3761 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3762 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3763 | TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm); |
| 3764 | |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3765 | if (fstrm->flags & FCGI_SF_NOTIFIED) { |
| 3766 | /* some data processing remains to be done first */ |
| 3767 | goto end; |
| 3768 | } |
| 3769 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3770 | if (fstrm->flags & FCGI_SF_WANT_SHUTW) |
| 3771 | fcgi_do_shutw(fstrm); |
| 3772 | |
| 3773 | if (fstrm->flags & FCGI_SF_WANT_SHUTR) |
| 3774 | fcgi_do_shutr(fstrm); |
| 3775 | |
| 3776 | if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) { |
| 3777 | /* We're done trying to send, remove ourself from the send_list */ |
| 3778 | LIST_DEL_INIT(&fstrm->send_list); |
| 3779 | |
| 3780 | if (!fstrm->cs) { |
| 3781 | fcgi_strm_destroy(fstrm); |
| 3782 | if (fcgi_conn_is_dead(fconn)) |
| 3783 | fcgi_release(fconn); |
| 3784 | } |
| 3785 | } |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3786 | end: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3787 | TRACE_LEAVE(FCGI_EV_STRM_SHUT); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3788 | return NULL; |
| 3789 | } |
| 3790 | |
| 3791 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
| 3792 | static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3793 | { |
| 3794 | struct fcgi_strm *fstrm = cs->ctx; |
| 3795 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3796 | TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3797 | if (cs->flags & CS_FL_KILL_CONN) |
| 3798 | fstrm->flags |= FCGI_SF_KILL_CONN; |
| 3799 | |
| 3800 | if (!mode) |
| 3801 | return; |
| 3802 | |
| 3803 | fcgi_do_shutr(fstrm); |
| 3804 | } |
| 3805 | |
| 3806 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
| 3807 | static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3808 | { |
| 3809 | struct fcgi_strm *fstrm = cs->ctx; |
| 3810 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3811 | TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3812 | if (cs->flags & CS_FL_KILL_CONN) |
| 3813 | fstrm->flags |= FCGI_SF_KILL_CONN; |
| 3814 | |
| 3815 | fcgi_do_shutw(fstrm); |
| 3816 | } |
| 3817 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3818 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 3819 | * event subscriber <es> is not allowed to change from a previous call as long |
| 3820 | * as at least one event is still subscribed. The <event_type> must only be a |
| 3821 | * 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] | 3822 | */ |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3823 | 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] | 3824 | { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3825 | struct fcgi_strm *fstrm = cs->ctx; |
| 3826 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3827 | |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3828 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3829 | BUG_ON(fstrm->subs && fstrm->subs != es); |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3830 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3831 | es->events |= event_type; |
| 3832 | fstrm->subs = es; |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3833 | |
| 3834 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3835 | TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3836 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3837 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3838 | TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3839 | if (!LIST_ADDED(&fstrm->send_list)) |
| 3840 | LIST_ADDQ(&fconn->send_list, &fstrm->send_list); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3841 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3842 | return 0; |
| 3843 | } |
| 3844 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3845 | /* Called from the upper layer, to unsubscribe <es> from events <event_type> |
| 3846 | * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one |
| 3847 | * passed to the subscribe() call. It always returns zero. |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3848 | */ |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3849 | 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] | 3850 | { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3851 | struct fcgi_strm *fstrm = cs->ctx; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3852 | struct fcgi_conn *fconn = fstrm->fconn; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3853 | |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3854 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3855 | BUG_ON(fstrm->subs && fstrm->subs != es); |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3856 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3857 | es->events &= ~event_type; |
| 3858 | if (!es->events) |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3859 | fstrm->subs = NULL; |
| 3860 | |
| 3861 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3862 | TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3863 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3864 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3865 | TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm); |
Willy Tarreau | 8907e4d | 2020-01-16 17:55:37 +0100 | [diff] [blame] | 3866 | fstrm->flags &= ~FCGI_SF_NOTIFIED; |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 3867 | if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) |
| 3868 | LIST_DEL_INIT(&fstrm->send_list); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3869 | } |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
| 3873 | /* Called from the upper layer, to receive data */ |
| 3874 | static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 3875 | { |
| 3876 | struct fcgi_strm *fstrm = cs->ctx; |
| 3877 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3878 | size_t ret = 0; |
| 3879 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3880 | TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
| 3881 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3882 | if (!(fconn->flags & FCGI_CF_DEM_SALLOC)) |
| 3883 | ret = fcgi_strm_parse_response(fstrm, buf, count); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3884 | else |
| 3885 | 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] | 3886 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 3887 | if (b_data(&fstrm->rxbuf)) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3888 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 3889 | else { |
| 3890 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 3891 | if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) { |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3892 | cs->flags |= CS_FL_EOI; |
| 3893 | if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN))) |
| 3894 | cs->flags |= CS_FL_EOS; |
| 3895 | } |
Christopher Faulet | 6670e3e | 2020-10-08 15:26:33 +0200 | [diff] [blame] | 3896 | if (fcgi_conn_read0_pending(fconn)) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3897 | cs->flags |= CS_FL_EOS; |
| 3898 | if (cs->flags & CS_FL_ERR_PENDING) |
| 3899 | cs->flags |= CS_FL_ERROR; |
| 3900 | fcgi_release_buf(fconn, &fstrm->rxbuf); |
| 3901 | } |
| 3902 | |
| 3903 | if (ret && fconn->dsi == fstrm->id) { |
| 3904 | /* demux is blocking on this stream's buffer */ |
| 3905 | fconn->flags &= ~FCGI_CF_DEM_SFULL; |
| 3906 | fcgi_conn_restart_reading(fconn, 1); |
| 3907 | } |
| 3908 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3909 | TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3910 | return ret; |
| 3911 | } |
| 3912 | |
| 3913 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3914 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 3915 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 3916 | * flags may be updated on the conn_stream. |
| 3917 | */ |
| 3918 | static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 3919 | { |
| 3920 | struct fcgi_strm *fstrm = cs->ctx; |
| 3921 | struct fcgi_conn *fconn = fstrm->fconn; |
| 3922 | size_t total = 0; |
| 3923 | size_t ret; |
| 3924 | struct htx *htx = NULL; |
| 3925 | struct htx_sl *sl; |
| 3926 | struct htx_blk *blk; |
| 3927 | uint32_t bsize; |
| 3928 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3929 | 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] | 3930 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3931 | /* If we were not just woken because we wanted to send but couldn't, |
| 3932 | * and there's somebody else that is waiting to send, do nothing, |
| 3933 | * we will subscribe later and be put at the end of the list |
| 3934 | */ |
Willy Tarreau | f11be0e | 2020-01-16 16:59:45 +0100 | [diff] [blame] | 3935 | if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3936 | 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] | 3937 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3938 | } |
Willy Tarreau | f11be0e | 2020-01-16 16:59:45 +0100 | [diff] [blame] | 3939 | fstrm->flags &= ~FCGI_SF_NOTIFIED; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3940 | |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3941 | if (fconn->state < FCGI_CS_RECORD_H) { |
| 3942 | 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] | 3943 | return 0; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3944 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3945 | |
| 3946 | htx = htxbuf(buf); |
| 3947 | if (fstrm->id == 0) { |
| 3948 | int32_t id = fcgi_conn_get_next_sid(fconn); |
| 3949 | |
| 3950 | if (id < 0) { |
| 3951 | fcgi_strm_close(fstrm); |
| 3952 | cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3953 | 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] | 3954 | return 0; |
| 3955 | } |
| 3956 | |
| 3957 | eb32_delete(&fstrm->by_id); |
| 3958 | fstrm->by_id.key = fstrm->id = id; |
| 3959 | fconn->max_id = id; |
| 3960 | fconn->nb_reserved--; |
| 3961 | eb32_insert(&fconn->streams_by_id, &fstrm->by_id); |
| 3962 | |
| 3963 | |
| 3964 | /* Check if length of the body is known or if the message is |
| 3965 | * full. Otherwise, the request is invalid. |
| 3966 | */ |
| 3967 | sl = http_get_stline(htx); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 3968 | 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] | 3969 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 3970 | fcgi_strm_error(fstrm); |
| 3971 | goto done; |
| 3972 | } |
| 3973 | } |
| 3974 | |
| 3975 | if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3976 | 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] | 3977 | if (!fcgi_strm_send_begin_request(fconn, fstrm)) |
| 3978 | goto done; |
| 3979 | } |
| 3980 | |
| 3981 | if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count) |
| 3982 | fstrm->flags |= FCGI_SF_OUTGOING_DATA; |
| 3983 | |
Christopher Faulet | fe410d6 | 2020-05-19 15:13:00 +0200 | [diff] [blame] | 3984 | while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) && |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3985 | count && !htx_is_empty(htx)) { |
| 3986 | blk = htx_get_head_blk(htx); |
William Lallemand | 13ed9fa | 2019-09-25 21:21:57 +0200 | [diff] [blame] | 3987 | ALREADY_CHECKED(blk); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 3988 | bsize = htx_get_blksz(blk); |
| 3989 | |
| 3990 | switch (htx_get_blk_type(blk)) { |
| 3991 | case HTX_BLK_REQ_SL: |
| 3992 | case HTX_BLK_HDR: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 3993 | 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] | 3994 | ret = fcgi_strm_send_params(fconn, fstrm, htx); |
| 3995 | if (!ret) { |
| 3996 | goto done; |
| 3997 | } |
| 3998 | total += ret; |
| 3999 | count -= ret; |
| 4000 | break; |
| 4001 | |
| 4002 | case HTX_BLK_EOH: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4003 | if (!(fstrm->flags & FCGI_SF_EP_SENT)) { |
| 4004 | TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx); |
| 4005 | ret = fcgi_strm_send_empty_params(fconn, fstrm); |
| 4006 | if (!ret) |
| 4007 | goto done; |
| 4008 | } |
| 4009 | if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) { |
| 4010 | TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx); |
| 4011 | ret = fcgi_strm_send_empty_stdin(fconn, fstrm); |
| 4012 | if (!ret) |
| 4013 | goto done; |
| 4014 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4015 | goto remove_blk; |
| 4016 | |
| 4017 | case HTX_BLK_DATA: |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 4018 | 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] | 4019 | ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf); |
| 4020 | if (ret > 0) { |
| 4021 | htx = htx_from_buf(buf); |
| 4022 | total += ret; |
| 4023 | count -= ret; |
| 4024 | if (ret < bsize) |
| 4025 | goto done; |
| 4026 | } |
| 4027 | break; |
| 4028 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4029 | default: |
| 4030 | remove_blk: |
| 4031 | htx_remove_blk(htx, blk); |
| 4032 | total += bsize; |
| 4033 | count -= bsize; |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | done: |
| 4039 | if (fstrm->state >= FCGI_SS_HLOC) { |
| 4040 | /* trim any possibly pending data after we close (extra CR-LF, |
| 4041 | * unprocessed trailers, abnormal extra data, ...) |
| 4042 | */ |
| 4043 | total += count; |
| 4044 | count = 0; |
| 4045 | } |
| 4046 | |
| 4047 | if (fstrm->state == FCGI_SS_ERROR) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 4048 | 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] | 4049 | cs_set_error(cs); |
| 4050 | if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm)) |
| 4051 | fcgi_strm_close(fstrm); |
| 4052 | } |
| 4053 | |
| 4054 | if (htx) |
| 4055 | htx_to_buf(htx, buf); |
| 4056 | |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4057 | if (total > 0) { |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 4058 | if (!(fconn->wait_event.events & SUB_RETRY_SEND)) { |
| 4059 | 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] | 4060 | tasklet_wakeup(fconn->wait_event.tasklet); |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 4061 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4062 | |
| 4063 | /* Ok we managed to send something, leave the send_list */ |
Willy Tarreau | 7aad703 | 2020-01-16 17:20:57 +0100 | [diff] [blame] | 4064 | if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) |
| 4065 | LIST_DEL_INIT(&fstrm->send_list); |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4066 | } |
Christopher Faulet | 5c0f859 | 2019-10-04 15:21:17 +0200 | [diff] [blame] | 4067 | |
| 4068 | 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] | 4069 | return total; |
| 4070 | } |
| 4071 | |
| 4072 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 4073 | static int fcgi_show_fd(struct buffer *msg, struct connection *conn) |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4074 | { |
| 4075 | struct fcgi_conn *fconn = conn->ctx; |
| 4076 | struct fcgi_strm *fstrm = NULL; |
| 4077 | struct eb32_node *node; |
| 4078 | int send_cnt = 0; |
| 4079 | int tree_cnt = 0; |
| 4080 | int orph_cnt = 0; |
| 4081 | struct buffer *hmbuf, *tmbuf; |
| 4082 | |
| 4083 | if (!fconn) |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 4084 | return 0; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4085 | |
| 4086 | list_for_each_entry(fstrm, &fconn->send_list, send_list) |
| 4087 | send_cnt++; |
| 4088 | |
| 4089 | fstrm = NULL; |
| 4090 | node = eb32_first(&fconn->streams_by_id); |
| 4091 | while (node) { |
| 4092 | fstrm = container_of(node, struct fcgi_strm, by_id); |
| 4093 | tree_cnt++; |
| 4094 | if (!fstrm->cs) |
| 4095 | orph_cnt++; |
| 4096 | node = eb32_next(node); |
| 4097 | } |
| 4098 | |
| 4099 | hmbuf = br_head(fconn->mbuf); |
| 4100 | tmbuf = br_tail(fconn->mbuf); |
| 4101 | chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u" |
| 4102 | " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d " |
| 4103 | ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
| 4104 | fconn->state, fconn->max_id, fconn->flags, |
| 4105 | fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt, |
| 4106 | fconn->wait_event.events, fconn->dsi, |
| 4107 | (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf), |
| 4108 | (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf), |
| 4109 | br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf), |
| 4110 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 4111 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
| 4112 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 4113 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
| 4114 | |
| 4115 | if (fstrm) { |
| 4116 | chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 4117 | fstrm, fstrm->id, fstrm->flags, |
| 4118 | (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf), |
| 4119 | (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf), |
| 4120 | fstrm->cs); |
| 4121 | if (fstrm->cs) |
| 4122 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 4123 | fstrm->cs->flags, fstrm->cs->data); |
Willy Tarreau | 1776ffb | 2021-01-20 17:10:46 +0100 | [diff] [blame] | 4124 | chunk_appendf(&trash, " .subs=%p", fstrm->subs); |
| 4125 | if (fstrm->subs) { |
| 4126 | if (fstrm->subs) { |
| 4127 | chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet); |
| 4128 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 4129 | fstrm->subs->tasklet->calls, |
| 4130 | fstrm->subs->tasklet->context); |
| 4131 | resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process); |
| 4132 | chunk_appendf(&trash, ")"); |
| 4133 | } |
| 4134 | } |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4135 | } |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 4136 | return 0; |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | /* Migrate the the connection to the current thread. |
| 4140 | * Return 0 if successful, non-zero otherwise. |
| 4141 | * Expected to be called with the old thread lock held. |
| 4142 | */ |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 4143 | static int fcgi_takeover(struct connection *conn, int orig_tid) |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4144 | { |
| 4145 | struct fcgi_conn *fcgi = conn->ctx; |
Willy Tarreau | 88d18f8 | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 4146 | struct task *task; |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4147 | |
| 4148 | if (fd_takeover(conn->handle.fd, conn) != 0) |
| 4149 | return -1; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 4150 | |
| 4151 | if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) { |
| 4152 | /* We failed to takeover the xprt, even if the connection may |
| 4153 | * still be valid, flag it as error'd, as we have already |
| 4154 | * taken over the fd, and wake the tasklet, so that it will |
| 4155 | * destroy it. |
| 4156 | */ |
| 4157 | conn->flags |= CO_FL_ERROR; |
| 4158 | tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid); |
| 4159 | return -1; |
| 4160 | } |
| 4161 | |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4162 | if (fcgi->wait_event.events) |
| 4163 | fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx, |
| 4164 | fcgi->wait_event.events, &fcgi->wait_event); |
| 4165 | /* To let the tasklet know it should free itself, and do nothing else, |
| 4166 | * set its context to NULL; |
| 4167 | */ |
| 4168 | fcgi->wait_event.tasklet->context = NULL; |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 4169 | tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid); |
Willy Tarreau | 88d18f8 | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 4170 | |
| 4171 | task = fcgi->task; |
| 4172 | if (task) { |
| 4173 | task->context = NULL; |
| 4174 | fcgi->task = NULL; |
| 4175 | __ha_barrier_store(); |
| 4176 | task_kill(task); |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4177 | |
| 4178 | fcgi->task = task_new(tid_bit); |
| 4179 | if (!fcgi->task) { |
| 4180 | fcgi_release(fcgi); |
| 4181 | return -1; |
| 4182 | } |
| 4183 | fcgi->task->process = fcgi_timeout_task; |
| 4184 | fcgi->task->context = fcgi; |
| 4185 | } |
| 4186 | fcgi->wait_event.tasklet = tasklet_new(); |
| 4187 | if (!fcgi->wait_event.tasklet) { |
| 4188 | fcgi_release(fcgi); |
| 4189 | return -1; |
| 4190 | } |
| 4191 | fcgi->wait_event.tasklet->process = fcgi_io_cb; |
| 4192 | fcgi->wait_event.tasklet->context = fcgi; |
| 4193 | fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx, |
| 4194 | SUB_RETRY_RECV, &fcgi->wait_event); |
| 4195 | |
| 4196 | return 0; |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | /****************************************/ |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4200 | /* MUX initialization and instantiation */ |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4201 | /****************************************/ |
| 4202 | |
| 4203 | /* The mux operations */ |
| 4204 | static const struct mux_ops mux_fcgi_ops = { |
| 4205 | .init = fcgi_init, |
| 4206 | .wake = fcgi_wake, |
| 4207 | .attach = fcgi_attach, |
| 4208 | .get_first_cs = fcgi_get_first_cs, |
| 4209 | .detach = fcgi_detach, |
| 4210 | .destroy = fcgi_destroy, |
| 4211 | .avail_streams = fcgi_avail_streams, |
| 4212 | .used_streams = fcgi_used_streams, |
| 4213 | .rcv_buf = fcgi_rcv_buf, |
| 4214 | .snd_buf = fcgi_snd_buf, |
| 4215 | .subscribe = fcgi_subscribe, |
| 4216 | .unsubscribe = fcgi_unsubscribe, |
| 4217 | .shutr = fcgi_shutr, |
| 4218 | .shutw = fcgi_shutw, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 4219 | .ctl = fcgi_ctl, |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4220 | .show_fd = fcgi_show_fd, |
Olivier Houchard | a41bb0b | 2020-03-10 18:46:06 +0100 | [diff] [blame] | 4221 | .takeover = fcgi_takeover, |
Amaury Denoyelle | 3d3c091 | 2020-10-14 18:17:06 +0200 | [diff] [blame] | 4222 | .flags = MX_FL_HTX|MX_FL_HOL_RISK, |
Christopher Faulet | 99eff65 | 2019-08-11 23:11:30 +0200 | [diff] [blame] | 4223 | .name = "FCGI", |
| 4224 | }; |
| 4225 | |
| 4226 | |
| 4227 | /* this mux registers FCGI proto */ |
| 4228 | static struct mux_proto_list mux_proto_fcgi = |
| 4229 | { .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops }; |
| 4230 | |
| 4231 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi); |
| 4232 | |
| 4233 | /* |
| 4234 | * Local variables: |
| 4235 | * c-indent-level: 8 |
| 4236 | * c-basic-offset: 8 |
| 4237 | * End: |
| 4238 | */ |