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