blob: 0ae5e5e2ce105bc79a597a665af66a59a9a1a50f [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{
455 if (b_data(&fconn->dbuf) == 0 &&
456 (fconn->state == FCGI_CS_CLOSED ||
457 fconn->conn->flags & CO_FL_ERROR ||
458 conn_xprt_read0_pending(fconn->conn)))
459 return 0;
460
461 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
462 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
463 return 1;
464
465 return 0;
466}
467
468/* Restarts reading on the connection if it was not enabled */
469static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
470{
471 if (!fcgi_recv_allowed(fconn))
472 return;
473 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
474 (fconn->wait_event.events & SUB_RETRY_RECV))
475 return;
476 tasklet_wakeup(fconn->wait_event.tasklet);
477}
478
479
480/* Tries to grab a buffer and to re-enable processing on mux <target>. The
481 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
482 * the allocation succeeds, in which case the connection is woken up, or 0 if
483 * it's impossible to wake up and we prefer to be woken up later.
484 */
485static int fcgi_buf_available(void *target)
486{
487 struct fcgi_conn *fconn = target;
488 struct fcgi_strm *fstrm;
489
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100490 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200491 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200492 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
493 fcgi_conn_restart_reading(fconn, 1);
494 return 1;
495 }
496
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100497 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200498 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200499 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200500 if (fconn->flags & FCGI_CF_DEM_MROOM) {
501 fconn->flags &= ~FCGI_CF_DEM_MROOM;
502 fcgi_conn_restart_reading(fconn, 1);
503 }
504 return 1;
505 }
506
507 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
Willy Tarreau77534272022-05-18 07:34:16 +0200508 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fcgi_strm_sc(fstrm) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100509 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200510 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200511 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
512 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200513 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200514 return 1;
515 }
516
517 return 0;
518}
519
520static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
521{
522 struct buffer *buf = NULL;
523
Willy Tarreau2b718102021-04-21 07:32:39 +0200524 if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100525 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200526 fconn->buf_wait.target = fconn;
527 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200528 LIST_APPEND(&th_ctx->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200529 }
530 return buf;
531}
532
533static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
534{
535 if (bptr->size) {
536 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100537 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200538 }
539}
540
541static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
542{
543 struct buffer *buf;
544 unsigned int count = 0;
545
546 while (b_size(buf = br_head_pick(fconn->mbuf))) {
547 b_free(buf);
548 count++;
549 }
550 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100551 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200552}
553
554/* Returns the number of allocatable outgoing streams for the connection taking
555 * the number reserved streams into account.
556 */
557static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
558{
559 int ret;
560
561 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
562 if (ret < 0)
563 ret = 0;
564 return ret;
565}
566
567/* Returns the number of streams in use on a connection to figure if it's
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200568 * idle or not. We check nb_sc and not nb_streams as the caller will want
Christopher Faulet99eff652019-08-11 23:11:30 +0200569 * to know if it was the last one after a detach().
570 */
571static int fcgi_used_streams(struct connection *conn)
572{
573 struct fcgi_conn *fconn = conn->ctx;
574
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200575 return fconn->nb_sc;
Christopher Faulet99eff652019-08-11 23:11:30 +0200576}
577
578/* Returns the number of concurrent streams available on the connection */
579static int fcgi_avail_streams(struct connection *conn)
580{
581 struct server *srv = objt_server(conn->target);
582 struct fcgi_conn *fconn = conn->ctx;
583 int ret1, ret2;
584
585 /* Don't open new stream if the connection is closed */
586 if (fconn->state == FCGI_CS_CLOSED)
587 return 0;
588
589 /* May be negative if this setting has changed */
590 ret1 = (fconn->streams_limit - fconn->nb_streams);
591
592 /* we must also consider the limit imposed by stream IDs */
593 ret2 = fcgi_streams_left(fconn);
594 ret1 = MIN(ret1, ret2);
595 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
596 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
597 ret1 = MIN(ret1, ret2);
598 }
599 return ret1;
600}
601
602/*****************************************************************/
603/* functions below are dedicated to the mux setup and management */
604/*****************************************************************/
605
606/* Initializes the mux once it's attached. Only outgoing connections are
607 * supported. So the context is already initialized before installing the
608 * mux. <input> is always used as Input buffer and may contain data. It is the
609 * caller responsibility to not reuse it anymore. Returns < 0 on error.
610 */
611static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
612 struct buffer *input)
613{
614 struct fcgi_conn *fconn;
615 struct fcgi_strm *fstrm;
616 struct fcgi_app *app = get_px_fcgi_app(px);
617 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200618 void *conn_ctx = conn->ctx;
619
620 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200621
Christopher Faulet73518be2021-01-27 12:06:54 +0100622 if (!app) {
623 TRACE_ERROR("No FCGI app found, don't create fconn", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200624 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100625 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200626
627 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100628 if (!fconn) {
629 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200630 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100631 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200632
633 fconn->shut_timeout = fconn->timeout = px->timeout.server;
634 if (tick_isset(px->timeout.serverfin))
635 fconn->shut_timeout = px->timeout.serverfin;
636
637 fconn->flags = FCGI_CF_NONE;
638
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500639 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200640 if (app->flags & FCGI_APP_FL_KEEP_CONN)
641 fconn->flags |= FCGI_CF_KEEP_CONN;
642 if (app->flags & FCGI_APP_FL_GET_VALUES)
643 fconn->flags |= FCGI_CF_GET_VALUES;
644 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
645 fconn->flags |= FCGI_CF_MPXS_CONNS;
646
647 fconn->proxy = px;
648 fconn->app = app;
649 fconn->task = NULL;
650 if (tick_isset(fconn->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200651 t = task_new_here();
Christopher Faulet73518be2021-01-27 12:06:54 +0100652 if (!t) {
653 TRACE_ERROR("fconn task allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200654 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100655 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200656
657 fconn->task = t;
658 t->process = fcgi_timeout_task;
659 t->context = fconn;
660 t->expire = tick_add(now_ms, fconn->timeout);
661 }
662
663 fconn->wait_event.tasklet = tasklet_new();
664 if (!fconn->wait_event.tasklet)
665 goto fail;
666 fconn->wait_event.tasklet->process = fcgi_io_cb;
667 fconn->wait_event.tasklet->context = fconn;
668 fconn->wait_event.events = 0;
669
670 /* Initialise the context. */
671 fconn->state = FCGI_CS_INIT;
672 fconn->conn = conn;
673 fconn->streams_limit = app->maxreqs;
674 fconn->max_id = -1;
675 fconn->nb_streams = 0;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200676 fconn->nb_sc = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +0200677 fconn->nb_reserved = 0;
678 fconn->stream_cnt = 0;
679
680 fconn->dbuf = *input;
681 fconn->dsi = -1;
682
683 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
684 fconn->streams_by_id = EB_ROOT;
685 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100686 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200687
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200688 conn->ctx = fconn;
689
Christopher Faulet99eff652019-08-11 23:11:30 +0200690 if (t)
691 task_queue(t);
692
693 /* FIXME: this is temporary, for outgoing connections we need to
694 * immediately allocate a stream until the code is modified so that the
Willy Tarreauc92a6ca2022-05-27 10:38:10 +0200695 * caller calls ->attach(). For now the outgoing sc is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200696 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200697 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200698 fstrm = fcgi_stconn_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200699 if (!fstrm)
700 goto fail;
701
Christopher Faulet99eff652019-08-11 23:11:30 +0200702
703 /* Repare to read something */
704 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200705 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200706 return 0;
707
708 fail:
709 task_destroy(t);
710 if (fconn->wait_event.tasklet)
711 tasklet_free(fconn->wait_event.tasklet);
712 pool_free(pool_head_fcgi_conn, fconn);
713 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200714 conn->ctx = conn_ctx; // restore saved ctx
715 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200716 return -1;
717}
718
719/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
720 * -1 if no more is allocatable.
721 */
722static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
723{
724 int32_t id = (fconn->max_id + 1) | 1;
725
726 if ((id & 0x80000000U))
727 id = -1;
728 return id;
729}
730
731/* Returns the stream associated with id <id> or NULL if not found */
732static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
733{
734 struct eb32_node *node;
735
736 if (id == 0)
737 return (struct fcgi_strm *)fcgi_mgmt_stream;
738
739 if (id > fconn->max_id)
740 return (struct fcgi_strm *)fcgi_unknown_stream;
741
742 node = eb32_lookup(&fconn->streams_by_id, id);
743 if (!node)
744 return (struct fcgi_strm *)fcgi_unknown_stream;
745 return container_of(node, struct fcgi_strm, by_id);
746}
747
748
749/* Release function. This one should be called to free all resources allocated
750 * to the mux.
751 */
752static void fcgi_release(struct fcgi_conn *fconn)
753{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200754 struct connection *conn = fconn->conn;
Christopher Faulet99eff652019-08-11 23:11:30 +0200755
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200756 TRACE_POINT(FCGI_EV_FCONN_END);
757
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200758 if (LIST_INLIST(&fconn->buf_wait.list))
759 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200760
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200761 fcgi_release_buf(fconn, &fconn->dbuf);
762 fcgi_release_mbuf(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200763
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200764 if (fconn->task) {
765 fconn->task->context = NULL;
766 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
767 fconn->task = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200768 }
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200769 if (fconn->wait_event.tasklet)
770 tasklet_free(fconn->wait_event.tasklet);
771 if (conn && fconn->wait_event.events != 0)
772 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
773 &fconn->wait_event);
774
775 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200776
777 if (conn) {
778 conn->mux = NULL;
779 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200780 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200781
782 conn_stop_tracking(conn);
783 conn_full_close(conn);
784 if (conn->destroy_cb)
785 conn->destroy_cb(conn);
786 conn_free(conn);
787 }
788}
789
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200790/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
791 * pending on the connection AND if there is no more data in the demux
792 * buffer. The function returns 1 to report a read0 or 0 otherwise.
793 */
794static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
795{
796 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
797 return 1;
798 return 0;
799}
800
Christopher Faulet99eff652019-08-11 23:11:30 +0200801
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500802/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200803static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
804{
805 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
806 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
807 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
808 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
809 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
810 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
811 conn_xprt_read0_pending(fconn->conn))))
812 return 1;
813 return 0;
814}
815
816
817/********************************************************/
818/* functions below are for the FCGI protocol processing */
819/********************************************************/
820
Christopher Faulet99eff652019-08-11 23:11:30 +0200821/* Marks an error on the stream. */
822static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
823{
824 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200825 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
826 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200827 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200828 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
829 }
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200830 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200831 }
832}
833
834/* Attempts to notify the data layer of recv availability */
835static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
836{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100837 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200838 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100839 tasklet_wakeup(fstrm->subs->tasklet);
840 fstrm->subs->events &= ~SUB_RETRY_RECV;
841 if (!fstrm->subs->events)
842 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200843 }
844}
845
846/* Attempts to notify the data layer of send availability */
847static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
848{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100849 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200850 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100851 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100852 tasklet_wakeup(fstrm->subs->tasklet);
853 fstrm->subs->events &= ~SUB_RETRY_SEND;
854 if (!fstrm->subs->events)
855 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200856 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100857 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
858 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
859 tasklet_wakeup(fstrm->shut_tl);
860 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200861}
862
863/* Alerts the data layer, trying to wake it up by all means, following
864 * this sequence :
865 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
866 * for recv
867 * - if its subscribed to send, then it's woken up for send
868 * - if it was subscribed to neither, its ->wake() callback is called
869 * It is safe to call this function with a closed stream which doesn't have a
Willy Tarreau4596fe22022-05-17 19:07:51 +0200870 * stream connector anymore.
Christopher Faulet99eff652019-08-11 23:11:30 +0200871 */
872static void fcgi_strm_alert(struct fcgi_strm *fstrm)
873{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200874 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100875 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100876 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200877 fcgi_strm_notify_recv(fstrm);
878 fcgi_strm_notify_send(fstrm);
879 }
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200880 else if (fcgi_strm_sc(fstrm) && fcgi_strm_sc(fstrm)->app_ops->wake != NULL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200881 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200882 fcgi_strm_sc(fstrm)->app_ops->wake(fcgi_strm_sc(fstrm));
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200883 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200884}
885
886/* Writes the 16-bit record size <len> at address <record> */
887static inline void fcgi_set_record_size(void *record, uint16_t len)
888{
889 uint8_t *out = (record + 4);
890
891 *out = (len >> 8);
892 *(out + 1) = (len & 0xff);
893}
894
895/* Writes the 16-bit stream id <id> at address <record> */
896static inline void fcgi_set_record_id(void *record, uint16_t id)
897{
898 uint8_t *out = (record + 2);
899
900 *out = (id >> 8);
901 *(out + 1) = (id & 0xff);
902}
903
904/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
905 * its connection if the stream was not yet closed. Please use this exclusively
906 * before closing a stream to ensure stream count is well maintained.
907 */
908static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
909{
910 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200911 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200912 fstrm->fconn->nb_streams--;
913 if (!fstrm->id)
914 fstrm->fconn->nb_reserved--;
Willy Tarreau77534272022-05-18 07:34:16 +0200915 if (fcgi_strm_sc(fstrm)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200916 if (!se_fl_test(fstrm->sd, SE_FL_EOS) && !b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +0200917 fcgi_strm_notify_recv(fstrm);
918 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200919 fstrm->state = FCGI_SS_CLOSED;
920 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
921 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200922 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200923}
924
925/* Detaches a FCGI stream from its FCGI connection and releases it to the
926 * fcgi_strm pool.
927 */
928static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
929{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200930 struct connection *conn = fstrm->fconn->conn;
931
932 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
933
Christopher Faulet99eff652019-08-11 23:11:30 +0200934 fcgi_strm_close(fstrm);
935 eb32_delete(&fstrm->by_id);
936 if (b_size(&fstrm->rxbuf)) {
937 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100938 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200939 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100940 if (fstrm->subs)
941 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +0200942 /* There's no need to explicitly call unsubscribe here, the only
943 * reference left would be in the fconn send_list/fctl_list, and if
944 * we're in it, we're getting out anyway
945 */
946 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +0100947 tasklet_free(fstrm->shut_tl);
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200948 BUG_ON(fstrm->sd && !se_fl_test(fstrm->sd, SE_FL_ORPHAN));
949 sedesc_free(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +0200950 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200951
952 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200953}
954
955/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
956 * stream tree. In case of error, nothing is added and NULL is returned. The
957 * causes of errors can be any failed memory allocation. The caller is
958 * responsible for checking if the connection may support an extra stream prior
959 * to calling this function.
960 */
961static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
962{
963 struct fcgi_strm *fstrm;
964
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200965 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
966
Christopher Faulet99eff652019-08-11 23:11:30 +0200967 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +0100968 if (!fstrm) {
969 TRACE_ERROR("fstrm allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200970 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +0100971 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200972
Willy Tarreau7aad7032020-01-16 17:20:57 +0100973 fstrm->shut_tl = tasklet_new();
974 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +0100975 TRACE_ERROR("fstrm shut tasklet allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200976 pool_free(pool_head_fcgi_strm, fstrm);
977 goto out;
978 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100979 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +0100980 fstrm->shut_tl->process = fcgi_deferred_shut;
981 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +0200982 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200983 fstrm->fconn = fconn;
Willy Tarreau5aa5e772022-05-27 16:15:32 +0200984 fstrm->sd = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200985 fstrm->flags = FCGI_SF_NONE;
986 fstrm->proto_status = 0;
987 fstrm->state = FCGI_SS_IDLE;
988 fstrm->rxbuf = BUF_NULL;
989
990 h1m_init_res(&fstrm->h1m);
991 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
992 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
993
994 fstrm->by_id.key = fstrm->id = id;
995 if (id > 0)
996 fconn->max_id = id;
997 else
998 fconn->nb_reserved++;
999
1000 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1001 fconn->nb_streams++;
1002 fconn->stream_cnt++;
1003
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001004 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001005 return fstrm;
1006
1007 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001008 TRACE_DEVEL("leaving in error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001009 return NULL;
1010}
1011
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001012/* Allocates a new stream associated to stream connector <sc> on the FCGI connection
Christopher Faulet99eff652019-08-11 23:11:30 +02001013 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1014 * highest possible stream ID was reached.
1015 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001016static struct fcgi_strm *fcgi_stconn_new(struct fcgi_conn *fconn, struct stconn *sc,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001017 struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02001018{
1019 struct fcgi_strm *fstrm = NULL;
1020
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001021 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1022 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001023 TRACE_ERROR("streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001024 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001025 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001026
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001027 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001028 TRACE_ERROR("!streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001029 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001030 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001031
1032 /* Defer choosing the ID until we send the first message to create the stream */
1033 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001034 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001035 TRACE_ERROR("fstream allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001036 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001037 }
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001038 if (sc_attach_mux(sc, fstrm, fconn->conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +02001039 goto out;
Willy Tarreau5aa5e772022-05-27 16:15:32 +02001040 fstrm->sd = sc->sedesc;
Christopher Faulet99eff652019-08-11 23:11:30 +02001041 fstrm->sess = sess;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02001042 fconn->nb_sc++;
Christopher Faulet99eff652019-08-11 23:11:30 +02001043
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001044 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001045 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001046
1047 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001048 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet070b91b2022-03-31 19:27:18 +02001049 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001050 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001051}
1052
Willy Tarreau4596fe22022-05-17 19:07:51 +02001053/* Wakes a specific stream and assign its stream connector some SE_FL_* flags among
Willy Tarreaub605c422022-05-17 17:04:55 +02001054 * SE_FL_ERR_PENDING and SE_FL_ERROR if needed. The stream's state is
Christopher Faulet99eff652019-08-11 23:11:30 +02001055 * automatically updated accordingly. If the stream is orphaned, it is
1056 * destroyed.
1057 */
1058static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1059{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001060 struct fcgi_conn *fconn = fstrm->fconn;
1061
1062 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1063
Willy Tarreau77534272022-05-18 07:34:16 +02001064 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001065 /* this stream was already orphaned */
1066 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001067 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001068 return;
1069 }
1070
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001071 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001072 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001073 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001074 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001075 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001076 else if (fstrm->state == FCGI_SS_HLOC)
1077 fcgi_strm_close(fstrm);
1078 }
1079
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001080 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02001081 se_fl_set(fstrm->sd, SE_FL_ERR_PENDING);
1082 if (se_fl_test(fstrm->sd, SE_FL_EOS))
1083 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001084
1085 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001086 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001087 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1088 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001089 }
1090
1091 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001092
1093 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001094}
1095
1096/* Wakes unassigned streams (ID == 0) attached to the connection. */
1097static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1098{
1099 struct eb32_node *node;
1100 struct fcgi_strm *fstrm;
1101
1102 node = eb32_lookup(&fconn->streams_by_id, 0);
1103 while (node) {
1104 fstrm = container_of(node, struct fcgi_strm, by_id);
1105 if (fstrm->id > 0)
1106 break;
1107 node = eb32_next(node);
1108 fcgi_strm_wake_one_stream(fstrm);
1109 }
1110}
1111
1112/* Wakes the streams attached to the connection, whose id is greater than <last>
1113 * or unassigned.
1114 */
1115static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1116{
1117 struct eb32_node *node;
1118 struct fcgi_strm *fstrm;
1119
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001120 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1121
Christopher Faulet99eff652019-08-11 23:11:30 +02001122 /* Wake all streams with ID > last */
1123 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1124 while (node) {
1125 fstrm = container_of(node, struct fcgi_strm, by_id);
1126 node = eb32_next(node);
1127 fcgi_strm_wake_one_stream(fstrm);
1128 }
1129 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001130
1131 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001132}
1133
1134static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1135 struct htx *htx, struct htx_sl *sl,
1136 struct fcgi_strm_params *params)
1137{
1138 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
Willy Tarreau74568cf2022-05-27 09:03:30 +02001139 const struct sockaddr_storage *src = (sc_check(fcgi_strm_sc(fstrm)) ? conn_src(fconn->conn) : sc_src(sc_opposite(fcgi_strm_sc(fstrm))));
1140 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 +02001141 struct ist p;
1142
1143 if (!sl)
1144 goto error;
1145
1146 if (!(params->mask & FCGI_SP_DOC_ROOT))
1147 params->docroot = fconn->app->docroot;
1148
1149 if (!(params->mask & FCGI_SP_REQ_METH)) {
1150 p = htx_sl_req_meth(sl);
1151 params->meth = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001152 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 }
1154 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001155 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001156 params->uri = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001157 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001158 }
1159 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1160 p = htx_sl_req_vsn(sl);
1161 params->vsn = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001162 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001163 }
1164 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1165 char *end;
1166 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001167 if (dst)
1168 port = get_host_port(dst);
Christopher Faulet99eff652019-08-11 23:11:30 +02001169 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1170 if (!end)
1171 goto error;
1172 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1173 params->p->data += params->srv_port.len;
1174 }
1175 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1176 /* If no Host header found, use the server address to fill
1177 * srv_name */
1178 if (!istlen(params->srv_name)) {
1179 char *ptr = NULL;
1180
Christopher Faulet568008d2021-10-25 07:56:51 +02001181 if (dst)
1182 if (addr_to_str(dst, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001183 ptr = b_tail(params->p);
1184 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001185 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001186 params->p->data += params->srv_name.len;
1187 }
1188 }
1189 }
1190 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1191 char *ptr = NULL;
1192
Christopher Faulet568008d2021-10-25 07:56:51 +02001193 if (src)
1194 if (addr_to_str(src, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001195 ptr = b_tail(params->p);
1196 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001197 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001198 params->p->data += params->rem_addr.len;
1199 }
1200 }
1201 if (!(params->mask & FCGI_SP_REM_PORT)) {
1202 char *end;
1203 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001204 if (src)
1205 port = get_host_port(src);
Christopher Faulet99eff652019-08-11 23:11:30 +02001206 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1207 if (!end)
1208 goto error;
1209 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1210 params->p->data += params->rem_port.len;
1211 }
1212 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1213 struct htx_blk *blk;
1214 enum htx_blk_type type;
1215 char *end;
1216 size_t len = 0;
1217
1218 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1219 type = htx_get_blk_type(blk);
1220
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001221 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001222 break;
1223 if (type == HTX_BLK_DATA)
1224 len += htx_get_blksz(blk);
1225 }
1226 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1227 if (!end)
1228 goto error;
1229 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1230 params->p->data += params->cont_len.len;
1231 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001232
Christopher Faulet99eff652019-08-11 23:11:30 +02001233 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001234 if (cli_conn)
Willy Tarreau1057bee2021-10-06 11:38:44 +02001235 params->https = conn_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001236 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001237
Christopher Faulet99eff652019-08-11 23:11:30 +02001238 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1239 /* one of scriptname, pathinfo or query_string is no set */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001240 struct http_uri_parser parser = http_uri_parser_init(params->uri);
1241 struct ist path = http_parse_path(&parser);
Christopher Faulet99eff652019-08-11 23:11:30 +02001242 int len;
1243
Christopher Faulet99eff652019-08-11 23:11:30 +02001244 /* No scrit_name set but no valid path ==> error */
1245 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1246 goto error;
1247
Christopher Faulet99eff652019-08-11 23:11:30 +02001248 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001249 if (!(params->mask & FCGI_SP_REQ_QS)) {
1250 struct ist qs = istfind(path, '?');
1251
1252 /* Update the path length */
1253 path.len -= qs.len;
1254
1255 /* Set the query-string skipping the '?', if any */
1256 if (istlen(qs))
1257 params->qs = istnext(qs);
1258 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001259
1260 /* If the script_name is set, don't try to deduce the path_info
1261 * too. The opposite is not true.
1262 */
1263 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1264 params->mask |= FCGI_SP_PATH_INFO;
1265 goto end;
1266 }
1267
Christopher Faulet0f17a442020-07-23 15:44:37 +02001268 /* Decode the path. it must first be copied to keep the URI
1269 * untouched.
1270 */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001271 chunk_istcat(params->p, path);
Christopher Faulet0f17a442020-07-23 15:44:37 +02001272 path.ptr = b_tail(params->p) - path.len;
1273 len = url_decode(ist0(path), 0);
1274 if (len < 0)
1275 goto error;
1276 path.len = len;
1277
Christopher Faulet99eff652019-08-11 23:11:30 +02001278 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001279 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001280
1281 /* If there is no regex to match the pathinfo, just to the last
1282 * part and see if the index must be used.
1283 */
1284 if (!fconn->app->pathinfo_re)
1285 goto check_index;
1286
Christopher Faulet28cb3662020-02-14 14:47:37 +01001287 /* If some special characters are found in the decoded path (\n
Ilya Shipitsin01881082021-08-07 14:41:56 +05001288 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001289 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001290 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001291 * case.
1292 */
1293 if (istchr(path, '\n') || istchr(path, '\0'))
1294 goto error;
1295
Christopher Faulet99eff652019-08-11 23:11:30 +02001296 /* The regex does not match, just to the last part and see if
1297 * the index must be used.
1298 */
1299 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1300 goto check_index;
1301
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001302 /* We must have at least 1 capture for the script name,
1303 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001304 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001305 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001306 goto check_index;
1307
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001308 /* Finally we can set the script_name and the path_info. The
1309 * path_info is set if not already defined, and if it was
1310 * captured
1311 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001312 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001313 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1314 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001315
1316 check_index:
1317 len = params->scriptname.len;
1318 /* the script_name if finished by a '/' so we can add the index
1319 * part, if any.
1320 */
1321 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1322 struct ist sn = params->scriptname;
1323
1324 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001325 chunk_istcat(params->p, sn);
Tim Duesterhus77508502022-03-15 13:11:06 +01001326 chunk_istcat(params->p, fconn->app->index);
Christopher Faulet99eff652019-08-11 23:11:30 +02001327 }
1328 }
1329
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001330 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1331 params->srv_soft = ist2(b_tail(params->p), 0);
1332 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1333 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1334 }
1335
Christopher Faulet99eff652019-08-11 23:11:30 +02001336 end:
1337 return 1;
1338 error:
1339 return 0;
1340}
1341
1342static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1343 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1344{
1345 struct fcgi_param p;
1346
1347 if (params->mask & flag)
1348 return 1;
1349
1350 chunk_reset(&trash);
1351
1352 switch (flag) {
1353 case FCGI_SP_CGI_GATEWAY:
1354 p.n = ist("GATEWAY_INTERFACE");
1355 p.v = ist("CGI/1.1");
1356 goto encode;
1357 case FCGI_SP_DOC_ROOT:
1358 p.n = ist("DOCUMENT_ROOT");
1359 p.v = params->docroot;
1360 goto encode;
1361 case FCGI_SP_SCRIPT_NAME:
1362 p.n = ist("SCRIPT_NAME");
1363 p.v = params->scriptname;
1364 goto encode;
1365 case FCGI_SP_PATH_INFO:
1366 p.n = ist("PATH_INFO");
1367 p.v = params->pathinfo;
1368 goto encode;
1369 case FCGI_SP_REQ_URI:
1370 p.n = ist("REQUEST_URI");
1371 p.v = params->uri;
1372 goto encode;
1373 case FCGI_SP_REQ_METH:
1374 p.n = ist("REQUEST_METHOD");
1375 p.v = params->meth;
1376 goto encode;
1377 case FCGI_SP_REQ_QS:
1378 p.n = ist("QUERY_STRING");
1379 p.v = params->qs;
1380 goto encode;
1381 case FCGI_SP_SRV_NAME:
1382 p.n = ist("SERVER_NAME");
1383 p.v = params->srv_name;
1384 goto encode;
1385 case FCGI_SP_SRV_PORT:
1386 p.n = ist("SERVER_PORT");
1387 p.v = params->srv_port;
1388 goto encode;
1389 case FCGI_SP_SRV_PROTO:
1390 p.n = ist("SERVER_PROTOCOL");
1391 p.v = params->vsn;
1392 goto encode;
1393 case FCGI_SP_REM_ADDR:
1394 p.n = ist("REMOTE_ADDR");
1395 p.v = params->rem_addr;
1396 goto encode;
1397 case FCGI_SP_REM_PORT:
1398 p.n = ist("REMOTE_PORT");
1399 p.v = params->rem_port;
1400 goto encode;
1401 case FCGI_SP_SCRIPT_FILE:
1402 p.n = ist("SCRIPT_FILENAME");
Tim Duesterhus77508502022-03-15 13:11:06 +01001403 chunk_istcat(&trash, params->docroot);
1404 chunk_istcat(&trash, params->scriptname);
Christopher Faulet99eff652019-08-11 23:11:30 +02001405 p.v = ist2(b_head(&trash), b_data(&trash));
1406 goto encode;
1407 case FCGI_SP_PATH_TRANS:
1408 if (!istlen(params->pathinfo))
1409 goto skip;
1410 p.n = ist("PATH_TRANSLATED");
Tim Duesterhus77508502022-03-15 13:11:06 +01001411 chunk_istcat(&trash, params->docroot);
1412 chunk_istcat(&trash, params->pathinfo);
Christopher Faulet99eff652019-08-11 23:11:30 +02001413 p.v = ist2(b_head(&trash), b_data(&trash));
1414 goto encode;
1415 case FCGI_SP_CONT_LEN:
1416 p.n = ist("CONTENT_LENGTH");
1417 p.v = params->cont_len;
1418 goto encode;
1419 case FCGI_SP_HTTPS:
1420 if (!params->https)
1421 goto skip;
1422 p.n = ist("HTTPS");
1423 p.v = ist("on");
1424 goto encode;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001425 case FCGI_SP_SRV_SOFT:
1426 p.n = ist("SERVER_SOFTWARE");
1427 p.v = params->srv_soft;
1428 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001429 default:
1430 goto skip;
1431 }
1432
1433 encode:
1434 if (!istlen(p.v))
1435 goto skip;
1436 if (!fcgi_encode_param(outbuf, &p))
1437 return 0;
1438 skip:
1439 params->mask |= flag;
1440 return 1;
1441}
1442
1443/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1444 * anything. It is highly unexpected, but if the record is larger than a buffer
1445 * and cannot be encoded in one time, an error is triggered and the connection is
1446 * closed. GET_VALUES record cannot be split.
1447 */
1448static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1449{
1450 struct buffer outbuf;
1451 struct buffer *mbuf;
1452 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1453 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001454 int ret = 0;
1455
1456 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001457
1458 mbuf = br_tail(fconn->mbuf);
1459 retry:
1460 if (!fcgi_get_buf(fconn, mbuf)) {
1461 fconn->flags |= FCGI_CF_MUX_MALLOC;
1462 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001463 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1464 ret = 0;
1465 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001466 }
1467
1468 while (1) {
1469 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001470 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001471 break;
1472 realign_again:
1473 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1474 }
1475
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001476 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001477 goto full;
1478
1479 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1480 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001481 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1482 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001483
1484 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1485 * handled by HAProxy.
1486 */
1487 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1488 goto full;
1489
1490 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001491 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 +01001492 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001493 b_add(mbuf, outbuf.data);
1494 ret = 1;
1495
1496 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001497 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001498 return ret;
1499 full:
1500 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001501 if (!b_data(mbuf)) {
1502 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 +02001503 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001504 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001505
1506 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1507 goto retry;
1508 fconn->flags |= FCGI_CF_MUX_MFULL;
1509 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001510 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001511 ret = 0;
1512 goto end;
1513 fail:
1514 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001515 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1516 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1517 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001518}
1519
1520/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1521 * couldn't do anything. It is highly unexpected, but if the record is larger
1522 * than a buffer and cannot be decoded in one time, an error is triggered and
1523 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1524 */
1525static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1526{
1527 struct buffer inbuf;
1528 struct buffer *dbuf;
1529 size_t offset;
1530
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001531 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1532
Christopher Faulet99eff652019-08-11 23:11:30 +02001533 dbuf = &fconn->dbuf;
1534
1535 /* Record too large to be fully decoded */
1536 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1537 goto fail;
1538
1539 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001540 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1541 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001542 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001543 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001544
1545 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1546 /* Realign the dmux buffer if the record wraps. It is unexpected
1547 * at this stage because it should be the first record received
1548 * from the FCGI application.
1549 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01001550 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001551 }
1552
1553 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1554
1555 for (offset = 0; offset < b_data(&inbuf); ) {
1556 struct fcgi_param p;
1557 size_t ret;
1558
1559 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1560 if (!ret) {
1561 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001562 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 +02001563 goto fail;
1564 }
1565 offset += ret;
1566
1567 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001568 if (isteq(p.v, ist("1"))) {
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[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001570 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001571 }
1572 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001573 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 +02001574 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001575 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001576 }
1577 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1578 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001579 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 +02001580 }
1581 /*
1582 * Ignore all other params
1583 */
1584 }
1585
1586 /* Reset the number of concurrent streams supported if the FCGI
1587 * application does not support connection multiplexing
1588 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001589 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001590 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001591 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1592 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001593
1594 /* We must be sure to have read exactly the announced record length, no
1595 * more no less
1596 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001597 if (offset != fconn->drl) {
1598 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 +02001599 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001600 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001601
Willy Tarreau022e5e52020-09-10 09:33:15 +02001602 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 +02001603 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1604 fconn->drl = 0;
1605 fconn->drp = 0;
1606 fconn->state = FCGI_CS_RECORD_H;
1607 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001608 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1609 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001610 return 1;
1611 fail:
1612 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001613 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1614 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 +02001615 return 0;
1616}
1617
1618/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1619 * excluded, as the streams which already received the end-of-stream. It returns
1620 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1621 */
1622static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1623{
1624 struct eb32_node *node;
1625 struct fcgi_strm *fstrm;
1626
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001627 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1628
Christopher Faulet99eff652019-08-11 23:11:30 +02001629 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1630 while (node) {
1631 fstrm = container_of(node, struct fcgi_strm, by_id);
1632 node = eb32_next(node);
1633 if (fstrm->state != FCGI_SS_CLOSED &&
1634 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1635 !fcgi_strm_send_abort(fconn, fstrm))
1636 return 0;
1637 }
1638 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001639 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1640 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001641 return 1;
1642}
1643
1644/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1645 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1646 * space to proceed. It is small enough to be encoded in an empty buffer.
1647 */
1648static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1649{
1650 struct buffer outbuf;
1651 struct buffer *mbuf;
1652 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1653 int ret;
1654
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001655 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1656
Christopher Faulet99eff652019-08-11 23:11:30 +02001657 mbuf = br_tail(fconn->mbuf);
1658 retry:
1659 if (!fcgi_get_buf(fconn, mbuf)) {
1660 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001661 fstrm->flags |= FCGI_SF_BLK_MROOM;
1662 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1663 ret = 0;
1664 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001665 }
1666
1667 while (1) {
1668 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001669 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001670 break;
1671 realign_again:
1672 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1673 }
1674
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001675 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001676 goto full;
1677
1678 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1679 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001680 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001681 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001682 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001683
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001684 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1685 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001687 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001688 if (!fcgi_encode_begin_request(&outbuf, &rec))
1689 goto full;
1690
1691 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001692 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 +02001693 b_add(mbuf, outbuf.data);
1694 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1695 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001696 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001697 ret = 1;
1698
1699 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001700 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001701 return ret;
1702 full:
1703 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1704 goto retry;
1705 fconn->flags |= FCGI_CF_MUX_MFULL;
1706 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001707 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 +02001708 ret = 0;
1709 goto end;
1710}
1711
1712/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1713 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1714 * space to proceed. It is small enough to be encoded in an empty buffer.
1715 */
1716static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1717 enum fcgi_record_type rtype)
1718{
1719 struct buffer outbuf;
1720 struct buffer *mbuf;
1721 int ret;
1722
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001723 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001724 mbuf = br_tail(fconn->mbuf);
1725 retry:
1726 if (!fcgi_get_buf(fconn, mbuf)) {
1727 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001728 fstrm->flags |= FCGI_SF_BLK_MROOM;
1729 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1730 ret = 0;
1731 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001732 }
1733
1734 while (1) {
1735 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001736 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001737 break;
1738 realign_again:
1739 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1740 }
1741
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001742 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001743 goto full;
1744
1745 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1746 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001747 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001748 outbuf.area[1] = rtype;
1749 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001750 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001751
1752 /* commit the record */
1753 b_add(mbuf, outbuf.data);
1754 ret = 1;
1755
1756 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001757 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001758 return ret;
1759 full:
1760 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1761 goto retry;
1762 fconn->flags |= FCGI_CF_MUX_MFULL;
1763 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001764 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 +02001765 ret = 0;
1766 goto end;
1767}
1768
1769
1770/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1771 * marks the end of params.
1772 */
1773static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1774{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001775 int ret;
1776
1777 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1778 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001779 if (ret) {
1780 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001781 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 +01001782 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001783 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001784}
1785
1786/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1787 * marks the end of input. On success, all the request was successfully sent.
1788 */
1789static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1790{
1791 int ret;
1792
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001793 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001794 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001795 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001796 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001797 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 +02001798 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1799 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1800 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001801 return ret;
1802}
1803
1804/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1805 * stops the request processing.
1806 */
1807static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1808{
1809 int ret;
1810
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001811 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001812 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001813 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001814 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001815 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 +02001816 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1817 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1818 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001819 return ret;
1820}
1821
1822/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1823 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1824 * several records are sent. However, a K/V param cannot be split between 2
1825 * records.
1826 */
1827static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1828 struct htx *htx)
1829{
1830 struct buffer outbuf;
1831 struct buffer *mbuf;
1832 struct htx_blk *blk;
1833 struct htx_sl *sl = NULL;
1834 struct fcgi_strm_params params;
1835 size_t total = 0;
1836
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001837 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1838
Christopher Faulet99eff652019-08-11 23:11:30 +02001839 memset(&params, 0, sizeof(params));
1840 params.p = get_trash_chunk();
1841
1842 mbuf = br_tail(fconn->mbuf);
1843 retry:
1844 if (!fcgi_get_buf(fconn, mbuf)) {
1845 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001846 fstrm->flags |= FCGI_SF_BLK_MROOM;
1847 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1848 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001849 }
1850
1851 while (1) {
1852 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001853 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001854 break;
1855 realign_again:
1856 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1857 }
1858
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001859 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001860 goto full;
1861
1862 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1863 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001864 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001865 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001866 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001867
1868 blk = htx_get_head_blk(htx);
1869 while (blk) {
1870 enum htx_blk_type type;
1871 uint32_t size = htx_get_blksz(blk);
1872 struct fcgi_param p;
1873
1874 type = htx_get_blk_type(blk);
1875 switch (type) {
1876 case HTX_BLK_REQ_SL:
1877 sl = htx_get_blk_ptr(htx, blk);
1878 if (sl->info.req.meth == HTTP_METH_HEAD)
1879 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1880 if (sl->flags & HTX_SL_F_VER_11)
1881 fstrm->h1m.flags |= H1_MF_VER_11;
1882 break;
1883
1884 case HTX_BLK_HDR:
1885 p.n = htx_get_blk_name(htx, blk);
1886 p.v = htx_get_blk_value(htx, blk);
1887
1888 if (istmatch(p.n, ist(":fcgi-"))) {
Tim Duesterhusa6a32792022-03-05 00:52:45 +01001889 p.n = istadv(p.n, 6);
Christopher Faulet99eff652019-08-11 23:11:30 +02001890 if (isteq(p.n, ist("gateway_interface")))
1891 params.mask |= FCGI_SP_CGI_GATEWAY;
1892 else if (isteq(p.n, ist("document_root"))) {
1893 params.mask |= FCGI_SP_DOC_ROOT;
1894 params.docroot = p.v;
1895 }
1896 else if (isteq(p.n, ist("script_name"))) {
1897 params.mask |= FCGI_SP_SCRIPT_NAME;
1898 params.scriptname = p.v;
1899 }
1900 else if (isteq(p.n, ist("path_info"))) {
1901 params.mask |= FCGI_SP_PATH_INFO;
1902 params.pathinfo = p.v;
1903 }
1904 else if (isteq(p.n, ist("request_uri"))) {
1905 params.mask |= FCGI_SP_REQ_URI;
1906 params.uri = p.v;
1907 }
1908 else if (isteq(p.n, ist("request_meth")))
1909 params.mask |= FCGI_SP_REQ_METH;
1910 else if (isteq(p.n, ist("query_string")))
1911 params.mask |= FCGI_SP_REQ_QS;
1912 else if (isteq(p.n, ist("server_name")))
1913 params.mask |= FCGI_SP_SRV_NAME;
1914 else if (isteq(p.n, ist("server_port")))
1915 params.mask |= FCGI_SP_SRV_PORT;
1916 else if (isteq(p.n, ist("server_protocol")))
1917 params.mask |= FCGI_SP_SRV_PROTO;
1918 else if (isteq(p.n, ist("remote_addr")))
1919 params.mask |= FCGI_SP_REM_ADDR;
1920 else if (isteq(p.n, ist("remote_port")))
1921 params.mask |= FCGI_SP_REM_PORT;
1922 else if (isteq(p.n, ist("script_filename")))
1923 params.mask |= FCGI_SP_SCRIPT_FILE;
1924 else if (isteq(p.n, ist("path_translated")))
1925 params.mask |= FCGI_SP_PATH_TRANS;
1926 else if (isteq(p.n, ist("https")))
1927 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001928 else if (isteq(p.n, ist("server_software")))
1929 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02001930 }
1931 else if (isteq(p.n, ist("content-length"))) {
1932 p.n = ist("CONTENT_LENGTH");
1933 params.mask |= FCGI_SP_CONT_LEN;
1934 }
1935 else if (isteq(p.n, ist("content-type")))
1936 p.n = ist("CONTENT_TYPE");
1937 else {
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001938 struct ist n;
1939
Christopher Faulet99eff652019-08-11 23:11:30 +02001940 if (isteq(p.n, ist("host")))
1941 params.srv_name = p.v;
Christopher Fauletf56e8462021-09-28 10:56:36 +02001942 else if (isteq(p.n, ist("te"))) {
1943 /* "te" may only be sent with "trailers" if this value
1944 * is present, otherwise it must be deleted.
1945 */
1946 p.v = istist(p.v, ist("trailers"));
1947 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
1948 break;
1949 p.v = ist("trailers");
1950 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001951
Christopher Faulet67d58092019-10-02 10:51:38 +02001952 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001953 if (isttest(fconn->proxy->server_id_hdr_name) && isteq(p.n, fconn->proxy->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02001954 break;
1955
Tim Duesterhus98f05f62022-03-05 00:52:44 +01001956 n = ist2(trash.area, 0);
1957 istcat(&n, ist("http_"), trash.size);
1958 istcat(&n, p.n, trash.size);
1959 p.n = n;
Christopher Faulet99eff652019-08-11 23:11:30 +02001960 }
1961
1962 if (!fcgi_encode_param(&outbuf, &p)) {
1963 if (b_space_wraps(mbuf))
1964 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001965 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001966 goto full;
1967 goto done;
1968 }
1969 break;
1970
1971 case HTX_BLK_EOH:
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001972 if (isttest(fconn->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001973 struct server *srv = objt_server(fconn->conn->target);
1974
1975 if (!srv)
1976 goto done;
1977
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001978 p.n = ist2(trash.area, 0);
1979 istcat(&p.n, ist("http_"), trash.size);
1980 istcat(&p.n, fconn->proxy->server_id_hdr_name, trash.size);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001981 p.v = ist(srv->id);
1982
1983 if (!fcgi_encode_param(&outbuf, &p)) {
1984 if (b_space_wraps(mbuf))
1985 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001986 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001987 goto full;
1988 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001989 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001990 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001991 goto done;
1992
1993 default:
1994 break;
1995 }
1996 total += size;
1997 blk = htx_remove_blk(htx, blk);
1998 }
1999
2000 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002001 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2002 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002003 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002004 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002005
2006 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2007 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2008 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2009 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2010 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2011 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2012 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2013 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2014 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2015 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2016 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2017 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2018 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2019 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2020 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002021 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002022 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2023 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002024 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002025 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002026
2027 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002028 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});
2029 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002030 b_add(mbuf, outbuf.data);
2031
2032 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002033 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002034 return total;
2035 full:
2036 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2037 goto retry;
2038 fconn->flags |= FCGI_CF_MUX_MFULL;
2039 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002040 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 +02002041 if (total)
2042 goto error;
2043 goto end;
2044
2045 error:
2046 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002047 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 +02002048 fcgi_strm_error(fstrm);
2049 goto end;
2050}
2051
2052/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2053 * anything. STDIN records contain the request body.
2054 */
2055static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2056 struct htx *htx, size_t count, struct buffer *buf)
2057{
2058 struct buffer outbuf;
2059 struct buffer *mbuf;
2060 struct htx_blk *blk;
2061 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002062 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002063 size_t total = 0;
2064
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002065 extra_bytes = 0;
2066
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002067 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002068 if (!count)
2069 goto end;
2070
2071 mbuf = br_tail(fconn->mbuf);
2072 retry:
2073 if (!fcgi_get_buf(fconn, mbuf)) {
2074 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002075 fstrm->flags |= FCGI_SF_BLK_MROOM;
2076 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2077 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002078 }
2079
2080 /* Perform some optimizations to reduce the number of buffer copies.
2081 * First, if the mux's buffer is empty and the htx area contains exactly
2082 * one data block of the same size as the requested count, and this
2083 * count fits within the record size, then it's possible to simply swap
2084 * the caller's buffer with the mux's output buffer and adjust offsets
2085 * and length to match the entire DATA HTX block in the middle. In this
2086 * case we perform a true zero-copy operation from end-to-end. This is
2087 * the situation that happens all the time with large files. Second, if
2088 * this is not possible, but the mux's output buffer is empty, we still
2089 * have an opportunity to avoid the copy to the intermediary buffer, by
2090 * making the intermediary buffer's area point to the output buffer's
2091 * area. In this case we want to skip the HTX header to make sure that
2092 * copies remain aligned and that this operation remains possible all
2093 * the time. This goes for headers, data blocks and any data extracted
2094 * from the HTX blocks.
2095 */
2096 blk = htx_get_head_blk(htx);
2097 if (!blk)
2098 goto end;
2099 type = htx_get_blk_type(blk);
2100 size = htx_get_blksz(blk);
2101 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2102 void *old_area = mbuf->area;
2103
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002104 /* Last block of the message: Reserve the size for the empty stdin record */
2105 if (htx->flags & HTX_FL_EOM)
2106 extra_bytes = FCGI_RECORD_HEADER_SZ;
2107
Christopher Faulet99eff652019-08-11 23:11:30 +02002108 if (b_data(mbuf)) {
2109 /* Too bad there are data left there. We're willing to memcpy/memmove
2110 * up to 1/4 of the buffer, which means that it's OK to copy a large
2111 * record into a buffer containing few data if it needs to be realigned,
2112 * and that it's also OK to copy few data without realigning. Otherwise
2113 * we'll pretend the mbuf is full and wait for it to become empty.
2114 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002115 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002116 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002117 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002118 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002119 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002120 }
2121
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002122 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 +02002123 /* map a FCGI record to the HTX block so that we can put the
2124 * record header there.
2125 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002126 *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 +02002127 outbuf.area = b_head(mbuf);
2128
2129 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002130 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002131 fcgi_set_record_id(outbuf.area, fstrm->id);
2132 fcgi_set_record_size(outbuf.area, size);
2133
2134 /* and exchange with our old area */
2135 buf->area = old_area;
2136 buf->data = buf->head = 0;
2137 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002138
2139 htx = (struct htx *)buf->area;
2140 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002141 goto end;
2142 }
2143
2144 copy:
2145 while (1) {
2146 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002147 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002148 break;
2149 realign_again:
2150 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2151 }
2152
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002153 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002154 goto full;
2155
2156 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2157 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002158 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002159 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002160 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002161
2162 blk = htx_get_head_blk(htx);
2163 while (blk && count) {
2164 enum htx_blk_type type = htx_get_blk_type(blk);
2165 uint32_t size = htx_get_blksz(blk);
2166 struct ist v;
2167
2168 switch (type) {
2169 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002170 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 +02002171 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002172
2173 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2174 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2175
2176 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002177 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002178 extra_bytes = 0;
2179 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002180
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002181 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002182 /* It doesn't fit at once. If it at least fits once split and
2183 * the amount of data to move is low, let's defragment the
2184 * buffer now.
2185 */
2186 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002187 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002188 b_data(mbuf) <= MAX_DATA_REALIGN)
2189 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002190 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002191 }
2192 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002193 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002194 goto full;
2195 goto done;
2196 }
2197 if (v.len != size) {
2198 total += v.len;
2199 count -= v.len;
2200 htx_cut_data_blk(htx, blk, v.len);
2201 goto done;
2202 }
2203 break;
2204
Christopher Faulet99eff652019-08-11 23:11:30 +02002205 default:
2206 break;
2207 }
2208 total += size;
2209 count -= size;
2210 blk = htx_remove_blk(htx, blk);
2211 }
2212
2213 done:
2214 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002215 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});
2216 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002217 b_add(mbuf, outbuf.data);
2218
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002219 /* Send the empty stding here to finish the message */
2220 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2221 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2222 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2223 /* bytes already reserved for this record. It should not fail */
2224 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002225 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 +01002226 fcgi_strm_error(fstrm);
2227 }
2228 }
2229
Christopher Faulet99eff652019-08-11 23:11:30 +02002230 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002231 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002232 return total;
2233 full:
2234 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2235 goto retry;
2236 fconn->flags |= FCGI_CF_MUX_MFULL;
2237 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002238 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 +02002239 goto end;
2240}
2241
2242/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2243 * anything. STDOUT records contain the entire response. All the content is
2244 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2245 */
2246static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2247{
2248 struct buffer *dbuf;
2249 size_t ret;
2250 size_t max;
2251
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002252 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2253
Christopher Faulet99eff652019-08-11 23:11:30 +02002254 dbuf = &fconn->dbuf;
2255
2256 /* Only padding remains */
2257 if (fconn->state == FCGI_CS_RECORD_P)
2258 goto end_transfer;
2259
2260 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2261 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2262 buf_room_for_htx_data(dbuf))
2263 goto fail; // incomplete record
2264
2265 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2266 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002267 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2268 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002269 }
2270
2271 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2272 max = buf_room_for_htx_data(&fstrm->rxbuf);
2273 if (!b_data(&fstrm->rxbuf))
2274 fstrm->rxbuf.head = sizeof(struct htx);
2275 if (max > fconn->drl)
2276 max = fconn->drl;
2277
2278 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2279 if (!ret)
2280 goto fail;
2281 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002282 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2283 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 +02002284
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002285 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002286 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002287 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2288 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002289
2290 if (fconn->drl)
2291 goto fail;
2292
2293 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002294 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002295 fconn->drl += fconn->drp;
2296 fconn->drp = 0;
2297 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2298 b_del(&fconn->dbuf, ret);
2299 fconn->drl -= ret;
2300 if (fconn->drl)
2301 goto fail;
2302
2303 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002304 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2305 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002306 return 1;
2307 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002308 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 +02002309 return 0;
2310}
2311
2312
2313/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2314 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002315 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002316 */
2317static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2318{
2319 int ret;
2320
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002321 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2322
Christopher Faulet99eff652019-08-11 23:11:30 +02002323 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002324 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002325 fconn->drl += fconn->drp;
2326 fconn->drp = 0;
2327 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2328 b_del(&fconn->dbuf, ret);
2329 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 if (fconn->drl) {
2331 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 +02002332 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002333 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002334 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002335 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002336 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 +02002337 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);
2338 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002339 return 1;
2340}
2341
2342/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2343 * anything.
2344 */
2345static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2346{
2347 struct buffer *dbuf;
2348 struct buffer tag;
2349 size_t ret;
2350
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002351 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002352 dbuf = &fconn->dbuf;
2353
2354 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002355 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002356 goto end_transfer;
2357
2358 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2359 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2360 buf_room_for_htx_data(dbuf))
2361 goto fail; // incomplete record
2362
2363 chunk_reset(&trash);
2364 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2365 if (!ret)
2366 goto fail;
2367 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002368 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 +02002369
2370 trash.area[ret] = '\n';
2371 trash.area[ret+1] = '\0';
2372 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002373 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002374
2375 if (fconn->drl)
2376 goto fail;
2377
2378 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002379 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002380 fconn->drl += fconn->drp;
2381 fconn->drp = 0;
2382 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2383 b_del(&fconn->dbuf, ret);
2384 fconn->drl -= ret;
2385 if (fconn->drl)
2386 goto fail;
2387 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002388 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2389 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002390 return 1;
2391 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 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 +02002393 return 0;
2394}
2395
2396/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2397 * anything. If the empty STDOUT record is not already received, this one marks
2398 * the end of the response. It is highly unexpected, but if the record is larger
2399 * than a buffer and cannot be decoded in one time, an error is triggered and
2400 * the connection is closed. END_REQUEST record cannot be split.
2401 */
2402static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2403{
2404 struct buffer inbuf;
2405 struct buffer *dbuf;
2406 struct fcgi_end_request endreq;
2407
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002408 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002409 dbuf = &fconn->dbuf;
2410
2411 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002412 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2413 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 +02002414 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002415 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002416
2417 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002418 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2419 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002420 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002421 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002422
2423 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2424 /* Realign the dmux buffer if the record wraps. It is unexpected
2425 * at this stage because it should be the first record received
2426 * from the FCGI application.
2427 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002428 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002429 }
2430
2431 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2432
Christopher Faulet73518be2021-01-27 12:06:54 +01002433 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2434 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 +02002435 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002436 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002437
2438 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002439 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 +02002440 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 +02002441 fstrm->proto_status = endreq.errcode;
2442 fcgi_strm_close(fstrm);
2443
2444 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2445 fconn->drl = 0;
2446 fconn->drp = 0;
2447 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002448 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2449 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002450 return 1;
2451
2452 fail:
2453 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002454 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 +02002455 return 0;
2456}
2457
2458/* process Rx records to be demultiplexed */
2459static void fcgi_process_demux(struct fcgi_conn *fconn)
2460{
2461 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2462 struct fcgi_header hdr;
2463 int ret;
2464
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002465 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2466
Christopher Faulet99eff652019-08-11 23:11:30 +02002467 if (fconn->state == FCGI_CS_CLOSED)
2468 return;
2469
2470 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 if (fconn->state == FCGI_CS_INIT) {
2472 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2473 return;
2474 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002475 if (fconn->state == FCGI_CS_SETTINGS) {
2476 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002477 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002478 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002479 if (!ret) {
2480 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 +02002481 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002482 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002483 b_del(&fconn->dbuf, ret);
2484
2485 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2486 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002487 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 +02002488 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 +02002489 goto fail;
2490 }
2491 goto new_record;
2492 }
2493 }
2494
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002495 /* process as many incoming records as possible below */
2496 while (1) {
2497 if (!b_data(&fconn->dbuf)) {
2498 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2499 break;
2500 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002501
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002502 if (fconn->state == FCGI_CS_CLOSED) {
2503 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002504 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002505 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002506
2507 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002508 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002509 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2510 if (!ret)
2511 break;
2512 b_del(&fconn->dbuf, ret);
2513
2514 new_record:
2515 fconn->dsi = hdr.id;
2516 fconn->drt = hdr.type;
2517 fconn->drl = hdr.len;
2518 fconn->drp = hdr.padding;
2519 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002520 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 +02002521 }
2522
2523 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2524 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2525
Willy Tarreau77534272022-05-18 07:34:16 +02002526 if (tmp_fstrm != fstrm && fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002527 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002528 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002529 fstrm->state == FCGI_SS_CLOSED ||
2530 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002531 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002532 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002533 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 +02002534 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002535 fcgi_strm_notify_recv(fstrm);
2536 }
2537 fstrm = tmp_fstrm;
2538
2539 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2540 /* ignore all record for closed streams */
2541 goto ignore_record;
2542 }
2543 if (fstrm->state == FCGI_SS_IDLE) {
2544 /* ignore all record for unknown streams */
2545 goto ignore_record;
2546 }
2547
2548 switch (fconn->drt) {
2549 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002550 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 +02002551 ret = fcgi_conn_handle_values_result(fconn);
2552 break;
2553
2554 case FCGI_STDOUT:
2555 if (fstrm->flags & FCGI_SF_ES_RCVD)
2556 goto ignore_record;
2557
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002558 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002559 if (fconn->drl)
2560 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2561 else
2562 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2563 break;
2564
2565 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002566 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002567 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2568 break;
2569
2570 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002571 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 +02002572 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2573 break;
2574
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002575 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002576 default:
2577 ignore_record:
2578 /* drop records that we ignore. They may be
2579 * larger than the buffer so we drain all of
2580 * their contents until we reach the end.
2581 */
2582 fconn->state = FCGI_CS_RECORD_P;
2583 fconn->drl += fconn->drp;
2584 fconn->drp = 0;
2585 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002586 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002587 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002588 b_del(&fconn->dbuf, ret);
2589 fconn->drl -= ret;
2590 ret = (fconn->drl == 0);
2591 }
2592
2593 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002594 if (ret <= 0) {
2595 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002596 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002597 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002598
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002599 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002600 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2602 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002603 }
2604
2605 fail:
2606 /* we can go here on missing data, blocked response or error */
Willy Tarreau77534272022-05-18 07:34:16 +02002607 if (fstrm && fcgi_strm_sc(fstrm) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002608 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002609 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002610 fstrm->state == FCGI_SS_CLOSED ||
2611 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Willy Tarreau5aa5e772022-05-27 16:15:32 +02002612 se_fl_test(fstrm->sd, SE_FL_ERROR | SE_FL_ERR_PENDING | SE_FL_EOS))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002613 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002614 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 +02002615 se_fl_set(fstrm->sd, SE_FL_RCV_MORE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002616 fcgi_strm_notify_recv(fstrm);
2617 }
2618
2619 fcgi_conn_restart_reading(fconn, 0);
2620}
2621
2622/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2623 * the end.
2624 */
2625static int fcgi_process_mux(struct fcgi_conn *fconn)
2626{
2627 struct fcgi_strm *fstrm, *fstrm_back;
2628
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002629 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2630
Christopher Faulet99eff652019-08-11 23:11:30 +02002631 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2632 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2633 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2634 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002635 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 +02002636 fcgi_wake_unassigned_streams(fconn);
2637 goto mux;
2638 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002639 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002640 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2641 goto fail;
2642 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002643 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 +02002644 }
2645 /* need to wait for the other side */
2646 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002647 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002648 }
2649
2650 mux:
2651 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2652 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2653 break;
2654
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002655 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002656 continue;
2657
Willy Tarreau7aad7032020-01-16 17:20:57 +01002658 /* If the sender changed his mind and unsubscribed, let's just
2659 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002660 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002661 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2662 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002663 LIST_DEL_INIT(&fstrm->send_list);
2664 continue;
2665 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002666
2667 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002668 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2669 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002670 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002671 tasklet_wakeup(fstrm->subs->tasklet);
2672 fstrm->subs->events &= ~SUB_RETRY_SEND;
2673 if (!fstrm->subs->events)
2674 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002675 } else {
2676 /* it's the shut request that was queued */
2677 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2678 tasklet_wakeup(fstrm->shut_tl);
2679 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002680 }
2681
2682 fail:
2683 if (fconn->state == FCGI_CS_CLOSED) {
2684 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2685 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002686 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2687 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002688 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002689 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 }
2691 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002692
2693 done:
2694 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002695 return 1;
2696}
2697
2698
2699/* Attempt to read data, and subscribe if none available.
2700 * The function returns 1 if data has been received, otherwise zero.
2701 */
2702static int fcgi_recv(struct fcgi_conn *fconn)
2703{
2704 struct connection *conn = fconn->conn;
2705 struct buffer *buf;
2706 int max;
2707 size_t ret;
2708
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002709 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2710
2711 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2712 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002713 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002714 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002715
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002716 if (!fcgi_recv_allowed(fconn)) {
2717 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002718 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002719 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002720
2721 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2722 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002723 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002724 fconn->flags |= FCGI_CF_DEM_DALLOC;
2725 return 0;
2726 }
2727
Christopher Faulet99eff652019-08-11 23:11:30 +02002728 if (!b_data(buf)) {
2729 /* try to pre-align the buffer like the
2730 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002731 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002732 * HTX block to alias it upon recv. We cannot use the
2733 * head because rcv_buf() will realign the buffer if
2734 * it's empty. Thus we cheat and pretend we already
2735 * have a few bytes there.
2736 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002737 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2738 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002739 }
2740 else
2741 max = buf_room_for_htx_data(buf);
2742
2743 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2744
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002745 if (max && !ret && fcgi_recv_allowed(fconn)) {
2746 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002747 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002748 }
2749 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002750 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002751
2752 if (!b_data(buf)) {
2753 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002754 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002755 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2756 }
2757
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002758 if (ret == max) {
2759 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002760 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002761 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002762
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002763 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002764 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2765}
2766
2767
2768/* Try to send data if possible.
2769 * The function returns 1 if data have been sent, otherwise zero.
2770 */
2771static int fcgi_send(struct fcgi_conn *fconn)
2772{
2773 struct connection *conn = fconn->conn;
2774 int done;
2775 int sent = 0;
2776
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2778
2779 if (conn->flags & CO_FL_ERROR) {
2780 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002781 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002782 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002783
2784
Willy Tarreau911db9b2020-01-23 16:27:54 +01002785 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002786 /* a handshake was requested */
2787 goto schedule;
2788 }
2789
2790 /* This loop is quite simple : it tries to fill as much as it can from
2791 * pending streams into the existing buffer until it's reportedly full
2792 * or the end of send requests is reached. Then it tries to send this
2793 * buffer's contents out, marks it not full if at least one byte could
2794 * be sent, and tries again.
2795 *
2796 * The snd_buf() function normally takes a "flags" argument which may
2797 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2798 * data immediately comes and CO_SFL_STREAMER to indicate that the
2799 * connection is streaming lots of data (used to increase TLS record
2800 * size at the expense of latency). The former can be sent any time
2801 * there's a buffer full flag, as it indicates at least one stream
2802 * attempted to send and failed so there are pending data. An
2803 * alternative would be to set it as long as there's an active stream
2804 * but that would be problematic for ACKs until we have an absolute
2805 * guarantee that all waiters have at least one byte to send. The
2806 * latter should possibly not be set for now.
2807 */
2808
2809 done = 0;
2810 while (!done) {
2811 unsigned int flags = 0;
2812 unsigned int released = 0;
2813 struct buffer *buf;
2814
2815 /* fill as much as we can into the current buffer */
2816 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2817 done = fcgi_process_mux(fconn);
2818
2819 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2820 done = 1; // we won't go further without extra buffers
2821
2822 if (conn->flags & CO_FL_ERROR)
2823 break;
2824
2825 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2826 flags |= CO_SFL_MSG_MORE;
2827
2828 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2829 if (b_data(buf)) {
2830 int ret;
2831
2832 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2833 if (!ret) {
2834 done = 1;
2835 break;
2836 }
2837 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002838 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002839 b_del(buf, ret);
2840 if (b_data(buf)) {
2841 done = 1;
2842 break;
2843 }
2844 }
2845 b_free(buf);
2846 released++;
2847 }
2848
2849 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002850 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002851
2852 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002853 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2854 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002855 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2856 }
2857
2858 if (conn->flags & CO_FL_SOCK_WR_SH) {
2859 /* output closed, nothing to send, clear the buffer to release it */
2860 b_reset(br_tail(fconn->mbuf));
2861 }
2862 /* We're not full anymore, so we can wake any task that are waiting
2863 * for us.
2864 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002865 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002866 struct fcgi_strm *fstrm;
2867
2868 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2869 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2870 break;
2871
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002872 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002873 continue;
2874
Willy Tarreau7aad7032020-01-16 17:20:57 +01002875 /* If the sender changed his mind and unsubscribed, let's just
2876 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002877 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002878 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2879 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002880 LIST_DEL_INIT(&fstrm->send_list);
2881 continue;
2882 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002883
2884 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002885 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002886 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002887 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002888 tasklet_wakeup(fstrm->subs->tasklet);
2889 fstrm->subs->events &= ~SUB_RETRY_SEND;
2890 if (!fstrm->subs->events)
2891 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002892 } else {
2893 /* it's the shut request that was queued */
2894 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2895 tasklet_wakeup(fstrm->shut_tl);
2896 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002897 }
2898 }
2899 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002900 if (!br_data(fconn->mbuf)) {
2901 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002902 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002903 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002904schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002905 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2906 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002907 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002908 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002909
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002910 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002911 return sent;
2912}
2913
2914/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002915struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02002916{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002917 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002918 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002919 struct tasklet *tl = (struct tasklet *)t;
2920 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002921 int ret = 0;
2922
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002923 if (state & TASK_F_USR1) {
2924 /* the tasklet was idling on an idle connection, it might have
2925 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002926 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002927 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2928 if (tl->context == NULL) {
2929 /* The connection has been taken over by another thread,
2930 * we're no longer responsible for it, so just free the
2931 * tasklet, and do nothing.
2932 */
2933 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2934 tasklet_free(tl);
2935 return NULL;
2936 }
2937 conn = fconn->conn;
2938 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002939
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002940 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2941 if (conn_in_list)
2942 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002943
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002944 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2945 } else {
2946 /* we're certain the connection was not in an idle list */
2947 conn = fconn->conn;
2948 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
2949 conn_in_list = 0;
2950 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002951
Christopher Faulet99eff652019-08-11 23:11:30 +02002952 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2953 ret = fcgi_send(fconn);
2954 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2955 ret |= fcgi_recv(fconn);
2956 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002957 ret = fcgi_process(fconn);
2958
2959 /* If we were in an idle list, we want to add it back into it,
2960 * unless fcgi_process() returned -1, which mean it has destroyed
2961 * the connection (testing !ret is enough, if fcgi_process() wasn't
2962 * called then ret will be 0 anyway.
2963 */
Willy Tarreau74163142021-03-13 11:30:19 +01002964 if (ret < 0)
2965 t = NULL;
2966
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002967 if (!ret && conn_in_list) {
2968 struct server *srv = objt_server(conn->target);
2969
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002970 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002971 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02002972 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002973 else
Willy Tarreau85223482022-09-29 20:32:43 +02002974 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002975 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002976 }
Willy Tarreau74163142021-03-13 11:30:19 +01002977 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02002978}
2979
2980/* callback called on any event by the connection handler.
2981 * It applies changes and returns zero, or < 0 if it wants immediate
2982 * destruction of the connection (which normally doesn not happen in FCGI).
2983 */
2984static int fcgi_process(struct fcgi_conn *fconn)
2985{
2986 struct connection *conn = fconn->conn;
2987
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002988 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2989
Christopher Faulet99eff652019-08-11 23:11:30 +02002990 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2991 fcgi_process_demux(fconn);
2992
2993 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2994 b_reset(&fconn->dbuf);
2995
2996 if (buf_room_for_htx_data(&fconn->dbuf))
2997 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2998 }
2999 fcgi_send(fconn);
3000
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003001 if (unlikely(fconn->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003002 /* frontend is stopping, reload likely in progress, let's try
3003 * to announce a graceful shutdown if not yet done. We don't
3004 * care if it fails, it will be tried again later.
3005 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003006 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 +02003007 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3008 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3009 fcgi_conn_send_aborts(fconn);
3010 }
3011 }
3012
3013 /*
3014 * If we received early data, and the handshake is done, wake
3015 * any stream that was waiting for it.
3016 */
3017 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003018 (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 +02003019 struct eb32_node *node;
3020 struct fcgi_strm *fstrm;
3021
3022 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3023 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3024
3025 while (node) {
3026 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003027 if (fcgi_strm_sc(fstrm) && se_fl_test(fstrm->sd, SE_FL_WAIT_FOR_HS))
Christopher Faulet99eff652019-08-11 23:11:30 +02003028 fcgi_strm_notify_recv(fstrm);
3029 node = eb32_next(node);
3030 }
3031 }
3032
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003033 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003034 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3035 eb_is_empty(&fconn->streams_by_id)) {
3036 fcgi_wake_some_streams(fconn, 0);
3037
3038 if (eb_is_empty(&fconn->streams_by_id)) {
3039 /* no more stream, kill the connection now */
3040 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003041 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003042 return -1;
3043 }
3044 }
3045
3046 if (!b_data(&fconn->dbuf))
3047 fcgi_release_buf(fconn, &fconn->dbuf);
3048
3049 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3050 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3051 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3052 fcgi_release_mbuf(fconn);
3053
3054 if (fconn->task) {
3055 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3056 task_queue(fconn->task);
3057 }
3058
3059 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003060 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003061 return 0;
3062}
3063
3064
3065/* wake-up function called by the connection layer (mux_ops.wake) */
3066static int fcgi_wake(struct connection *conn)
3067{
3068 struct fcgi_conn *fconn = conn->ctx;
3069
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003070 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003071 return (fcgi_process(fconn));
3072}
3073
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003074
3075static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3076{
3077 int ret = 0;
3078 switch (mux_ctl) {
3079 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003080 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003081 ret |= MUX_STATUS_READY;
3082 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003083 case MUX_EXIT_STATUS:
3084 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003085 default:
3086 return -1;
3087 }
3088}
3089
Christopher Faulet99eff652019-08-11 23:11:30 +02003090/* Connection timeout management. The principle is that if there's no receipt
3091 * nor sending for a certain amount of time, the connection is closed. If the
3092 * MUX buffer still has lying data or is not allocatable, the connection is
3093 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003094 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003095 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003096struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003097{
3098 struct fcgi_conn *fconn = context;
3099 int expired = tick_is_expired(t->expire, now_ms);
3100
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003101 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3102
Willy Tarreau60814ff2020-06-30 11:19:23 +02003103 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003104 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003105
3106 /* Somebody already stole the connection from us, so we should not
3107 * free it, we just have to free the task.
3108 */
3109 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003110 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003111 fconn = NULL;
3112 goto do_leave;
3113 }
3114
Willy Tarreau60814ff2020-06-30 11:19:23 +02003115 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003116 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003117 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3118 return t;
3119 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003120
Willy Tarreau60814ff2020-06-30 11:19:23 +02003121 /* We're about to destroy the connection, so make sure nobody attempts
3122 * to steal it from us.
3123 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003124 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003125 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003126
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003127 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003128 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003129
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003130do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003131 task_destroy(t);
3132
3133 if (!fconn) {
3134 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003135 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003136 return NULL;
3137 }
3138
3139 fconn->task = NULL;
3140 fconn->state = FCGI_CS_CLOSED;
3141 fcgi_wake_some_streams(fconn, 0);
3142
3143 if (br_data(fconn->mbuf)) {
3144 /* don't even try to send aborts, the buffer is stuck */
3145 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3146 goto end;
3147 }
3148
3149 /* try to send but no need to insist */
3150 if (!fcgi_conn_send_aborts(fconn))
3151 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3152
3153 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3154 conn_xprt_ready(fconn->conn)) {
3155 unsigned int released = 0;
3156 struct buffer *buf;
3157
3158 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3159 if (b_data(buf)) {
3160 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3161 buf, b_data(buf), 0);
3162 if (!ret)
3163 break;
3164 b_del(buf, ret);
3165 if (b_data(buf))
3166 break;
3167 b_free(buf);
3168 released++;
3169 }
3170 }
3171
3172 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003173 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003174 }
3175
3176 end:
3177 /* either we can release everything now or it will be done later once
3178 * the last stream closes.
3179 */
3180 if (eb_is_empty(&fconn->streams_by_id))
3181 fcgi_release(fconn);
3182
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003183 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003184 return NULL;
3185}
3186
3187
3188/*******************************************/
3189/* functions below are used by the streams */
3190/*******************************************/
3191
3192/* Append the description of what is present in error snapshot <es> into <out>.
3193 * The description must be small enough to always fit in a buffer. The output
3194 * buffer may be the trash so the trash must not be used inside this function.
3195 */
3196static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3197{
3198 chunk_appendf(out,
3199 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3200 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3201 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3202 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3203 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3204 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3205}
3206/*
3207 * Capture a bad response and archive it in the proxy's structure. By default
3208 * it tries to report the error position as h1m->err_pos. However if this one is
3209 * not set, it will then report h1m->next, which is the last known parsing
3210 * point. The function is able to deal with wrapping buffers. It always displays
3211 * buffers as a contiguous area starting at buf->p. The direction is determined
3212 * thanks to the h1m's flags.
3213 */
3214static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3215 struct h1m *h1m, struct buffer *buf)
3216{
3217 struct session *sess = fstrm->sess;
3218 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003219 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003220 union error_snapshot_ctx ctx;
3221
Willy Tarreauea27f482022-05-18 16:10:52 +02003222 if (fcgi_strm_sc(fstrm) && sc_strm(fcgi_strm_sc(fstrm))) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003223 if (sess == NULL)
Willy Tarreauea27f482022-05-18 16:10:52 +02003224 sess = __sc_strm(fcgi_strm_sc(fstrm))->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003225 if (!(h1m->flags & H1_MF_RESP))
Willy Tarreauea27f482022-05-18 16:10:52 +02003226 other_end = __sc_strm(fcgi_strm_sc(fstrm))->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003227 else
3228 other_end = sess->fe;
3229 } else
3230 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003231 /* http-specific part now */
3232 ctx.h1.state = h1m->state;
3233 ctx.h1.c_flags = fconn->flags;
3234 ctx.h1.s_flags = fstrm->flags;
3235 ctx.h1.m_flags = h1m->flags;
3236 ctx.h1.m_clen = h1m->curr_len;
3237 ctx.h1.m_blen = h1m->body_len;
3238
3239 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3240 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3241 &ctx, fcgi_show_error_snapshot);
3242}
3243
3244static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3245 struct buffer *buf, size_t *ofs, size_t max)
3246{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003247 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003248
Willy Tarreau022e5e52020-09-10 09:33:15 +02003249 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 +02003250 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003251 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003252 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 +02003253 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003254 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 +02003255 fcgi_strm_error(fstrm);
3256 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3257 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003258 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003259 goto end;
3260 }
3261
Christopher Fauletda3adeb2021-09-28 09:50:07 +02003262 /* Reject any message with an unknown transfer-encoding. In fact if any
3263 * encoding other than "chunked". A 422-Unprocessable-Content is
3264 * returned for an invalid request, a 502-Bad-Gateway for an invalid
3265 * response.
3266 */
3267 if (h1m->flags & H1_MF_TE_OTHER) {
3268 htx->flags |= HTX_FL_PARSING_ERROR;
3269 TRACE_ERROR("Unknown transfer-encoding", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3270 fcgi_strm_error(fstrm);
3271 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3272 ret = 0;
3273 goto end;
3274 }
3275
Christopher Faulet99eff652019-08-11 23:11:30 +02003276 *ofs += ret;
3277 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003278 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 +02003279 return ret;
3280
3281}
3282
Christopher Fauletaf542632019-10-01 21:52:49 +02003283static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003284 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3285{
Christopher Fauletde471a42021-02-01 16:37:28 +01003286 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003287
Willy Tarreau022e5e52020-09-10 09:33:15 +02003288 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 +02003289 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003290 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003291 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 +02003292 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003293 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 +02003294 fcgi_strm_error(fstrm);
3295 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3296 }
3297 goto end;
3298 }
3299 *ofs += ret;
3300 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003301 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 +02003302 return ret;
3303}
3304
3305static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3306 struct buffer *buf, size_t *ofs, size_t max)
3307{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003308 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003309
Willy Tarreau022e5e52020-09-10 09:33:15 +02003310 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 +02003311 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003312 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003313 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 +02003314 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003315 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 +02003316 fcgi_strm_error(fstrm);
3317 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3318 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003319 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003320 goto end;
3321 }
3322 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003323 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003324 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 +02003325 return ret;
3326}
3327
Christopher Faulet99eff652019-08-11 23:11:30 +02003328static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3329{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003330 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003331 struct htx *htx;
3332 struct h1m *h1m = &fstrm->h1m;
3333 size_t ret, data, total = 0;
3334
3335 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003336 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3337
Christopher Faulet99eff652019-08-11 23:11:30 +02003338 data = htx->data;
3339 if (fstrm->state == FCGI_SS_ERROR)
3340 goto end;
3341
3342 do {
3343 size_t used = htx_used_space(htx);
3344
3345 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003346 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003347 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3348 if (!ret)
3349 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003350
3351 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3352
Christopher Faulet99eff652019-08-11 23:11:30 +02003353 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3354 struct htx_blk *blk = htx_get_head_blk(htx);
3355 struct htx_sl *sl;
3356
3357 if (!blk)
3358 break;
3359 sl = htx_get_blk_ptr(htx, blk);
3360 sl->flags |= HTX_SL_F_XFER_LEN;
3361 htx->extra = 0;
3362 }
3363 }
3364 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003365 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003366 fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003367
3368 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3369 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3370 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet2db904e2022-05-05 09:24:52 +02003371 if (htx_is_empty(htx) && !htx_add_endof(htx, HTX_BLK_EOT))
3372 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003373 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003374 h1m->state = H1_MSG_DONE;
3375 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3376 }
3377
Christopher Faulet16a524c2021-02-02 21:16:03 +01003378 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003379 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003380
3381 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 +02003382 }
3383 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003384 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003385 fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01003386 if (h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003387 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003388
Christopher Faulet76014fd2019-12-10 11:47:22 +01003389 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 +02003390 }
3391 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003392 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 +02003393 if (b_data(&fstrm->rxbuf) > total) {
3394 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003395 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003396 fcgi_strm_error(fstrm);
3397 }
3398 break;
3399 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003400 else {
3401 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003402 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003403 fcgi_strm_error(fstrm);
3404 break;
3405 }
3406
3407 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003408 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003409
3410 if (fstrm->state == FCGI_SS_ERROR) {
3411 b_reset(&fstrm->rxbuf);
3412 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003413 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003414 return 0;
3415 }
3416
3417 b_del(&fstrm->rxbuf, total);
3418
3419 end:
3420 htx_to_buf(htx, buf);
3421 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003422 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003423 return ret;
3424}
3425
3426/*
3427 * Attach a new stream to a connection
3428 * (Used for outgoing connections)
3429 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003430static int fcgi_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02003431{
Christopher Faulet99eff652019-08-11 23:11:30 +02003432 struct fcgi_strm *fstrm;
3433 struct fcgi_conn *fconn = conn->ctx;
3434
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003436 fstrm = fcgi_stconn_new(fconn, sd->sc, sess);
Christopher Faulete00ad352021-12-16 14:44:31 +01003437 if (!fstrm)
Christopher Faulet73518be2021-01-27 12:06:54 +01003438 goto err;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003439
3440 /* the connection is not idle anymore, let's mark this */
3441 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003442 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003443
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003444 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulete00ad352021-12-16 14:44:31 +01003445 return 0;
Christopher Faulet73518be2021-01-27 12:06:54 +01003446
3447 err:
3448 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01003449 return -1;
Christopher Faulet99eff652019-08-11 23:11:30 +02003450}
3451
Willy Tarreau4596fe22022-05-17 19:07:51 +02003452/* Retrieves the first valid stream connector from this connection, or returns NULL.
Christopher Faulet99eff652019-08-11 23:11:30 +02003453 * We have to scan because we may have some orphan streams. It might be
3454 * beneficial to scan backwards from the end to reduce the likeliness to find
3455 * orphans.
3456 */
Willy Tarreaud1373532022-05-27 11:00:59 +02003457static struct stconn *fcgi_get_first_sc(const struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02003458{
3459 struct fcgi_conn *fconn = conn->ctx;
3460 struct fcgi_strm *fstrm;
3461 struct eb32_node *node;
3462
3463 node = eb32_first(&fconn->streams_by_id);
3464 while (node) {
3465 fstrm = container_of(node, struct fcgi_strm, by_id);
Willy Tarreau77534272022-05-18 07:34:16 +02003466 if (fcgi_strm_sc(fstrm))
3467 return fcgi_strm_sc(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003468 node = eb32_next(node);
3469 }
3470 return NULL;
3471}
3472
3473/*
3474 * Destroy the mux and the associated connection, if it is no longer used
3475 */
3476static void fcgi_destroy(void *ctx)
3477{
3478 struct fcgi_conn *fconn = ctx;
3479
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003480 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003481 if (eb_is_empty(&fconn->streams_by_id)) {
3482 BUG_ON(fconn->conn->ctx != fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003483 fcgi_release(fconn);
Christopher Faulet4e610962022-04-14 11:23:50 +02003484 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003485}
3486
3487/*
3488 * Detach the stream from the connection and possibly release the connection.
3489 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003490static void fcgi_detach(struct sedesc *sd)
Christopher Faulet99eff652019-08-11 23:11:30 +02003491{
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003492 struct fcgi_strm *fstrm = sd->se;
Christopher Faulet99eff652019-08-11 23:11:30 +02003493 struct fcgi_conn *fconn;
3494 struct session *sess;
3495
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003496 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3497
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003498 if (!fstrm) {
3499 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003500 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003501 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003502
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003503 /* there's no txbuf so we're certain no to be able to send anything */
3504 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003505
3506 sess = fstrm->sess;
3507 fconn = fstrm->fconn;
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003508 fconn->nb_sc--;
Christopher Faulet99eff652019-08-11 23:11:30 +02003509
3510 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3511 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3512 fconn->streams_limit = 1;
3513 }
3514 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3515 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3516 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3517 fconn->state = FCGI_CS_CLOSED;
3518 }
3519
3520 /* this stream may be blocked waiting for some data to leave, so orphan
3521 * it in this case.
3522 */
Christopher Faulet897d6122021-12-17 17:28:35 +01003523 if (!(fconn->conn->flags & CO_FL_ERROR) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003524 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003525 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003526 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003527 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003528 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003529 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003530
3531 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3532 /* unblock the connection if it was blocked on this stream. */
3533 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3534 fcgi_conn_restart_reading(fconn, 1);
3535 }
3536
3537 fcgi_strm_destroy(fstrm);
3538
3539 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003540 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003541 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003542 /* Add the connection in the session serverlist, if not already done */
3543 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3544 fconn->conn->owner = NULL;
3545 if (eb_is_empty(&fconn->streams_by_id)) {
3546 /* let's kill the connection right away */
3547 fconn->conn->mux->destroy(fconn);
3548 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3549 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003550 }
3551 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003552 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003553 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3554 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003555 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003556 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003557 }
3558 }
3559 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003560 else {
3561 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003562 /* If the connection is owned by the session, first remove it
3563 * from its list
3564 */
3565 if (fconn->conn->owner) {
3566 session_unown_conn(fconn->conn->owner, fconn->conn);
3567 fconn->conn->owner = NULL;
3568 }
3569
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003570 /* mark that the tasklet may lose its context to another thread and
3571 * that the handler needs to check it under the idle conns lock.
3572 */
3573 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003574 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3575
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003576 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003577 /* The server doesn't want it, let's kill the connection right away */
3578 fconn->conn->mux->destroy(fconn);
3579 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3580 return;
3581 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003582 /* At this point, the connection has been added to the
3583 * server idle list, so another thread may already have
3584 * hijacked it, so we can't do anything with it.
3585 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003586 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3587 return;
3588 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003589 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003590 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02003591 !LIST_INLIST(&fconn->conn->session_list)) {
Willy Tarreau85223482022-09-29 20:32:43 +02003592 eb64_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
3593 &fconn->conn->hash_node->node);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003594 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003595 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003596 }
3597
3598 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003599 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003600 */
3601 if (fcgi_conn_is_dead(fconn)) {
3602 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003603 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003604 fcgi_release(fconn);
3605 }
3606 else if (fconn->task) {
3607 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3608 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003609 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003610 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003611 else
3612 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003613}
3614
3615
3616/* Performs a synchronous or asynchronous shutr(). */
3617static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3618{
3619 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003620
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003621 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3622
Christopher Faulet99eff652019-08-11 23:11:30 +02003623 if (fstrm->state == FCGI_SS_CLOSED)
3624 goto done;
3625
3626 /* a connstream may require us to immediately kill the whole connection
3627 * for example because of a "tcp-request content reject" rule that is
3628 * normally used to limit abuse.
3629 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003630 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003631 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3632 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003633 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003634 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003636 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3638 !fcgi_strm_send_abort(fconn, fstrm))
3639 goto add_to_list;
3640 }
3641
3642 fcgi_strm_close(fstrm);
3643
3644 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3645 tasklet_wakeup(fconn->wait_event.tasklet);
3646 done:
3647 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003648 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003649 return;
3650
3651 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003652 /* Let the handler know we want to shutr, and add ourselves to the
3653 * send list if not yet done. fcgi_deferred_shut() will be
3654 * automatically called via the shut_tl tasklet when there's room
3655 * again.
3656 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003657 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003658 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003659 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003660 }
3661 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003662 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003663 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003664 return;
3665}
3666
3667/* Performs a synchronous or asynchronous shutw(). */
3668static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3669{
3670 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003671
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003672 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3673
Christopher Faulet99eff652019-08-11 23:11:30 +02003674 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3675 goto done;
3676
3677 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3678 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3679 !fcgi_strm_send_abort(fconn, fstrm))
3680 goto add_to_list;
3681
3682 if (fstrm->state == FCGI_SS_HREM)
3683 fcgi_strm_close(fstrm);
3684 else
3685 fstrm->state = FCGI_SS_HLOC;
3686 } else {
3687 /* a connstream may require us to immediately kill the whole connection
3688 * for example because of a "tcp-request content reject" rule that is
3689 * normally used to limit abuse.
3690 */
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003691 if (se_fl_test(fstrm->sd, SE_FL_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003692 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3693 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003694 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003695 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003696
3697 fcgi_strm_close(fstrm);
3698 }
3699
3700 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3701 tasklet_wakeup(fconn->wait_event.tasklet);
3702 done:
3703 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003704 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 return;
3706
3707 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003708 /* Let the handler know we want to shutr, and add ourselves to the
3709 * send list if not yet done. fcgi_deferred_shut() will be
3710 * automatically called via the shut_tl tasklet when there's room
3711 * again.
3712 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003713 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003714 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003715 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 }
3717 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003719 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003720 return;
3721}
3722
Willy Tarreau7aad7032020-01-16 17:20:57 +01003723/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003724 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003725 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003726 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003727struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003728{
3729 struct fcgi_strm *fstrm = ctx;
3730 struct fcgi_conn *fconn = fstrm->fconn;
3731
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003732 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3733
Willy Tarreau7aad7032020-01-16 17:20:57 +01003734 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3735 /* some data processing remains to be done first */
3736 goto end;
3737 }
3738
Christopher Faulet99eff652019-08-11 23:11:30 +02003739 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3740 fcgi_do_shutw(fstrm);
3741
3742 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3743 fcgi_do_shutr(fstrm);
3744
3745 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3746 /* We're done trying to send, remove ourself from the send_list */
3747 LIST_DEL_INIT(&fstrm->send_list);
3748
Willy Tarreau77534272022-05-18 07:34:16 +02003749 if (!fcgi_strm_sc(fstrm)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003750 fcgi_strm_destroy(fstrm);
3751 if (fcgi_conn_is_dead(fconn))
3752 fcgi_release(fconn);
3753 }
3754 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003755 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003756 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 return NULL;
3758}
3759
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05003760/* shutr() called by the stream connector (mux_ops.shutr) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003761static void fcgi_shutr(struct stconn *sc, enum co_shr_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003762{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003763 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003764
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003765 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003766 if (!mode)
3767 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003768 fcgi_do_shutr(fstrm);
3769}
3770
Willy Tarreau4596fe22022-05-17 19:07:51 +02003771/* shutw() called by the stream connector (mux_ops.shutw) */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003772static void fcgi_shutw(struct stconn *sc, enum co_shw_mode mode)
Christopher Faulet99eff652019-08-11 23:11:30 +02003773{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003774 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003775
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003776 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003777 fcgi_do_shutw(fstrm);
3778}
3779
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003780/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3781 * event subscriber <es> is not allowed to change from a previous call as long
3782 * as at least one event is still subscribed. The <event_type> must only be a
3783 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003784 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003785static int fcgi_subscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003786{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003787 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 struct fcgi_conn *fconn = fstrm->fconn;
3789
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003790 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003791 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003792
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003793 es->events |= event_type;
3794 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003795
3796 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003797 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003798
Christopher Faulet99eff652019-08-11 23:11:30 +02003799 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003800 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003801 if (!LIST_INLIST(&fstrm->send_list))
3802 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003803 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003804 return 0;
3805}
3806
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003807/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3808 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3809 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003810 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003811static int fcgi_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003812{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003813 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003814 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003815
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003816 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003817 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003818
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003819 es->events &= ~event_type;
3820 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003821 fstrm->subs = NULL;
3822
3823 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003824 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003825
Christopher Faulet99eff652019-08-11 23:11:30 +02003826 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003827 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003828 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003829 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3830 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003831 }
3832 return 0;
3833}
3834
Christopher Faulet564e39c2021-09-21 15:50:55 +02003835/* Called from the upper layer, to receive data
3836 *
3837 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3838 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3839 * means the caller wants to flush input data (from the mux buffer and the
3840 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3841 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3842 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3843 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3844 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3845 * copy as much data as possible.
3846 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003847static size_t fcgi_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003848{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003849 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003850 struct fcgi_conn *fconn = fstrm->fconn;
3851 size_t ret = 0;
3852
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003853 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3854
Christopher Faulet99eff652019-08-11 23:11:30 +02003855 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3856 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003857 else
3858 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003859
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003860 if (b_data(&fstrm->rxbuf))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003861 se_fl_set(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet99eff652019-08-11 23:11:30 +02003862 else {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003863 se_fl_clr(fstrm->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003864 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003865 se_fl_set(fstrm->sd, SE_FL_EOI);
Christopher Faulet99eff652019-08-11 23:11:30 +02003866 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003867 se_fl_set(fstrm->sd, SE_FL_EOS);
Christopher Faulet99eff652019-08-11 23:11:30 +02003868 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003869 if (fcgi_conn_read0_pending(fconn))
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003870 se_fl_set(fstrm->sd, SE_FL_EOS);
3871 if (se_fl_test(fstrm->sd, SE_FL_ERR_PENDING))
3872 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003873 fcgi_release_buf(fconn, &fstrm->rxbuf);
3874 }
3875
3876 if (ret && fconn->dsi == fstrm->id) {
3877 /* demux is blocking on this stream's buffer */
3878 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3879 fcgi_conn_restart_reading(fconn, 1);
3880 }
3881
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003882 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003883 return ret;
3884}
3885
3886
Christopher Faulet99eff652019-08-11 23:11:30 +02003887/* Called from the upper layer, to send data from buffer <buf> for no more than
3888 * <count> bytes. Returns the number of bytes effectively sent. Some status
Willy Tarreau4596fe22022-05-17 19:07:51 +02003889 * flags may be updated on the stream connector.
Christopher Faulet99eff652019-08-11 23:11:30 +02003890 */
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003891static size_t fcgi_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Christopher Faulet99eff652019-08-11 23:11:30 +02003892{
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02003893 struct fcgi_strm *fstrm = __sc_mux_strm(sc);
Christopher Faulet99eff652019-08-11 23:11:30 +02003894 struct fcgi_conn *fconn = fstrm->fconn;
3895 size_t total = 0;
3896 size_t ret;
3897 struct htx *htx = NULL;
3898 struct htx_sl *sl;
3899 struct htx_blk *blk;
3900 uint32_t bsize;
3901
Willy Tarreau022e5e52020-09-10 09:33:15 +02003902 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003903
Christopher Faulet99eff652019-08-11 23:11:30 +02003904 /* If we were not just woken because we wanted to send but couldn't,
3905 * and there's somebody else that is waiting to send, do nothing,
3906 * we will subscribe later and be put at the end of the list
3907 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003908 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003909 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 +02003910 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003911 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003912 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003913
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003914 if (fconn->state < FCGI_CS_RECORD_H) {
3915 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003916 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003917 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003918
3919 htx = htxbuf(buf);
3920 if (fstrm->id == 0) {
3921 int32_t id = fcgi_conn_get_next_sid(fconn);
3922
3923 if (id < 0) {
3924 fcgi_strm_close(fstrm);
Willy Tarreau5aa5e772022-05-27 16:15:32 +02003925 se_fl_set(fstrm->sd, SE_FL_ERROR);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003926 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 +02003927 return 0;
3928 }
3929
3930 eb32_delete(&fstrm->by_id);
3931 fstrm->by_id.key = fstrm->id = id;
3932 fconn->max_id = id;
3933 fconn->nb_reserved--;
3934 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3935
3936
3937 /* Check if length of the body is known or if the message is
3938 * full. Otherwise, the request is invalid.
3939 */
3940 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003941 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003942 htx->flags |= HTX_FL_PARSING_ERROR;
3943 fcgi_strm_error(fstrm);
3944 goto done;
3945 }
3946 }
3947
3948 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003949 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 +02003950 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3951 goto done;
3952 }
3953
3954 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3955 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3956
Christopher Fauletfe410d62020-05-19 15:13:00 +02003957 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003958 count && !htx_is_empty(htx)) {
3959 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003960 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003961 bsize = htx_get_blksz(blk);
3962
3963 switch (htx_get_blk_type(blk)) {
3964 case HTX_BLK_REQ_SL:
3965 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003966 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 +02003967 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3968 if (!ret) {
3969 goto done;
3970 }
3971 total += ret;
3972 count -= ret;
3973 break;
3974
3975 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003976 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
3977 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
3978 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3979 if (!ret)
3980 goto done;
3981 }
3982 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
3983 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
3984 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3985 if (!ret)
3986 goto done;
3987 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 goto remove_blk;
3989
3990 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003991 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 +02003992 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3993 if (ret > 0) {
3994 htx = htx_from_buf(buf);
3995 total += ret;
3996 count -= ret;
3997 if (ret < bsize)
3998 goto done;
3999 }
4000 break;
4001
Christopher Faulet99eff652019-08-11 23:11:30 +02004002 default:
4003 remove_blk:
4004 htx_remove_blk(htx, blk);
4005 total += bsize;
4006 count -= bsize;
4007 break;
4008 }
4009 }
4010
4011 done:
4012 if (fstrm->state >= FCGI_SS_HLOC) {
4013 /* trim any possibly pending data after we close (extra CR-LF,
4014 * unprocessed trailers, abnormal extra data, ...)
4015 */
4016 total += count;
4017 count = 0;
4018 }
4019
4020 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004021 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 +02004022 se_fl_set_error(fstrm->sd);
Christopher Faulet99eff652019-08-11 23:11:30 +02004023 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4024 fcgi_strm_close(fstrm);
4025 }
4026
4027 if (htx)
4028 htx_to_buf(htx, buf);
4029
Christopher Faulet99eff652019-08-11 23:11:30 +02004030 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004031 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4032 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 +02004033 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004034 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004035
4036 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004037 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4038 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004039 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004040
4041 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004042 return total;
4043}
4044
4045/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004046static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004047{
4048 struct fcgi_conn *fconn = conn->ctx;
4049 struct fcgi_strm *fstrm = NULL;
4050 struct eb32_node *node;
4051 int send_cnt = 0;
4052 int tree_cnt = 0;
4053 int orph_cnt = 0;
4054 struct buffer *hmbuf, *tmbuf;
4055
4056 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004057 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004058
4059 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4060 send_cnt++;
4061
4062 fstrm = NULL;
4063 node = eb32_first(&fconn->streams_by_id);
4064 while (node) {
4065 fstrm = container_of(node, struct fcgi_strm, by_id);
4066 tree_cnt++;
Willy Tarreau77534272022-05-18 07:34:16 +02004067 if (!fcgi_strm_sc(fstrm))
Christopher Faulet99eff652019-08-11 23:11:30 +02004068 orph_cnt++;
4069 node = eb32_next(node);
4070 }
4071
4072 hmbuf = br_head(fconn->mbuf);
4073 tmbuf = br_tail(fconn->mbuf);
4074 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4075 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4076 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4077 fconn->state, fconn->max_id, fconn->flags,
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004078 fconn->nb_streams, fconn->nb_sc, send_cnt, tree_cnt, orph_cnt,
Christopher Faulet99eff652019-08-11 23:11:30 +02004079 fconn->wait_event.events, fconn->dsi,
4080 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4081 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4082 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4083 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4084 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4085 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4086 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4087
4088 if (fstrm) {
Willy Tarreauc92a6ca2022-05-27 10:38:10 +02004089 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 +02004090 fstrm, fstrm->id, fstrm->flags,
4091 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4092 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
Willy Tarreau77534272022-05-18 07:34:16 +02004093 fcgi_strm_sc(fstrm));
Christopher Faulet186367f2022-05-30 08:45:15 +02004094
4095 chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(fstrm->sd));
4096 if (!se_fl_test(fstrm->sd, SE_FL_ORPHAN))
4097 chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p",
4098 fcgi_strm_sc(fstrm)->flags, fcgi_strm_sc(fstrm)->app);
4099
Willy Tarreau41054612022-09-02 14:22:38 +02004100 chunk_appendf(msg, " .subs=%p", fstrm->subs);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004101 if (fstrm->subs) {
Willy Tarreau41054612022-09-02 14:22:38 +02004102 chunk_appendf(msg, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4103 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004104 fstrm->subs->tasklet->calls,
4105 fstrm->subs->tasklet->context);
Willy Tarreau41054612022-09-02 14:22:38 +02004106 resolve_sym_name(msg, NULL, fstrm->subs->tasklet->process);
4107 chunk_appendf(msg, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004108 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004109 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004110 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004111}
4112
4113/* Migrate the the connection to the current thread.
4114 * Return 0 if successful, non-zero otherwise.
4115 * Expected to be called with the old thread lock held.
4116 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004117static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004118{
4119 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004120 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004121
4122 if (fd_takeover(conn->handle.fd, conn) != 0)
4123 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004124
4125 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4126 /* We failed to takeover the xprt, even if the connection may
4127 * still be valid, flag it as error'd, as we have already
4128 * taken over the fd, and wake the tasklet, so that it will
4129 * destroy it.
4130 */
4131 conn->flags |= CO_FL_ERROR;
4132 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4133 return -1;
4134 }
4135
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004136 if (fcgi->wait_event.events)
4137 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4138 fcgi->wait_event.events, &fcgi->wait_event);
4139 /* To let the tasklet know it should free itself, and do nothing else,
4140 * set its context to NULL;
4141 */
4142 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004143 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004144
4145 task = fcgi->task;
4146 if (task) {
4147 task->context = NULL;
4148 fcgi->task = NULL;
4149 __ha_barrier_store();
4150 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004151
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004152 fcgi->task = task_new_here();
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004153 if (!fcgi->task) {
4154 fcgi_release(fcgi);
4155 return -1;
4156 }
4157 fcgi->task->process = fcgi_timeout_task;
4158 fcgi->task->context = fcgi;
4159 }
4160 fcgi->wait_event.tasklet = tasklet_new();
4161 if (!fcgi->wait_event.tasklet) {
4162 fcgi_release(fcgi);
4163 return -1;
4164 }
4165 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4166 fcgi->wait_event.tasklet->context = fcgi;
4167 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4168 SUB_RETRY_RECV, &fcgi->wait_event);
4169
4170 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004171}
4172
4173/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004174/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004175/****************************************/
4176
4177/* The mux operations */
4178static const struct mux_ops mux_fcgi_ops = {
4179 .init = fcgi_init,
4180 .wake = fcgi_wake,
4181 .attach = fcgi_attach,
Willy Tarreaud1373532022-05-27 11:00:59 +02004182 .get_first_sc = fcgi_get_first_sc,
Christopher Faulet99eff652019-08-11 23:11:30 +02004183 .detach = fcgi_detach,
4184 .destroy = fcgi_destroy,
4185 .avail_streams = fcgi_avail_streams,
4186 .used_streams = fcgi_used_streams,
4187 .rcv_buf = fcgi_rcv_buf,
4188 .snd_buf = fcgi_snd_buf,
4189 .subscribe = fcgi_subscribe,
4190 .unsubscribe = fcgi_unsubscribe,
4191 .shutr = fcgi_shutr,
4192 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004193 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004194 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004195 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004196 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004197 .name = "FCGI",
4198};
4199
4200
4201/* this mux registers FCGI proto */
4202static struct mux_proto_list mux_proto_fcgi =
4203{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4204
4205INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4206
4207/*
4208 * Local variables:
4209 * c-indent-level: 8
4210 * c-basic-offset: 8
4211 * End:
4212 */