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