blob: 9b48271740d96f3945cbb33e7a7b91cb18520e8d [file] [log] [blame]
Christopher Faulet99eff652019-08-11 23:11:30 +02001/*
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 Tarreaub2551052020-06-09 09:07:15 +020013#include <import/ist.h>
Willy Tarreau63617db2021-10-06 18:23:40 +020014#include <import/eb32tree.h>
15#include <import/ebmbtree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020019#include <haproxy/connection.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020020#include <haproxy/dynbuf.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020022#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020024#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020025#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020028#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Christopher Faulet3965aa72022-10-12 16:57:19 +020030#include <haproxy/mux_fcgi-t.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreauc5396bd2021-05-08 20:28:54 +020032#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020033#include <haproxy/regex.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020034#include <haproxy/sc_strm.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020035#include <haproxy/session-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020036#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020038#include <haproxy/trace.h>
Christopher Faulet5cd0e522021-06-11 13:34:42 +020039#include <haproxy/version.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020040
Christopher Faulet99eff652019-08-11 23:11:30 +020041/* 32 buffers: one for the ring's root, rest for the mbuf itself */
42#define FCGI_C_MBUF_CNT 32
43
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010044/* Size for a record header (also size of empty record) */
45#define FCGI_RECORD_HEADER_SZ 8
46
Christopher Faulet99eff652019-08-11 23:11:30 +020047/* FCGI connection descriptor */
48struct fcgi_conn {
49 struct connection *conn;
50
51 enum fcgi_conn_st state; /* FCGI connection state */
52 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
53 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
54 uint32_t flags; /* Connection flags: FCGI_CF_* */
55
56 int16_t dsi; /* dmux stream ID (<0 = idle ) */
57 uint16_t drl; /* demux record length (if dsi >= 0) */
58 uint8_t drt; /* demux record type (if dsi >= 0) */
59 uint8_t drp; /* demux record padding (if dsi >= 0) */
60
61 struct buffer dbuf; /* demux buffer */
62 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
63
64 int timeout; /* idle timeout duration in ticks */
65 int shut_timeout; /* idle timeout duration in ticks after shutdown */
66 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +020067 unsigned int nb_sc; /* number of attached stream connectors */
Christopher Faulet99eff652019-08-11 23:11:30 +020068 unsigned int nb_reserved; /* number of reserved streams */
69 unsigned int stream_cnt; /* total number of streams seen */
70
71 struct proxy *proxy; /* the proxy this connection was created for */
72 struct fcgi_app *app; /* FCGI application used by this mux */
73 struct task *task; /* timeout management task */
74 struct eb_root streams_by_id; /* all active streams by their ID */
75
76 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +020077
78 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
79 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
80};
81
Christopher Faulet99eff652019-08-11 23:11:30 +020082
83/* FCGI stream descriptor */
84struct fcgi_strm {
Willy Tarreau5aa5e772022-05-27 16:15:32 +020085 struct sedesc *sd;
Christopher Faulet99eff652019-08-11 23:11:30 +020086 struct session *sess;
87 struct fcgi_conn *fconn;
88
89 int32_t id; /* stream ID */
90
91 uint32_t flags; /* Connection flags: FCGI_SF_* */
92 enum fcgi_strm_st state; /* FCGI stream state */
93 int proto_status; /* FCGI_PS_* */
94
95 struct h1m h1m; /* response parser state for H1 */
96
97 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
98
99 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200100 struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200101 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100102 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to close after we failed to by lack of space */
Christopher Faulet99eff652019-08-11 23:11:30 +0200103};
104
105/* Flags representing all default FCGI parameters */
106#define FCGI_SP_CGI_GATEWAY 0x00000001
107#define FCGI_SP_DOC_ROOT 0x00000002
108#define FCGI_SP_SCRIPT_NAME 0x00000004
109#define FCGI_SP_PATH_INFO 0x00000008
110#define FCGI_SP_REQ_URI 0x00000010
111#define FCGI_SP_REQ_METH 0x00000020
112#define FCGI_SP_REQ_QS 0x00000040
113#define FCGI_SP_SRV_PORT 0x00000080
114#define FCGI_SP_SRV_PROTO 0x00000100
115#define FCGI_SP_SRV_NAME 0x00000200
116#define FCGI_SP_REM_ADDR 0x00000400
117#define FCGI_SP_REM_PORT 0x00000800
118#define FCGI_SP_SCRIPT_FILE 0x00001000
119#define FCGI_SP_PATH_TRANS 0x00002000
120#define FCGI_SP_CONT_LEN 0x00004000
121#define FCGI_SP_HTTPS 0x00008000
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200122#define FCGI_SP_SRV_SOFT 0x00010000
123#define FCGI_SP_MASK 0x0001FFFF
Christopher Faulet99eff652019-08-11 23:11:30 +0200124#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
125
126/* FCGI parameters used when PARAMS record is sent */
127struct fcgi_strm_params {
128 uint32_t mask;
129 struct ist docroot;
130 struct ist scriptname;
131 struct ist pathinfo;
132 struct ist meth;
133 struct ist uri;
134 struct ist vsn;
135 struct ist qs;
136 struct ist srv_name;
137 struct ist srv_port;
138 struct ist rem_addr;
139 struct ist rem_port;
140 struct ist cont_len;
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200141 struct ist srv_soft;
Christopher Faulet99eff652019-08-11 23:11:30 +0200142 int https;
143 struct buffer *p;
144};
145
146/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
147#define MAX_DATA_REALIGN 1024
148
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200149/* trace source and events */
150static void fcgi_trace(enum trace_level level, uint64_t mask,
151 const struct trace_source *src,
152 const struct ist where, const struct ist func,
153 const void *a1, const void *a2, const void *a3, const void *a4);
154
155/* The event representation is split like this :
156 * fconn - internal FCGI connection
157 * fstrm - internal FCGI stream
158 * strm - application layer
159 * rx - data receipt
160 * tx - data transmission
161 * rsp - response parsing
162 */
163static const struct trace_event fcgi_trace_events[] = {
164#define FCGI_EV_FCONN_NEW (1ULL << 0)
165 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
166#define FCGI_EV_FCONN_RECV (1ULL << 1)
167 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
168#define FCGI_EV_FCONN_SEND (1ULL << 2)
169 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
170#define FCGI_EV_FCONN_BLK (1ULL << 3)
171 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
172#define FCGI_EV_FCONN_WAKE (1ULL << 4)
173 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
174#define FCGI_EV_FCONN_END (1ULL << 5)
175 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
176#define FCGI_EV_FCONN_ERR (1ULL << 6)
177 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
178
179#define FCGI_EV_RX_FHDR (1ULL << 7)
180 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
181#define FCGI_EV_RX_RECORD (1ULL << 8)
182 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
183#define FCGI_EV_RX_EOI (1ULL << 9)
184 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
185#define FCGI_EV_RX_GETVAL (1ULL << 10)
186 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
187#define FCGI_EV_RX_STDOUT (1ULL << 11)
188 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
189#define FCGI_EV_RX_STDERR (1ULL << 12)
190 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
191#define FCGI_EV_RX_ENDREQ (1ULL << 13)
192 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
193
194#define FCGI_EV_TX_RECORD (1ULL << 14)
195 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
196#define FCGI_EV_TX_EOI (1ULL << 15)
197 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
198#define FCGI_EV_TX_BEGREQ (1ULL << 16)
199 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
200#define FCGI_EV_TX_GETVAL (1ULL << 17)
201 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
202#define FCGI_EV_TX_PARAMS (1ULL << 18)
203 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
204#define FCGI_EV_TX_STDIN (1ULL << 19)
205 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
206#define FCGI_EV_TX_ABORT (1ULL << 20)
207 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
208
209#define FCGI_EV_RSP_DATA (1ULL << 21)
210 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
211#define FCGI_EV_RSP_EOM (1ULL << 22)
212 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
213#define FCGI_EV_RSP_HDRS (1ULL << 23)
214 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
215#define FCGI_EV_RSP_BODY (1ULL << 24)
216 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
217#define FCGI_EV_RSP_TLRS (1ULL << 25)
218 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
219
220#define FCGI_EV_FSTRM_NEW (1ULL << 26)
221 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
222#define FCGI_EV_FSTRM_BLK (1ULL << 27)
223 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
224#define FCGI_EV_FSTRM_END (1ULL << 28)
225 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
226#define FCGI_EV_FSTRM_ERR (1ULL << 29)
227 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
228
229#define FCGI_EV_STRM_NEW (1ULL << 30)
230 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
231#define FCGI_EV_STRM_RECV (1ULL << 31)
232 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
233#define FCGI_EV_STRM_SEND (1ULL << 32)
234 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
235#define FCGI_EV_STRM_FULL (1ULL << 33)
236 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
237#define FCGI_EV_STRM_WAKE (1ULL << 34)
238 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
239#define FCGI_EV_STRM_SHUT (1ULL << 35)
240 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
241#define FCGI_EV_STRM_END (1ULL << 36)
242 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
243#define FCGI_EV_STRM_ERR (1ULL << 37)
244 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
245
246 { }
247};
248
249static const struct name_desc fcgi_trace_lockon_args[4] = {
250 /* arg1 */ { /* already used by the connection */ },
251 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
252 /* arg3 */ { },
253 /* arg4 */ { }
254};
255
256
257static const struct name_desc fcgi_trace_decoding[] = {
258#define FCGI_VERB_CLEAN 1
259 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
260#define FCGI_VERB_MINIMAL 2
261 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
262#define FCGI_VERB_SIMPLE 3
263 { .name="simple", .desc="add request/response status line or htx info when available" },
264#define FCGI_VERB_ADVANCED 4
265 { .name="advanced", .desc="add header fields or record decoding when available" },
266#define FCGI_VERB_COMPLETE 5
267 { .name="complete", .desc="add full data dump when available" },
268 { /* end */ }
269};
270
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200271static struct trace_source trace_fcgi __read_mostly = {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200272 .name = IST("fcgi"),
273 .desc = "FastCGI multiplexer",
274 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
275 .default_cb = fcgi_trace,
276 .known_events = fcgi_trace_events,
277 .lockon_args = fcgi_trace_lockon_args,
278 .decoding = fcgi_trace_decoding,
279 .report_events = ~0, // report everything by default
280};
281
282#define TRACE_SOURCE &trace_fcgi
283INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
284
Christopher Faulet99eff652019-08-11 23:11:30 +0200285/* FCGI connection and stream pools */
286DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
287DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
288
Willy Tarreau144f84a2021-03-02 16:09:26 +0100289struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200290static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau691d5032021-01-20 14:55:01 +0100291/* fcgi_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100292struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200293static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100294struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200295static struct fcgi_strm *fcgi_stconn_new(struct fcgi_conn *fconn, struct stconn *sc, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200296static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
297static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200298static void fcgi_strm_alert(struct fcgi_strm *fstrm);
299static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
300
Willy Tarreauc84610c2022-05-10 15:02:32 +0200301/* a dummy closed endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +0200302static const struct sedesc closed_ep = {
Willy Tarreauc1054922022-05-18 07:43:52 +0200303 .sc = NULL,
Willy Tarreaub605c422022-05-17 17:04:55 +0200304 .flags = SE_FL_DETACHED,
Willy Tarreauc84610c2022-05-10 15:02:32 +0200305};
306
Christopher Faulet99eff652019-08-11 23:11:30 +0200307/* a dmumy management stream */
308static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200309 .sd = (struct sedesc*)&closed_ep,
Christopher Faulet99eff652019-08-11 23:11:30 +0200310 .fconn = NULL,
311 .state = FCGI_SS_CLOSED,
312 .flags = FCGI_SF_NONE,
313 .id = 0,
314};
315
316/* and a dummy idle stream for use with any unknown stream */
317static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200318 .sd = (struct sedesc*)&closed_ep,
Christopher Faulet99eff652019-08-11 23:11:30 +0200319 .fconn = NULL,
320 .state = FCGI_SS_IDLE,
321 .flags = FCGI_SF_NONE,
322 .id = 0,
323};
324
Willy Tarreau77534272022-05-18 07:34:16 +0200325/* returns the stconn associated to the FCGI stream */
326static forceinline struct stconn *fcgi_strm_sc(const struct fcgi_strm *fstrm)
327{
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200328 return fstrm->sd->sc;
Willy Tarreau77534272022-05-18 07:34:16 +0200329}
330
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200331
332/* the FCGI traces always expect that arg1, if non-null, is of type connection
333 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
334 * and that arg3, if non-null, is a htx for rx/tx headers.
335 */
336static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
337 const struct ist where, const struct ist func,
338 const void *a1, const void *a2, const void *a3, const void *a4)
339{
340 const struct connection *conn = a1;
Willy Tarreau31a83062022-01-28 09:36:35 +0100341 struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200342 const struct fcgi_strm *fstrm = a2;
343 const struct htx *htx = a3;
344 const size_t *val = a4;
345
346 if (!fconn)
347 fconn = (fstrm ? fstrm->fconn : NULL);
348
349 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
350 return;
351
352 /* Display the response state if fstrm is defined */
353 if (fstrm)
354 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
355
356 if (src->verbosity == FCGI_VERB_CLEAN)
357 return;
358
359 /* Display the value to the 4th argument (level > STATE) */
360 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100361 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200362
363 /* Display status-line if possible (verbosity > MINIMAL) */
364 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
Willy Tarreaud46b5b92022-05-30 16:27:48 +0200365 const struct htx_blk *blk = __htx_get_head_blk(htx);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200366 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
367 enum htx_blk_type type = htx_get_blk_type(blk);
368
369 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
370 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
371 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
372 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
373 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
374 }
375
376 /* Display fconn info and, if defined, fstrm info */
377 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
378 if (fstrm)
379 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
380
381 if (!fstrm || fstrm->id <= 0)
382 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
383 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
384 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
385
386 if (src->verbosity == FCGI_VERB_MINIMAL)
387 return;
388
389 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
390 if (src->level > TRACE_LEVEL_USER) {
391 if (src->verbosity == FCGI_VERB_COMPLETE ||
392 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
393 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
394 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
395 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
396 if (src->verbosity == FCGI_VERB_COMPLETE ||
397 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
Willy Tarreau31a83062022-01-28 09:36:35 +0100398 struct buffer *hmbuf = br_head(fconn->mbuf);
399 struct buffer *tmbuf = br_tail(fconn->mbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200400
401 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
402 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
403 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
404 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
405 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
406 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
407 }
408
409 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
410 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
411 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
412 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
413 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
414 }
415
416 /* Display htx info if defined (level > USER) */
417 if (src->level > TRACE_LEVEL_USER && htx) {
418 int full = 0;
419
420 /* Full htx info (level > STATE && verbosity > SIMPLE) */
421 if (src->level > TRACE_LEVEL_STATE) {
422 if (src->verbosity == FCGI_VERB_COMPLETE)
423 full = 1;
424 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
425 full = 1;
426 }
427
428 chunk_memcat(&trace_buf, "\n\t", 2);
429 htx_dump(&trace_buf, htx, full);
430 }
431}
Christopher Faulet99eff652019-08-11 23:11:30 +0200432
433/*****************************************************/
434/* functions below are for dynamic buffer management */
435/*****************************************************/
436
437/* Indicates whether or not the we may call the fcgi_recv() function to attempt
438 * to receive data into the buffer and/or demux pending data. The condition is
439 * a bit complex due to some API limits for now. The rules are the following :
440 * - if an error or a shutdown was detected on the connection and the buffer
441 * is empty, we must not attempt to receive
442 * - if the demux buf failed to be allocated, we must not try to receive and
443 * we know there is nothing pending
444 * - if no flag indicates a blocking condition, we may attempt to receive,
445 * regardless of whether the demux buffer is full or not, so that only
446 * de demux part decides whether or not to block. This is needed because
447 * the connection API indeed prevents us from re-enabling receipt that is
448 * already enabled in a polled state, so we must always immediately stop
449 * as soon as the demux can't proceed so as never to hit an end of read
450 * with data pending in the buffers.
451 * - otherwise must may not attempt
452 */
453static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
454{
Christopher Fauletab79b322022-10-12 17:51:51 +0200455 if (fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERROR))
Christopher Faulet99eff652019-08-11 23:11:30 +0200456 return 0;
457
Christopher Fauletab79b322022-10-12 17:51:51 +0200458 if (b_data(&fconn->dbuf) == 0 && fconn->state == FCGI_CS_CLOSED)
459 return 0;
460
Christopher Faulet99eff652019-08-11 23:11:30 +0200461 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
462 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
463 return 1;
464
465 return 0;
466}
467
468/* Restarts reading on the connection if it was not enabled */
469static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
470{
471 if (!fcgi_recv_allowed(fconn))
472 return;
473 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
474 (fconn->wait_event.events & SUB_RETRY_RECV))
475 return;
476 tasklet_wakeup(fconn->wait_event.tasklet);
477}
478
479
480/* Tries to grab a buffer and to re-enable processing on mux <target>. The
481 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
482 * the allocation succeeds, in which case the connection is woken up, or 0 if
483 * it's impossible to wake up and we prefer to be woken up later.
484 */
485static int fcgi_buf_available(void *target)
486{
487 struct fcgi_conn *fconn = target;
488 struct fcgi_strm *fstrm;
489
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100490 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200491 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200492 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
493 fcgi_conn_restart_reading(fconn, 1);
494 return 1;
495 }
496
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100497 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200498 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200499 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200500 if (fconn->flags & FCGI_CF_DEM_MROOM) {
501 fconn->flags &= ~FCGI_CF_DEM_MROOM;
502 fcgi_conn_restart_reading(fconn, 1);
503 }
504 return 1;
505 }
506
507 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
Willy Tarreau77534272022-05-18 07:34:16 +0200508 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fcgi_strm_sc(fstrm) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100509 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200510 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200511 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
512 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200513 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200514 return 1;
515 }
516
517 return 0;
518}
519
520static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
521{
522 struct buffer *buf = NULL;
523
Willy Tarreau2b718102021-04-21 07:32:39 +0200524 if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100525 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200526 fconn->buf_wait.target = fconn;
527 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200528 LIST_APPEND(&th_ctx->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200529 }
530 return buf;
531}
532
533static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
534{
535 if (bptr->size) {
536 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100537 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200538 }
539}
540
541static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
542{
543 struct buffer *buf;
544 unsigned int count = 0;
545
546 while (b_size(buf = br_head_pick(fconn->mbuf))) {
547 b_free(buf);
548 count++;
549 }
550 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100551 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200552}
553
554/* Returns the number of allocatable outgoing streams for the connection taking
555 * the number reserved streams into account.
556 */
557static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
558{
559 int ret;
560
561 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
562 if (ret < 0)
563 ret = 0;
564 return ret;
565}
566
567/* Returns the number of streams in use on a connection to figure if it's
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200568 * idle or not. We check nb_sc and not nb_streams as the caller will want
Christopher Faulet99eff652019-08-11 23:11:30 +0200569 * to know if it was the last one after a detach().
570 */
571static int fcgi_used_streams(struct connection *conn)
572{
573 struct fcgi_conn *fconn = conn->ctx;
574
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200575 return fconn->nb_sc;
Christopher Faulet99eff652019-08-11 23:11:30 +0200576}
577
578/* Returns the number of concurrent streams available on the connection */
579static int fcgi_avail_streams(struct connection *conn)
580{
581 struct server *srv = objt_server(conn->target);
582 struct fcgi_conn *fconn = conn->ctx;
583 int ret1, ret2;
584
585 /* Don't open new stream if the connection is closed */
586 if (fconn->state == FCGI_CS_CLOSED)
587 return 0;
588
589 /* May be negative if this setting has changed */
590 ret1 = (fconn->streams_limit - fconn->nb_streams);
591
592 /* we must also consider the limit imposed by stream IDs */
593 ret2 = fcgi_streams_left(fconn);
594 ret1 = MIN(ret1, ret2);
595 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
596 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
597 ret1 = MIN(ret1, ret2);
598 }
599 return ret1;
600}
601
602/*****************************************************************/
603/* functions below are dedicated to the mux setup and management */
604/*****************************************************************/
605
606/* Initializes the mux once it's attached. Only outgoing connections are
607 * supported. So the context is already initialized before installing the
608 * mux. <input> is always used as Input buffer and may contain data. It is the
609 * caller responsibility to not reuse it anymore. Returns < 0 on error.
610 */
611static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
612 struct buffer *input)
613{
614 struct fcgi_conn *fconn;
615 struct fcgi_strm *fstrm;
616 struct fcgi_app *app = get_px_fcgi_app(px);
617 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200618 void *conn_ctx = conn->ctx;
619
620 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200621
Christopher Faulet73518be2021-01-27 12:06:54 +0100622 if (!app) {
623 TRACE_ERROR("No FCGI app found, don't create fconn", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200624 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100625 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200626
627 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100628 if (!fconn) {
629 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200630 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100631 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200632
633 fconn->shut_timeout = fconn->timeout = px->timeout.server;
634 if (tick_isset(px->timeout.serverfin))
635 fconn->shut_timeout = px->timeout.serverfin;
636
637 fconn->flags = FCGI_CF_NONE;
638
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500639 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200640 if (app->flags & FCGI_APP_FL_KEEP_CONN)
641 fconn->flags |= FCGI_CF_KEEP_CONN;
642 if (app->flags & FCGI_APP_FL_GET_VALUES)
643 fconn->flags |= FCGI_CF_GET_VALUES;
644 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
645 fconn->flags |= FCGI_CF_MPXS_CONNS;
646
647 fconn->proxy = px;
648 fconn->app = app;
649 fconn->task = NULL;
650 if (tick_isset(fconn->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200651 t = task_new_here();
Christopher Faulet73518be2021-01-27 12:06:54 +0100652 if (!t) {
653 TRACE_ERROR("fconn task allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200654 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100655 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200656
657 fconn->task = t;
658 t->process = fcgi_timeout_task;
659 t->context = fconn;
660 t->expire = tick_add(now_ms, fconn->timeout);
661 }
662
663 fconn->wait_event.tasklet = tasklet_new();
664 if (!fconn->wait_event.tasklet)
665 goto fail;
666 fconn->wait_event.tasklet->process = fcgi_io_cb;
667 fconn->wait_event.tasklet->context = fconn;
668 fconn->wait_event.events = 0;
669
670 /* Initialise the context. */
671 fconn->state = FCGI_CS_INIT;
672 fconn->conn = conn;
673 fconn->streams_limit = app->maxreqs;
674 fconn->max_id = -1;
675 fconn->nb_streams = 0;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200676 fconn->nb_sc = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +0200677 fconn->nb_reserved = 0;
678 fconn->stream_cnt = 0;
679
680 fconn->dbuf = *input;
681 fconn->dsi = -1;
682
683 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
684 fconn->streams_by_id = EB_ROOT;
685 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100686 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200687
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200688 conn->ctx = fconn;
689
Christopher Faulet99eff652019-08-11 23:11:30 +0200690 if (t)
691 task_queue(t);
692
693 /* FIXME: this is temporary, for outgoing connections we need to
694 * immediately allocate a stream until the code is modified so that the
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200695 * caller calls ->attach(). For now the outgoing sc is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200696 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200697 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200698 fstrm = fcgi_stconn_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200699 if (!fstrm)
700 goto fail;
701
Christopher Faulet99eff652019-08-11 23:11:30 +0200702
703 /* Repare to read something */
704 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200705 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200706 return 0;
707
708 fail:
709 task_destroy(t);
710 if (fconn->wait_event.tasklet)
711 tasklet_free(fconn->wait_event.tasklet);
712 pool_free(pool_head_fcgi_conn, fconn);
713 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200714 conn->ctx = conn_ctx; // restore saved ctx
715 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200716 return -1;
717}
718
719/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
720 * -1 if no more is allocatable.
721 */
722static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
723{
724 int32_t id = (fconn->max_id + 1) | 1;
725
726 if ((id & 0x80000000U))
727 id = -1;
728 return id;
729}
730
731/* Returns the stream associated with id <id> or NULL if not found */
732static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
733{
734 struct eb32_node *node;
735
736 if (id == 0)
737 return (struct fcgi_strm *)fcgi_mgmt_stream;
738
739 if (id > fconn->max_id)
740 return (struct fcgi_strm *)fcgi_unknown_stream;
741
742 node = eb32_lookup(&fconn->streams_by_id, id);
743 if (!node)
744 return (struct fcgi_strm *)fcgi_unknown_stream;
745 return container_of(node, struct fcgi_strm, by_id);
746}
747
748
749/* Release function. This one should be called to free all resources allocated
750 * to the mux.
751 */
752static void fcgi_release(struct fcgi_conn *fconn)
753{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200754 struct connection *conn = fconn->conn;
Christopher Faulet99eff652019-08-11 23:11:30 +0200755
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200756 TRACE_POINT(FCGI_EV_FCONN_END);
757
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200758 if (LIST_INLIST(&fconn->buf_wait.list))
759 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200760
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200761 fcgi_release_buf(fconn, &fconn->dbuf);
762 fcgi_release_mbuf(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200763
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200764 if (fconn->task) {
765 fconn->task->context = NULL;
766 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
767 fconn->task = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200768 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200769 if (fconn->wait_event.tasklet)
770 tasklet_free(fconn->wait_event.tasklet);
771 if (conn && fconn->wait_event.events != 0)
772 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
773 &fconn->wait_event);
774
775 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200776
777 if (conn) {
778 conn->mux = NULL;
779 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200780 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200781
782 conn_stop_tracking(conn);
783 conn_full_close(conn);
784 if (conn->destroy_cb)
785 conn->destroy_cb(conn);
786 conn_free(conn);
787 }
788}
789
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200790/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
791 * pending on the connection AND if there is no more data in the demux
792 * buffer. The function returns 1 to report a read0 or 0 otherwise.
793 */
794static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
795{
Christopher Fauletab79b322022-10-12 17:51:51 +0200796 if ((fconn->flags & FCGI_CF_EOS) && !b_data(&fconn->dbuf))
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200797 return 1;
798 return 0;
799}
800
Christopher Faulet99eff652019-08-11 23:11:30 +0200801
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500802/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200803static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
804{
805 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
806 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
Christopher Fauletab79b322022-10-12 17:51:51 +0200807 (fconn->flags & FCGI_CF_ERROR) || /* errors close immediately */
Christopher Faulet99eff652019-08-11 23:11:30 +0200808 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
809 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
810 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
Christopher Fauletab79b322022-10-12 17:51:51 +0200811 (fconn->flags & FCGI_CF_EOS))))
812 return 1;
Christopher Faulet99eff652019-08-11 23:11:30 +0200813 return 0;
814}
815
816
817/********************************************************/
818/* functions below are for the FCGI protocol processing */
819/********************************************************/
820
Christopher Faulet99eff652019-08-11 23:11:30 +0200821/* Marks an error on the stream. */
822static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
823{
824 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200825 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
826 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200827 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200828 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
829 }
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200830 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200831 }
832}
833
834/* Attempts to notify the data layer of recv availability */
835static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
836{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100837 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200838 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100839 tasklet_wakeup(fstrm->subs->tasklet);
840 fstrm->subs->events &= ~SUB_RETRY_RECV;
841 if (!fstrm->subs->events)
842 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200843 }
844}
845
846/* Attempts to notify the data layer of send availability */
847static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
848{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100849 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200850 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100851 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100852 tasklet_wakeup(fstrm->subs->tasklet);
853 fstrm->subs->events &= ~SUB_RETRY_SEND;
854 if (!fstrm->subs->events)
855 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200856 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100857 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
858 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
859 tasklet_wakeup(fstrm->shut_tl);
860 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200861}
862
863/* Alerts the data layer, trying to wake it up by all means, following
864 * this sequence :
865 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
866 * for recv
867 * - if its subscribed to send, then it's woken up for send
868 * - if it was subscribed to neither, its ->wake() callback is called
869 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +0200870 * stream connector anymore.
Christopher Faulet99eff652019-08-11 23:11:30 +0200871 */
872static void fcgi_strm_alert(struct fcgi_strm *fstrm)
873{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200874 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100875 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100876 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200877 fcgi_strm_notify_recv(fstrm);
878 fcgi_strm_notify_send(fstrm);
879 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200880 else if (fcgi_strm_sc(fstrm) && fcgi_strm_sc(fstrm)->app_ops->wake != NULL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200881 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200882 fcgi_strm_sc(fstrm)->app_ops->wake(fcgi_strm_sc(fstrm));
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200883 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200884}
885
886/* Writes the 16-bit record size <len> at address <record> */
887static inline void fcgi_set_record_size(void *record, uint16_t len)
888{
889 uint8_t *out = (record + 4);
890
891 *out = (len >> 8);
892 *(out + 1) = (len & 0xff);
893}
894
895/* Writes the 16-bit stream id <id> at address <record> */
896static inline void fcgi_set_record_id(void *record, uint16_t id)
897{
898 uint8_t *out = (record + 2);
899
900 *out = (id >> 8);
901 *(out + 1) = (id & 0xff);
902}
903
904/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
905 * its connection if the stream was not yet closed. Please use this exclusively
906 * before closing a stream to ensure stream count is well maintained.
907 */
908static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
909{
910 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200911 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200912 fstrm->fconn->nb_streams--;
913 if (!fstrm->id)
914 fstrm->fconn->nb_reserved--;
Willy Tarreau77534272022-05-18 07:34:16 +0200915 if (fcgi_strm_sc(fstrm)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200916 if (!se_fl_test(fstrm->sd, SE_FL_EOS) && !b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +0200917 fcgi_strm_notify_recv(fstrm);
918 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200919 fstrm->state = FCGI_SS_CLOSED;
920 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
921 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200922 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200923}
924
925/* Detaches a FCGI stream from its FCGI connection and releases it to the
926 * fcgi_strm pool.
927 */
928static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
929{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200930 struct connection *conn = fstrm->fconn->conn;
931
932 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
933
Christopher Faulet99eff652019-08-11 23:11:30 +0200934 fcgi_strm_close(fstrm);
935 eb32_delete(&fstrm->by_id);
936 if (b_size(&fstrm->rxbuf)) {
937 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100938 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200939 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100940 if (fstrm->subs)
941 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +0200942 /* There's no need to explicitly call unsubscribe here, the only
943 * reference left would be in the fconn send_list/fctl_list, and if
944 * we're in it, we're getting out anyway
945 */
946 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +0100947 tasklet_free(fstrm->shut_tl);
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200948 BUG_ON(fstrm->sd && !se_fl_test(fstrm->sd, SE_FL_ORPHAN));
949 sedesc_free(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200950 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200951
952 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200953}
954
955/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
956 * stream tree. In case of error, nothing is added and NULL is returned. The
957 * causes of errors can be any failed memory allocation. The caller is
958 * responsible for checking if the connection may support an extra stream prior
959 * to calling this function.
960 */
961static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
962{
963 struct fcgi_strm *fstrm;
964
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200965 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
966
Christopher Faulet99eff652019-08-11 23:11:30 +0200967 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +0100968 if (!fstrm) {
969 TRACE_ERROR("fstrm allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200970 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +0100971 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200972
Willy Tarreau7aad7032020-01-16 17:20:57 +0100973 fstrm->shut_tl = tasklet_new();
974 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +0100975 TRACE_ERROR("fstrm shut tasklet allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200976 pool_free(pool_head_fcgi_strm, fstrm);
977 goto out;
978 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100979 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +0100980 fstrm->shut_tl->process = fcgi_deferred_shut;
981 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +0200982 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200983 fstrm->fconn = fconn;
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200984 fstrm->sd = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200985 fstrm->flags = FCGI_SF_NONE;
986 fstrm->proto_status = 0;
987 fstrm->state = FCGI_SS_IDLE;
988 fstrm->rxbuf = BUF_NULL;
989
990 h1m_init_res(&fstrm->h1m);
991 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
992 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
993
994 fstrm->by_id.key = fstrm->id = id;
995 if (id > 0)
996 fconn->max_id = id;
997 else
998 fconn->nb_reserved++;
999
1000 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1001 fconn->nb_streams++;
1002 fconn->stream_cnt++;
1003
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001004 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001005 return fstrm;
1006
1007 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001008 TRACE_DEVEL("leaving in error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001009 return NULL;
1010}
1011
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001012/* Allocates a new stream associated to stream connector <sc> on the FCGI connection
Christopher Faulet99eff652019-08-11 23:11:30 +02001013 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1014 * highest possible stream ID was reached.
1015 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001016static struct fcgi_strm *fcgi_stconn_new(struct fcgi_conn *fconn, struct stconn *sc,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001017 struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02001018{
1019 struct fcgi_strm *fstrm = NULL;
1020
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001021 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1022 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001023 TRACE_ERROR("streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001024 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001025 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001026
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001027 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001028 TRACE_ERROR("!streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001029 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001030 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001031
1032 /* Defer choosing the ID until we send the first message to create the stream */
1033 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001034 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001035 TRACE_ERROR("fstream allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001036 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001037 }
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001038 if (sc_attach_mux(sc, fstrm, fconn->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +02001039 goto out;
Willy Tarreau5aa5e772022-05-27 16:15:32 +02001040 fstrm->sd = sc->sedesc;
Christopher Faulet99eff652019-08-11 23:11:30 +02001041 fstrm->sess = sess;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001042 fconn->nb_sc++;
Christopher Faulet99eff652019-08-11 23:11:30 +02001043
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001044 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001045 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001046
1047 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001048 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +02001049 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001050 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001051}
1052
Willy Tarreau4596fe22022-05-17 19:07:51 +02001053/* Wakes a specific stream and assign its stream connector some SE_FL_* flags among
Willy Tarreaub605c422022-05-17 17:04:55 +02001054 * SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state is
Christopher Faulet99eff652019-08-11 23:11:30 +02001055 * automatically updated accordingly. If the stream is orphaned, it is
1056 * destroyed.
1057 */
1058static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1059{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001060 struct fcgi_conn *fconn = fstrm->fconn;
1061
1062 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1063
Willy Tarreau77534272022-05-18 07:34:16 +02001064 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001065 /* this stream was already orphaned */
1066 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001067 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001068 return;
1069 }
1070
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001071 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001072 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001073 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001074 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001075 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001076 else if (fstrm->state == FCGI_SS_HLOC)
1077 fcgi_strm_close(fstrm);
1078 }
1079
Christopher Fauletab79b322022-10-12 17:51:51 +02001080 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR))) {
1081 se_fl_set_error(fstrm->sd);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001082
1083 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001084 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001085 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1086 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001087 }
1088
1089 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001090
1091 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001092}
1093
1094/* Wakes unassigned streams (ID == 0) attached to the connection. */
1095static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1096{
1097 struct eb32_node *node;
1098 struct fcgi_strm *fstrm;
1099
1100 node = eb32_lookup(&fconn->streams_by_id, 0);
1101 while (node) {
1102 fstrm = container_of(node, struct fcgi_strm, by_id);
1103 if (fstrm->id > 0)
1104 break;
1105 node = eb32_next(node);
1106 fcgi_strm_wake_one_stream(fstrm);
1107 }
1108}
1109
1110/* Wakes the streams attached to the connection, whose id is greater than <last>
1111 * or unassigned.
1112 */
1113static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1114{
1115 struct eb32_node *node;
1116 struct fcgi_strm *fstrm;
1117
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001118 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1119
Christopher Faulet99eff652019-08-11 23:11:30 +02001120 /* Wake all streams with ID > last */
1121 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1122 while (node) {
1123 fstrm = container_of(node, struct fcgi_strm, by_id);
1124 node = eb32_next(node);
1125 fcgi_strm_wake_one_stream(fstrm);
1126 }
1127 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001128
1129 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001130}
1131
1132static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1133 struct htx *htx, struct htx_sl *sl,
1134 struct fcgi_strm_params *params)
1135{
1136 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
Willy Tarreau74568cf2022-05-27 09:03:30 +02001137 const struct sockaddr_storage *src = (sc_check(fcgi_strm_sc(fstrm)) ? conn_src(fconn->conn) : sc_src(sc_opposite(fcgi_strm_sc(fstrm))));
1138 const struct sockaddr_storage *dst = (sc_check(fcgi_strm_sc(fstrm)) ? conn_dst(fconn->conn) : sc_dst(sc_opposite(fcgi_strm_sc(fstrm))));
Christopher Faulet99eff652019-08-11 23:11:30 +02001139 struct ist p;
1140
1141 if (!sl)
1142 goto error;
1143
1144 if (!(params->mask & FCGI_SP_DOC_ROOT))
1145 params->docroot = fconn->app->docroot;
1146
1147 if (!(params->mask & FCGI_SP_REQ_METH)) {
1148 p = htx_sl_req_meth(sl);
1149 params->meth = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001150 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001151 }
1152 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001153 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 params->uri = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001155 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001156 }
1157 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1158 p = htx_sl_req_vsn(sl);
1159 params->vsn = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001160 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001161 }
1162 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1163 char *end;
1164 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001165 if (dst)
1166 port = get_host_port(dst);
Christopher Faulet99eff652019-08-11 23:11:30 +02001167 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1168 if (!end)
1169 goto error;
1170 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1171 params->p->data += params->srv_port.len;
1172 }
1173 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1174 /* If no Host header found, use the server address to fill
1175 * srv_name */
1176 if (!istlen(params->srv_name)) {
1177 char *ptr = NULL;
1178
Christopher Faulet568008d2021-10-25 07:56:51 +02001179 if (dst)
1180 if (addr_to_str(dst, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001181 ptr = b_tail(params->p);
1182 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001183 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001184 params->p->data += params->srv_name.len;
1185 }
1186 }
1187 }
1188 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1189 char *ptr = NULL;
1190
Christopher Faulet568008d2021-10-25 07:56:51 +02001191 if (src)
1192 if (addr_to_str(src, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001193 ptr = b_tail(params->p);
1194 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001195 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001196 params->p->data += params->rem_addr.len;
1197 }
1198 }
1199 if (!(params->mask & FCGI_SP_REM_PORT)) {
1200 char *end;
1201 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001202 if (src)
1203 port = get_host_port(src);
Christopher Faulet99eff652019-08-11 23:11:30 +02001204 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1205 if (!end)
1206 goto error;
1207 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1208 params->p->data += params->rem_port.len;
1209 }
1210 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1211 struct htx_blk *blk;
1212 enum htx_blk_type type;
1213 char *end;
1214 size_t len = 0;
1215
1216 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1217 type = htx_get_blk_type(blk);
1218
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001219 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001220 break;
1221 if (type == HTX_BLK_DATA)
1222 len += htx_get_blksz(blk);
1223 }
1224 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1225 if (!end)
1226 goto error;
1227 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1228 params->p->data += params->cont_len.len;
1229 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001230
Christopher Faulet99eff652019-08-11 23:11:30 +02001231 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001232 if (cli_conn)
Willy Tarreau1057bee2021-10-06 11:38:44 +02001233 params->https = conn_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001234 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001235
Christopher Faulet99eff652019-08-11 23:11:30 +02001236 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1237 /* one of scriptname, pathinfo or query_string is no set */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001238 struct http_uri_parser parser = http_uri_parser_init(params->uri);
1239 struct ist path = http_parse_path(&parser);
Christopher Faulet99eff652019-08-11 23:11:30 +02001240 int len;
1241
Christopher Faulet99eff652019-08-11 23:11:30 +02001242 /* No scrit_name set but no valid path ==> error */
1243 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1244 goto error;
1245
Christopher Faulet99eff652019-08-11 23:11:30 +02001246 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001247 if (!(params->mask & FCGI_SP_REQ_QS)) {
1248 struct ist qs = istfind(path, '?');
1249
1250 /* Update the path length */
1251 path.len -= qs.len;
1252
1253 /* Set the query-string skipping the '?', if any */
1254 if (istlen(qs))
1255 params->qs = istnext(qs);
1256 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001257
1258 /* If the script_name is set, don't try to deduce the path_info
1259 * too. The opposite is not true.
1260 */
1261 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1262 params->mask |= FCGI_SP_PATH_INFO;
1263 goto end;
1264 }
1265
Christopher Faulet0f17a442020-07-23 15:44:37 +02001266 /* Decode the path. it must first be copied to keep the URI
1267 * untouched.
1268 */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001269 chunk_istcat(params->p, path);
Christopher Faulet0f17a442020-07-23 15:44:37 +02001270 path.ptr = b_tail(params->p) - path.len;
1271 len = url_decode(ist0(path), 0);
1272 if (len < 0)
1273 goto error;
1274 path.len = len;
1275
Christopher Faulet99eff652019-08-11 23:11:30 +02001276 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001277 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001278
1279 /* If there is no regex to match the pathinfo, just to the last
1280 * part and see if the index must be used.
1281 */
1282 if (!fconn->app->pathinfo_re)
1283 goto check_index;
1284
Christopher Faulet28cb3662020-02-14 14:47:37 +01001285 /* If some special characters are found in the decoded path (\n
Ilya Shipitsin01881082021-08-07 14:41:56 +05001286 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001287 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001288 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001289 * case.
1290 */
1291 if (istchr(path, '\n') || istchr(path, '\0'))
1292 goto error;
1293
Christopher Faulet99eff652019-08-11 23:11:30 +02001294 /* The regex does not match, just to the last part and see if
1295 * the index must be used.
1296 */
1297 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1298 goto check_index;
1299
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001300 /* We must have at least 1 capture for the script name,
1301 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001302 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001303 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001304 goto check_index;
1305
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001306 /* Finally we can set the script_name and the path_info. The
1307 * path_info is set if not already defined, and if it was
1308 * captured
1309 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001310 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Paul Barnetta26a9ac52023-01-17 09:44:11 +11001311 if (!(params->mask & FCGI_SP_PATH_INFO) && !(pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001312 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001313
1314 check_index:
1315 len = params->scriptname.len;
1316 /* the script_name if finished by a '/' so we can add the index
1317 * part, if any.
1318 */
1319 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1320 struct ist sn = params->scriptname;
1321
1322 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001323 chunk_istcat(params->p, sn);
Tim Duesterhus77508502022-03-15 13:11:06 +01001324 chunk_istcat(params->p, fconn->app->index);
Christopher Faulet99eff652019-08-11 23:11:30 +02001325 }
1326 }
1327
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001328 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1329 params->srv_soft = ist2(b_tail(params->p), 0);
1330 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1331 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1332 }
1333
Christopher Faulet99eff652019-08-11 23:11:30 +02001334 end:
1335 return 1;
1336 error:
1337 return 0;
1338}
1339
1340static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1341 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1342{
1343 struct fcgi_param p;
1344
1345 if (params->mask & flag)
1346 return 1;
1347
1348 chunk_reset(&trash);
1349
1350 switch (flag) {
1351 case FCGI_SP_CGI_GATEWAY:
1352 p.n = ist("GATEWAY_INTERFACE");
1353 p.v = ist("CGI/1.1");
1354 goto encode;
1355 case FCGI_SP_DOC_ROOT:
1356 p.n = ist("DOCUMENT_ROOT");
1357 p.v = params->docroot;
1358 goto encode;
1359 case FCGI_SP_SCRIPT_NAME:
1360 p.n = ist("SCRIPT_NAME");
1361 p.v = params->scriptname;
1362 goto encode;
1363 case FCGI_SP_PATH_INFO:
1364 p.n = ist("PATH_INFO");
1365 p.v = params->pathinfo;
1366 goto encode;
1367 case FCGI_SP_REQ_URI:
1368 p.n = ist("REQUEST_URI");
1369 p.v = params->uri;
1370 goto encode;
1371 case FCGI_SP_REQ_METH:
1372 p.n = ist("REQUEST_METHOD");
1373 p.v = params->meth;
1374 goto encode;
1375 case FCGI_SP_REQ_QS:
1376 p.n = ist("QUERY_STRING");
1377 p.v = params->qs;
1378 goto encode;
1379 case FCGI_SP_SRV_NAME:
1380 p.n = ist("SERVER_NAME");
1381 p.v = params->srv_name;
1382 goto encode;
1383 case FCGI_SP_SRV_PORT:
1384 p.n = ist("SERVER_PORT");
1385 p.v = params->srv_port;
1386 goto encode;
1387 case FCGI_SP_SRV_PROTO:
1388 p.n = ist("SERVER_PROTOCOL");
1389 p.v = params->vsn;
1390 goto encode;
1391 case FCGI_SP_REM_ADDR:
1392 p.n = ist("REMOTE_ADDR");
1393 p.v = params->rem_addr;
1394 goto encode;
1395 case FCGI_SP_REM_PORT:
1396 p.n = ist("REMOTE_PORT");
1397 p.v = params->rem_port;
1398 goto encode;
1399 case FCGI_SP_SCRIPT_FILE:
1400 p.n = ist("SCRIPT_FILENAME");
Tim Duesterhus77508502022-03-15 13:11:06 +01001401 chunk_istcat(&trash, params->docroot);
1402 chunk_istcat(&trash, params->scriptname);
Christopher Faulet99eff652019-08-11 23:11:30 +02001403 p.v = ist2(b_head(&trash), b_data(&trash));
1404 goto encode;
1405 case FCGI_SP_PATH_TRANS:
1406 if (!istlen(params->pathinfo))
1407 goto skip;
1408 p.n = ist("PATH_TRANSLATED");
Tim Duesterhus77508502022-03-15 13:11:06 +01001409 chunk_istcat(&trash, params->docroot);
1410 chunk_istcat(&trash, params->pathinfo);
Christopher Faulet99eff652019-08-11 23:11:30 +02001411 p.v = ist2(b_head(&trash), b_data(&trash));
1412 goto encode;
1413 case FCGI_SP_CONT_LEN:
1414 p.n = ist("CONTENT_LENGTH");
1415 p.v = params->cont_len;
1416 goto encode;
1417 case FCGI_SP_HTTPS:
1418 if (!params->https)
1419 goto skip;
1420 p.n = ist("HTTPS");
1421 p.v = ist("on");
1422 goto encode;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001423 case FCGI_SP_SRV_SOFT:
1424 p.n = ist("SERVER_SOFTWARE");
1425 p.v = params->srv_soft;
1426 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001427 default:
1428 goto skip;
1429 }
1430
1431 encode:
1432 if (!istlen(p.v))
1433 goto skip;
1434 if (!fcgi_encode_param(outbuf, &p))
1435 return 0;
1436 skip:
1437 params->mask |= flag;
1438 return 1;
1439}
1440
1441/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1442 * anything. It is highly unexpected, but if the record is larger than a buffer
1443 * and cannot be encoded in one time, an error is triggered and the connection is
1444 * closed. GET_VALUES record cannot be split.
1445 */
1446static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1447{
1448 struct buffer outbuf;
1449 struct buffer *mbuf;
1450 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1451 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001452 int ret = 0;
1453
1454 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001455
1456 mbuf = br_tail(fconn->mbuf);
1457 retry:
1458 if (!fcgi_get_buf(fconn, mbuf)) {
1459 fconn->flags |= FCGI_CF_MUX_MALLOC;
1460 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001461 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1462 ret = 0;
1463 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001464 }
1465
1466 while (1) {
1467 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001468 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001469 break;
1470 realign_again:
1471 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1472 }
1473
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001474 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001475 goto full;
1476
1477 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1478 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001479 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1480 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001481
1482 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1483 * handled by HAProxy.
1484 */
1485 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1486 goto full;
1487
1488 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001489 TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn, 0, 0, (size_t[]){outbuf.data-8});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001490 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001491 b_add(mbuf, outbuf.data);
1492 ret = 1;
1493
1494 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001495 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001496 return ret;
1497 full:
1498 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001499 if (!b_data(mbuf)) {
1500 TRACE_ERROR("GET_VALUES record too large", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001501 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001502 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001503
1504 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1505 goto retry;
1506 fconn->flags |= FCGI_CF_MUX_MFULL;
1507 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001508 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001509 ret = 0;
1510 goto end;
1511 fail:
1512 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001513 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1514 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1515 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001516}
1517
1518/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1519 * couldn't do anything. It is highly unexpected, but if the record is larger
1520 * than a buffer and cannot be decoded in one time, an error is triggered and
1521 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1522 */
1523static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1524{
1525 struct buffer inbuf;
1526 struct buffer *dbuf;
1527 size_t offset;
1528
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001529 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1530
Christopher Faulet99eff652019-08-11 23:11:30 +02001531 dbuf = &fconn->dbuf;
1532
1533 /* Record too large to be fully decoded */
1534 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1535 goto fail;
1536
1537 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001538 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1539 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001540 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001541 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001542
1543 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1544 /* Realign the dmux buffer if the record wraps. It is unexpected
1545 * at this stage because it should be the first record received
1546 * from the FCGI application.
1547 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01001548 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001549 }
1550
1551 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1552
1553 for (offset = 0; offset < b_data(&inbuf); ) {
1554 struct fcgi_param p;
1555 size_t ret;
1556
1557 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1558 if (!ret) {
1559 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001560 TRACE_ERROR("error decoding GET_VALUES_RESULT param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001561 goto fail;
1562 }
1563 offset += ret;
1564
1565 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001566 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001567 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001568 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001569 }
1570 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001571 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001572 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001573 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001574 }
1575 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1576 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001577 TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->streams_limit});
Christopher Faulet99eff652019-08-11 23:11:30 +02001578 }
1579 /*
1580 * Ignore all other params
1581 */
1582 }
1583
1584 /* Reset the number of concurrent streams supported if the FCGI
1585 * application does not support connection multiplexing
1586 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001587 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001588 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001589 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1590 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001591
1592 /* We must be sure to have read exactly the announced record length, no
1593 * more no less
1594 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001595 if (offset != fconn->drl) {
1596 TRACE_ERROR("invalid GET_VALUES_RESULT record length", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001597 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001598 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001599
Willy Tarreau022e5e52020-09-10 09:33:15 +02001600 TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02001601 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1602 fconn->drl = 0;
1603 fconn->drp = 0;
1604 fconn->state = FCGI_CS_RECORD_H;
1605 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001606 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1607 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001608 return 1;
1609 fail:
1610 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001611 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1612 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001613 return 0;
1614}
1615
1616/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1617 * excluded, as the streams which already received the end-of-stream. It returns
1618 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1619 */
1620static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1621{
1622 struct eb32_node *node;
1623 struct fcgi_strm *fstrm;
1624
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001625 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1626
Christopher Faulet99eff652019-08-11 23:11:30 +02001627 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1628 while (node) {
1629 fstrm = container_of(node, struct fcgi_strm, by_id);
1630 node = eb32_next(node);
1631 if (fstrm->state != FCGI_SS_CLOSED &&
1632 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1633 !fcgi_strm_send_abort(fconn, fstrm))
1634 return 0;
1635 }
1636 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001637 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1638 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001639 return 1;
1640}
1641
1642/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1643 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1644 * space to proceed. It is small enough to be encoded in an empty buffer.
1645 */
1646static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1647{
1648 struct buffer outbuf;
1649 struct buffer *mbuf;
1650 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1651 int ret;
1652
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001653 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1654
Christopher Faulet99eff652019-08-11 23:11:30 +02001655 mbuf = br_tail(fconn->mbuf);
1656 retry:
1657 if (!fcgi_get_buf(fconn, mbuf)) {
1658 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001659 fstrm->flags |= FCGI_SF_BLK_MROOM;
1660 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1661 ret = 0;
1662 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001663 }
1664
1665 while (1) {
1666 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001667 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001668 break;
1669 realign_again:
1670 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1671 }
1672
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001673 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001674 goto full;
1675
1676 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1677 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001678 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001679 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001680 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001681
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001682 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1683 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001684 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001685 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 if (!fcgi_encode_begin_request(&outbuf, &rec))
1687 goto full;
1688
1689 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001690 TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001691 b_add(mbuf, outbuf.data);
1692 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1693 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001694 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001695 ret = 1;
1696
1697 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001698 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001699 return ret;
1700 full:
1701 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1702 goto retry;
1703 fconn->flags |= FCGI_CF_MUX_MFULL;
1704 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001705 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001706 ret = 0;
1707 goto end;
1708}
1709
1710/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1711 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1712 * space to proceed. It is small enough to be encoded in an empty buffer.
1713 */
1714static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1715 enum fcgi_record_type rtype)
1716{
1717 struct buffer outbuf;
1718 struct buffer *mbuf;
1719 int ret;
1720
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001721 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001722 mbuf = br_tail(fconn->mbuf);
1723 retry:
1724 if (!fcgi_get_buf(fconn, mbuf)) {
1725 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001726 fstrm->flags |= FCGI_SF_BLK_MROOM;
1727 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1728 ret = 0;
1729 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001730 }
1731
1732 while (1) {
1733 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001734 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001735 break;
1736 realign_again:
1737 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1738 }
1739
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001740 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001741 goto full;
1742
1743 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1744 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001745 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 outbuf.area[1] = rtype;
1747 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001748 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001749
1750 /* commit the record */
1751 b_add(mbuf, outbuf.data);
1752 ret = 1;
1753
1754 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001755 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001756 return ret;
1757 full:
1758 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1759 goto retry;
1760 fconn->flags |= FCGI_CF_MUX_MFULL;
1761 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001762 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001763 ret = 0;
1764 goto end;
1765}
1766
1767
1768/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1769 * marks the end of params.
1770 */
1771static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1772{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001773 int ret;
1774
1775 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1776 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001777 if (ret) {
1778 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001779 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001780 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001781 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001782}
1783
1784/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1785 * marks the end of input. On success, all the request was successfully sent.
1786 */
1787static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1788{
1789 int ret;
1790
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001791 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001792 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001793 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001794 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001795 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001796 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1797 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1798 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001799 return ret;
1800}
1801
1802/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1803 * stops the request processing.
1804 */
1805static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1806{
1807 int ret;
1808
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001809 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001810 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001811 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001812 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001813 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001814 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1815 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1816 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001817 return ret;
1818}
1819
1820/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1821 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1822 * several records are sent. However, a K/V param cannot be split between 2
1823 * records.
1824 */
1825static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1826 struct htx *htx)
1827{
1828 struct buffer outbuf;
1829 struct buffer *mbuf;
1830 struct htx_blk *blk;
1831 struct htx_sl *sl = NULL;
1832 struct fcgi_strm_params params;
1833 size_t total = 0;
1834
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001835 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1836
Christopher Faulet99eff652019-08-11 23:11:30 +02001837 memset(&params, 0, sizeof(params));
1838 params.p = get_trash_chunk();
1839
1840 mbuf = br_tail(fconn->mbuf);
1841 retry:
1842 if (!fcgi_get_buf(fconn, mbuf)) {
1843 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001844 fstrm->flags |= FCGI_SF_BLK_MROOM;
1845 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1846 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001847 }
1848
1849 while (1) {
1850 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001851 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001852 break;
1853 realign_again:
1854 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1855 }
1856
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001857 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001858 goto full;
1859
1860 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1861 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001862 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001863 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001864 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001865
1866 blk = htx_get_head_blk(htx);
1867 while (blk) {
1868 enum htx_blk_type type;
1869 uint32_t size = htx_get_blksz(blk);
1870 struct fcgi_param p;
1871
1872 type = htx_get_blk_type(blk);
1873 switch (type) {
1874 case HTX_BLK_REQ_SL:
1875 sl = htx_get_blk_ptr(htx, blk);
1876 if (sl->info.req.meth == HTTP_METH_HEAD)
1877 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1878 if (sl->flags & HTX_SL_F_VER_11)
1879 fstrm->h1m.flags |= H1_MF_VER_11;
1880 break;
1881
1882 case HTX_BLK_HDR:
1883 p.n = htx_get_blk_name(htx, blk);
1884 p.v = htx_get_blk_value(htx, blk);
1885
1886 if (istmatch(p.n, ist(":fcgi-"))) {
Tim Duesterhusa6a32792022-03-05 00:52:45 +01001887 p.n = istadv(p.n, 6);
Christopher Faulet99eff652019-08-11 23:11:30 +02001888 if (isteq(p.n, ist("gateway_interface")))
1889 params.mask |= FCGI_SP_CGI_GATEWAY;
1890 else if (isteq(p.n, ist("document_root"))) {
1891 params.mask |= FCGI_SP_DOC_ROOT;
1892 params.docroot = p.v;
1893 }
1894 else if (isteq(p.n, ist("script_name"))) {
1895 params.mask |= FCGI_SP_SCRIPT_NAME;
1896 params.scriptname = p.v;
1897 }
1898 else if (isteq(p.n, ist("path_info"))) {
1899 params.mask |= FCGI_SP_PATH_INFO;
1900 params.pathinfo = p.v;
1901 }
1902 else if (isteq(p.n, ist("request_uri"))) {
1903 params.mask |= FCGI_SP_REQ_URI;
1904 params.uri = p.v;
1905 }
1906 else if (isteq(p.n, ist("request_meth")))
1907 params.mask |= FCGI_SP_REQ_METH;
1908 else if (isteq(p.n, ist("query_string")))
1909 params.mask |= FCGI_SP_REQ_QS;
1910 else if (isteq(p.n, ist("server_name")))
1911 params.mask |= FCGI_SP_SRV_NAME;
1912 else if (isteq(p.n, ist("server_port")))
1913 params.mask |= FCGI_SP_SRV_PORT;
1914 else if (isteq(p.n, ist("server_protocol")))
1915 params.mask |= FCGI_SP_SRV_PROTO;
1916 else if (isteq(p.n, ist("remote_addr")))
1917 params.mask |= FCGI_SP_REM_ADDR;
1918 else if (isteq(p.n, ist("remote_port")))
1919 params.mask |= FCGI_SP_REM_PORT;
1920 else if (isteq(p.n, ist("script_filename")))
1921 params.mask |= FCGI_SP_SCRIPT_FILE;
1922 else if (isteq(p.n, ist("path_translated")))
1923 params.mask |= FCGI_SP_PATH_TRANS;
1924 else if (isteq(p.n, ist("https")))
1925 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001926 else if (isteq(p.n, ist("server_software")))
1927 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02001928 }
1929 else if (isteq(p.n, ist("content-length"))) {
1930 p.n = ist("CONTENT_LENGTH");
1931 params.mask |= FCGI_SP_CONT_LEN;
1932 }
1933 else if (isteq(p.n, ist("content-type")))
1934 p.n = ist("CONTENT_TYPE");
1935 else {
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001936 struct ist n;
1937
Christopher Faulet99eff652019-08-11 23:11:30 +02001938 if (isteq(p.n, ist("host")))
1939 params.srv_name = p.v;
Christopher Fauletf56e8462021-09-28 10:56:36 +02001940 else if (isteq(p.n, ist("te"))) {
1941 /* "te" may only be sent with "trailers" if this value
1942 * is present, otherwise it must be deleted.
1943 */
1944 p.v = istist(p.v, ist("trailers"));
1945 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
1946 break;
1947 p.v = ist("trailers");
1948 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001949
Christopher Faulet67d58092019-10-02 10:51:38 +02001950 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001951 if (isttest(fconn->proxy->server_id_hdr_name) && isteq(p.n, fconn->proxy->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02001952 break;
1953
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001954 n = ist2(trash.area, 0);
1955 istcat(&n, ist("http_"), trash.size);
1956 istcat(&n, p.n, trash.size);
1957 p.n = n;
Christopher Faulet99eff652019-08-11 23:11:30 +02001958 }
1959
1960 if (!fcgi_encode_param(&outbuf, &p)) {
1961 if (b_space_wraps(mbuf))
1962 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001963 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001964 goto full;
1965 goto done;
1966 }
1967 break;
1968
1969 case HTX_BLK_EOH:
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001970 if (isttest(fconn->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001971 struct server *srv = objt_server(fconn->conn->target);
1972
1973 if (!srv)
1974 goto done;
1975
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001976 p.n = ist2(trash.area, 0);
1977 istcat(&p.n, ist("http_"), trash.size);
1978 istcat(&p.n, fconn->proxy->server_id_hdr_name, trash.size);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001979 p.v = ist(srv->id);
1980
1981 if (!fcgi_encode_param(&outbuf, &p)) {
1982 if (b_space_wraps(mbuf))
1983 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001984 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001985 goto full;
1986 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001987 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001988 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001989 goto done;
1990
1991 default:
1992 break;
1993 }
1994 total += size;
1995 blk = htx_remove_blk(htx, blk);
1996 }
1997
1998 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01001999 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2000 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002001 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002002 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002003
2004 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2005 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2006 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2007 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2008 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2009 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2010 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2011 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2012 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2013 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2014 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2015 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2016 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2017 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2018 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002019 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002020 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2021 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002022 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002023 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002024
2025 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002026 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - FCGI_RECORD_HEADER_SZ});
2027 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002028 b_add(mbuf, outbuf.data);
2029
2030 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002031 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002032 return total;
2033 full:
2034 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2035 goto retry;
2036 fconn->flags |= FCGI_CF_MUX_MFULL;
2037 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002038 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002039 if (total)
2040 goto error;
2041 goto end;
2042
2043 error:
2044 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002045 TRACE_ERROR("processing error sending PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002046 fcgi_strm_error(fstrm);
2047 goto end;
2048}
2049
2050/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2051 * anything. STDIN records contain the request body.
2052 */
2053static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2054 struct htx *htx, size_t count, struct buffer *buf)
2055{
2056 struct buffer outbuf;
2057 struct buffer *mbuf;
2058 struct htx_blk *blk;
2059 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002060 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002061 size_t total = 0;
2062
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002063 extra_bytes = 0;
2064
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002065 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002066 if (!count)
2067 goto end;
2068
2069 mbuf = br_tail(fconn->mbuf);
2070 retry:
2071 if (!fcgi_get_buf(fconn, mbuf)) {
2072 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002073 fstrm->flags |= FCGI_SF_BLK_MROOM;
2074 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2075 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002076 }
2077
2078 /* Perform some optimizations to reduce the number of buffer copies.
2079 * First, if the mux's buffer is empty and the htx area contains exactly
2080 * one data block of the same size as the requested count, and this
2081 * count fits within the record size, then it's possible to simply swap
2082 * the caller's buffer with the mux's output buffer and adjust offsets
2083 * and length to match the entire DATA HTX block in the middle. In this
2084 * case we perform a true zero-copy operation from end-to-end. This is
2085 * the situation that happens all the time with large files. Second, if
2086 * this is not possible, but the mux's output buffer is empty, we still
2087 * have an opportunity to avoid the copy to the intermediary buffer, by
2088 * making the intermediary buffer's area point to the output buffer's
2089 * area. In this case we want to skip the HTX header to make sure that
2090 * copies remain aligned and that this operation remains possible all
2091 * the time. This goes for headers, data blocks and any data extracted
2092 * from the HTX blocks.
2093 */
2094 blk = htx_get_head_blk(htx);
2095 if (!blk)
2096 goto end;
2097 type = htx_get_blk_type(blk);
2098 size = htx_get_blksz(blk);
2099 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2100 void *old_area = mbuf->area;
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002101 int eom = (htx->flags & HTX_FL_EOM);
Christopher Faulet99eff652019-08-11 23:11:30 +02002102
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002103 /* Last block of the message: Reserve the size for the empty stdin record */
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002104 if (eom)
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002105 extra_bytes = FCGI_RECORD_HEADER_SZ;
2106
Christopher Faulet99eff652019-08-11 23:11:30 +02002107 if (b_data(mbuf)) {
2108 /* Too bad there are data left there. We're willing to memcpy/memmove
2109 * up to 1/4 of the buffer, which means that it's OK to copy a large
2110 * record into a buffer containing few data if it needs to be realigned,
2111 * and that it's also OK to copy few data without realigning. Otherwise
2112 * we'll pretend the mbuf is full and wait for it to become empty.
2113 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002114 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002115 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002116 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002117 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002118 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002119 }
2120
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002121 TRACE_PROTO("sending stding data (zero-copy)", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002122 /* map a FCGI record to the HTX block so that we can put the
2123 * record header there.
2124 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002125 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - FCGI_RECORD_HEADER_SZ, size + FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002126 outbuf.area = b_head(mbuf);
2127
2128 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002129 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002130 fcgi_set_record_id(outbuf.area, fstrm->id);
2131 fcgi_set_record_size(outbuf.area, size);
2132
2133 /* and exchange with our old area */
2134 buf->area = old_area;
2135 buf->data = buf->head = 0;
2136 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002137
2138 htx = (struct htx *)buf->area;
2139 htx_reset(htx);
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002140 if (eom)
2141 goto empty_stdin;
Christopher Faulet99eff652019-08-11 23:11:30 +02002142 goto end;
2143 }
2144
2145 copy:
2146 while (1) {
2147 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002148 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002149 break;
2150 realign_again:
2151 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2152 }
2153
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002154 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002155 goto full;
2156
2157 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2158 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002159 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002160 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002161 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002162
2163 blk = htx_get_head_blk(htx);
2164 while (blk && count) {
2165 enum htx_blk_type type = htx_get_blk_type(blk);
2166 uint32_t size = htx_get_blksz(blk);
2167 struct ist v;
2168
2169 switch (type) {
2170 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002171 TRACE_PROTO("sending stding data", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002172 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002173
2174 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2175 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2176
2177 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002178 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002179 extra_bytes = 0;
2180 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002181
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002182 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002183 /* It doesn't fit at once. If it at least fits once split and
2184 * the amount of data to move is low, let's defragment the
2185 * buffer now.
2186 */
2187 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002188 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002189 b_data(mbuf) <= MAX_DATA_REALIGN)
2190 goto realign_again;
Christopher Faulet52fd8a12022-11-15 10:46:28 +01002191 v.len = (FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)
2192 ? 0
2193 : b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes);
Christopher Faulet99eff652019-08-11 23:11:30 +02002194 }
2195 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002196 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002197 goto full;
2198 goto done;
2199 }
2200 if (v.len != size) {
2201 total += v.len;
2202 count -= v.len;
2203 htx_cut_data_blk(htx, blk, v.len);
2204 goto done;
2205 }
2206 break;
2207
Christopher Faulet99eff652019-08-11 23:11:30 +02002208 default:
2209 break;
2210 }
2211 total += size;
2212 count -= size;
2213 blk = htx_remove_blk(htx, blk);
2214 }
2215
2216 done:
2217 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002218 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - FCGI_RECORD_HEADER_SZ});
2219 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002220 b_add(mbuf, outbuf.data);
2221
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002222 /* Send the empty stding here to finish the message */
2223 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002224 empty_stdin:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002225 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2226 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2227 /* bytes already reserved for this record. It should not fail */
2228 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002229 TRACE_ERROR("processing error sending empty STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002230 fcgi_strm_error(fstrm);
2231 }
2232 }
2233
Christopher Faulet99eff652019-08-11 23:11:30 +02002234 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002235 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002236 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 Faulet5c0f8592019-10-04 15:21:17 +02002242 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002243 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 */
2250static 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 Faulet5c0f8592019-10-04 15:21:17 +02002256 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2257
Christopher Faulet99eff652019-08-11 23:11:30 +02002258 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 Faulet5c0f8592019-10-04 15:21:17 +02002271 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2272 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002273 }
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;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002286 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2287 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002288
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002289 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002290 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002291 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2292 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002293
2294 if (fconn->drl)
2295 goto fail;
2296
2297 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002298 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002299 fconn->drl += fconn->drp;
2300 fconn->drp = 0;
2301 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2302 b_del(&fconn->dbuf, ret);
2303 fconn->drl -= ret;
2304 if (fconn->drl)
2305 goto fail;
2306
2307 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002308 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2309 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002310 return 1;
2311 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002312 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002313 return 0;
2314}
2315
2316
2317/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2318 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002319 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002320 */
2321static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2322{
2323 int ret;
2324
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002325 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2326
Christopher Faulet99eff652019-08-11 23:11:30 +02002327 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002328 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002329 fconn->drl += fconn->drp;
2330 fconn->drp = 0;
2331 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2332 b_del(&fconn->dbuf, ret);
2333 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002334 if (fconn->drl) {
2335 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002336 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002337 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002338 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002339 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002340 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002341 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);
2342 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002343 return 1;
2344}
2345
2346/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2347 * anything.
2348 */
2349static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2350{
2351 struct buffer *dbuf;
2352 struct buffer tag;
2353 size_t ret;
2354
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002355 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002356 dbuf = &fconn->dbuf;
2357
2358 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002359 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002360 goto end_transfer;
2361
2362 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2363 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2364 buf_room_for_htx_data(dbuf))
2365 goto fail; // incomplete record
2366
2367 chunk_reset(&trash);
2368 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2369 if (!ret)
2370 goto fail;
2371 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002372 TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002373
2374 trash.area[ret] = '\n';
2375 trash.area[ret+1] = '\0';
2376 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002377 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002378
2379 if (fconn->drl)
2380 goto fail;
2381
2382 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002383 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002384 fconn->drl += fconn->drp;
2385 fconn->drp = 0;
2386 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2387 b_del(&fconn->dbuf, ret);
2388 fconn->drl -= ret;
2389 if (fconn->drl)
2390 goto fail;
2391 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2393 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002394 return 1;
2395 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002397 return 0;
2398}
2399
2400/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2401 * anything. If the empty STDOUT record is not already received, this one marks
2402 * the end of the response. It is highly unexpected, but if the record is larger
2403 * than a buffer and cannot be decoded in one time, an error is triggered and
2404 * the connection is closed. END_REQUEST record cannot be split.
2405 */
2406static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2407{
2408 struct buffer inbuf;
2409 struct buffer *dbuf;
2410 struct fcgi_end_request endreq;
2411
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002413 dbuf = &fconn->dbuf;
2414
2415 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002416 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2417 TRACE_ERROR("END_REQUEST record too large", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002418 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002419 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002420
2421 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002422 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2423 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002424 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002425 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002426
2427 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2428 /* Realign the dmux buffer if the record wraps. It is unexpected
2429 * at this stage because it should be the first record received
2430 * from the FCGI application.
2431 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002432 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002433 }
2434
2435 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2436
Christopher Faulet73518be2021-01-27 12:06:54 +01002437 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2438 TRACE_ERROR("END_REQUEST record decoding failure", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002439 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002440 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002441
2442 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002443 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002444 TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02002445 fstrm->proto_status = endreq.errcode;
2446 fcgi_strm_close(fstrm);
2447
2448 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2449 fconn->drl = 0;
2450 fconn->drp = 0;
2451 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002452 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2453 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002454 return 1;
2455
2456 fail:
2457 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002458 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002459 return 0;
2460}
2461
2462/* process Rx records to be demultiplexed */
2463static void fcgi_process_demux(struct fcgi_conn *fconn)
2464{
2465 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2466 struct fcgi_header hdr;
2467 int ret;
2468
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002469 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2470
Christopher Faulet99eff652019-08-11 23:11:30 +02002471 if (fconn->state == FCGI_CS_CLOSED)
2472 return;
2473
2474 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002475 if (fconn->state == FCGI_CS_INIT) {
2476 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2477 return;
2478 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002479 if (fconn->state == FCGI_CS_SETTINGS) {
2480 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002482 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002483 if (!ret) {
2484 TRACE_ERROR("header record decoding failure", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002485 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002486 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002487 b_del(&fconn->dbuf, ret);
2488
2489 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2490 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002491 TRACE_ERROR("unexpected record type or flags", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002492 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002493 goto fail;
2494 }
2495 goto new_record;
2496 }
2497 }
2498
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002499 /* process as many incoming records as possible below */
2500 while (1) {
2501 if (!b_data(&fconn->dbuf)) {
2502 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2503 break;
2504 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002505
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002506 if (fconn->state == FCGI_CS_CLOSED) {
2507 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002508 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002509 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002510
2511 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002512 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002513 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2514 if (!ret)
2515 break;
2516 b_del(&fconn->dbuf, ret);
2517
2518 new_record:
2519 fconn->dsi = hdr.id;
2520 fconn->drt = hdr.type;
2521 fconn->drl = hdr.len;
2522 fconn->drp = hdr.padding;
2523 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002524 TRACE_STATE("FCGI record header rcvd, switching to RECORD_D", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002525 }
2526
2527 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2528 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2529
Willy Tarreau77534272022-05-18 07:34:16 +02002530 if (tmp_fstrm != fstrm && fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002531 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002532 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002533 fstrm->state == FCGI_SS_CLOSED ||
2534 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002535 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002536 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002537 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002538 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002539 fcgi_strm_notify_recv(fstrm);
2540 }
2541 fstrm = tmp_fstrm;
2542
2543 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2544 /* ignore all record for closed streams */
2545 goto ignore_record;
2546 }
2547 if (fstrm->state == FCGI_SS_IDLE) {
2548 /* ignore all record for unknown streams */
2549 goto ignore_record;
2550 }
2551
2552 switch (fconn->drt) {
2553 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002554 TRACE_PROTO("receiving FCGI GET_VALUES_RESULT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002555 ret = fcgi_conn_handle_values_result(fconn);
2556 break;
2557
2558 case FCGI_STDOUT:
2559 if (fstrm->flags & FCGI_SF_ES_RCVD)
2560 goto ignore_record;
2561
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002562 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002563 if (fconn->drl)
2564 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2565 else
2566 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2567 break;
2568
2569 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002570 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002571 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2572 break;
2573
2574 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002575 TRACE_PROTO("receiving FCGI END_REQUEST record", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002576 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2577 break;
2578
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002579 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002580 default:
2581 ignore_record:
2582 /* drop records that we ignore. They may be
2583 * larger than the buffer so we drain all of
2584 * their contents until we reach the end.
2585 */
2586 fconn->state = FCGI_CS_RECORD_P;
2587 fconn->drl += fconn->drp;
2588 fconn->drp = 0;
2589 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002590 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002591 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002592 b_del(&fconn->dbuf, ret);
2593 fconn->drl -= ret;
2594 ret = (fconn->drl == 0);
2595 }
2596
2597 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002598 if (ret <= 0) {
2599 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002600 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002602
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002603 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002604 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002605 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2606 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002607 }
2608
2609 fail:
2610 /* we can go here on missing data, blocked response or error */
Willy Tarreau77534272022-05-18 07:34:16 +02002611 if (fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002612 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002613 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002614 fstrm->state == FCGI_SS_CLOSED ||
2615 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002616 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002617 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002618 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002619 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002620 fcgi_strm_notify_recv(fstrm);
2621 }
2622
2623 fcgi_conn_restart_reading(fconn, 0);
2624}
2625
2626/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2627 * the end.
2628 */
2629static int fcgi_process_mux(struct fcgi_conn *fconn)
2630{
2631 struct fcgi_strm *fstrm, *fstrm_back;
2632
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002633 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2634
Christopher Faulet99eff652019-08-11 23:11:30 +02002635 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2636 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2637 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2638 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002639 TRACE_STATE("switching to RECORD_H", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002640 fcgi_wake_unassigned_streams(fconn);
2641 goto mux;
2642 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002643 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002644 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2645 goto fail;
2646 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002647 TRACE_STATE("switching to SETTINGS", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002648 }
2649 /* need to wait for the other side */
2650 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002651 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002652 }
2653
2654 mux:
2655 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2656 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2657 break;
2658
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002659 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002660 continue;
2661
Willy Tarreau7aad7032020-01-16 17:20:57 +01002662 /* If the sender changed his mind and unsubscribed, let's just
2663 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002664 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002665 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2666 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002667 LIST_DEL_INIT(&fstrm->send_list);
2668 continue;
2669 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002670
2671 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002672 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2673 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002674 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002675 tasklet_wakeup(fstrm->subs->tasklet);
2676 fstrm->subs->events &= ~SUB_RETRY_SEND;
2677 if (!fstrm->subs->events)
2678 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002679 } else {
2680 /* it's the shut request that was queued */
2681 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2682 tasklet_wakeup(fstrm->shut_tl);
2683 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002684 }
2685
2686 fail:
2687 if (fconn->state == FCGI_CS_CLOSED) {
2688 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2689 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002690 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2691 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002692 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002693 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 }
2695 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002696
2697 done:
2698 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002699 return 1;
2700}
2701
2702
2703/* Attempt to read data, and subscribe if none available.
2704 * The function returns 1 if data has been received, otherwise zero.
2705 */
2706static int fcgi_recv(struct fcgi_conn *fconn)
2707{
2708 struct connection *conn = fconn->conn;
2709 struct buffer *buf;
2710 int max;
2711 size_t ret;
2712
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002713 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2714
2715 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2716 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002717 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002718 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002719
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002720 if (!fcgi_recv_allowed(fconn)) {
2721 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002722 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002723 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002724
2725 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2726 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002728 fconn->flags |= FCGI_CF_DEM_DALLOC;
2729 return 0;
2730 }
2731
Christopher Faulet99eff652019-08-11 23:11:30 +02002732 if (!b_data(buf)) {
2733 /* try to pre-align the buffer like the
2734 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002735 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002736 * HTX block to alias it upon recv. We cannot use the
2737 * head because rcv_buf() will realign the buffer if
2738 * it's empty. Thus we cheat and pretend we already
2739 * have a few bytes there.
2740 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002741 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2742 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 }
2744 else
2745 max = buf_room_for_htx_data(buf);
2746
2747 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2748
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 if (max && !ret && fcgi_recv_allowed(fconn)) {
2750 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002752 }
2753 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002754 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002755
Christopher Fauletab79b322022-10-12 17:51:51 +02002756 if (conn_xprt_read0_pending(conn)) {
2757 TRACE_DATA("received read0", FCGI_EV_FCONN_RECV, conn);
2758 fconn->flags |= FCGI_CF_EOS;
2759 }
2760 if (conn->flags & CO_FL_ERROR) {
2761 TRACE_DATA("connection error", FCGI_EV_FCONN_RECV, conn);
2762 fconn->flags |= FCGI_CF_ERROR;
2763 }
2764
Christopher Faulet99eff652019-08-11 23:11:30 +02002765 if (!b_data(buf)) {
2766 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Fauletab79b322022-10-12 17:51:51 +02002767 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002768 }
2769
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002770 if (ret == max) {
2771 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002772 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002773 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002774
Christopher Fauletab79b322022-10-12 17:51:51 +02002775end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002776 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002777 return !!ret || (fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERROR));
Christopher Faulet99eff652019-08-11 23:11:30 +02002778}
2779
2780
2781/* Try to send data if possible.
2782 * The function returns 1 if data have been sent, otherwise zero.
2783 */
2784static int fcgi_send(struct fcgi_conn *fconn)
2785{
2786 struct connection *conn = fconn->conn;
2787 int done;
2788 int sent = 0;
2789
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002790 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2791
Christopher Fauletab79b322022-10-12 17:51:51 +02002792 if (fconn->flags & (FCGI_CF_ERROR|FCGI_CF_ERR_PENDING)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002793 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002794 if (fconn->flags & FCGI_CF_EOS)
2795 fconn->flags |= FCGI_CF_ERROR;
2796 b_reset(br_tail(fconn->mbuf));
Christopher Faulet99eff652019-08-11 23:11:30 +02002797 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002798 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002799
2800
Willy Tarreau911db9b2020-01-23 16:27:54 +01002801 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002802 /* a handshake was requested */
2803 goto schedule;
2804 }
2805
2806 /* This loop is quite simple : it tries to fill as much as it can from
2807 * pending streams into the existing buffer until it's reportedly full
2808 * or the end of send requests is reached. Then it tries to send this
2809 * buffer's contents out, marks it not full if at least one byte could
2810 * be sent, and tries again.
2811 *
2812 * The snd_buf() function normally takes a "flags" argument which may
2813 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2814 * data immediately comes and CO_SFL_STREAMER to indicate that the
2815 * connection is streaming lots of data (used to increase TLS record
2816 * size at the expense of latency). The former can be sent any time
2817 * there's a buffer full flag, as it indicates at least one stream
2818 * attempted to send and failed so there are pending data. An
2819 * alternative would be to set it as long as there's an active stream
2820 * but that would be problematic for ACKs until we have an absolute
2821 * guarantee that all waiters have at least one byte to send. The
2822 * latter should possibly not be set for now.
2823 */
2824
2825 done = 0;
2826 while (!done) {
2827 unsigned int flags = 0;
2828 unsigned int released = 0;
2829 struct buffer *buf;
2830
2831 /* fill as much as we can into the current buffer */
2832 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2833 done = fcgi_process_mux(fconn);
2834
2835 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2836 done = 1; // we won't go further without extra buffers
2837
2838 if (conn->flags & CO_FL_ERROR)
2839 break;
2840
2841 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2842 flags |= CO_SFL_MSG_MORE;
2843
2844 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2845 if (b_data(buf)) {
2846 int ret;
2847
2848 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2849 if (!ret) {
2850 done = 1;
2851 break;
2852 }
2853 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002854 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002855 b_del(buf, ret);
2856 if (b_data(buf)) {
2857 done = 1;
2858 break;
2859 }
2860 }
2861 b_free(buf);
2862 released++;
2863 }
2864
2865 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002866 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002867
2868 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002869 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2870 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002871 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2872 }
2873
Christopher Fauletab79b322022-10-12 17:51:51 +02002874 if (conn->flags & CO_FL_ERROR) {
2875 fconn->flags |= FCGI_CF_ERR_PENDING;
2876 if (fconn->flags & FCGI_CF_EOS)
2877 fconn->flags |= FCGI_CF_ERROR;
Christopher Faulet99eff652019-08-11 23:11:30 +02002878 b_reset(br_tail(fconn->mbuf));
2879 }
Christopher Fauletab79b322022-10-12 17:51:51 +02002880
Christopher Faulet99eff652019-08-11 23:11:30 +02002881 /* We're not full anymore, so we can wake any task that are waiting
2882 * for us.
2883 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002884 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002885 struct fcgi_strm *fstrm;
2886
2887 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2888 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2889 break;
2890
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002891 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002892 continue;
2893
Willy Tarreau7aad7032020-01-16 17:20:57 +01002894 /* If the sender changed his mind and unsubscribed, let's just
2895 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002896 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002897 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2898 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002899 LIST_DEL_INIT(&fstrm->send_list);
2900 continue;
2901 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002902
2903 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002904 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002905 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002906 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002907 tasklet_wakeup(fstrm->subs->tasklet);
2908 fstrm->subs->events &= ~SUB_RETRY_SEND;
2909 if (!fstrm->subs->events)
2910 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002911 } else {
2912 /* it's the shut request that was queued */
2913 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2914 tasklet_wakeup(fstrm->shut_tl);
2915 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002916 }
2917 }
2918 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002919 if (!br_data(fconn->mbuf)) {
2920 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002921 goto end;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002922 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002923schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002924 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2925 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002926 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002927 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002928
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002929 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002930end:
2931 return sent || (fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR));
Christopher Faulet99eff652019-08-11 23:11:30 +02002932}
2933
2934/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002935struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02002936{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002937 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002938 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002939 struct tasklet *tl = (struct tasklet *)t;
2940 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002941 int ret = 0;
2942
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002943 if (state & TASK_F_USR1) {
2944 /* the tasklet was idling on an idle connection, it might have
2945 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002946 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002947 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2948 if (tl->context == NULL) {
2949 /* The connection has been taken over by another thread,
2950 * we're no longer responsible for it, so just free the
2951 * tasklet, and do nothing.
2952 */
2953 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2954 tasklet_free(tl);
2955 return NULL;
2956 }
2957 conn = fconn->conn;
2958 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002959
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002960 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2961 if (conn_in_list)
2962 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002963
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002964 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2965 } else {
2966 /* we're certain the connection was not in an idle list */
2967 conn = fconn->conn;
2968 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
2969 conn_in_list = 0;
2970 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002971
Christopher Faulet99eff652019-08-11 23:11:30 +02002972 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2973 ret = fcgi_send(fconn);
2974 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2975 ret |= fcgi_recv(fconn);
2976 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002977 ret = fcgi_process(fconn);
2978
2979 /* If we were in an idle list, we want to add it back into it,
2980 * unless fcgi_process() returned -1, which mean it has destroyed
2981 * the connection (testing !ret is enough, if fcgi_process() wasn't
2982 * called then ret will be 0 anyway.
2983 */
Willy Tarreau74163142021-03-13 11:30:19 +01002984 if (ret < 0)
2985 t = NULL;
2986
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002987 if (!ret && conn_in_list) {
2988 struct server *srv = objt_server(conn->target);
2989
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002990 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002991 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02002992 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002993 else
Willy Tarreau85223482022-09-29 20:32:43 +02002994 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002995 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002996 }
Willy Tarreau74163142021-03-13 11:30:19 +01002997 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02002998}
2999
3000/* callback called on any event by the connection handler.
3001 * It applies changes and returns zero, or < 0 if it wants immediate
3002 * destruction of the connection (which normally doesn not happen in FCGI).
3003 */
3004static int fcgi_process(struct fcgi_conn *fconn)
3005{
3006 struct connection *conn = fconn->conn;
3007
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003008 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3009
Christopher Faulet99eff652019-08-11 23:11:30 +02003010 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3011 fcgi_process_demux(fconn);
3012
Christopher Fauletab79b322022-10-12 17:51:51 +02003013 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ERROR))
Christopher Faulet99eff652019-08-11 23:11:30 +02003014 b_reset(&fconn->dbuf);
3015
3016 if (buf_room_for_htx_data(&fconn->dbuf))
3017 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3018 }
3019 fcgi_send(fconn);
3020
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003021 if (unlikely(fconn->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003022 /* frontend is stopping, reload likely in progress, let's try
3023 * to announce a graceful shutdown if not yet done. We don't
3024 * care if it fails, it will be tried again later.
3025 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003026 TRACE_STATE("proxy stopped, sending ABORT to all streams", FCGI_EV_FCONN_WAKE|FCGI_EV_TX_RECORD, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003027 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3028 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3029 fcgi_conn_send_aborts(fconn);
3030 }
3031 }
3032
3033 /*
3034 * If we received early data, and the handshake is done, wake
3035 * any stream that was waiting for it.
3036 */
3037 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003038 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003039 struct eb32_node *node;
3040 struct fcgi_strm *fstrm;
3041
3042 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3043 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3044
3045 while (node) {
3046 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003047 if (fcgi_strm_sc(fstrm) && se_fl_test(fstrm->sd, SE_FL_WAIT_FOR_HS))
Christopher Faulet99eff652019-08-11 23:11:30 +02003048 fcgi_strm_notify_recv(fstrm);
3049 node = eb32_next(node);
3050 }
3051 }
3052
Christopher Fauletab79b322022-10-12 17:51:51 +02003053 if ((fconn->flags & FCGI_CF_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003054 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3055 eb_is_empty(&fconn->streams_by_id)) {
3056 fcgi_wake_some_streams(fconn, 0);
3057
3058 if (eb_is_empty(&fconn->streams_by_id)) {
3059 /* no more stream, kill the connection now */
3060 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003061 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003062 return -1;
3063 }
3064 }
3065
3066 if (!b_data(&fconn->dbuf))
3067 fcgi_release_buf(fconn, &fconn->dbuf);
3068
Christopher Fauletab79b322022-10-12 17:51:51 +02003069 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003070 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3071 fcgi_release_mbuf(fconn);
3072
3073 if (fconn->task) {
3074 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3075 task_queue(fconn->task);
3076 }
3077
3078 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003079 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003080 return 0;
3081}
3082
3083
3084/* wake-up function called by the connection layer (mux_ops.wake) */
3085static int fcgi_wake(struct connection *conn)
3086{
3087 struct fcgi_conn *fconn = conn->ctx;
3088
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003089 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003090 return (fcgi_process(fconn));
3091}
3092
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003093
3094static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3095{
3096 int ret = 0;
3097 switch (mux_ctl) {
3098 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003099 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003100 ret |= MUX_STATUS_READY;
3101 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003102 case MUX_EXIT_STATUS:
3103 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003104 default:
3105 return -1;
3106 }
3107}
3108
Christopher Faulet99eff652019-08-11 23:11:30 +02003109/* Connection timeout management. The principle is that if there's no receipt
3110 * nor sending for a certain amount of time, the connection is closed. If the
3111 * MUX buffer still has lying data or is not allocatable, the connection is
3112 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003113 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003114 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003115struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003116{
3117 struct fcgi_conn *fconn = context;
3118 int expired = tick_is_expired(t->expire, now_ms);
3119
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003120 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3121
Willy Tarreau60814ff2020-06-30 11:19:23 +02003122 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003123 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003124
3125 /* Somebody already stole the connection from us, so we should not
3126 * free it, we just have to free the task.
3127 */
3128 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003129 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003130 fconn = NULL;
3131 goto do_leave;
3132 }
3133
Willy Tarreau60814ff2020-06-30 11:19:23 +02003134 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003135 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003136 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3137 return t;
3138 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003139
Willy Tarreau60814ff2020-06-30 11:19:23 +02003140 /* We're about to destroy the connection, so make sure nobody attempts
3141 * to steal it from us.
3142 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003143 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003144 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003145
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003146 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003147 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003148
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003149do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003150 task_destroy(t);
3151
3152 if (!fconn) {
3153 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003154 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003155 return NULL;
3156 }
3157
3158 fconn->task = NULL;
3159 fconn->state = FCGI_CS_CLOSED;
3160 fcgi_wake_some_streams(fconn, 0);
3161
3162 if (br_data(fconn->mbuf)) {
3163 /* don't even try to send aborts, the buffer is stuck */
3164 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3165 goto end;
3166 }
3167
3168 /* try to send but no need to insist */
3169 if (!fcgi_conn_send_aborts(fconn))
3170 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3171
3172 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3173 conn_xprt_ready(fconn->conn)) {
3174 unsigned int released = 0;
3175 struct buffer *buf;
3176
3177 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3178 if (b_data(buf)) {
3179 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3180 buf, b_data(buf), 0);
3181 if (!ret)
3182 break;
3183 b_del(buf, ret);
3184 if (b_data(buf))
3185 break;
3186 b_free(buf);
3187 released++;
3188 }
3189 }
3190
3191 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003192 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003193 }
3194
3195 end:
3196 /* either we can release everything now or it will be done later once
3197 * the last stream closes.
3198 */
3199 if (eb_is_empty(&fconn->streams_by_id))
3200 fcgi_release(fconn);
3201
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003202 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003203 return NULL;
3204}
3205
3206
3207/*******************************************/
3208/* functions below are used by the streams */
3209/*******************************************/
3210
3211/* Append the description of what is present in error snapshot <es> into <out>.
3212 * The description must be small enough to always fit in a buffer. The output
3213 * buffer may be the trash so the trash must not be used inside this function.
3214 */
3215static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3216{
3217 chunk_appendf(out,
3218 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3219 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3220 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3221 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3222 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3223 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3224}
3225/*
3226 * Capture a bad response and archive it in the proxy's structure. By default
3227 * it tries to report the error position as h1m->err_pos. However if this one is
3228 * not set, it will then report h1m->next, which is the last known parsing
3229 * point. The function is able to deal with wrapping buffers. It always displays
3230 * buffers as a contiguous area starting at buf->p. The direction is determined
3231 * thanks to the h1m's flags.
3232 */
3233static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3234 struct h1m *h1m, struct buffer *buf)
3235{
3236 struct session *sess = fstrm->sess;
3237 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003238 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003239 union error_snapshot_ctx ctx;
3240
Willy Tarreauea27f482022-05-18 16:10:52 +02003241 if (fcgi_strm_sc(fstrm) && sc_strm(fcgi_strm_sc(fstrm))) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003242 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02003243 sess = __sc_strm(fcgi_strm_sc(fstrm))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003244 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02003245 other_end = __sc_strm(fcgi_strm_sc(fstrm))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003246 else
3247 other_end = sess->fe;
3248 } else
3249 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003250 /* http-specific part now */
3251 ctx.h1.state = h1m->state;
3252 ctx.h1.c_flags = fconn->flags;
3253 ctx.h1.s_flags = fstrm->flags;
3254 ctx.h1.m_flags = h1m->flags;
3255 ctx.h1.m_clen = h1m->curr_len;
3256 ctx.h1.m_blen = h1m->body_len;
3257
3258 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3259 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3260 &ctx, fcgi_show_error_snapshot);
3261}
3262
3263static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3264 struct buffer *buf, size_t *ofs, size_t max)
3265{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003266 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003267
Willy Tarreau022e5e52020-09-10 09:33:15 +02003268 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003269 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003270 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003271 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003272 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003273 TRACE_ERROR("parsing error, reject H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003274 fcgi_strm_error(fstrm);
3275 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3276 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003277 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003278 goto end;
3279 }
3280
Christopher Fauletda3adeb2021-09-28 09:50:07 +02003281 /* Reject any message with an unknown transfer-encoding. In fact if any
3282 * encoding other than "chunked". A 422-Unprocessable-Content is
3283 * returned for an invalid request, a 502-Bad-Gateway for an invalid
3284 * response.
3285 */
3286 if (h1m->flags & H1_MF_TE_OTHER) {
3287 htx->flags |= HTX_FL_PARSING_ERROR;
3288 TRACE_ERROR("Unknown transfer-encoding", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3289 fcgi_strm_error(fstrm);
3290 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3291 ret = 0;
3292 goto end;
3293 }
3294
Christopher Faulet99eff652019-08-11 23:11:30 +02003295 *ofs += ret;
3296 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003297 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003298 return ret;
3299
3300}
3301
Christopher Fauletaf542632019-10-01 21:52:49 +02003302static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003303 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3304{
Christopher Fauletde471a42021-02-01 16:37:28 +01003305 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003306
Willy Tarreau022e5e52020-09-10 09:33:15 +02003307 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003308 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003309 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003310 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003311 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003312 TRACE_ERROR("parsing error, reject H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003313 fcgi_strm_error(fstrm);
3314 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3315 }
3316 goto end;
3317 }
3318 *ofs += ret;
3319 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003320 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003321 return ret;
3322}
3323
3324static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3325 struct buffer *buf, size_t *ofs, size_t max)
3326{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003327 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003328
Willy Tarreau022e5e52020-09-10 09:33:15 +02003329 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003330 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003331 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003332 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003333 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003334 TRACE_ERROR("parsing error, reject H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003335 fcgi_strm_error(fstrm);
3336 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3337 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003338 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003339 goto end;
3340 }
3341 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003342 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003343 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003344 return ret;
3345}
3346
Christopher Faulet99eff652019-08-11 23:11:30 +02003347static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3348{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003349 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003350 struct htx *htx;
3351 struct h1m *h1m = &fstrm->h1m;
3352 size_t ret, data, total = 0;
3353
3354 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003355 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3356
Christopher Faulet99eff652019-08-11 23:11:30 +02003357 data = htx->data;
3358 if (fstrm->state == FCGI_SS_ERROR)
3359 goto end;
3360
3361 do {
3362 size_t used = htx_used_space(htx);
3363
3364 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003365 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003366 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3367 if (!ret)
3368 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003369
3370 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3371
Christopher Faulet99eff652019-08-11 23:11:30 +02003372 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3373 struct htx_blk *blk = htx_get_head_blk(htx);
3374 struct htx_sl *sl;
3375
3376 if (!blk)
3377 break;
3378 sl = htx_get_blk_ptr(htx, blk);
3379 sl->flags |= HTX_SL_F_XFER_LEN;
3380 htx->extra = 0;
3381 }
3382 }
3383 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003384 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003385 fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003386
3387 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3388 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3389 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet2db904e2022-05-05 09:24:52 +02003390 if (htx_is_empty(htx) && !htx_add_endof(htx, HTX_BLK_EOT))
3391 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003392 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003393 h1m->state = H1_MSG_DONE;
3394 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3395 }
3396
Christopher Faulet16a524c2021-02-02 21:16:03 +01003397 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003398 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003399
3400 TRACE_PROTO("rcvd response payload data", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003401 }
3402 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003403 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003404 fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01003405 if (h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003406 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003407
Christopher Faulet76014fd2019-12-10 11:47:22 +01003408 TRACE_PROTO("rcvd H1 response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003409 }
3410 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003411 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003412 if (b_data(&fstrm->rxbuf) > total) {
3413 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003414 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003415 fcgi_strm_error(fstrm);
3416 }
3417 break;
3418 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003419 else {
3420 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003421 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 fcgi_strm_error(fstrm);
3423 break;
3424 }
3425
3426 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003427 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003428
3429 if (fstrm->state == FCGI_SS_ERROR) {
3430 b_reset(&fstrm->rxbuf);
3431 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003432 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003433 return 0;
3434 }
3435
3436 b_del(&fstrm->rxbuf, total);
3437
3438 end:
3439 htx_to_buf(htx, buf);
3440 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003441 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003442 return ret;
3443}
3444
3445/*
3446 * Attach a new stream to a connection
3447 * (Used for outgoing connections)
3448 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003449static int fcgi_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02003450{
Christopher Faulet99eff652019-08-11 23:11:30 +02003451 struct fcgi_strm *fstrm;
3452 struct fcgi_conn *fconn = conn->ctx;
3453
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003454 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003455 fstrm = fcgi_stconn_new(fconn, sd->sc, sess);
Christopher Faulete00ad352021-12-16 14:44:31 +01003456 if (!fstrm)
Christopher Faulet73518be2021-01-27 12:06:54 +01003457 goto err;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003458
3459 /* the connection is not idle anymore, let's mark this */
3460 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003461 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003462
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003463 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulete00ad352021-12-16 14:44:31 +01003464 return 0;
Christopher Faulet73518be2021-01-27 12:06:54 +01003465
3466 err:
3467 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01003468 return -1;
Christopher Faulet99eff652019-08-11 23:11:30 +02003469}
3470
Willy Tarreau4596fe22022-05-17 19:07:51 +02003471/* Retrieves the first valid stream connector from this connection, or returns NULL.
Christopher Faulet99eff652019-08-11 23:11:30 +02003472 * We have to scan because we may have some orphan streams. It might be
3473 * beneficial to scan backwards from the end to reduce the likeliness to find
3474 * orphans.
3475 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003476static struct stconn *fcgi_get_first_sc(const struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02003477{
3478 struct fcgi_conn *fconn = conn->ctx;
3479 struct fcgi_strm *fstrm;
3480 struct eb32_node *node;
3481
3482 node = eb32_first(&fconn->streams_by_id);
3483 while (node) {
3484 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau77534272022-05-18 07:34:16 +02003485 if (fcgi_strm_sc(fstrm))
3486 return fcgi_strm_sc(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003487 node = eb32_next(node);
3488 }
3489 return NULL;
3490}
3491
3492/*
3493 * Destroy the mux and the associated connection, if it is no longer used
3494 */
3495static void fcgi_destroy(void *ctx)
3496{
3497 struct fcgi_conn *fconn = ctx;
3498
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003499 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003500 if (eb_is_empty(&fconn->streams_by_id)) {
3501 BUG_ON(fconn->conn->ctx != fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003502 fcgi_release(fconn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003503 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003504}
3505
3506/*
3507 * Detach the stream from the connection and possibly release the connection.
3508 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003509static void fcgi_detach(struct sedesc *sd)
Christopher Faulet99eff652019-08-11 23:11:30 +02003510{
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003511 struct fcgi_strm *fstrm = sd->se;
Christopher Faulet99eff652019-08-11 23:11:30 +02003512 struct fcgi_conn *fconn;
3513 struct session *sess;
3514
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003515 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3516
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003517 if (!fstrm) {
3518 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003519 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003520 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003521
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003522 /* there's no txbuf so we're certain no to be able to send anything */
3523 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003524
3525 sess = fstrm->sess;
3526 fconn = fstrm->fconn;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003527 fconn->nb_sc--;
Christopher Faulet99eff652019-08-11 23:11:30 +02003528
3529 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3530 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3531 fconn->streams_limit = 1;
3532 }
3533 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3534 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3535 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3536 fconn->state = FCGI_CS_CLOSED;
3537 }
3538
3539 /* this stream may be blocked waiting for some data to leave, so orphan
3540 * it in this case.
3541 */
Christopher Fauletab79b322022-10-12 17:51:51 +02003542 if (!(fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR)) && // FIXME: Be sure for ERR_PENDING
Christopher Faulet99eff652019-08-11 23:11:30 +02003543 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003544 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003545 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003546 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003547 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003548 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003549
3550 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3551 /* unblock the connection if it was blocked on this stream. */
3552 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3553 fcgi_conn_restart_reading(fconn, 1);
3554 }
3555
3556 fcgi_strm_destroy(fstrm);
3557
Christopher Fauletab79b322022-10-12 17:51:51 +02003558 if (!(fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERR_PENDING|FCGI_CF_ERROR)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003559 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003560 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003561 /* Add the connection in the session serverlist, if not already done */
3562 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3563 fconn->conn->owner = NULL;
3564 if (eb_is_empty(&fconn->streams_by_id)) {
3565 /* let's kill the connection right away */
3566 fconn->conn->mux->destroy(fconn);
3567 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3568 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003569 }
3570 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003571 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003572 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3573 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003574 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003575 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003576 }
3577 }
3578 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003579 else {
3580 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003581 /* If the connection is owned by the session, first remove it
3582 * from its list
3583 */
3584 if (fconn->conn->owner) {
3585 session_unown_conn(fconn->conn->owner, fconn->conn);
3586 fconn->conn->owner = NULL;
3587 }
3588
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003589 /* mark that the tasklet may lose its context to another thread and
3590 * that the handler needs to check it under the idle conns lock.
3591 */
3592 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003593 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3594
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003595 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003596 /* The server doesn't want it, let's kill the connection right away */
3597 fconn->conn->mux->destroy(fconn);
3598 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3599 return;
3600 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003601 /* At this point, the connection has been added to the
3602 * server idle list, so another thread may already have
3603 * hijacked it, so we can't do anything with it.
3604 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003605 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3606 return;
3607 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003608 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003609 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02003610 !LIST_INLIST(&fconn->conn->session_list)) {
Willy Tarreau85223482022-09-29 20:32:43 +02003611 eb64_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
3612 &fconn->conn->hash_node->node);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003613 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003614 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003615 }
3616
3617 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003618 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003619 */
3620 if (fcgi_conn_is_dead(fconn)) {
3621 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003622 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003623 fcgi_release(fconn);
3624 }
3625 else if (fconn->task) {
3626 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3627 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003628 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003629 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003630 else
3631 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003632}
3633
3634
3635/* Performs a synchronous or asynchronous shutr(). */
3636static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3637{
3638 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003639
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003640 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3641
Christopher Faulet99eff652019-08-11 23:11:30 +02003642 if (fstrm->state == FCGI_SS_CLOSED)
3643 goto done;
3644
3645 /* a connstream may require us to immediately kill the whole connection
3646 * for example because of a "tcp-request content reject" rule that is
3647 * normally used to limit abuse.
3648 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003649 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003650 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3651 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003653 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003655 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003656 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3657 !fcgi_strm_send_abort(fconn, fstrm))
3658 goto add_to_list;
3659 }
3660
3661 fcgi_strm_close(fstrm);
3662
3663 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3664 tasklet_wakeup(fconn->wait_event.tasklet);
3665 done:
3666 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003667 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003668 return;
3669
3670 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003671 /* Let the handler know we want to shutr, and add ourselves to the
3672 * send list if not yet done. fcgi_deferred_shut() will be
3673 * automatically called via the shut_tl tasklet when there's room
3674 * again.
3675 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003676 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003678 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003679 }
3680 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003681 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003683 return;
3684}
3685
3686/* Performs a synchronous or asynchronous shutw(). */
3687static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3688{
3689 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003690
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003691 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3692
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3694 goto done;
3695
3696 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3697 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3698 !fcgi_strm_send_abort(fconn, fstrm))
3699 goto add_to_list;
3700
3701 if (fstrm->state == FCGI_SS_HREM)
3702 fcgi_strm_close(fstrm);
3703 else
3704 fstrm->state = FCGI_SS_HLOC;
3705 } else {
3706 /* a connstream may require us to immediately kill the whole connection
3707 * for example because of a "tcp-request content reject" rule that is
3708 * normally used to limit abuse.
3709 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003710 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003711 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3712 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003713 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003714 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003715
3716 fcgi_strm_close(fstrm);
3717 }
3718
3719 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3720 tasklet_wakeup(fconn->wait_event.tasklet);
3721 done:
3722 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003723 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003724 return;
3725
3726 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003727 /* Let the handler know we want to shutr, and add ourselves to the
3728 * send list if not yet done. fcgi_deferred_shut() will be
3729 * automatically called via the shut_tl tasklet when there's room
3730 * again.
3731 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003732 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003733 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003734 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003735 }
3736 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003737 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003738 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003739 return;
3740}
3741
Willy Tarreau7aad7032020-01-16 17:20:57 +01003742/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003743 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003744 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003745 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003746struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003747{
3748 struct fcgi_strm *fstrm = ctx;
3749 struct fcgi_conn *fconn = fstrm->fconn;
3750
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003751 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3752
Willy Tarreau7aad7032020-01-16 17:20:57 +01003753 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3754 /* some data processing remains to be done first */
3755 goto end;
3756 }
3757
Christopher Faulet99eff652019-08-11 23:11:30 +02003758 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3759 fcgi_do_shutw(fstrm);
3760
3761 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3762 fcgi_do_shutr(fstrm);
3763
3764 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3765 /* We're done trying to send, remove ourself from the send_list */
3766 LIST_DEL_INIT(&fstrm->send_list);
3767
Willy Tarreau77534272022-05-18 07:34:16 +02003768 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003769 fcgi_strm_destroy(fstrm);
3770 if (fcgi_conn_is_dead(fconn))
3771 fcgi_release(fconn);
3772 }
3773 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003774 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003775 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003776 return NULL;
3777}
3778
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05003779/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003780static void fcgi_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003781{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003782 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003783
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003784 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003785 if (!mode)
3786 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003787 fcgi_do_shutr(fstrm);
3788}
3789
Willy Tarreau4596fe22022-05-17 19:07:51 +02003790/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003791static void fcgi_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003792{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003793 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003794
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003795 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003796 fcgi_do_shutw(fstrm);
3797}
3798
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003799/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3800 * event subscriber <es> is not allowed to change from a previous call as long
3801 * as at least one event is still subscribed. The <event_type> must only be a
3802 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003803 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003804static int fcgi_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003805{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003806 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003807 struct fcgi_conn *fconn = fstrm->fconn;
3808
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003809 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003810 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003811
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003812 es->events |= event_type;
3813 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003814
3815 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003816 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003817
Christopher Faulet99eff652019-08-11 23:11:30 +02003818 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003819 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003820 if (!LIST_INLIST(&fstrm->send_list))
3821 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003822 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003823 return 0;
3824}
3825
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003826/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3827 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3828 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003829 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003830static int fcgi_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003831{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003832 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003833 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003834
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003835 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003836 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003837
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003838 es->events &= ~event_type;
3839 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003840 fstrm->subs = NULL;
3841
3842 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003843 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003844
Christopher Faulet99eff652019-08-11 23:11:30 +02003845 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003846 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003847 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003848 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3849 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003850 }
3851 return 0;
3852}
3853
Christopher Faulet564e39c2021-09-21 15:50:55 +02003854/* Called from the upper layer, to receive data
3855 *
3856 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3857 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3858 * means the caller wants to flush input data (from the mux buffer and the
3859 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3860 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3861 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3862 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3863 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3864 * copy as much data as possible.
3865 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003866static size_t fcgi_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003867{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003868 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003869 struct fcgi_conn *fconn = fstrm->fconn;
3870 size_t ret = 0;
3871
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003872 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3873
Christopher Faulet99eff652019-08-11 23:11:30 +02003874 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3875 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003876 else
3877 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003878
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003879 if (b_data(&fstrm->rxbuf))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003880 se_fl_set(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet99eff652019-08-11 23:11:30 +02003881 else {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003882 se_fl_clr(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003883 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003884 se_fl_set(fstrm->sd, SE_FL_EOI);
Christopher Faulet99eff652019-08-11 23:11:30 +02003885 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003886 se_fl_set(fstrm->sd, SE_FL_EOS);
Christopher Faulet99eff652019-08-11 23:11:30 +02003887 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003888 if (fcgi_conn_read0_pending(fconn))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003889 se_fl_set(fstrm->sd, SE_FL_EOS);
3890 if (se_fl_test(fstrm->sd, SE_FL_ERR_PENDING))
3891 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 fcgi_release_buf(fconn, &fstrm->rxbuf);
3893 }
3894
3895 if (ret && fconn->dsi == fstrm->id) {
3896 /* demux is blocking on this stream's buffer */
3897 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3898 fcgi_conn_restart_reading(fconn, 1);
3899 }
3900
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003901 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003902 return ret;
3903}
3904
3905
Christopher Faulet99eff652019-08-11 23:11:30 +02003906/* Called from the upper layer, to send data from buffer <buf> for no more than
3907 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02003908 * flags may be updated on the stream connector.
Christopher Faulet99eff652019-08-11 23:11:30 +02003909 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003910static size_t fcgi_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003911{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003912 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003913 struct fcgi_conn *fconn = fstrm->fconn;
3914 size_t total = 0;
3915 size_t ret;
3916 struct htx *htx = NULL;
3917 struct htx_sl *sl;
3918 struct htx_blk *blk;
3919 uint32_t bsize;
3920
Willy Tarreau022e5e52020-09-10 09:33:15 +02003921 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003922
Christopher Faulet99eff652019-08-11 23:11:30 +02003923 /* If we were not just woken because we wanted to send but couldn't,
3924 * and there's somebody else that is waiting to send, do nothing,
3925 * we will subscribe later and be put at the end of the list
3926 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003927 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003928 TRACE_STATE("other streams already waiting, going to the queue and leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003929 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003930 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003931 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003932
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003933 if (fconn->state < FCGI_CS_RECORD_H) {
3934 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003935 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003936 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003937
3938 htx = htxbuf(buf);
3939 if (fstrm->id == 0) {
3940 int32_t id = fcgi_conn_get_next_sid(fconn);
3941
3942 if (id < 0) {
3943 fcgi_strm_close(fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003944 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003945 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 Faulet99eff652019-08-11 23:11:30 +02003946 return 0;
3947 }
3948
3949 eb32_delete(&fstrm->by_id);
3950 fstrm->by_id.key = fstrm->id = id;
3951 fconn->max_id = id;
3952 fconn->nb_reserved--;
3953 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3954
3955
3956 /* Check if length of the body is known or if the message is
3957 * full. Otherwise, the request is invalid.
3958 */
3959 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003960 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003961 htx->flags |= HTX_FL_PARSING_ERROR;
3962 fcgi_strm_error(fstrm);
3963 goto done;
3964 }
3965 }
3966
3967 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003968 TRACE_PROTO("sending FCGI BEGIN_REQUEST record", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003969 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3970 goto done;
3971 }
3972
3973 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3974 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3975
Christopher Fauletfe410d62020-05-19 15:13:00 +02003976 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003977 count && !htx_is_empty(htx)) {
3978 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003979 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003980 bsize = htx_get_blksz(blk);
3981
3982 switch (htx_get_blk_type(blk)) {
3983 case HTX_BLK_REQ_SL:
3984 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003985 TRACE_USER("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003986 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3987 if (!ret) {
3988 goto done;
3989 }
3990 total += ret;
3991 count -= ret;
3992 break;
3993
3994 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003995 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
3996 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
3997 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3998 if (!ret)
3999 goto done;
4000 }
4001 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4002 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4003 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4004 if (!ret)
4005 goto done;
4006 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004007 goto remove_blk;
4008
4009 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004010 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02004011 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4012 if (ret > 0) {
4013 htx = htx_from_buf(buf);
4014 total += ret;
4015 count -= ret;
4016 if (ret < bsize)
4017 goto done;
4018 }
4019 break;
4020
Christopher Faulet99eff652019-08-11 23:11:30 +02004021 default:
4022 remove_blk:
4023 htx_remove_blk(htx, blk);
4024 total += bsize;
4025 count -= bsize;
4026 break;
4027 }
4028 }
4029
4030 done:
4031 if (fstrm->state >= FCGI_SS_HLOC) {
4032 /* trim any possibly pending data after we close (extra CR-LF,
4033 * unprocessed trailers, abnormal extra data, ...)
4034 */
4035 total += count;
4036 count = 0;
4037 }
4038
4039 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004040 TRACE_DEVEL("reporting error to the app-layer stream", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_ERR|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02004041 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +02004042 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4043 fcgi_strm_close(fstrm);
4044 }
4045
4046 if (htx)
4047 htx_to_buf(htx, buf);
4048
Christopher Faulet99eff652019-08-11 23:11:30 +02004049 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004050 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4051 TRACE_DEVEL("data queued, waking up fconn sender", FCGI_EV_STRM_SEND|FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02004052 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004053 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004054
4055 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004056 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4057 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004058 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004059
4060 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004061 return total;
4062}
4063
4064/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004065static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004066{
4067 struct fcgi_conn *fconn = conn->ctx;
4068 struct fcgi_strm *fstrm = NULL;
4069 struct eb32_node *node;
4070 int send_cnt = 0;
4071 int tree_cnt = 0;
4072 int orph_cnt = 0;
4073 struct buffer *hmbuf, *tmbuf;
4074
4075 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004076 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004077
4078 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4079 send_cnt++;
4080
4081 fstrm = NULL;
4082 node = eb32_first(&fconn->streams_by_id);
4083 while (node) {
4084 fstrm = container_of(node, struct fcgi_strm, by_id);
4085 tree_cnt++;
Willy Tarreau77534272022-05-18 07:34:16 +02004086 if (!fcgi_strm_sc(fstrm))
Christopher Faulet99eff652019-08-11 23:11:30 +02004087 orph_cnt++;
4088 node = eb32_next(node);
4089 }
4090
4091 hmbuf = br_head(fconn->mbuf);
4092 tmbuf = br_tail(fconn->mbuf);
4093 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4094 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4095 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4096 fconn->state, fconn->max_id, fconn->flags,
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004097 fconn->nb_streams, fconn->nb_sc, send_cnt, tree_cnt, orph_cnt,
Christopher Faulet99eff652019-08-11 23:11:30 +02004098 fconn->wait_event.events, fconn->dsi,
4099 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4100 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4101 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4102 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4103 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4104 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4105 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4106
4107 if (fstrm) {
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004108 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .sc=%p",
Christopher Faulet99eff652019-08-11 23:11:30 +02004109 fstrm, fstrm->id, fstrm->flags,
4110 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4111 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
Willy Tarreau77534272022-05-18 07:34:16 +02004112 fcgi_strm_sc(fstrm));
Christopher Faulet186367f2022-05-30 08:45:15 +02004113
4114 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(fstrm->sd));
4115 if (!se_fl_test(fstrm->sd, SE_FL_ORPHAN))
4116 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
4117 fcgi_strm_sc(fstrm)->flags, fcgi_strm_sc(fstrm)->app);
4118
Willy Tarreau41054612022-09-02 14:22:38 +02004119 chunk_appendf(msg, " .subs=%p", fstrm->subs);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004120 if (fstrm->subs) {
Willy Tarreau41054612022-09-02 14:22:38 +02004121 chunk_appendf(msg, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4122 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004123 fstrm->subs->tasklet->calls,
4124 fstrm->subs->tasklet->context);
Willy Tarreau41054612022-09-02 14:22:38 +02004125 resolve_sym_name(msg, NULL, fstrm->subs->tasklet->process);
4126 chunk_appendf(msg, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004127 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004128 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004129 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004130}
4131
4132/* Migrate the the connection to the current thread.
4133 * Return 0 if successful, non-zero otherwise.
4134 * Expected to be called with the old thread lock held.
4135 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004136static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004137{
4138 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004139 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004140
4141 if (fd_takeover(conn->handle.fd, conn) != 0)
4142 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004143
4144 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4145 /* We failed to takeover the xprt, even if the connection may
4146 * still be valid, flag it as error'd, as we have already
4147 * taken over the fd, and wake the tasklet, so that it will
4148 * destroy it.
4149 */
4150 conn->flags |= CO_FL_ERROR;
4151 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4152 return -1;
4153 }
4154
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004155 if (fcgi->wait_event.events)
4156 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4157 fcgi->wait_event.events, &fcgi->wait_event);
4158 /* To let the tasklet know it should free itself, and do nothing else,
4159 * set its context to NULL;
4160 */
4161 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004162 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004163
4164 task = fcgi->task;
4165 if (task) {
4166 task->context = NULL;
4167 fcgi->task = NULL;
4168 __ha_barrier_store();
4169 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004170
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004171 fcgi->task = task_new_here();
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004172 if (!fcgi->task) {
4173 fcgi_release(fcgi);
4174 return -1;
4175 }
4176 fcgi->task->process = fcgi_timeout_task;
4177 fcgi->task->context = fcgi;
4178 }
4179 fcgi->wait_event.tasklet = tasklet_new();
4180 if (!fcgi->wait_event.tasklet) {
4181 fcgi_release(fcgi);
4182 return -1;
4183 }
4184 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4185 fcgi->wait_event.tasklet->context = fcgi;
4186 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4187 SUB_RETRY_RECV, &fcgi->wait_event);
4188
4189 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004190}
4191
4192/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004193/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004194/****************************************/
4195
4196/* The mux operations */
4197static const struct mux_ops mux_fcgi_ops = {
4198 .init = fcgi_init,
4199 .wake = fcgi_wake,
4200 .attach = fcgi_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004201 .get_first_sc = fcgi_get_first_sc,
Christopher Faulet99eff652019-08-11 23:11:30 +02004202 .detach = fcgi_detach,
4203 .destroy = fcgi_destroy,
4204 .avail_streams = fcgi_avail_streams,
4205 .used_streams = fcgi_used_streams,
4206 .rcv_buf = fcgi_rcv_buf,
4207 .snd_buf = fcgi_snd_buf,
4208 .subscribe = fcgi_subscribe,
4209 .unsubscribe = fcgi_unsubscribe,
4210 .shutr = fcgi_shutr,
4211 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004212 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004213 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004214 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004215 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004216 .name = "FCGI",
4217};
4218
4219
4220/* this mux registers FCGI proto */
4221static struct mux_proto_list mux_proto_fcgi =
4222{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4223
4224INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4225
4226/*
4227 * Local variables:
4228 * c-indent-level: 8
4229 * c-basic-offset: 8
4230 * End:
4231 */