blob: 117bc6e82a0e1de4abb4a7273e577a4bba84e0a4 [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);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200710 tasklet_free(fconn->wait_event.tasklet);
Christopher Faulet99eff652019-08-11 23:11:30 +0200711 pool_free(pool_head_fcgi_conn, fconn);
712 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200713 conn->ctx = conn_ctx; // restore saved ctx
714 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200715 return -1;
716}
717
718/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
719 * -1 if no more is allocatable.
720 */
721static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
722{
723 int32_t id = (fconn->max_id + 1) | 1;
724
725 if ((id & 0x80000000U))
726 id = -1;
727 return id;
728}
729
730/* Returns the stream associated with id <id> or NULL if not found */
731static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
732{
733 struct eb32_node *node;
734
735 if (id == 0)
736 return (struct fcgi_strm *)fcgi_mgmt_stream;
737
738 if (id > fconn->max_id)
739 return (struct fcgi_strm *)fcgi_unknown_stream;
740
741 node = eb32_lookup(&fconn->streams_by_id, id);
742 if (!node)
743 return (struct fcgi_strm *)fcgi_unknown_stream;
744 return container_of(node, struct fcgi_strm, by_id);
745}
746
747
748/* Release function. This one should be called to free all resources allocated
749 * to the mux.
750 */
751static void fcgi_release(struct fcgi_conn *fconn)
752{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200753 struct connection *conn = fconn->conn;
Christopher Faulet99eff652019-08-11 23:11:30 +0200754
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200755 TRACE_POINT(FCGI_EV_FCONN_END);
756
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200757 if (LIST_INLIST(&fconn->buf_wait.list))
758 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200759
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200760 fcgi_release_buf(fconn, &fconn->dbuf);
761 fcgi_release_mbuf(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200762
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200763 if (fconn->task) {
764 fconn->task->context = NULL;
765 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
766 fconn->task = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200767 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200768 tasklet_free(fconn->wait_event.tasklet);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200769 if (conn && fconn->wait_event.events != 0)
770 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
771 &fconn->wait_event);
772
773 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200774
775 if (conn) {
776 conn->mux = NULL;
777 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200778 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200779
780 conn_stop_tracking(conn);
781 conn_full_close(conn);
782 if (conn->destroy_cb)
783 conn->destroy_cb(conn);
784 conn_free(conn);
785 }
786}
787
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200788/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
789 * pending on the connection AND if there is no more data in the demux
790 * buffer. The function returns 1 to report a read0 or 0 otherwise.
791 */
792static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
793{
Christopher Fauletab79b322022-10-12 17:51:51 +0200794 if ((fconn->flags & FCGI_CF_EOS) && !b_data(&fconn->dbuf))
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200795 return 1;
796 return 0;
797}
798
Christopher Faulet99eff652019-08-11 23:11:30 +0200799
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500800/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200801static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
802{
803 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
804 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
Christopher Fauletab79b322022-10-12 17:51:51 +0200805 (fconn->flags & FCGI_CF_ERROR) || /* errors close immediately */
Christopher Faulet99eff652019-08-11 23:11:30 +0200806 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
807 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
808 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
Christopher Fauletab79b322022-10-12 17:51:51 +0200809 (fconn->flags & FCGI_CF_EOS))))
810 return 1;
Christopher Faulet99eff652019-08-11 23:11:30 +0200811 return 0;
812}
813
814
815/********************************************************/
816/* functions below are for the FCGI protocol processing */
817/********************************************************/
818
Christopher Faulet99eff652019-08-11 23:11:30 +0200819/* Marks an error on the stream. */
820static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
821{
822 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200823 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
824 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200825 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200826 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
827 }
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200828 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200829 }
830}
831
832/* Attempts to notify the data layer of recv availability */
833static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
834{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100835 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200836 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100837 tasklet_wakeup(fstrm->subs->tasklet);
838 fstrm->subs->events &= ~SUB_RETRY_RECV;
839 if (!fstrm->subs->events)
840 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200841 }
842}
843
844/* Attempts to notify the data layer of send availability */
845static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
846{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100847 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200848 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100849 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100850 tasklet_wakeup(fstrm->subs->tasklet);
851 fstrm->subs->events &= ~SUB_RETRY_SEND;
852 if (!fstrm->subs->events)
853 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200854 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100855 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
856 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
857 tasklet_wakeup(fstrm->shut_tl);
858 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200859}
860
861/* Alerts the data layer, trying to wake it up by all means, following
862 * this sequence :
863 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
864 * for recv
865 * - if its subscribed to send, then it's woken up for send
866 * - if it was subscribed to neither, its ->wake() callback is called
867 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +0200868 * stream connector anymore.
Christopher Faulet99eff652019-08-11 23:11:30 +0200869 */
870static void fcgi_strm_alert(struct fcgi_strm *fstrm)
871{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200872 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100873 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100874 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200875 fcgi_strm_notify_recv(fstrm);
876 fcgi_strm_notify_send(fstrm);
877 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200878 else if (fcgi_strm_sc(fstrm) && fcgi_strm_sc(fstrm)->app_ops->wake != NULL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200879 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200880 fcgi_strm_sc(fstrm)->app_ops->wake(fcgi_strm_sc(fstrm));
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200881 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200882}
883
884/* Writes the 16-bit record size <len> at address <record> */
885static inline void fcgi_set_record_size(void *record, uint16_t len)
886{
887 uint8_t *out = (record + 4);
888
889 *out = (len >> 8);
890 *(out + 1) = (len & 0xff);
891}
892
893/* Writes the 16-bit stream id <id> at address <record> */
894static inline void fcgi_set_record_id(void *record, uint16_t id)
895{
896 uint8_t *out = (record + 2);
897
898 *out = (id >> 8);
899 *(out + 1) = (id & 0xff);
900}
901
902/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
903 * its connection if the stream was not yet closed. Please use this exclusively
904 * before closing a stream to ensure stream count is well maintained.
905 */
906static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
907{
908 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200909 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200910 fstrm->fconn->nb_streams--;
911 if (!fstrm->id)
912 fstrm->fconn->nb_reserved--;
Willy Tarreau77534272022-05-18 07:34:16 +0200913 if (fcgi_strm_sc(fstrm)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200914 if (!se_fl_test(fstrm->sd, SE_FL_EOS) && !b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +0200915 fcgi_strm_notify_recv(fstrm);
916 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200917 fstrm->state = FCGI_SS_CLOSED;
918 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
919 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200920 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200921}
922
923/* Detaches a FCGI stream from its FCGI connection and releases it to the
924 * fcgi_strm pool.
925 */
926static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
927{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200928 struct connection *conn = fstrm->fconn->conn;
929
930 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
931
Christopher Faulet99eff652019-08-11 23:11:30 +0200932 fcgi_strm_close(fstrm);
933 eb32_delete(&fstrm->by_id);
934 if (b_size(&fstrm->rxbuf)) {
935 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100936 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200937 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100938 if (fstrm->subs)
939 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +0200940 /* There's no need to explicitly call unsubscribe here, the only
941 * reference left would be in the fconn send_list/fctl_list, and if
942 * we're in it, we're getting out anyway
943 */
944 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +0100945 tasklet_free(fstrm->shut_tl);
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200946 BUG_ON(fstrm->sd && !se_fl_test(fstrm->sd, SE_FL_ORPHAN));
947 sedesc_free(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200948 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200949
950 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200951}
952
953/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
954 * stream tree. In case of error, nothing is added and NULL is returned. The
955 * causes of errors can be any failed memory allocation. The caller is
956 * responsible for checking if the connection may support an extra stream prior
957 * to calling this function.
958 */
959static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
960{
961 struct fcgi_strm *fstrm;
962
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200963 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
964
Christopher Faulet99eff652019-08-11 23:11:30 +0200965 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +0100966 if (!fstrm) {
967 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 +0200968 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +0100969 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200970
Willy Tarreau7aad7032020-01-16 17:20:57 +0100971 fstrm->shut_tl = tasklet_new();
972 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +0100973 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 +0200974 pool_free(pool_head_fcgi_strm, fstrm);
975 goto out;
976 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100977 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +0100978 fstrm->shut_tl->process = fcgi_deferred_shut;
979 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +0200980 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200981 fstrm->fconn = fconn;
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200982 fstrm->sd = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200983 fstrm->flags = FCGI_SF_NONE;
984 fstrm->proto_status = 0;
985 fstrm->state = FCGI_SS_IDLE;
986 fstrm->rxbuf = BUF_NULL;
987
988 h1m_init_res(&fstrm->h1m);
989 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
990 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
991
992 fstrm->by_id.key = fstrm->id = id;
993 if (id > 0)
994 fconn->max_id = id;
995 else
996 fconn->nb_reserved++;
997
998 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
999 fconn->nb_streams++;
1000 fconn->stream_cnt++;
1001
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001002 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001003 return fstrm;
1004
1005 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001006 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 +02001007 return NULL;
1008}
1009
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001010/* Allocates a new stream associated to stream connector <sc> on the FCGI connection
Christopher Faulet99eff652019-08-11 23:11:30 +02001011 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1012 * highest possible stream ID was reached.
1013 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001014static struct fcgi_strm *fcgi_stconn_new(struct fcgi_conn *fconn, struct stconn *sc,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001015 struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02001016{
1017 struct fcgi_strm *fstrm = NULL;
1018
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001019 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1020 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001021 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 +02001022 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001023 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001024
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001025 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001026 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 +02001027 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001028 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001029
1030 /* Defer choosing the ID until we send the first message to create the stream */
1031 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001032 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001033 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 +02001034 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001035 }
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001036 if (sc_attach_mux(sc, fstrm, fconn->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +02001037 goto out;
Willy Tarreau5aa5e772022-05-27 16:15:32 +02001038 fstrm->sd = sc->sedesc;
Christopher Faulet99eff652019-08-11 23:11:30 +02001039 fstrm->sess = sess;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001040 fconn->nb_sc++;
Christopher Faulet99eff652019-08-11 23:11:30 +02001041
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001042 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001043 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001044
1045 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001046 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 +02001047 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001048 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001049}
1050
Willy Tarreau4596fe22022-05-17 19:07:51 +02001051/* Wakes a specific stream and assign its stream connector some SE_FL_* flags among
Willy Tarreaub605c422022-05-17 17:04:55 +02001052 * SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state is
Christopher Faulet99eff652019-08-11 23:11:30 +02001053 * automatically updated accordingly. If the stream is orphaned, it is
1054 * destroyed.
1055 */
1056static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1057{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001058 struct fcgi_conn *fconn = fstrm->fconn;
1059
1060 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1061
Willy Tarreau77534272022-05-18 07:34:16 +02001062 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001063 /* this stream was already orphaned */
1064 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001065 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001066 return;
1067 }
1068
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001069 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001070 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001071 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001072 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001073 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001074 else if (fstrm->state == FCGI_SS_HLOC)
1075 fcgi_strm_close(fstrm);
1076 }
1077
Christopher Fauletab79b322022-10-12 17:51:51 +02001078 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR))) {
1079 se_fl_set_error(fstrm->sd);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001080
1081 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001082 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001083 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1084 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001085 }
1086
1087 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001088
1089 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001090}
1091
1092/* Wakes unassigned streams (ID == 0) attached to the connection. */
1093static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1094{
1095 struct eb32_node *node;
1096 struct fcgi_strm *fstrm;
1097
1098 node = eb32_lookup(&fconn->streams_by_id, 0);
1099 while (node) {
1100 fstrm = container_of(node, struct fcgi_strm, by_id);
1101 if (fstrm->id > 0)
1102 break;
1103 node = eb32_next(node);
1104 fcgi_strm_wake_one_stream(fstrm);
1105 }
1106}
1107
1108/* Wakes the streams attached to the connection, whose id is greater than <last>
1109 * or unassigned.
1110 */
1111static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1112{
1113 struct eb32_node *node;
1114 struct fcgi_strm *fstrm;
1115
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001116 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1117
Christopher Faulet99eff652019-08-11 23:11:30 +02001118 /* Wake all streams with ID > last */
1119 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1120 while (node) {
1121 fstrm = container_of(node, struct fcgi_strm, by_id);
1122 node = eb32_next(node);
1123 fcgi_strm_wake_one_stream(fstrm);
1124 }
1125 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001126
1127 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001128}
1129
1130static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1131 struct htx *htx, struct htx_sl *sl,
1132 struct fcgi_strm_params *params)
1133{
1134 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
Willy Tarreau74568cf2022-05-27 09:03:30 +02001135 const struct sockaddr_storage *src = (sc_check(fcgi_strm_sc(fstrm)) ? conn_src(fconn->conn) : sc_src(sc_opposite(fcgi_strm_sc(fstrm))));
1136 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 +02001137 struct ist p;
1138
1139 if (!sl)
1140 goto error;
1141
1142 if (!(params->mask & FCGI_SP_DOC_ROOT))
1143 params->docroot = fconn->app->docroot;
1144
1145 if (!(params->mask & FCGI_SP_REQ_METH)) {
1146 p = htx_sl_req_meth(sl);
1147 params->meth = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001148 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001149 }
1150 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001151 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001152 params->uri = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001153 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 }
1155 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1156 p = htx_sl_req_vsn(sl);
1157 params->vsn = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001158 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001159 }
1160 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1161 char *end;
1162 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001163 if (dst)
1164 port = get_host_port(dst);
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1166 if (!end)
1167 goto error;
1168 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1169 params->p->data += params->srv_port.len;
1170 }
1171 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1172 /* If no Host header found, use the server address to fill
1173 * srv_name */
1174 if (!istlen(params->srv_name)) {
1175 char *ptr = NULL;
1176
Christopher Faulet568008d2021-10-25 07:56:51 +02001177 if (dst)
1178 if (addr_to_str(dst, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001179 ptr = b_tail(params->p);
1180 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001181 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001182 params->p->data += params->srv_name.len;
1183 }
1184 }
1185 }
1186 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1187 char *ptr = NULL;
1188
Christopher Faulet568008d2021-10-25 07:56:51 +02001189 if (src)
1190 if (addr_to_str(src, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001191 ptr = b_tail(params->p);
1192 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001193 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001194 params->p->data += params->rem_addr.len;
1195 }
1196 }
1197 if (!(params->mask & FCGI_SP_REM_PORT)) {
1198 char *end;
1199 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001200 if (src)
1201 port = get_host_port(src);
Christopher Faulet99eff652019-08-11 23:11:30 +02001202 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1203 if (!end)
1204 goto error;
1205 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1206 params->p->data += params->rem_port.len;
1207 }
1208 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1209 struct htx_blk *blk;
1210 enum htx_blk_type type;
1211 char *end;
1212 size_t len = 0;
1213
1214 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1215 type = htx_get_blk_type(blk);
1216
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001217 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001218 break;
1219 if (type == HTX_BLK_DATA)
1220 len += htx_get_blksz(blk);
1221 }
1222 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1223 if (!end)
1224 goto error;
1225 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1226 params->p->data += params->cont_len.len;
1227 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001228
Christopher Faulet99eff652019-08-11 23:11:30 +02001229 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001230 if (cli_conn)
Willy Tarreau1057bee2021-10-06 11:38:44 +02001231 params->https = conn_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001232 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001233
Christopher Faulet99eff652019-08-11 23:11:30 +02001234 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1235 /* one of scriptname, pathinfo or query_string is no set */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001236 struct http_uri_parser parser = http_uri_parser_init(params->uri);
1237 struct ist path = http_parse_path(&parser);
Christopher Faulet99eff652019-08-11 23:11:30 +02001238 int len;
1239
Christopher Faulet99eff652019-08-11 23:11:30 +02001240 /* No scrit_name set but no valid path ==> error */
1241 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1242 goto error;
1243
Christopher Faulet99eff652019-08-11 23:11:30 +02001244 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001245 if (!(params->mask & FCGI_SP_REQ_QS)) {
1246 struct ist qs = istfind(path, '?');
1247
1248 /* Update the path length */
1249 path.len -= qs.len;
1250
1251 /* Set the query-string skipping the '?', if any */
1252 if (istlen(qs))
1253 params->qs = istnext(qs);
1254 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001255
1256 /* If the script_name is set, don't try to deduce the path_info
1257 * too. The opposite is not true.
1258 */
1259 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1260 params->mask |= FCGI_SP_PATH_INFO;
1261 goto end;
1262 }
1263
Christopher Faulet0f17a442020-07-23 15:44:37 +02001264 /* Decode the path. it must first be copied to keep the URI
1265 * untouched.
1266 */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001267 chunk_istcat(params->p, path);
Christopher Faulet0f17a442020-07-23 15:44:37 +02001268 path.ptr = b_tail(params->p) - path.len;
1269 len = url_decode(ist0(path), 0);
1270 if (len < 0)
1271 goto error;
1272 path.len = len;
1273
Christopher Faulet99eff652019-08-11 23:11:30 +02001274 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001275 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001276
1277 /* If there is no regex to match the pathinfo, just to the last
1278 * part and see if the index must be used.
1279 */
1280 if (!fconn->app->pathinfo_re)
1281 goto check_index;
1282
Christopher Faulet28cb3662020-02-14 14:47:37 +01001283 /* If some special characters are found in the decoded path (\n
Ilya Shipitsin01881082021-08-07 14:41:56 +05001284 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001285 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001286 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001287 * case.
1288 */
1289 if (istchr(path, '\n') || istchr(path, '\0'))
1290 goto error;
1291
Christopher Faulet99eff652019-08-11 23:11:30 +02001292 /* The regex does not match, just to the last part and see if
1293 * the index must be used.
1294 */
1295 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1296 goto check_index;
1297
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001298 /* We must have at least 1 capture for the script name,
1299 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001300 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001301 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001302 goto check_index;
1303
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001304 /* Finally we can set the script_name and the path_info. The
1305 * path_info is set if not already defined, and if it was
1306 * captured
1307 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001308 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Paul Barnetta26a9ac52023-01-17 09:44:11 +11001309 if (!(params->mask & FCGI_SP_PATH_INFO) && !(pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001310 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001311
1312 check_index:
1313 len = params->scriptname.len;
1314 /* the script_name if finished by a '/' so we can add the index
1315 * part, if any.
1316 */
1317 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1318 struct ist sn = params->scriptname;
1319
1320 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001321 chunk_istcat(params->p, sn);
Tim Duesterhus77508502022-03-15 13:11:06 +01001322 chunk_istcat(params->p, fconn->app->index);
Christopher Faulet99eff652019-08-11 23:11:30 +02001323 }
1324 }
1325
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001326 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1327 params->srv_soft = ist2(b_tail(params->p), 0);
1328 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1329 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1330 }
1331
Christopher Faulet99eff652019-08-11 23:11:30 +02001332 end:
1333 return 1;
1334 error:
1335 return 0;
1336}
1337
1338static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1339 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1340{
1341 struct fcgi_param p;
1342
1343 if (params->mask & flag)
1344 return 1;
1345
1346 chunk_reset(&trash);
1347
1348 switch (flag) {
1349 case FCGI_SP_CGI_GATEWAY:
1350 p.n = ist("GATEWAY_INTERFACE");
1351 p.v = ist("CGI/1.1");
1352 goto encode;
1353 case FCGI_SP_DOC_ROOT:
1354 p.n = ist("DOCUMENT_ROOT");
1355 p.v = params->docroot;
1356 goto encode;
1357 case FCGI_SP_SCRIPT_NAME:
1358 p.n = ist("SCRIPT_NAME");
1359 p.v = params->scriptname;
1360 goto encode;
1361 case FCGI_SP_PATH_INFO:
1362 p.n = ist("PATH_INFO");
1363 p.v = params->pathinfo;
1364 goto encode;
1365 case FCGI_SP_REQ_URI:
1366 p.n = ist("REQUEST_URI");
1367 p.v = params->uri;
1368 goto encode;
1369 case FCGI_SP_REQ_METH:
1370 p.n = ist("REQUEST_METHOD");
1371 p.v = params->meth;
1372 goto encode;
1373 case FCGI_SP_REQ_QS:
1374 p.n = ist("QUERY_STRING");
1375 p.v = params->qs;
1376 goto encode;
1377 case FCGI_SP_SRV_NAME:
1378 p.n = ist("SERVER_NAME");
1379 p.v = params->srv_name;
1380 goto encode;
1381 case FCGI_SP_SRV_PORT:
1382 p.n = ist("SERVER_PORT");
1383 p.v = params->srv_port;
1384 goto encode;
1385 case FCGI_SP_SRV_PROTO:
1386 p.n = ist("SERVER_PROTOCOL");
1387 p.v = params->vsn;
1388 goto encode;
1389 case FCGI_SP_REM_ADDR:
1390 p.n = ist("REMOTE_ADDR");
1391 p.v = params->rem_addr;
1392 goto encode;
1393 case FCGI_SP_REM_PORT:
1394 p.n = ist("REMOTE_PORT");
1395 p.v = params->rem_port;
1396 goto encode;
1397 case FCGI_SP_SCRIPT_FILE:
1398 p.n = ist("SCRIPT_FILENAME");
Tim Duesterhus77508502022-03-15 13:11:06 +01001399 chunk_istcat(&trash, params->docroot);
1400 chunk_istcat(&trash, params->scriptname);
Christopher Faulet99eff652019-08-11 23:11:30 +02001401 p.v = ist2(b_head(&trash), b_data(&trash));
1402 goto encode;
1403 case FCGI_SP_PATH_TRANS:
1404 if (!istlen(params->pathinfo))
1405 goto skip;
1406 p.n = ist("PATH_TRANSLATED");
Tim Duesterhus77508502022-03-15 13:11:06 +01001407 chunk_istcat(&trash, params->docroot);
1408 chunk_istcat(&trash, params->pathinfo);
Christopher Faulet99eff652019-08-11 23:11:30 +02001409 p.v = ist2(b_head(&trash), b_data(&trash));
1410 goto encode;
1411 case FCGI_SP_CONT_LEN:
1412 p.n = ist("CONTENT_LENGTH");
1413 p.v = params->cont_len;
1414 goto encode;
1415 case FCGI_SP_HTTPS:
1416 if (!params->https)
1417 goto skip;
1418 p.n = ist("HTTPS");
1419 p.v = ist("on");
1420 goto encode;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001421 case FCGI_SP_SRV_SOFT:
1422 p.n = ist("SERVER_SOFTWARE");
1423 p.v = params->srv_soft;
1424 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001425 default:
1426 goto skip;
1427 }
1428
1429 encode:
1430 if (!istlen(p.v))
1431 goto skip;
1432 if (!fcgi_encode_param(outbuf, &p))
1433 return 0;
1434 skip:
1435 params->mask |= flag;
1436 return 1;
1437}
1438
1439/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1440 * anything. It is highly unexpected, but if the record is larger than a buffer
1441 * and cannot be encoded in one time, an error is triggered and the connection is
1442 * closed. GET_VALUES record cannot be split.
1443 */
1444static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1445{
1446 struct buffer outbuf;
1447 struct buffer *mbuf;
1448 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1449 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001450 int ret = 0;
1451
1452 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001453
1454 mbuf = br_tail(fconn->mbuf);
1455 retry:
1456 if (!fcgi_get_buf(fconn, mbuf)) {
1457 fconn->flags |= FCGI_CF_MUX_MALLOC;
1458 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001459 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1460 ret = 0;
1461 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001462 }
1463
1464 while (1) {
1465 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001466 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001467 break;
1468 realign_again:
1469 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1470 }
1471
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001472 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001473 goto full;
1474
1475 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1476 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001477 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1478 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001479
1480 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1481 * handled by HAProxy.
1482 */
1483 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1484 goto full;
1485
1486 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001487 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 +01001488 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001489 b_add(mbuf, outbuf.data);
1490 ret = 1;
1491
1492 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001493 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001494 return ret;
1495 full:
1496 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001497 if (!b_data(mbuf)) {
1498 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 +02001499 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001500 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001501
1502 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1503 goto retry;
1504 fconn->flags |= FCGI_CF_MUX_MFULL;
1505 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001506 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001507 ret = 0;
1508 goto end;
1509 fail:
1510 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001511 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1512 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1513 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001514}
1515
1516/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1517 * couldn't do anything. It is highly unexpected, but if the record is larger
1518 * than a buffer and cannot be decoded in one time, an error is triggered and
1519 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1520 */
1521static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1522{
1523 struct buffer inbuf;
1524 struct buffer *dbuf;
1525 size_t offset;
1526
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001527 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1528
Christopher Faulet99eff652019-08-11 23:11:30 +02001529 dbuf = &fconn->dbuf;
1530
1531 /* Record too large to be fully decoded */
1532 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1533 goto fail;
1534
1535 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001536 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1537 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001538 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001539 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001540
1541 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1542 /* Realign the dmux buffer if the record wraps. It is unexpected
1543 * at this stage because it should be the first record received
1544 * from the FCGI application.
1545 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01001546 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001547 }
1548
1549 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1550
1551 for (offset = 0; offset < b_data(&inbuf); ) {
1552 struct fcgi_param p;
1553 size_t ret;
1554
1555 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1556 if (!ret) {
1557 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001558 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 +02001559 goto fail;
1560 }
1561 offset += ret;
1562
1563 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001564 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001565 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 +02001566 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001567 }
1568 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001569 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 +02001570 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001571 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001572 }
1573 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1574 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001575 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 +02001576 }
1577 /*
1578 * Ignore all other params
1579 */
1580 }
1581
1582 /* Reset the number of concurrent streams supported if the FCGI
1583 * application does not support connection multiplexing
1584 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001585 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001586 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001587 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1588 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001589
1590 /* We must be sure to have read exactly the announced record length, no
1591 * more no less
1592 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001593 if (offset != fconn->drl) {
1594 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 +02001595 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001596 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001597
Willy Tarreau022e5e52020-09-10 09:33:15 +02001598 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 +02001599 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1600 fconn->drl = 0;
1601 fconn->drp = 0;
1602 fconn->state = FCGI_CS_RECORD_H;
1603 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001604 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1605 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001606 return 1;
1607 fail:
1608 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001609 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1610 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 +02001611 return 0;
1612}
1613
1614/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1615 * excluded, as the streams which already received the end-of-stream. It returns
1616 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1617 */
1618static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1619{
1620 struct eb32_node *node;
1621 struct fcgi_strm *fstrm;
1622
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001623 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1624
Christopher Faulet99eff652019-08-11 23:11:30 +02001625 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1626 while (node) {
1627 fstrm = container_of(node, struct fcgi_strm, by_id);
1628 node = eb32_next(node);
1629 if (fstrm->state != FCGI_SS_CLOSED &&
1630 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1631 !fcgi_strm_send_abort(fconn, fstrm))
1632 return 0;
1633 }
1634 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001635 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1636 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001637 return 1;
1638}
1639
1640/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1641 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1642 * space to proceed. It is small enough to be encoded in an empty buffer.
1643 */
1644static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1645{
1646 struct buffer outbuf;
1647 struct buffer *mbuf;
1648 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1649 int ret;
1650
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001651 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1652
Christopher Faulet99eff652019-08-11 23:11:30 +02001653 mbuf = br_tail(fconn->mbuf);
1654 retry:
1655 if (!fcgi_get_buf(fconn, mbuf)) {
1656 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001657 fstrm->flags |= FCGI_SF_BLK_MROOM;
1658 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1659 ret = 0;
1660 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001661 }
1662
1663 while (1) {
1664 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001665 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001666 break;
1667 realign_again:
1668 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1669 }
1670
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001671 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001672 goto full;
1673
1674 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1675 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001676 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001677 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001678 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001679
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001680 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1681 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001682 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001683 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001684 if (!fcgi_encode_begin_request(&outbuf, &rec))
1685 goto full;
1686
1687 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001688 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 +02001689 b_add(mbuf, outbuf.data);
1690 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1691 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001692 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001693 ret = 1;
1694
1695 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001696 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001697 return ret;
1698 full:
1699 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1700 goto retry;
1701 fconn->flags |= FCGI_CF_MUX_MFULL;
1702 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001703 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 +02001704 ret = 0;
1705 goto end;
1706}
1707
1708/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1709 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1710 * space to proceed. It is small enough to be encoded in an empty buffer.
1711 */
1712static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1713 enum fcgi_record_type rtype)
1714{
1715 struct buffer outbuf;
1716 struct buffer *mbuf;
1717 int ret;
1718
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001719 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001720 mbuf = br_tail(fconn->mbuf);
1721 retry:
1722 if (!fcgi_get_buf(fconn, mbuf)) {
1723 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001724 fstrm->flags |= FCGI_SF_BLK_MROOM;
1725 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1726 ret = 0;
1727 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001728 }
1729
1730 while (1) {
1731 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001732 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 break;
1734 realign_again:
1735 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1736 }
1737
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001738 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001739 goto full;
1740
1741 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1742 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001743 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001744 outbuf.area[1] = rtype;
1745 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001746 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001747
1748 /* commit the record */
1749 b_add(mbuf, outbuf.data);
1750 ret = 1;
1751
1752 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001753 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001754 return ret;
1755 full:
1756 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1757 goto retry;
1758 fconn->flags |= FCGI_CF_MUX_MFULL;
1759 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001760 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 +02001761 ret = 0;
1762 goto end;
1763}
1764
1765
1766/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1767 * marks the end of params.
1768 */
1769static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1770{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001771 int ret;
1772
1773 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1774 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001775 if (ret) {
1776 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001777 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 +01001778 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001779 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001780}
1781
1782/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1783 * marks the end of input. On success, all the request was successfully sent.
1784 */
1785static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1786{
1787 int ret;
1788
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001789 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001790 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001791 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001792 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001793 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 +02001794 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1795 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1796 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001797 return ret;
1798}
1799
1800/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1801 * stops the request processing.
1802 */
1803static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1804{
1805 int ret;
1806
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001807 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001808 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001809 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001810 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001811 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 +02001812 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1813 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1814 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001815 return ret;
1816}
1817
1818/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1819 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1820 * several records are sent. However, a K/V param cannot be split between 2
1821 * records.
1822 */
1823static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1824 struct htx *htx)
1825{
1826 struct buffer outbuf;
1827 struct buffer *mbuf;
1828 struct htx_blk *blk;
1829 struct htx_sl *sl = NULL;
1830 struct fcgi_strm_params params;
1831 size_t total = 0;
1832
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1834
Christopher Faulet99eff652019-08-11 23:11:30 +02001835 memset(&params, 0, sizeof(params));
1836 params.p = get_trash_chunk();
1837
1838 mbuf = br_tail(fconn->mbuf);
1839 retry:
1840 if (!fcgi_get_buf(fconn, mbuf)) {
1841 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001842 fstrm->flags |= FCGI_SF_BLK_MROOM;
1843 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1844 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001845 }
1846
1847 while (1) {
1848 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001849 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001850 break;
1851 realign_again:
1852 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1853 }
1854
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001855 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001856 goto full;
1857
1858 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1859 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001860 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001861 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001862 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001863
1864 blk = htx_get_head_blk(htx);
1865 while (blk) {
1866 enum htx_blk_type type;
1867 uint32_t size = htx_get_blksz(blk);
1868 struct fcgi_param p;
1869
1870 type = htx_get_blk_type(blk);
1871 switch (type) {
1872 case HTX_BLK_REQ_SL:
1873 sl = htx_get_blk_ptr(htx, blk);
1874 if (sl->info.req.meth == HTTP_METH_HEAD)
1875 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1876 if (sl->flags & HTX_SL_F_VER_11)
1877 fstrm->h1m.flags |= H1_MF_VER_11;
1878 break;
1879
1880 case HTX_BLK_HDR:
1881 p.n = htx_get_blk_name(htx, blk);
1882 p.v = htx_get_blk_value(htx, blk);
1883
1884 if (istmatch(p.n, ist(":fcgi-"))) {
Tim Duesterhusa6a32792022-03-05 00:52:45 +01001885 p.n = istadv(p.n, 6);
Christopher Faulet99eff652019-08-11 23:11:30 +02001886 if (isteq(p.n, ist("gateway_interface")))
1887 params.mask |= FCGI_SP_CGI_GATEWAY;
1888 else if (isteq(p.n, ist("document_root"))) {
1889 params.mask |= FCGI_SP_DOC_ROOT;
1890 params.docroot = p.v;
1891 }
1892 else if (isteq(p.n, ist("script_name"))) {
1893 params.mask |= FCGI_SP_SCRIPT_NAME;
1894 params.scriptname = p.v;
1895 }
1896 else if (isteq(p.n, ist("path_info"))) {
1897 params.mask |= FCGI_SP_PATH_INFO;
1898 params.pathinfo = p.v;
1899 }
1900 else if (isteq(p.n, ist("request_uri"))) {
1901 params.mask |= FCGI_SP_REQ_URI;
1902 params.uri = p.v;
1903 }
1904 else if (isteq(p.n, ist("request_meth")))
1905 params.mask |= FCGI_SP_REQ_METH;
1906 else if (isteq(p.n, ist("query_string")))
1907 params.mask |= FCGI_SP_REQ_QS;
1908 else if (isteq(p.n, ist("server_name")))
1909 params.mask |= FCGI_SP_SRV_NAME;
1910 else if (isteq(p.n, ist("server_port")))
1911 params.mask |= FCGI_SP_SRV_PORT;
1912 else if (isteq(p.n, ist("server_protocol")))
1913 params.mask |= FCGI_SP_SRV_PROTO;
1914 else if (isteq(p.n, ist("remote_addr")))
1915 params.mask |= FCGI_SP_REM_ADDR;
1916 else if (isteq(p.n, ist("remote_port")))
1917 params.mask |= FCGI_SP_REM_PORT;
1918 else if (isteq(p.n, ist("script_filename")))
1919 params.mask |= FCGI_SP_SCRIPT_FILE;
1920 else if (isteq(p.n, ist("path_translated")))
1921 params.mask |= FCGI_SP_PATH_TRANS;
1922 else if (isteq(p.n, ist("https")))
1923 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001924 else if (isteq(p.n, ist("server_software")))
1925 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02001926 }
1927 else if (isteq(p.n, ist("content-length"))) {
1928 p.n = ist("CONTENT_LENGTH");
1929 params.mask |= FCGI_SP_CONT_LEN;
1930 }
1931 else if (isteq(p.n, ist("content-type")))
1932 p.n = ist("CONTENT_TYPE");
1933 else {
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001934 struct ist n;
1935
Christopher Faulet99eff652019-08-11 23:11:30 +02001936 if (isteq(p.n, ist("host")))
1937 params.srv_name = p.v;
Christopher Fauletf56e8462021-09-28 10:56:36 +02001938 else if (isteq(p.n, ist("te"))) {
1939 /* "te" may only be sent with "trailers" if this value
1940 * is present, otherwise it must be deleted.
1941 */
1942 p.v = istist(p.v, ist("trailers"));
1943 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
1944 break;
1945 p.v = ist("trailers");
1946 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001947
Christopher Faulet67d58092019-10-02 10:51:38 +02001948 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001949 if (isttest(fconn->proxy->server_id_hdr_name) && isteq(p.n, fconn->proxy->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02001950 break;
1951
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001952 n = ist2(trash.area, 0);
1953 istcat(&n, ist("http_"), trash.size);
1954 istcat(&n, p.n, trash.size);
1955 p.n = n;
Christopher Faulet99eff652019-08-11 23:11:30 +02001956 }
1957
1958 if (!fcgi_encode_param(&outbuf, &p)) {
1959 if (b_space_wraps(mbuf))
1960 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001961 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001962 goto full;
1963 goto done;
1964 }
1965 break;
1966
1967 case HTX_BLK_EOH:
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001968 if (isttest(fconn->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001969 struct server *srv = objt_server(fconn->conn->target);
1970
1971 if (!srv)
1972 goto done;
1973
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001974 p.n = ist2(trash.area, 0);
1975 istcat(&p.n, ist("http_"), trash.size);
1976 istcat(&p.n, fconn->proxy->server_id_hdr_name, trash.size);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001977 p.v = ist(srv->id);
1978
1979 if (!fcgi_encode_param(&outbuf, &p)) {
1980 if (b_space_wraps(mbuf))
1981 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001982 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001983 goto full;
1984 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001985 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001986 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001987 goto done;
1988
1989 default:
1990 break;
1991 }
1992 total += size;
1993 blk = htx_remove_blk(htx, blk);
1994 }
1995
1996 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01001997 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
1998 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001999 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002000 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002001
2002 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2003 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2004 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2005 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2006 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2007 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2008 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2009 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2010 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2011 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2012 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2013 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2014 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2015 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2016 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002017 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002018 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2019 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002020 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002022
2023 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002024 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});
2025 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002026 b_add(mbuf, outbuf.data);
2027
2028 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002029 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002030 return total;
2031 full:
2032 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2033 goto retry;
2034 fconn->flags |= FCGI_CF_MUX_MFULL;
2035 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002036 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 +02002037 if (total)
2038 goto error;
2039 goto end;
2040
2041 error:
2042 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002043 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 +02002044 fcgi_strm_error(fstrm);
2045 goto end;
2046}
2047
2048/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2049 * anything. STDIN records contain the request body.
2050 */
2051static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2052 struct htx *htx, size_t count, struct buffer *buf)
2053{
2054 struct buffer outbuf;
2055 struct buffer *mbuf;
2056 struct htx_blk *blk;
2057 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002058 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002059 size_t total = 0;
2060
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002061 extra_bytes = 0;
2062
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002063 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002064 if (!count)
2065 goto end;
2066
2067 mbuf = br_tail(fconn->mbuf);
2068 retry:
2069 if (!fcgi_get_buf(fconn, mbuf)) {
2070 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002071 fstrm->flags |= FCGI_SF_BLK_MROOM;
2072 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2073 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002074 }
2075
2076 /* Perform some optimizations to reduce the number of buffer copies.
2077 * First, if the mux's buffer is empty and the htx area contains exactly
2078 * one data block of the same size as the requested count, and this
2079 * count fits within the record size, then it's possible to simply swap
2080 * the caller's buffer with the mux's output buffer and adjust offsets
2081 * and length to match the entire DATA HTX block in the middle. In this
2082 * case we perform a true zero-copy operation from end-to-end. This is
2083 * the situation that happens all the time with large files. Second, if
2084 * this is not possible, but the mux's output buffer is empty, we still
2085 * have an opportunity to avoid the copy to the intermediary buffer, by
2086 * making the intermediary buffer's area point to the output buffer's
2087 * area. In this case we want to skip the HTX header to make sure that
2088 * copies remain aligned and that this operation remains possible all
2089 * the time. This goes for headers, data blocks and any data extracted
2090 * from the HTX blocks.
2091 */
2092 blk = htx_get_head_blk(htx);
2093 if (!blk)
2094 goto end;
2095 type = htx_get_blk_type(blk);
2096 size = htx_get_blksz(blk);
2097 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2098 void *old_area = mbuf->area;
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002099 int eom = (htx->flags & HTX_FL_EOM);
Christopher Faulet99eff652019-08-11 23:11:30 +02002100
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002101 /* Last block of the message: Reserve the size for the empty stdin record */
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002102 if (eom)
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002103 extra_bytes = FCGI_RECORD_HEADER_SZ;
2104
Christopher Faulet99eff652019-08-11 23:11:30 +02002105 if (b_data(mbuf)) {
2106 /* Too bad there are data left there. We're willing to memcpy/memmove
2107 * up to 1/4 of the buffer, which means that it's OK to copy a large
2108 * record into a buffer containing few data if it needs to be realigned,
2109 * and that it's also OK to copy few data without realigning. Otherwise
2110 * we'll pretend the mbuf is full and wait for it to become empty.
2111 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002112 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002113 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002114 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002115 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002116 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002117 }
2118
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002119 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 +02002120 /* map a FCGI record to the HTX block so that we can put the
2121 * record header there.
2122 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002123 *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 +02002124 outbuf.area = b_head(mbuf);
2125
2126 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002127 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002128 fcgi_set_record_id(outbuf.area, fstrm->id);
2129 fcgi_set_record_size(outbuf.area, size);
2130
2131 /* and exchange with our old area */
2132 buf->area = old_area;
2133 buf->data = buf->head = 0;
2134 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002135
2136 htx = (struct htx *)buf->area;
2137 htx_reset(htx);
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002138 if (eom)
2139 goto empty_stdin;
Christopher Faulet99eff652019-08-11 23:11:30 +02002140 goto end;
2141 }
2142
2143 copy:
2144 while (1) {
2145 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002146 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002147 break;
2148 realign_again:
2149 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2150 }
2151
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002152 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002153 goto full;
2154
2155 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2156 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002157 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002158 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002159 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002160
2161 blk = htx_get_head_blk(htx);
2162 while (blk && count) {
2163 enum htx_blk_type type = htx_get_blk_type(blk);
2164 uint32_t size = htx_get_blksz(blk);
2165 struct ist v;
2166
2167 switch (type) {
2168 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002169 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 +02002170 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002171
2172 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2173 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2174
2175 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002176 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002177 extra_bytes = 0;
2178 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002179
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002180 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002181 /* It doesn't fit at once. If it at least fits once split and
2182 * the amount of data to move is low, let's defragment the
2183 * buffer now.
2184 */
2185 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002186 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002187 b_data(mbuf) <= MAX_DATA_REALIGN)
2188 goto realign_again;
Christopher Faulet52fd8a12022-11-15 10:46:28 +01002189 v.len = (FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)
2190 ? 0
2191 : b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes);
Christopher Faulet99eff652019-08-11 23:11:30 +02002192 }
2193 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002194 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002195 goto full;
2196 goto done;
2197 }
2198 if (v.len != size) {
2199 total += v.len;
2200 count -= v.len;
2201 htx_cut_data_blk(htx, blk, v.len);
2202 goto done;
2203 }
2204 break;
2205
Christopher Faulet99eff652019-08-11 23:11:30 +02002206 default:
2207 break;
2208 }
2209 total += size;
2210 count -= size;
2211 blk = htx_remove_blk(htx, blk);
2212 }
2213
2214 done:
2215 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 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});
2217 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002218 b_add(mbuf, outbuf.data);
2219
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002220 /* Send the empty stding here to finish the message */
2221 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
Christopher Faulete8c7fb32022-11-15 10:36:31 +01002222 empty_stdin:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002223 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2224 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2225 /* bytes already reserved for this record. It should not fail */
2226 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002227 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 +01002228 fcgi_strm_error(fstrm);
2229 }
2230 }
2231
Christopher Faulet99eff652019-08-11 23:11:30 +02002232 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002234 return total;
2235 full:
2236 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2237 goto retry;
2238 fconn->flags |= FCGI_CF_MUX_MFULL;
2239 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002240 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 +02002241 goto end;
2242}
2243
2244/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2245 * anything. STDOUT records contain the entire response. All the content is
2246 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2247 */
2248static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2249{
2250 struct buffer *dbuf;
2251 size_t ret;
2252 size_t max;
2253
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002254 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2255
Christopher Faulet99eff652019-08-11 23:11:30 +02002256 dbuf = &fconn->dbuf;
2257
2258 /* Only padding remains */
2259 if (fconn->state == FCGI_CS_RECORD_P)
2260 goto end_transfer;
2261
2262 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2263 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2264 buf_room_for_htx_data(dbuf))
2265 goto fail; // incomplete record
2266
2267 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2268 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002269 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2270 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002271 }
2272
2273 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2274 max = buf_room_for_htx_data(&fstrm->rxbuf);
2275 if (!b_data(&fstrm->rxbuf))
2276 fstrm->rxbuf.head = sizeof(struct htx);
2277 if (max > fconn->drl)
2278 max = fconn->drl;
2279
2280 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2281 if (!ret)
2282 goto fail;
2283 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002284 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2285 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 +02002286
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002287 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002288 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002289 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2290 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002291
2292 if (fconn->drl)
2293 goto fail;
2294
2295 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002296 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002297 fconn->drl += fconn->drp;
2298 fconn->drp = 0;
2299 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2300 b_del(&fconn->dbuf, ret);
2301 fconn->drl -= ret;
2302 if (fconn->drl)
2303 goto fail;
2304
2305 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002306 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2307 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002308 return 1;
2309 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002310 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 +02002311 return 0;
2312}
2313
2314
2315/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2316 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002317 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002318 */
2319static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2320{
2321 int ret;
2322
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002323 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2324
Christopher Faulet99eff652019-08-11 23:11:30 +02002325 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002326 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002327 fconn->drl += fconn->drp;
2328 fconn->drp = 0;
2329 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2330 b_del(&fconn->dbuf, ret);
2331 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002332 if (fconn->drl) {
2333 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 +02002334 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002335 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002336 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002337 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002338 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 +02002339 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);
2340 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002341 return 1;
2342}
2343
2344/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2345 * anything.
2346 */
2347static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2348{
2349 struct buffer *dbuf;
2350 struct buffer tag;
2351 size_t ret;
2352
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002353 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002354 dbuf = &fconn->dbuf;
2355
2356 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002357 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002358 goto end_transfer;
2359
2360 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2361 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2362 buf_room_for_htx_data(dbuf))
2363 goto fail; // incomplete record
2364
2365 chunk_reset(&trash);
Christopher Faulet6be40a42023-09-11 18:57:39 +02002366 ret = b_force_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
Christopher Faulet99eff652019-08-11 23:11:30 +02002367 if (!ret)
2368 goto fail;
2369 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002370 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 +02002371
2372 trash.area[ret] = '\n';
2373 trash.area[ret+1] = '\0';
2374 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002375 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002376
2377 if (fconn->drl)
2378 goto fail;
2379
2380 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002381 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002382 fconn->drl += fconn->drp;
2383 fconn->drp = 0;
2384 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2385 b_del(&fconn->dbuf, ret);
2386 fconn->drl -= ret;
2387 if (fconn->drl)
2388 goto fail;
2389 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002390 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2391 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002392 return 1;
2393 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002394 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 +02002395 return 0;
2396}
2397
2398/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2399 * anything. If the empty STDOUT record is not already received, this one marks
2400 * the end of the response. It is highly unexpected, but if the record is larger
2401 * than a buffer and cannot be decoded in one time, an error is triggered and
2402 * the connection is closed. END_REQUEST record cannot be split.
2403 */
2404static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2405{
2406 struct buffer inbuf;
2407 struct buffer *dbuf;
2408 struct fcgi_end_request endreq;
2409
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002410 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002411 dbuf = &fconn->dbuf;
2412
2413 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002414 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2415 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 +02002416 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002417 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002418
2419 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2421 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002422 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002423 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002424
2425 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2426 /* Realign the dmux buffer if the record wraps. It is unexpected
2427 * at this stage because it should be the first record received
2428 * from the FCGI application.
2429 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002430 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002431 }
2432
2433 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2434
Christopher Faulet73518be2021-01-27 12:06:54 +01002435 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2436 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 +02002437 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002438 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002439
2440 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002441 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 +02002442 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 +02002443 fstrm->proto_status = endreq.errcode;
2444 fcgi_strm_close(fstrm);
2445
2446 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2447 fconn->drl = 0;
2448 fconn->drp = 0;
2449 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002450 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2451 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002452 return 1;
2453
2454 fail:
2455 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002456 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 +02002457 return 0;
2458}
2459
2460/* process Rx records to be demultiplexed */
2461static void fcgi_process_demux(struct fcgi_conn *fconn)
2462{
2463 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2464 struct fcgi_header hdr;
2465 int ret;
2466
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002467 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2468
Christopher Faulet99eff652019-08-11 23:11:30 +02002469 if (fconn->state == FCGI_CS_CLOSED)
2470 return;
2471
2472 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002473 if (fconn->state == FCGI_CS_INIT) {
2474 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2475 return;
2476 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002477 if (fconn->state == FCGI_CS_SETTINGS) {
2478 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002479 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002480 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002481 if (!ret) {
2482 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 +02002483 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002484 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002485 b_del(&fconn->dbuf, ret);
2486
2487 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2488 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002489 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 +02002490 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 +02002491 goto fail;
2492 }
2493 goto new_record;
2494 }
2495 }
2496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002497 /* process as many incoming records as possible below */
2498 while (1) {
2499 if (!b_data(&fconn->dbuf)) {
2500 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2501 break;
2502 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002503
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002504 if (fconn->state == FCGI_CS_CLOSED) {
2505 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002506 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002507 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002508
2509 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002510 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002511 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2512 if (!ret)
2513 break;
2514 b_del(&fconn->dbuf, ret);
2515
2516 new_record:
2517 fconn->dsi = hdr.id;
2518 fconn->drt = hdr.type;
2519 fconn->drl = hdr.len;
2520 fconn->drp = hdr.padding;
2521 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002522 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 +02002523 }
2524
2525 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2526 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2527
Willy Tarreau77534272022-05-18 07:34:16 +02002528 if (tmp_fstrm != fstrm && fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002529 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002530 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002531 fstrm->state == FCGI_SS_CLOSED ||
2532 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002533 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002534 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002535 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 +02002536 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002537 fcgi_strm_notify_recv(fstrm);
2538 }
2539 fstrm = tmp_fstrm;
2540
2541 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2542 /* ignore all record for closed streams */
2543 goto ignore_record;
2544 }
2545 if (fstrm->state == FCGI_SS_IDLE) {
2546 /* ignore all record for unknown streams */
2547 goto ignore_record;
2548 }
2549
2550 switch (fconn->drt) {
2551 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002552 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 +02002553 ret = fcgi_conn_handle_values_result(fconn);
2554 break;
2555
2556 case FCGI_STDOUT:
2557 if (fstrm->flags & FCGI_SF_ES_RCVD)
2558 goto ignore_record;
2559
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002560 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002561 if (fconn->drl)
2562 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2563 else
2564 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2565 break;
2566
2567 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002568 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002569 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2570 break;
2571
2572 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002573 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 +02002574 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2575 break;
2576
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002577 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002578 default:
2579 ignore_record:
2580 /* drop records that we ignore. They may be
2581 * larger than the buffer so we drain all of
2582 * their contents until we reach the end.
2583 */
2584 fconn->state = FCGI_CS_RECORD_P;
2585 fconn->drl += fconn->drp;
2586 fconn->drp = 0;
2587 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002588 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002589 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002590 b_del(&fconn->dbuf, ret);
2591 fconn->drl -= ret;
2592 ret = (fconn->drl == 0);
2593 }
2594
2595 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 if (ret <= 0) {
2597 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002598 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002599 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002600
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002602 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002603 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2604 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002605 }
2606
2607 fail:
2608 /* we can go here on missing data, blocked response or error */
Willy Tarreau77534272022-05-18 07:34:16 +02002609 if (fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002610 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002611 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002612 fstrm->state == FCGI_SS_CLOSED ||
2613 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002614 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002615 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002616 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 +02002617 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002618 fcgi_strm_notify_recv(fstrm);
2619 }
2620
2621 fcgi_conn_restart_reading(fconn, 0);
2622}
2623
2624/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2625 * the end.
2626 */
2627static int fcgi_process_mux(struct fcgi_conn *fconn)
2628{
2629 struct fcgi_strm *fstrm, *fstrm_back;
2630
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002631 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2632
Christopher Faulet99eff652019-08-11 23:11:30 +02002633 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2634 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2635 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2636 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 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 +02002638 fcgi_wake_unassigned_streams(fconn);
2639 goto mux;
2640 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002641 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002642 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2643 goto fail;
2644 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002645 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 +02002646 }
2647 /* need to wait for the other side */
2648 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002649 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002650 }
2651
2652 mux:
2653 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2654 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2655 break;
2656
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002657 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002658 continue;
2659
Willy Tarreau7aad7032020-01-16 17:20:57 +01002660 /* If the sender changed his mind and unsubscribed, let's just
2661 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002662 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002663 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2664 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002665 LIST_DEL_INIT(&fstrm->send_list);
2666 continue;
2667 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002668
2669 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002670 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2671 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002672 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002673 tasklet_wakeup(fstrm->subs->tasklet);
2674 fstrm->subs->events &= ~SUB_RETRY_SEND;
2675 if (!fstrm->subs->events)
2676 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002677 } else {
2678 /* it's the shut request that was queued */
2679 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2680 tasklet_wakeup(fstrm->shut_tl);
2681 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002682 }
2683
2684 fail:
2685 if (fconn->state == FCGI_CS_CLOSED) {
2686 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2687 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002688 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2689 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002691 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002692 }
2693 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002694
2695 done:
2696 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002697 return 1;
2698}
2699
2700
2701/* Attempt to read data, and subscribe if none available.
2702 * The function returns 1 if data has been received, otherwise zero.
2703 */
2704static int fcgi_recv(struct fcgi_conn *fconn)
2705{
2706 struct connection *conn = fconn->conn;
2707 struct buffer *buf;
2708 int max;
2709 size_t ret;
2710
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002711 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2712
2713 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2714 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002715 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002716 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002717
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002718 if (!fcgi_recv_allowed(fconn)) {
2719 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002720 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002721 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002722
2723 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2724 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002725 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 fconn->flags |= FCGI_CF_DEM_DALLOC;
2727 return 0;
2728 }
2729
Christopher Faulet99eff652019-08-11 23:11:30 +02002730 if (!b_data(buf)) {
2731 /* try to pre-align the buffer like the
2732 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002733 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002734 * HTX block to alias it upon recv. We cannot use the
2735 * head because rcv_buf() will realign the buffer if
2736 * it's empty. Thus we cheat and pretend we already
2737 * have a few bytes there.
2738 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002739 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2740 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002741 }
2742 else
2743 max = buf_room_for_htx_data(buf);
2744
2745 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2746
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002747 if (max && !ret && fcgi_recv_allowed(fconn)) {
2748 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002749 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002750 }
2751 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002752 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002753
Christopher Fauletab79b322022-10-12 17:51:51 +02002754 if (conn_xprt_read0_pending(conn)) {
2755 TRACE_DATA("received read0", FCGI_EV_FCONN_RECV, conn);
2756 fconn->flags |= FCGI_CF_EOS;
2757 }
2758 if (conn->flags & CO_FL_ERROR) {
2759 TRACE_DATA("connection error", FCGI_EV_FCONN_RECV, conn);
2760 fconn->flags |= FCGI_CF_ERROR;
2761 }
2762
Christopher Faulet99eff652019-08-11 23:11:30 +02002763 if (!b_data(buf)) {
2764 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Fauletab79b322022-10-12 17:51:51 +02002765 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002766 }
2767
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002768 if (ret == max) {
2769 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002770 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002771 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002772
Christopher Fauletab79b322022-10-12 17:51:51 +02002773end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002774 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002775 return !!ret || (fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERROR));
Christopher Faulet99eff652019-08-11 23:11:30 +02002776}
2777
2778
2779/* Try to send data if possible.
2780 * The function returns 1 if data have been sent, otherwise zero.
2781 */
2782static int fcgi_send(struct fcgi_conn *fconn)
2783{
2784 struct connection *conn = fconn->conn;
2785 int done;
2786 int sent = 0;
2787
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002788 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2789
Christopher Fauletab79b322022-10-12 17:51:51 +02002790 if (fconn->flags & (FCGI_CF_ERROR|FCGI_CF_ERR_PENDING)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002791 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002792 if (fconn->flags & FCGI_CF_EOS)
2793 fconn->flags |= FCGI_CF_ERROR;
2794 b_reset(br_tail(fconn->mbuf));
Christopher Faulet99eff652019-08-11 23:11:30 +02002795 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002796 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002797
2798
Willy Tarreau911db9b2020-01-23 16:27:54 +01002799 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002800 /* a handshake was requested */
2801 goto schedule;
2802 }
2803
2804 /* This loop is quite simple : it tries to fill as much as it can from
2805 * pending streams into the existing buffer until it's reportedly full
2806 * or the end of send requests is reached. Then it tries to send this
2807 * buffer's contents out, marks it not full if at least one byte could
2808 * be sent, and tries again.
2809 *
2810 * The snd_buf() function normally takes a "flags" argument which may
2811 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2812 * data immediately comes and CO_SFL_STREAMER to indicate that the
2813 * connection is streaming lots of data (used to increase TLS record
2814 * size at the expense of latency). The former can be sent any time
2815 * there's a buffer full flag, as it indicates at least one stream
2816 * attempted to send and failed so there are pending data. An
2817 * alternative would be to set it as long as there's an active stream
2818 * but that would be problematic for ACKs until we have an absolute
2819 * guarantee that all waiters have at least one byte to send. The
2820 * latter should possibly not be set for now.
2821 */
2822
2823 done = 0;
2824 while (!done) {
2825 unsigned int flags = 0;
2826 unsigned int released = 0;
2827 struct buffer *buf;
2828
2829 /* fill as much as we can into the current buffer */
2830 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2831 done = fcgi_process_mux(fconn);
2832
2833 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2834 done = 1; // we won't go further without extra buffers
2835
2836 if (conn->flags & CO_FL_ERROR)
2837 break;
2838
2839 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2840 flags |= CO_SFL_MSG_MORE;
2841
2842 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2843 if (b_data(buf)) {
2844 int ret;
2845
2846 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2847 if (!ret) {
2848 done = 1;
2849 break;
2850 }
2851 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002852 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002853 b_del(buf, ret);
2854 if (b_data(buf)) {
2855 done = 1;
2856 break;
2857 }
2858 }
2859 b_free(buf);
2860 released++;
2861 }
2862
2863 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002864 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002865
2866 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002867 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2868 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2870 }
2871
Christopher Fauletab79b322022-10-12 17:51:51 +02002872 if (conn->flags & CO_FL_ERROR) {
2873 fconn->flags |= FCGI_CF_ERR_PENDING;
2874 if (fconn->flags & FCGI_CF_EOS)
2875 fconn->flags |= FCGI_CF_ERROR;
Christopher Faulet99eff652019-08-11 23:11:30 +02002876 b_reset(br_tail(fconn->mbuf));
2877 }
Christopher Fauletab79b322022-10-12 17:51:51 +02002878
Christopher Faulet99eff652019-08-11 23:11:30 +02002879 /* We're not full anymore, so we can wake any task that are waiting
2880 * for us.
2881 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002882 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002883 struct fcgi_strm *fstrm;
2884
2885 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2886 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2887 break;
2888
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002889 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002890 continue;
2891
Willy Tarreau7aad7032020-01-16 17:20:57 +01002892 /* If the sender changed his mind and unsubscribed, let's just
2893 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002894 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002895 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2896 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002897 LIST_DEL_INIT(&fstrm->send_list);
2898 continue;
2899 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002900
2901 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002902 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002903 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002904 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002905 tasklet_wakeup(fstrm->subs->tasklet);
2906 fstrm->subs->events &= ~SUB_RETRY_SEND;
2907 if (!fstrm->subs->events)
2908 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002909 } else {
2910 /* it's the shut request that was queued */
2911 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2912 tasklet_wakeup(fstrm->shut_tl);
2913 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002914 }
2915 }
2916 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002917 if (!br_data(fconn->mbuf)) {
2918 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002919 goto end;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002920 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002921schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002922 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2923 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002924 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002925 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002926
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002927 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Fauletab79b322022-10-12 17:51:51 +02002928end:
2929 return sent || (fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR));
Christopher Faulet99eff652019-08-11 23:11:30 +02002930}
2931
2932/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002933struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02002934{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002935 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002936 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002937 struct tasklet *tl = (struct tasklet *)t;
2938 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002939 int ret = 0;
2940
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002941 if (state & TASK_F_USR1) {
2942 /* the tasklet was idling on an idle connection, it might have
2943 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002944 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002945 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2946 if (tl->context == NULL) {
2947 /* The connection has been taken over by another thread,
2948 * we're no longer responsible for it, so just free the
2949 * tasklet, and do nothing.
2950 */
2951 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2952 tasklet_free(tl);
2953 return NULL;
2954 }
2955 conn = fconn->conn;
2956 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002957
Christopher Faulet3a7b5392023-03-16 11:43:05 +01002958 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002959 if (conn_in_list)
2960 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002961
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002962 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2963 } else {
2964 /* we're certain the connection was not in an idle list */
2965 conn = fconn->conn;
2966 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
2967 conn_in_list = 0;
2968 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002969
Christopher Faulet99eff652019-08-11 23:11:30 +02002970 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2971 ret = fcgi_send(fconn);
2972 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2973 ret |= fcgi_recv(fconn);
2974 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002975 ret = fcgi_process(fconn);
2976
2977 /* If we were in an idle list, we want to add it back into it,
2978 * unless fcgi_process() returned -1, which mean it has destroyed
2979 * the connection (testing !ret is enough, if fcgi_process() wasn't
2980 * called then ret will be 0 anyway.
2981 */
Willy Tarreau74163142021-03-13 11:30:19 +01002982 if (ret < 0)
2983 t = NULL;
2984
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002985 if (!ret && conn_in_list) {
2986 struct server *srv = objt_server(conn->target);
2987
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002988 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002989 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02002990 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002991 else
Willy Tarreau85223482022-09-29 20:32:43 +02002992 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002993 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002994 }
Willy Tarreau74163142021-03-13 11:30:19 +01002995 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02002996}
2997
2998/* callback called on any event by the connection handler.
2999 * It applies changes and returns zero, or < 0 if it wants immediate
3000 * destruction of the connection (which normally doesn not happen in FCGI).
3001 */
3002static int fcgi_process(struct fcgi_conn *fconn)
3003{
3004 struct connection *conn = fconn->conn;
3005
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003006 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3007
Christopher Faulet99eff652019-08-11 23:11:30 +02003008 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3009 fcgi_process_demux(fconn);
3010
Christopher Fauletab79b322022-10-12 17:51:51 +02003011 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ERROR))
Christopher Faulet99eff652019-08-11 23:11:30 +02003012 b_reset(&fconn->dbuf);
3013
3014 if (buf_room_for_htx_data(&fconn->dbuf))
3015 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3016 }
3017 fcgi_send(fconn);
3018
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003019 if (unlikely(fconn->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003020 /* frontend is stopping, reload likely in progress, let's try
3021 * to announce a graceful shutdown if not yet done. We don't
3022 * care if it fails, it will be tried again later.
3023 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003024 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 +02003025 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3026 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3027 fcgi_conn_send_aborts(fconn);
3028 }
3029 }
3030
3031 /*
3032 * If we received early data, and the handshake is done, wake
3033 * any stream that was waiting for it.
3034 */
3035 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003036 (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 +02003037 struct eb32_node *node;
3038 struct fcgi_strm *fstrm;
3039
3040 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3041 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3042
3043 while (node) {
3044 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003045 if (fcgi_strm_sc(fstrm) && se_fl_test(fstrm->sd, SE_FL_WAIT_FOR_HS))
Christopher Faulet99eff652019-08-11 23:11:30 +02003046 fcgi_strm_notify_recv(fstrm);
3047 node = eb32_next(node);
3048 }
3049 }
3050
Christopher Fauletab79b322022-10-12 17:51:51 +02003051 if ((fconn->flags & FCGI_CF_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003052 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3053 eb_is_empty(&fconn->streams_by_id)) {
3054 fcgi_wake_some_streams(fconn, 0);
3055
3056 if (eb_is_empty(&fconn->streams_by_id)) {
3057 /* no more stream, kill the connection now */
3058 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003059 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003060 return -1;
3061 }
3062 }
3063
3064 if (!b_data(&fconn->dbuf))
3065 fcgi_release_buf(fconn, &fconn->dbuf);
3066
Christopher Fauletab79b322022-10-12 17:51:51 +02003067 if (fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003068 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3069 fcgi_release_mbuf(fconn);
3070
3071 if (fconn->task) {
3072 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3073 task_queue(fconn->task);
3074 }
3075
3076 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003077 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003078 return 0;
3079}
3080
3081
3082/* wake-up function called by the connection layer (mux_ops.wake) */
3083static int fcgi_wake(struct connection *conn)
3084{
3085 struct fcgi_conn *fconn = conn->ctx;
3086
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003087 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003088 return (fcgi_process(fconn));
3089}
3090
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003091
3092static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3093{
3094 int ret = 0;
3095 switch (mux_ctl) {
3096 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003097 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003098 ret |= MUX_STATUS_READY;
3099 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003100 case MUX_EXIT_STATUS:
3101 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003102 default:
3103 return -1;
3104 }
3105}
3106
Christopher Faulet99eff652019-08-11 23:11:30 +02003107/* Connection timeout management. The principle is that if there's no receipt
3108 * nor sending for a certain amount of time, the connection is closed. If the
3109 * MUX buffer still has lying data or is not allocatable, the connection is
3110 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003111 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003112 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003113struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003114{
3115 struct fcgi_conn *fconn = context;
3116 int expired = tick_is_expired(t->expire, now_ms);
3117
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003118 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3119
Willy Tarreau60814ff2020-06-30 11:19:23 +02003120 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003121 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003122
3123 /* Somebody already stole the connection from us, so we should not
3124 * free it, we just have to free the task.
3125 */
3126 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003127 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003128 fconn = NULL;
3129 goto do_leave;
3130 }
3131
Willy Tarreau60814ff2020-06-30 11:19:23 +02003132 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003133 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003134 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3135 return t;
3136 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003137
Willy Tarreau60814ff2020-06-30 11:19:23 +02003138 /* We're about to destroy the connection, so make sure nobody attempts
3139 * to steal it from us.
3140 */
Christopher Faulet3a7b5392023-03-16 11:43:05 +01003141 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003142 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003143
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003144 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003145 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003146
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003147do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003148 task_destroy(t);
3149
3150 if (!fconn) {
3151 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003152 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003153 return NULL;
3154 }
3155
3156 fconn->task = NULL;
3157 fconn->state = FCGI_CS_CLOSED;
3158 fcgi_wake_some_streams(fconn, 0);
3159
3160 if (br_data(fconn->mbuf)) {
3161 /* don't even try to send aborts, the buffer is stuck */
3162 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3163 goto end;
3164 }
3165
3166 /* try to send but no need to insist */
3167 if (!fcgi_conn_send_aborts(fconn))
3168 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3169
3170 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3171 conn_xprt_ready(fconn->conn)) {
3172 unsigned int released = 0;
3173 struct buffer *buf;
3174
3175 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3176 if (b_data(buf)) {
3177 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3178 buf, b_data(buf), 0);
3179 if (!ret)
3180 break;
3181 b_del(buf, ret);
3182 if (b_data(buf))
3183 break;
3184 b_free(buf);
3185 released++;
3186 }
3187 }
3188
3189 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003190 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003191 }
3192
3193 end:
3194 /* either we can release everything now or it will be done later once
3195 * the last stream closes.
3196 */
3197 if (eb_is_empty(&fconn->streams_by_id))
3198 fcgi_release(fconn);
3199
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003200 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003201 return NULL;
3202}
3203
3204
3205/*******************************************/
3206/* functions below are used by the streams */
3207/*******************************************/
3208
3209/* Append the description of what is present in error snapshot <es> into <out>.
3210 * The description must be small enough to always fit in a buffer. The output
3211 * buffer may be the trash so the trash must not be used inside this function.
3212 */
3213static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3214{
3215 chunk_appendf(out,
3216 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3217 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3218 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3219 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3220 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3221 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3222}
3223/*
3224 * Capture a bad response and archive it in the proxy's structure. By default
3225 * it tries to report the error position as h1m->err_pos. However if this one is
3226 * not set, it will then report h1m->next, which is the last known parsing
3227 * point. The function is able to deal with wrapping buffers. It always displays
3228 * buffers as a contiguous area starting at buf->p. The direction is determined
3229 * thanks to the h1m's flags.
3230 */
3231static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3232 struct h1m *h1m, struct buffer *buf)
3233{
3234 struct session *sess = fstrm->sess;
3235 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003236 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003237 union error_snapshot_ctx ctx;
3238
Willy Tarreauea27f482022-05-18 16:10:52 +02003239 if (fcgi_strm_sc(fstrm) && sc_strm(fcgi_strm_sc(fstrm))) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003240 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02003241 sess = __sc_strm(fcgi_strm_sc(fstrm))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003242 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02003243 other_end = __sc_strm(fcgi_strm_sc(fstrm))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003244 else
3245 other_end = sess->fe;
3246 } else
3247 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003248 /* http-specific part now */
3249 ctx.h1.state = h1m->state;
3250 ctx.h1.c_flags = fconn->flags;
3251 ctx.h1.s_flags = fstrm->flags;
3252 ctx.h1.m_flags = h1m->flags;
3253 ctx.h1.m_clen = h1m->curr_len;
3254 ctx.h1.m_blen = h1m->body_len;
3255
3256 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3257 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3258 &ctx, fcgi_show_error_snapshot);
3259}
3260
3261static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3262 struct buffer *buf, size_t *ofs, size_t max)
3263{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003264 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003265
Willy Tarreau022e5e52020-09-10 09:33:15 +02003266 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 +02003267 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003268 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003269 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 +02003270 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003271 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 +02003272 fcgi_strm_error(fstrm);
3273 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3274 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003275 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003276 goto end;
3277 }
3278
Christopher Fauletda3adeb2021-09-28 09:50:07 +02003279 /* Reject any message with an unknown transfer-encoding. In fact if any
3280 * encoding other than "chunked". A 422-Unprocessable-Content is
3281 * returned for an invalid request, a 502-Bad-Gateway for an invalid
3282 * response.
3283 */
3284 if (h1m->flags & H1_MF_TE_OTHER) {
3285 htx->flags |= HTX_FL_PARSING_ERROR;
3286 TRACE_ERROR("Unknown transfer-encoding", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3287 fcgi_strm_error(fstrm);
3288 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3289 ret = 0;
3290 goto end;
3291 }
3292
Christopher Faulet99eff652019-08-11 23:11:30 +02003293 *ofs += ret;
3294 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003295 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 +02003296 return ret;
3297
3298}
3299
Christopher Fauletaf542632019-10-01 21:52:49 +02003300static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003301 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3302{
Christopher Fauletde471a42021-02-01 16:37:28 +01003303 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003304
Willy Tarreau022e5e52020-09-10 09:33:15 +02003305 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 +02003306 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003307 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003308 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 +02003309 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003310 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 +02003311 fcgi_strm_error(fstrm);
3312 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3313 }
3314 goto end;
3315 }
3316 *ofs += ret;
3317 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003318 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 +02003319 return ret;
3320}
3321
3322static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3323 struct buffer *buf, size_t *ofs, size_t max)
3324{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003325 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003326
Willy Tarreau022e5e52020-09-10 09:33:15 +02003327 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 +02003328 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003329 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003330 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 +02003331 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003332 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 +02003333 fcgi_strm_error(fstrm);
3334 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3335 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003336 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003337 goto end;
3338 }
3339 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003340 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003341 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 +02003342 return ret;
3343}
3344
Christopher Faulet99eff652019-08-11 23:11:30 +02003345static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3346{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003347 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003348 struct htx *htx;
3349 struct h1m *h1m = &fstrm->h1m;
3350 size_t ret, data, total = 0;
3351
3352 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003353 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3354
Christopher Faulet99eff652019-08-11 23:11:30 +02003355 data = htx->data;
3356 if (fstrm->state == FCGI_SS_ERROR)
3357 goto end;
3358
3359 do {
3360 size_t used = htx_used_space(htx);
3361
3362 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003363 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003364 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3365 if (!ret)
3366 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003367
3368 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3369
Christopher Faulet99eff652019-08-11 23:11:30 +02003370 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3371 struct htx_blk *blk = htx_get_head_blk(htx);
3372 struct htx_sl *sl;
3373
3374 if (!blk)
3375 break;
3376 sl = htx_get_blk_ptr(htx, blk);
3377 sl->flags |= HTX_SL_F_XFER_LEN;
3378 htx->extra = 0;
3379 }
3380 }
3381 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003382 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003383 fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003384
3385 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3386 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3387 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet2db904e2022-05-05 09:24:52 +02003388 if (htx_is_empty(htx) && !htx_add_endof(htx, HTX_BLK_EOT))
3389 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003390 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003391 h1m->state = H1_MSG_DONE;
3392 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3393 }
3394
Christopher Faulet16a524c2021-02-02 21:16:03 +01003395 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003396 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003397
3398 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 +02003399 }
3400 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003401 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003402 fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01003403 if (h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003404 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003405
Christopher Faulet76014fd2019-12-10 11:47:22 +01003406 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 +02003407 }
3408 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003409 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 +02003410 if (b_data(&fstrm->rxbuf) > total) {
3411 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003412 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003413 fcgi_strm_error(fstrm);
3414 }
3415 break;
3416 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003417 else {
3418 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003419 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003420 fcgi_strm_error(fstrm);
3421 break;
3422 }
3423
3424 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003425 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003426
3427 if (fstrm->state == FCGI_SS_ERROR) {
3428 b_reset(&fstrm->rxbuf);
3429 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003430 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 return 0;
3432 }
3433
3434 b_del(&fstrm->rxbuf, total);
3435
3436 end:
3437 htx_to_buf(htx, buf);
3438 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003439 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003440 return ret;
3441}
3442
3443/*
3444 * Attach a new stream to a connection
3445 * (Used for outgoing connections)
3446 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003447static int fcgi_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02003448{
Christopher Faulet99eff652019-08-11 23:11:30 +02003449 struct fcgi_strm *fstrm;
3450 struct fcgi_conn *fconn = conn->ctx;
3451
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003452 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003453 fstrm = fcgi_stconn_new(fconn, sd->sc, sess);
Christopher Faulete00ad352021-12-16 14:44:31 +01003454 if (!fstrm)
Christopher Faulet73518be2021-01-27 12:06:54 +01003455 goto err;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003456
3457 /* the connection is not idle anymore, let's mark this */
3458 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003459 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003460
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003461 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulete00ad352021-12-16 14:44:31 +01003462 return 0;
Christopher Faulet73518be2021-01-27 12:06:54 +01003463
3464 err:
3465 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01003466 return -1;
Christopher Faulet99eff652019-08-11 23:11:30 +02003467}
3468
Willy Tarreau4596fe22022-05-17 19:07:51 +02003469/* Retrieves the first valid stream connector from this connection, or returns NULL.
Christopher Faulet99eff652019-08-11 23:11:30 +02003470 * We have to scan because we may have some orphan streams. It might be
3471 * beneficial to scan backwards from the end to reduce the likeliness to find
3472 * orphans.
3473 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003474static struct stconn *fcgi_get_first_sc(const struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02003475{
3476 struct fcgi_conn *fconn = conn->ctx;
3477 struct fcgi_strm *fstrm;
3478 struct eb32_node *node;
3479
3480 node = eb32_first(&fconn->streams_by_id);
3481 while (node) {
3482 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau77534272022-05-18 07:34:16 +02003483 if (fcgi_strm_sc(fstrm))
3484 return fcgi_strm_sc(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003485 node = eb32_next(node);
3486 }
3487 return NULL;
3488}
3489
3490/*
3491 * Destroy the mux and the associated connection, if it is no longer used
3492 */
3493static void fcgi_destroy(void *ctx)
3494{
3495 struct fcgi_conn *fconn = ctx;
3496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003497 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003498 if (eb_is_empty(&fconn->streams_by_id)) {
3499 BUG_ON(fconn->conn->ctx != fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003500 fcgi_release(fconn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003501 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003502}
3503
3504/*
3505 * Detach the stream from the connection and possibly release the connection.
3506 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003507static void fcgi_detach(struct sedesc *sd)
Christopher Faulet99eff652019-08-11 23:11:30 +02003508{
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003509 struct fcgi_strm *fstrm = sd->se;
Christopher Faulet99eff652019-08-11 23:11:30 +02003510 struct fcgi_conn *fconn;
3511 struct session *sess;
3512
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003513 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3514
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003515 if (!fstrm) {
3516 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003517 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003518 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003519
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003520 /* there's no txbuf so we're certain no to be able to send anything */
3521 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003522
3523 sess = fstrm->sess;
3524 fconn = fstrm->fconn;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003525 fconn->nb_sc--;
Christopher Faulet99eff652019-08-11 23:11:30 +02003526
3527 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3528 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3529 fconn->streams_limit = 1;
3530 }
3531 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3532 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3533 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3534 fconn->state = FCGI_CS_CLOSED;
3535 }
3536
3537 /* this stream may be blocked waiting for some data to leave, so orphan
3538 * it in this case.
3539 */
Christopher Fauletab79b322022-10-12 17:51:51 +02003540 if (!(fconn->flags & (FCGI_CF_ERR_PENDING|FCGI_CF_ERROR)) && // FIXME: Be sure for ERR_PENDING
Christopher Faulet99eff652019-08-11 23:11:30 +02003541 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003542 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003543 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003544 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003545 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003546 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003547
3548 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3549 /* unblock the connection if it was blocked on this stream. */
3550 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3551 fcgi_conn_restart_reading(fconn, 1);
3552 }
3553
3554 fcgi_strm_destroy(fstrm);
3555
Christopher Fauletab79b322022-10-12 17:51:51 +02003556 if (!(fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERR_PENDING|FCGI_CF_ERROR)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003557 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003558 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003559 /* Add the connection in the session serverlist, if not already done */
3560 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3561 fconn->conn->owner = NULL;
3562 if (eb_is_empty(&fconn->streams_by_id)) {
3563 /* let's kill the connection right away */
3564 fconn->conn->mux->destroy(fconn);
3565 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3566 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003567 }
3568 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003569 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003570 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3571 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003572 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003573 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003574 }
3575 }
3576 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003577 else {
3578 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003579 /* If the connection is owned by the session, first remove it
3580 * from its list
3581 */
3582 if (fconn->conn->owner) {
3583 session_unown_conn(fconn->conn->owner, fconn->conn);
3584 fconn->conn->owner = NULL;
3585 }
3586
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003587 /* mark that the tasklet may lose its context to another thread and
3588 * that the handler needs to check it under the idle conns lock.
3589 */
3590 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003591 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3592
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003593 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003594 /* The server doesn't want it, let's kill the connection right away */
3595 fconn->conn->mux->destroy(fconn);
3596 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3597 return;
3598 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003599 /* At this point, the connection has been added to the
3600 * server idle list, so another thread may already have
3601 * hijacked it, so we can't do anything with it.
3602 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003603 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3604 return;
3605 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003606 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003607 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Amaury Denoyelle926a5612024-03-14 11:24:10 +01003608 !LIST_INLIST(&fconn->conn->sess_el)) {
Willy Tarreau85223482022-09-29 20:32:43 +02003609 eb64_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
3610 &fconn->conn->hash_node->node);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003611 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003612 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003613 }
3614
3615 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003616 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003617 */
3618 if (fcgi_conn_is_dead(fconn)) {
3619 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003620 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003621 fcgi_release(fconn);
3622 }
3623 else if (fconn->task) {
3624 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3625 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003626 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003628 else
3629 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003630}
3631
3632
3633/* Performs a synchronous or asynchronous shutr(). */
3634static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3635{
3636 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003637
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003638 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3639
Christopher Faulet99eff652019-08-11 23:11:30 +02003640 if (fstrm->state == FCGI_SS_CLOSED)
3641 goto done;
3642
3643 /* a connstream may require us to immediately kill the whole connection
3644 * for example because of a "tcp-request content reject" rule that is
3645 * normally used to limit abuse.
3646 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003647 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003648 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3649 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003650 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003651 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003653 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3655 !fcgi_strm_send_abort(fconn, fstrm))
3656 goto add_to_list;
3657 }
3658
3659 fcgi_strm_close(fstrm);
3660
3661 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3662 tasklet_wakeup(fconn->wait_event.tasklet);
3663 done:
3664 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003665 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003666 return;
3667
3668 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003669 /* Let the handler know we want to shutr, and add ourselves to the
3670 * send list if not yet done. fcgi_deferred_shut() will be
3671 * automatically called via the shut_tl tasklet when there's room
3672 * again.
3673 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003674 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003675 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003676 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 }
3678 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003679 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003680 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003681 return;
3682}
3683
3684/* Performs a synchronous or asynchronous shutw(). */
3685static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3686{
3687 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003688
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003689 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3690
Christopher Faulet99eff652019-08-11 23:11:30 +02003691 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3692 goto done;
3693
3694 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3695 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3696 !fcgi_strm_send_abort(fconn, fstrm))
3697 goto add_to_list;
3698
3699 if (fstrm->state == FCGI_SS_HREM)
3700 fcgi_strm_close(fstrm);
3701 else
3702 fstrm->state = FCGI_SS_HLOC;
3703 } else {
3704 /* a connstream may require us to immediately kill the whole connection
3705 * for example because of a "tcp-request content reject" rule that is
3706 * normally used to limit abuse.
3707 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003708 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003709 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3710 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003711 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003712 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003713
3714 fcgi_strm_close(fstrm);
3715 }
3716
3717 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3718 tasklet_wakeup(fconn->wait_event.tasklet);
3719 done:
3720 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003721 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003722 return;
3723
3724 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003725 /* Let the handler know we want to shutr, and add ourselves to the
3726 * send list if not yet done. fcgi_deferred_shut() will be
3727 * automatically called via the shut_tl tasklet when there's room
3728 * again.
3729 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003730 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003731 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003732 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003733 }
3734 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003735 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003736 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003737 return;
3738}
3739
Willy Tarreau7aad7032020-01-16 17:20:57 +01003740/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003741 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003742 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003743 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003744struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003745{
3746 struct fcgi_strm *fstrm = ctx;
3747 struct fcgi_conn *fconn = fstrm->fconn;
3748
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003749 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3750
Willy Tarreau7aad7032020-01-16 17:20:57 +01003751 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3752 /* some data processing remains to be done first */
3753 goto end;
3754 }
3755
Christopher Faulet99eff652019-08-11 23:11:30 +02003756 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3757 fcgi_do_shutw(fstrm);
3758
3759 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3760 fcgi_do_shutr(fstrm);
3761
3762 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3763 /* We're done trying to send, remove ourself from the send_list */
3764 LIST_DEL_INIT(&fstrm->send_list);
3765
Willy Tarreau77534272022-05-18 07:34:16 +02003766 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003767 fcgi_strm_destroy(fstrm);
3768 if (fcgi_conn_is_dead(fconn))
3769 fcgi_release(fconn);
3770 }
3771 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003772 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003773 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003774 return NULL;
3775}
3776
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05003777/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003778static void fcgi_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003779{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003780 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003781
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003782 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003783 if (!mode)
3784 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003785 fcgi_do_shutr(fstrm);
3786}
3787
Willy Tarreau4596fe22022-05-17 19:07:51 +02003788/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003789static void fcgi_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003790{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003791 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003792
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003793 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003794 fcgi_do_shutw(fstrm);
3795}
3796
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003797/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3798 * event subscriber <es> is not allowed to change from a previous call as long
3799 * as at least one event is still subscribed. The <event_type> must only be a
3800 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003801 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003802static int fcgi_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003803{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003804 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003805 struct fcgi_conn *fconn = fstrm->fconn;
3806
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003807 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003808 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003809
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003810 es->events |= event_type;
3811 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003812
3813 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003814 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003815
Christopher Faulet99eff652019-08-11 23:11:30 +02003816 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003817 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003818 if (!LIST_INLIST(&fstrm->send_list))
3819 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003820 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003821 return 0;
3822}
3823
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003824/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3825 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3826 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003827 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003828static int fcgi_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003829{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003830 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003831 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003832
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003833 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003834 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003835
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003836 es->events &= ~event_type;
3837 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003838 fstrm->subs = NULL;
3839
3840 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003841 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003842
Christopher Faulet99eff652019-08-11 23:11:30 +02003843 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003844 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003845 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003846 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3847 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003848 }
3849 return 0;
3850}
3851
Christopher Faulet564e39c2021-09-21 15:50:55 +02003852/* Called from the upper layer, to receive data
3853 *
3854 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3855 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3856 * means the caller wants to flush input data (from the mux buffer and the
3857 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3858 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3859 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3860 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3861 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3862 * copy as much data as possible.
3863 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003864static size_t fcgi_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003865{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003866 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003867 struct fcgi_conn *fconn = fstrm->fconn;
3868 size_t ret = 0;
3869
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003870 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3871
Christopher Faulet99eff652019-08-11 23:11:30 +02003872 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3873 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003874 else
3875 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003876
Christopher Fauletefebff32023-05-11 11:33:05 +02003877 if (b_data(&fstrm->rxbuf)) {
3878 /* If the channel buffer is not empty, consider the mux is
3879 * blocked because it needs more room. But if the channel buffer
3880 * is empty, it means partial data were received and the mux
3881 * needs to receive more data to be able to parse it.
3882 */
3883 if (b_data(buf))
3884 se_fl_set(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
3885 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003886 else {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003887 se_fl_clr(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003888 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003889 se_fl_set(fstrm->sd, SE_FL_EOI);
Christopher Faulet99eff652019-08-11 23:11:30 +02003890 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003891 se_fl_set(fstrm->sd, SE_FL_EOS);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 }
Christopher Fauleta272c392023-05-11 11:16:59 +02003893 if (fcgi_conn_read0_pending(fconn)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003894 se_fl_set(fstrm->sd, SE_FL_EOS);
Christopher Fauleta272c392023-05-11 11:16:59 +02003895 if (!se_fl_test(fstrm->sd, SE_FL_EOI))
3896 se_fl_set(fstrm->sd, SE_FL_ERROR);
3897 }
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003898 if (se_fl_test(fstrm->sd, SE_FL_ERR_PENDING))
3899 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003900 fcgi_release_buf(fconn, &fstrm->rxbuf);
3901 }
3902
3903 if (ret && fconn->dsi == fstrm->id) {
3904 /* demux is blocking on this stream's buffer */
3905 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3906 fcgi_conn_restart_reading(fconn, 1);
3907 }
3908
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003909 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003910 return ret;
3911}
3912
3913
Christopher Faulet99eff652019-08-11 23:11:30 +02003914/* Called from the upper layer, to send data from buffer <buf> for no more than
3915 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02003916 * flags may be updated on the stream connector.
Christopher Faulet99eff652019-08-11 23:11:30 +02003917 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003918static size_t fcgi_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003919{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003920 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003921 struct fcgi_conn *fconn = fstrm->fconn;
3922 size_t total = 0;
3923 size_t ret;
3924 struct htx *htx = NULL;
3925 struct htx_sl *sl;
3926 struct htx_blk *blk;
3927 uint32_t bsize;
3928
Willy Tarreau022e5e52020-09-10 09:33:15 +02003929 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003930
Christopher Faulet99eff652019-08-11 23:11:30 +02003931 /* If we were not just woken because we wanted to send but couldn't,
3932 * and there's somebody else that is waiting to send, do nothing,
3933 * we will subscribe later and be put at the end of the list
3934 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003935 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003936 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 +02003937 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003938 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003939 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003940
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003941 if (fconn->state < FCGI_CS_RECORD_H) {
3942 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003943 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003944 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003945
3946 htx = htxbuf(buf);
3947 if (fstrm->id == 0) {
3948 int32_t id = fcgi_conn_get_next_sid(fconn);
3949
3950 if (id < 0) {
3951 fcgi_strm_close(fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003952 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953 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 +02003954 return 0;
3955 }
3956
3957 eb32_delete(&fstrm->by_id);
3958 fstrm->by_id.key = fstrm->id = id;
3959 fconn->max_id = id;
3960 fconn->nb_reserved--;
3961 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3962
3963
3964 /* Check if length of the body is known or if the message is
3965 * full. Otherwise, the request is invalid.
3966 */
3967 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003968 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003969 htx->flags |= HTX_FL_PARSING_ERROR;
3970 fcgi_strm_error(fstrm);
3971 goto done;
3972 }
3973 }
3974
3975 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 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 +02003977 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3978 goto done;
3979 }
3980
3981 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3982 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3983
Christopher Fauletfe410d62020-05-19 15:13:00 +02003984 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003985 count && !htx_is_empty(htx)) {
3986 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003987 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 bsize = htx_get_blksz(blk);
3989
3990 switch (htx_get_blk_type(blk)) {
3991 case HTX_BLK_REQ_SL:
3992 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993 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 +02003994 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3995 if (!ret) {
3996 goto done;
3997 }
3998 total += ret;
3999 count -= ret;
4000 break;
4001
4002 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004003 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4004 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4005 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4006 if (!ret)
4007 goto done;
4008 }
4009 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4010 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4011 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4012 if (!ret)
4013 goto done;
4014 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004015 goto remove_blk;
4016
4017 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004018 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 +02004019 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4020 if (ret > 0) {
4021 htx = htx_from_buf(buf);
4022 total += ret;
4023 count -= ret;
4024 if (ret < bsize)
4025 goto done;
4026 }
4027 break;
4028
Christopher Faulet2b85a642024-03-25 08:02:06 +01004029 case HTX_BLK_EOT:
4030 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4031 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4032 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4033 if (!ret)
4034 goto done;
4035 }
4036 __fallthrough;
4037
Christopher Faulet99eff652019-08-11 23:11:30 +02004038 default:
4039 remove_blk:
4040 htx_remove_blk(htx, blk);
4041 total += bsize;
4042 count -= bsize;
4043 break;
4044 }
4045 }
4046
4047 done:
4048 if (fstrm->state >= FCGI_SS_HLOC) {
4049 /* trim any possibly pending data after we close (extra CR-LF,
4050 * unprocessed trailers, abnormal extra data, ...)
4051 */
4052 total += count;
4053 count = 0;
4054 }
4055
4056 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004057 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 +02004058 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +02004059 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4060 fcgi_strm_close(fstrm);
4061 }
4062
4063 if (htx)
4064 htx_to_buf(htx, buf);
4065
Christopher Faulet99eff652019-08-11 23:11:30 +02004066 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004067 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4068 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 +02004069 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004070 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004071
4072 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004073 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4074 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004075 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004076
4077 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004078 return total;
4079}
4080
4081/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004082static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004083{
4084 struct fcgi_conn *fconn = conn->ctx;
4085 struct fcgi_strm *fstrm = NULL;
4086 struct eb32_node *node;
4087 int send_cnt = 0;
4088 int tree_cnt = 0;
4089 int orph_cnt = 0;
4090 struct buffer *hmbuf, *tmbuf;
4091
4092 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004093 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004094
4095 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4096 send_cnt++;
4097
4098 fstrm = NULL;
4099 node = eb32_first(&fconn->streams_by_id);
4100 while (node) {
4101 fstrm = container_of(node, struct fcgi_strm, by_id);
4102 tree_cnt++;
Willy Tarreau77534272022-05-18 07:34:16 +02004103 if (!fcgi_strm_sc(fstrm))
Christopher Faulet99eff652019-08-11 23:11:30 +02004104 orph_cnt++;
4105 node = eb32_next(node);
4106 }
4107
4108 hmbuf = br_head(fconn->mbuf);
4109 tmbuf = br_tail(fconn->mbuf);
4110 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4111 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4112 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4113 fconn->state, fconn->max_id, fconn->flags,
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004114 fconn->nb_streams, fconn->nb_sc, send_cnt, tree_cnt, orph_cnt,
Christopher Faulet99eff652019-08-11 23:11:30 +02004115 fconn->wait_event.events, fconn->dsi,
4116 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4117 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4118 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4119 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4120 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4121 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4122 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4123
4124 if (fstrm) {
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004125 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 +02004126 fstrm, fstrm->id, fstrm->flags,
4127 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4128 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
Willy Tarreau77534272022-05-18 07:34:16 +02004129 fcgi_strm_sc(fstrm));
Christopher Faulet186367f2022-05-30 08:45:15 +02004130
4131 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(fstrm->sd));
4132 if (!se_fl_test(fstrm->sd, SE_FL_ORPHAN))
4133 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
4134 fcgi_strm_sc(fstrm)->flags, fcgi_strm_sc(fstrm)->app);
4135
Willy Tarreau41054612022-09-02 14:22:38 +02004136 chunk_appendf(msg, " .subs=%p", fstrm->subs);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004137 if (fstrm->subs) {
Willy Tarreau41054612022-09-02 14:22:38 +02004138 chunk_appendf(msg, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4139 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004140 fstrm->subs->tasklet->calls,
4141 fstrm->subs->tasklet->context);
Willy Tarreau41054612022-09-02 14:22:38 +02004142 resolve_sym_name(msg, NULL, fstrm->subs->tasklet->process);
4143 chunk_appendf(msg, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004144 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004145 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004146 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004147}
4148
4149/* Migrate the the connection to the current thread.
4150 * Return 0 if successful, non-zero otherwise.
4151 * Expected to be called with the old thread lock held.
4152 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004153static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004154{
4155 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004156 struct task *task;
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004157 struct task *new_task;
4158 struct tasklet *new_tasklet;
4159
4160 /* Pre-allocate tasks so that we don't have to roll back after the xprt
4161 * has been migrated.
4162 */
4163 new_task = task_new_here();
4164 new_tasklet = tasklet_new();
4165 if (!new_task || !new_tasklet)
4166 goto fail;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004167
4168 if (fd_takeover(conn->handle.fd, conn) != 0)
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004169 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004170
4171 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4172 /* We failed to takeover the xprt, even if the connection may
4173 * still be valid, flag it as error'd, as we have already
4174 * taken over the fd, and wake the tasklet, so that it will
4175 * destroy it.
4176 */
4177 conn->flags |= CO_FL_ERROR;
4178 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004179 goto fail;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004180 }
4181
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004182 if (fcgi->wait_event.events)
4183 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4184 fcgi->wait_event.events, &fcgi->wait_event);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004185
4186 task = fcgi->task;
4187 if (task) {
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004188 /* only assign a task if there was already one, otherwise
4189 * the preallocated new task will be released.
4190 */
Willy Tarreau88d18f82020-07-01 16:39:33 +02004191 task->context = NULL;
4192 fcgi->task = NULL;
4193 __ha_barrier_store();
4194 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004195
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004196 fcgi->task = new_task;
4197 new_task = NULL;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004198 fcgi->task->process = fcgi_timeout_task;
4199 fcgi->task->context = fcgi;
4200 }
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004201
4202 /* To let the tasklet know it should free itself, and do nothing else,
4203 * set its context to NULL;
4204 */
4205 fcgi->wait_event.tasklet->context = NULL;
4206 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4207
4208 fcgi->wait_event.tasklet = new_tasklet;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004209 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4210 fcgi->wait_event.tasklet->context = fcgi;
4211 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4212 SUB_RETRY_RECV, &fcgi->wait_event);
4213
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004214 if (new_task)
4215 __task_free(new_task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004216 return 0;
Willy Tarreaue0c6ef42023-11-17 10:56:33 +01004217 fail:
4218 if (new_task)
4219 __task_free(new_task);
4220 tasklet_free(new_tasklet);
4221 return -1;
Christopher Faulet99eff652019-08-11 23:11:30 +02004222}
4223
4224/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004225/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004226/****************************************/
4227
4228/* The mux operations */
4229static const struct mux_ops mux_fcgi_ops = {
4230 .init = fcgi_init,
4231 .wake = fcgi_wake,
4232 .attach = fcgi_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004233 .get_first_sc = fcgi_get_first_sc,
Christopher Faulet99eff652019-08-11 23:11:30 +02004234 .detach = fcgi_detach,
4235 .destroy = fcgi_destroy,
4236 .avail_streams = fcgi_avail_streams,
4237 .used_streams = fcgi_used_streams,
4238 .rcv_buf = fcgi_rcv_buf,
4239 .snd_buf = fcgi_snd_buf,
4240 .subscribe = fcgi_subscribe,
4241 .unsubscribe = fcgi_unsubscribe,
4242 .shutr = fcgi_shutr,
4243 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004244 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004245 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004246 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004247 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004248 .name = "FCGI",
4249};
4250
4251
4252/* this mux registers FCGI proto */
4253static struct mux_proto_list mux_proto_fcgi =
4254{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4255
4256INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4257
4258/*
4259 * Local variables:
4260 * c-indent-level: 8
4261 * c-basic-offset: 8
4262 * End:
4263 */