blob: 50f271f22a333bf39c6dfb06445457198a5c70b6 [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 Faulet1329f2a2021-12-16 17:32:56 +010020#include <haproxy/conn_stream.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010021#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020023#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020025#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020026#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020029#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.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 Tarreau48d25b32020-06-04 18:58:52 +020034#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020037#include <haproxy/trace.h>
Christopher Faulet5cd0e522021-06-11 13:34:42 +020038#include <haproxy/version.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020039
Willy Tarreaub2551052020-06-09 09:07:15 +020040
Christopher Faulet99eff652019-08-11 23:11:30 +020041/* FCGI Connection flags (32 bits) */
42#define FCGI_CF_NONE 0x00000000
43
44/* Flags indicating why writing to the mux is blockes */
45#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
46#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
47#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
48
49/* Flags indicating why writing to the demux is blocked.
50 * The first two ones directly affect the ability for the mux to receive data
51 * from the connection. The other ones affect the mux's ability to demux
52 * received data.
53 */
54#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
55#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
56#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
57#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
58#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
59#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
60#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
61
62/* Other flags */
63#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
64#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
65#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
66#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
Willy Tarreau714f3452021-05-09 06:47:26 +020067#define FCGI_CF_KEEP_CONN 0x00001000 /* HAProxy is responsible to close the connection */
Christopher Faulet99eff652019-08-11 23:11:30 +020068#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
69
70/* FCGI connection state (fcgi_conn->state) */
71enum fcgi_conn_st {
72 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
73 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
74 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
75 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
76 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
77 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
78 FCGI_CS_ENTRIES
79} __attribute__((packed));
80
81/* 32 buffers: one for the ring's root, rest for the mbuf itself */
82#define FCGI_C_MBUF_CNT 32
83
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010084/* Size for a record header (also size of empty record) */
85#define FCGI_RECORD_HEADER_SZ 8
86
Christopher Faulet99eff652019-08-11 23:11:30 +020087/* FCGI connection descriptor */
88struct fcgi_conn {
89 struct connection *conn;
90
91 enum fcgi_conn_st state; /* FCGI connection state */
92 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
93 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
94 uint32_t flags; /* Connection flags: FCGI_CF_* */
95
96 int16_t dsi; /* dmux stream ID (<0 = idle ) */
97 uint16_t drl; /* demux record length (if dsi >= 0) */
98 uint8_t drt; /* demux record type (if dsi >= 0) */
99 uint8_t drp; /* demux record padding (if dsi >= 0) */
100
101 struct buffer dbuf; /* demux buffer */
102 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
103
104 int timeout; /* idle timeout duration in ticks */
105 int shut_timeout; /* idle timeout duration in ticks after shutdown */
106 unsigned int nb_streams; /* number of streams in the tree */
107 unsigned int nb_cs; /* number of attached conn_streams */
108 unsigned int nb_reserved; /* number of reserved streams */
109 unsigned int stream_cnt; /* total number of streams seen */
110
111 struct proxy *proxy; /* the proxy this connection was created for */
112 struct fcgi_app *app; /* FCGI application used by this mux */
113 struct task *task; /* timeout management task */
114 struct eb_root streams_by_id; /* all active streams by their ID */
115
116 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200117
118 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
119 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
120};
121
122
123/* FCGI stream state, in fcgi_strm->state */
124enum fcgi_strm_st {
125 FCGI_SS_IDLE = 0,
126 FCGI_SS_OPEN,
127 FCGI_SS_HREM, // half-closed(remote)
128 FCGI_SS_HLOC, // half-closed(local)
129 FCGI_SS_ERROR,
130 FCGI_SS_CLOSED,
131 FCGI_SS_ENTRIES
132} __attribute__((packed));
133
134
135/* FCGI stream flags (32 bits) */
136#define FCGI_SF_NONE 0x00000000
137#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100138#define FCGI_SF_ES_SENT 0x00000002 /* end-of-stream sent (empty STDIN record) */
139#define FCGI_SF_EP_SENT 0x00000004 /* end-of-param sent (empty PARAMS record) */
140#define FCGI_SF_ABRT_SENT 0x00000008 /* abort sent (ABORT_REQUEST record) */
Christopher Faulet99eff652019-08-11 23:11:30 +0200141
142/* Stream flags indicating the reason the stream is blocked */
143#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
144#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
145#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
146
147#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
148#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100149#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200150
151#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
152#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
153#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
154
Christopher Faulet99eff652019-08-11 23:11:30 +0200155
156/* FCGI stream descriptor */
157struct fcgi_strm {
158 struct conn_stream *cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100159 struct cs_endpoint *endp;
Christopher Faulet99eff652019-08-11 23:11:30 +0200160 struct session *sess;
161 struct fcgi_conn *fconn;
162
163 int32_t id; /* stream ID */
164
165 uint32_t flags; /* Connection flags: FCGI_SF_* */
166 enum fcgi_strm_st state; /* FCGI stream state */
167 int proto_status; /* FCGI_PS_* */
168
169 struct h1m h1m; /* response parser state for H1 */
170
171 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
172
173 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100174 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200175 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100176 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 +0200177};
178
179/* Flags representing all default FCGI parameters */
180#define FCGI_SP_CGI_GATEWAY 0x00000001
181#define FCGI_SP_DOC_ROOT 0x00000002
182#define FCGI_SP_SCRIPT_NAME 0x00000004
183#define FCGI_SP_PATH_INFO 0x00000008
184#define FCGI_SP_REQ_URI 0x00000010
185#define FCGI_SP_REQ_METH 0x00000020
186#define FCGI_SP_REQ_QS 0x00000040
187#define FCGI_SP_SRV_PORT 0x00000080
188#define FCGI_SP_SRV_PROTO 0x00000100
189#define FCGI_SP_SRV_NAME 0x00000200
190#define FCGI_SP_REM_ADDR 0x00000400
191#define FCGI_SP_REM_PORT 0x00000800
192#define FCGI_SP_SCRIPT_FILE 0x00001000
193#define FCGI_SP_PATH_TRANS 0x00002000
194#define FCGI_SP_CONT_LEN 0x00004000
195#define FCGI_SP_HTTPS 0x00008000
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200196#define FCGI_SP_SRV_SOFT 0x00010000
197#define FCGI_SP_MASK 0x0001FFFF
Christopher Faulet99eff652019-08-11 23:11:30 +0200198#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
199
200/* FCGI parameters used when PARAMS record is sent */
201struct fcgi_strm_params {
202 uint32_t mask;
203 struct ist docroot;
204 struct ist scriptname;
205 struct ist pathinfo;
206 struct ist meth;
207 struct ist uri;
208 struct ist vsn;
209 struct ist qs;
210 struct ist srv_name;
211 struct ist srv_port;
212 struct ist rem_addr;
213 struct ist rem_port;
214 struct ist cont_len;
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200215 struct ist srv_soft;
Christopher Faulet99eff652019-08-11 23:11:30 +0200216 int https;
217 struct buffer *p;
218};
219
220/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
221#define MAX_DATA_REALIGN 1024
222
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200223/* trace source and events */
224static void fcgi_trace(enum trace_level level, uint64_t mask,
225 const struct trace_source *src,
226 const struct ist where, const struct ist func,
227 const void *a1, const void *a2, const void *a3, const void *a4);
228
229/* The event representation is split like this :
230 * fconn - internal FCGI connection
231 * fstrm - internal FCGI stream
232 * strm - application layer
233 * rx - data receipt
234 * tx - data transmission
235 * rsp - response parsing
236 */
237static const struct trace_event fcgi_trace_events[] = {
238#define FCGI_EV_FCONN_NEW (1ULL << 0)
239 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
240#define FCGI_EV_FCONN_RECV (1ULL << 1)
241 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
242#define FCGI_EV_FCONN_SEND (1ULL << 2)
243 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
244#define FCGI_EV_FCONN_BLK (1ULL << 3)
245 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
246#define FCGI_EV_FCONN_WAKE (1ULL << 4)
247 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
248#define FCGI_EV_FCONN_END (1ULL << 5)
249 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
250#define FCGI_EV_FCONN_ERR (1ULL << 6)
251 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
252
253#define FCGI_EV_RX_FHDR (1ULL << 7)
254 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
255#define FCGI_EV_RX_RECORD (1ULL << 8)
256 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
257#define FCGI_EV_RX_EOI (1ULL << 9)
258 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
259#define FCGI_EV_RX_GETVAL (1ULL << 10)
260 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
261#define FCGI_EV_RX_STDOUT (1ULL << 11)
262 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
263#define FCGI_EV_RX_STDERR (1ULL << 12)
264 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
265#define FCGI_EV_RX_ENDREQ (1ULL << 13)
266 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
267
268#define FCGI_EV_TX_RECORD (1ULL << 14)
269 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
270#define FCGI_EV_TX_EOI (1ULL << 15)
271 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
272#define FCGI_EV_TX_BEGREQ (1ULL << 16)
273 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
274#define FCGI_EV_TX_GETVAL (1ULL << 17)
275 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
276#define FCGI_EV_TX_PARAMS (1ULL << 18)
277 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
278#define FCGI_EV_TX_STDIN (1ULL << 19)
279 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
280#define FCGI_EV_TX_ABORT (1ULL << 20)
281 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
282
283#define FCGI_EV_RSP_DATA (1ULL << 21)
284 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
285#define FCGI_EV_RSP_EOM (1ULL << 22)
286 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
287#define FCGI_EV_RSP_HDRS (1ULL << 23)
288 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
289#define FCGI_EV_RSP_BODY (1ULL << 24)
290 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
291#define FCGI_EV_RSP_TLRS (1ULL << 25)
292 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
293
294#define FCGI_EV_FSTRM_NEW (1ULL << 26)
295 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
296#define FCGI_EV_FSTRM_BLK (1ULL << 27)
297 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
298#define FCGI_EV_FSTRM_END (1ULL << 28)
299 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
300#define FCGI_EV_FSTRM_ERR (1ULL << 29)
301 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
302
303#define FCGI_EV_STRM_NEW (1ULL << 30)
304 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
305#define FCGI_EV_STRM_RECV (1ULL << 31)
306 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
307#define FCGI_EV_STRM_SEND (1ULL << 32)
308 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
309#define FCGI_EV_STRM_FULL (1ULL << 33)
310 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
311#define FCGI_EV_STRM_WAKE (1ULL << 34)
312 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
313#define FCGI_EV_STRM_SHUT (1ULL << 35)
314 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
315#define FCGI_EV_STRM_END (1ULL << 36)
316 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
317#define FCGI_EV_STRM_ERR (1ULL << 37)
318 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
319
320 { }
321};
322
323static const struct name_desc fcgi_trace_lockon_args[4] = {
324 /* arg1 */ { /* already used by the connection */ },
325 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
326 /* arg3 */ { },
327 /* arg4 */ { }
328};
329
330
331static const struct name_desc fcgi_trace_decoding[] = {
332#define FCGI_VERB_CLEAN 1
333 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
334#define FCGI_VERB_MINIMAL 2
335 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
336#define FCGI_VERB_SIMPLE 3
337 { .name="simple", .desc="add request/response status line or htx info when available" },
338#define FCGI_VERB_ADVANCED 4
339 { .name="advanced", .desc="add header fields or record decoding when available" },
340#define FCGI_VERB_COMPLETE 5
341 { .name="complete", .desc="add full data dump when available" },
342 { /* end */ }
343};
344
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200345static struct trace_source trace_fcgi __read_mostly = {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200346 .name = IST("fcgi"),
347 .desc = "FastCGI multiplexer",
348 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
349 .default_cb = fcgi_trace,
350 .known_events = fcgi_trace_events,
351 .lockon_args = fcgi_trace_lockon_args,
352 .decoding = fcgi_trace_decoding,
353 .report_events = ~0, // report everything by default
354};
355
356#define TRACE_SOURCE &trace_fcgi
357INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
358
Christopher Faulet99eff652019-08-11 23:11:30 +0200359/* FCGI connection and stream pools */
360DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
361DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
362
Willy Tarreau144f84a2021-03-02 16:09:26 +0100363struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200364static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau691d5032021-01-20 14:55:01 +0100365/* fcgi_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100366struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200367static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100368struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200369static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200370static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
371static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200372static void fcgi_strm_alert(struct fcgi_strm *fstrm);
373static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
374
375/* a dmumy management stream */
376static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
377 .cs = NULL,
378 .fconn = NULL,
379 .state = FCGI_SS_CLOSED,
380 .flags = FCGI_SF_NONE,
381 .id = 0,
382};
383
384/* and a dummy idle stream for use with any unknown stream */
385static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
386 .cs = NULL,
387 .fconn = NULL,
388 .state = FCGI_SS_IDLE,
389 .flags = FCGI_SF_NONE,
390 .id = 0,
391};
392
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200393/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
394static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
395{
396 switch (st) {
397 case FCGI_CS_INIT : return "INI";
398 case FCGI_CS_SETTINGS : return "STG";
399 case FCGI_CS_RECORD_H : return "RDH";
400 case FCGI_CS_RECORD_D : return "RDD";
401 case FCGI_CS_RECORD_P : return "RDP";
402 case FCGI_CS_CLOSED : return "CLO";
403 default : return "???";
404 }
405}
406
407/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
408static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
409{
410 switch (st) {
411 case FCGI_SS_IDLE : return "IDL";
412 case FCGI_SS_OPEN : return "OPN";
413 case FCGI_SS_HREM : return "RCL";
414 case FCGI_SS_HLOC : return "HCL";
415 case FCGI_SS_ERROR : return "ERR";
416 case FCGI_SS_CLOSED : return "CLO";
417 default : return "???";
418 }
419}
420
421
422/* the FCGI traces always expect that arg1, if non-null, is of type connection
423 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
424 * and that arg3, if non-null, is a htx for rx/tx headers.
425 */
426static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
427 const struct ist where, const struct ist func,
428 const void *a1, const void *a2, const void *a3, const void *a4)
429{
430 const struct connection *conn = a1;
Willy Tarreau31a83062022-01-28 09:36:35 +0100431 struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200432 const struct fcgi_strm *fstrm = a2;
433 const struct htx *htx = a3;
434 const size_t *val = a4;
435
436 if (!fconn)
437 fconn = (fstrm ? fstrm->fconn : NULL);
438
439 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
440 return;
441
442 /* Display the response state if fstrm is defined */
443 if (fstrm)
444 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
445
446 if (src->verbosity == FCGI_VERB_CLEAN)
447 return;
448
449 /* Display the value to the 4th argument (level > STATE) */
450 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100451 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200452
453 /* Display status-line if possible (verbosity > MINIMAL) */
454 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
455 const struct htx_blk *blk = htx_get_head_blk(htx);
456 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
457 enum htx_blk_type type = htx_get_blk_type(blk);
458
459 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
460 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
461 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
462 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
463 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
464 }
465
466 /* Display fconn info and, if defined, fstrm info */
467 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
468 if (fstrm)
469 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
470
471 if (!fstrm || fstrm->id <= 0)
472 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
473 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
474 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
475
476 if (src->verbosity == FCGI_VERB_MINIMAL)
477 return;
478
479 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
480 if (src->level > TRACE_LEVEL_USER) {
481 if (src->verbosity == FCGI_VERB_COMPLETE ||
482 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
483 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
484 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
485 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
486 if (src->verbosity == FCGI_VERB_COMPLETE ||
487 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
Willy Tarreau31a83062022-01-28 09:36:35 +0100488 struct buffer *hmbuf = br_head(fconn->mbuf);
489 struct buffer *tmbuf = br_tail(fconn->mbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200490
491 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
492 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
493 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
494 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
495 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
496 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
497 }
498
499 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
500 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
501 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
502 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
503 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
504 }
505
506 /* Display htx info if defined (level > USER) */
507 if (src->level > TRACE_LEVEL_USER && htx) {
508 int full = 0;
509
510 /* Full htx info (level > STATE && verbosity > SIMPLE) */
511 if (src->level > TRACE_LEVEL_STATE) {
512 if (src->verbosity == FCGI_VERB_COMPLETE)
513 full = 1;
514 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
515 full = 1;
516 }
517
518 chunk_memcat(&trace_buf, "\n\t", 2);
519 htx_dump(&trace_buf, htx, full);
520 }
521}
Christopher Faulet99eff652019-08-11 23:11:30 +0200522
523/*****************************************************/
524/* functions below are for dynamic buffer management */
525/*****************************************************/
526
527/* Indicates whether or not the we may call the fcgi_recv() function to attempt
528 * to receive data into the buffer and/or demux pending data. The condition is
529 * a bit complex due to some API limits for now. The rules are the following :
530 * - if an error or a shutdown was detected on the connection and the buffer
531 * is empty, we must not attempt to receive
532 * - if the demux buf failed to be allocated, we must not try to receive and
533 * we know there is nothing pending
534 * - if no flag indicates a blocking condition, we may attempt to receive,
535 * regardless of whether the demux buffer is full or not, so that only
536 * de demux part decides whether or not to block. This is needed because
537 * the connection API indeed prevents us from re-enabling receipt that is
538 * already enabled in a polled state, so we must always immediately stop
539 * as soon as the demux can't proceed so as never to hit an end of read
540 * with data pending in the buffers.
541 * - otherwise must may not attempt
542 */
543static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
544{
545 if (b_data(&fconn->dbuf) == 0 &&
546 (fconn->state == FCGI_CS_CLOSED ||
547 fconn->conn->flags & CO_FL_ERROR ||
548 conn_xprt_read0_pending(fconn->conn)))
549 return 0;
550
551 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
552 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
553 return 1;
554
555 return 0;
556}
557
558/* Restarts reading on the connection if it was not enabled */
559static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
560{
561 if (!fcgi_recv_allowed(fconn))
562 return;
563 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
564 (fconn->wait_event.events & SUB_RETRY_RECV))
565 return;
566 tasklet_wakeup(fconn->wait_event.tasklet);
567}
568
569
570/* Tries to grab a buffer and to re-enable processing on mux <target>. The
571 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
572 * the allocation succeeds, in which case the connection is woken up, or 0 if
573 * it's impossible to wake up and we prefer to be woken up later.
574 */
575static int fcgi_buf_available(void *target)
576{
577 struct fcgi_conn *fconn = target;
578 struct fcgi_strm *fstrm;
579
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100580 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200581 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 +0200582 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
583 fcgi_conn_restart_reading(fconn, 1);
584 return 1;
585 }
586
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100587 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200588 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 +0200589 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200590 if (fconn->flags & FCGI_CF_DEM_MROOM) {
591 fconn->flags &= ~FCGI_CF_DEM_MROOM;
592 fcgi_conn_restart_reading(fconn, 1);
593 }
594 return 1;
595 }
596
597 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
598 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100599 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200600 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 +0200601 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
602 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200603 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200604 return 1;
605 }
606
607 return 0;
608}
609
610static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
611{
612 struct buffer *buf = NULL;
613
Willy Tarreau2b718102021-04-21 07:32:39 +0200614 if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100615 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200616 fconn->buf_wait.target = fconn;
617 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200618 LIST_APPEND(&th_ctx->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200619 }
620 return buf;
621}
622
623static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
624{
625 if (bptr->size) {
626 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100627 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200628 }
629}
630
631static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
632{
633 struct buffer *buf;
634 unsigned int count = 0;
635
636 while (b_size(buf = br_head_pick(fconn->mbuf))) {
637 b_free(buf);
638 count++;
639 }
640 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100641 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200642}
643
644/* Returns the number of allocatable outgoing streams for the connection taking
645 * the number reserved streams into account.
646 */
647static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
648{
649 int ret;
650
651 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
652 if (ret < 0)
653 ret = 0;
654 return ret;
655}
656
657/* Returns the number of streams in use on a connection to figure if it's
658 * idle or not. We check nb_cs and not nb_streams as the caller will want
659 * to know if it was the last one after a detach().
660 */
661static int fcgi_used_streams(struct connection *conn)
662{
663 struct fcgi_conn *fconn = conn->ctx;
664
665 return fconn->nb_cs;
666}
667
668/* Returns the number of concurrent streams available on the connection */
669static int fcgi_avail_streams(struct connection *conn)
670{
671 struct server *srv = objt_server(conn->target);
672 struct fcgi_conn *fconn = conn->ctx;
673 int ret1, ret2;
674
675 /* Don't open new stream if the connection is closed */
676 if (fconn->state == FCGI_CS_CLOSED)
677 return 0;
678
679 /* May be negative if this setting has changed */
680 ret1 = (fconn->streams_limit - fconn->nb_streams);
681
682 /* we must also consider the limit imposed by stream IDs */
683 ret2 = fcgi_streams_left(fconn);
684 ret1 = MIN(ret1, ret2);
685 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
686 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
687 ret1 = MIN(ret1, ret2);
688 }
689 return ret1;
690}
691
692/*****************************************************************/
693/* functions below are dedicated to the mux setup and management */
694/*****************************************************************/
695
696/* Initializes the mux once it's attached. Only outgoing connections are
697 * supported. So the context is already initialized before installing the
698 * mux. <input> is always used as Input buffer and may contain data. It is the
699 * caller responsibility to not reuse it anymore. Returns < 0 on error.
700 */
701static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
702 struct buffer *input)
703{
704 struct fcgi_conn *fconn;
705 struct fcgi_strm *fstrm;
706 struct fcgi_app *app = get_px_fcgi_app(px);
707 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200708 void *conn_ctx = conn->ctx;
709
710 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200711
Christopher Faulet73518be2021-01-27 12:06:54 +0100712 if (!app) {
713 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 +0200714 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100715 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200716
717 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100718 if (!fconn) {
719 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200720 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100721 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200722
723 fconn->shut_timeout = fconn->timeout = px->timeout.server;
724 if (tick_isset(px->timeout.serverfin))
725 fconn->shut_timeout = px->timeout.serverfin;
726
727 fconn->flags = FCGI_CF_NONE;
728
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500729 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200730 if (app->flags & FCGI_APP_FL_KEEP_CONN)
731 fconn->flags |= FCGI_CF_KEEP_CONN;
732 if (app->flags & FCGI_APP_FL_GET_VALUES)
733 fconn->flags |= FCGI_CF_GET_VALUES;
734 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
735 fconn->flags |= FCGI_CF_MPXS_CONNS;
736
737 fconn->proxy = px;
738 fconn->app = app;
739 fconn->task = NULL;
740 if (tick_isset(fconn->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200741 t = task_new_here();
Christopher Faulet73518be2021-01-27 12:06:54 +0100742 if (!t) {
743 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 +0200744 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100745 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200746
747 fconn->task = t;
748 t->process = fcgi_timeout_task;
749 t->context = fconn;
750 t->expire = tick_add(now_ms, fconn->timeout);
751 }
752
753 fconn->wait_event.tasklet = tasklet_new();
754 if (!fconn->wait_event.tasklet)
755 goto fail;
756 fconn->wait_event.tasklet->process = fcgi_io_cb;
757 fconn->wait_event.tasklet->context = fconn;
758 fconn->wait_event.events = 0;
759
760 /* Initialise the context. */
761 fconn->state = FCGI_CS_INIT;
762 fconn->conn = conn;
763 fconn->streams_limit = app->maxreqs;
764 fconn->max_id = -1;
765 fconn->nb_streams = 0;
766 fconn->nb_cs = 0;
767 fconn->nb_reserved = 0;
768 fconn->stream_cnt = 0;
769
770 fconn->dbuf = *input;
771 fconn->dsi = -1;
772
773 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
774 fconn->streams_by_id = EB_ROOT;
775 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100776 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200777
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200778 conn->ctx = fconn;
779
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 if (t)
781 task_queue(t);
782
783 /* FIXME: this is temporary, for outgoing connections we need to
784 * immediately allocate a stream until the code is modified so that the
785 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200786 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200787 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200788 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200789 if (!fstrm)
790 goto fail;
791
Christopher Faulet99eff652019-08-11 23:11:30 +0200792
793 /* Repare to read something */
794 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200795 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200796 return 0;
797
798 fail:
799 task_destroy(t);
800 if (fconn->wait_event.tasklet)
801 tasklet_free(fconn->wait_event.tasklet);
802 pool_free(pool_head_fcgi_conn, fconn);
803 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200804 conn->ctx = conn_ctx; // restore saved ctx
805 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200806 return -1;
807}
808
809/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
810 * -1 if no more is allocatable.
811 */
812static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
813{
814 int32_t id = (fconn->max_id + 1) | 1;
815
816 if ((id & 0x80000000U))
817 id = -1;
818 return id;
819}
820
821/* Returns the stream associated with id <id> or NULL if not found */
822static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
823{
824 struct eb32_node *node;
825
826 if (id == 0)
827 return (struct fcgi_strm *)fcgi_mgmt_stream;
828
829 if (id > fconn->max_id)
830 return (struct fcgi_strm *)fcgi_unknown_stream;
831
832 node = eb32_lookup(&fconn->streams_by_id, id);
833 if (!node)
834 return (struct fcgi_strm *)fcgi_unknown_stream;
835 return container_of(node, struct fcgi_strm, by_id);
836}
837
838
839/* Release function. This one should be called to free all resources allocated
840 * to the mux.
841 */
842static void fcgi_release(struct fcgi_conn *fconn)
843{
William Dauchy477757c2020-08-07 22:19:23 +0200844 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200845
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200846 TRACE_POINT(FCGI_EV_FCONN_END);
847
Christopher Faulet99eff652019-08-11 23:11:30 +0200848 if (fconn) {
849 /* The connection must be attached to this mux to be released */
850 if (fconn->conn && fconn->conn->ctx == fconn)
851 conn = fconn->conn;
852
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200853 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
854
Willy Tarreau2b718102021-04-21 07:32:39 +0200855 if (LIST_INLIST(&fconn->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +0100856 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200857
858 fcgi_release_buf(fconn, &fconn->dbuf);
859 fcgi_release_mbuf(fconn);
860
861 if (fconn->task) {
862 fconn->task->context = NULL;
863 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
864 fconn->task = NULL;
865 }
866 if (fconn->wait_event.tasklet)
867 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200868 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200869 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
870 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200871
872 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200873 }
874
875 if (conn) {
876 conn->mux = NULL;
877 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200878 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200879
880 conn_stop_tracking(conn);
881 conn_full_close(conn);
882 if (conn->destroy_cb)
883 conn->destroy_cb(conn);
884 conn_free(conn);
885 }
886}
887
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200888/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
889 * pending on the connection AND if there is no more data in the demux
890 * buffer. The function returns 1 to report a read0 or 0 otherwise.
891 */
892static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
893{
894 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
895 return 1;
896 return 0;
897}
898
Christopher Faulet99eff652019-08-11 23:11:30 +0200899
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500900/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200901static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
902{
903 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
904 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
905 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
906 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
907 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
908 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
909 conn_xprt_read0_pending(fconn->conn))))
910 return 1;
911 return 0;
912}
913
914
915/********************************************************/
916/* functions below are for the FCGI protocol processing */
917/********************************************************/
918
Christopher Faulet99eff652019-08-11 23:11:30 +0200919/* Marks an error on the stream. */
920static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
921{
922 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200923 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
924 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200925 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200926 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
927 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200928 if (fstrm->cs)
929 cs_set_error(fstrm->cs);
930 }
931}
932
933/* Attempts to notify the data layer of recv availability */
934static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
935{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100936 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200937 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100938 tasklet_wakeup(fstrm->subs->tasklet);
939 fstrm->subs->events &= ~SUB_RETRY_RECV;
940 if (!fstrm->subs->events)
941 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200942 }
943}
944
945/* Attempts to notify the data layer of send availability */
946static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
947{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100948 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200949 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100950 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100951 tasklet_wakeup(fstrm->subs->tasklet);
952 fstrm->subs->events &= ~SUB_RETRY_SEND;
953 if (!fstrm->subs->events)
954 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200955 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100956 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
957 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
958 tasklet_wakeup(fstrm->shut_tl);
959 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200960}
961
962/* Alerts the data layer, trying to wake it up by all means, following
963 * this sequence :
964 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
965 * for recv
966 * - if its subscribed to send, then it's woken up for send
967 * - if it was subscribed to neither, its ->wake() callback is called
968 * It is safe to call this function with a closed stream which doesn't have a
969 * conn_stream anymore.
970 */
971static void fcgi_strm_alert(struct fcgi_strm *fstrm)
972{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200973 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100974 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100975 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200976 fcgi_strm_notify_recv(fstrm);
977 fcgi_strm_notify_send(fstrm);
978 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200979 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
980 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200981 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200982 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200983}
984
985/* Writes the 16-bit record size <len> at address <record> */
986static inline void fcgi_set_record_size(void *record, uint16_t len)
987{
988 uint8_t *out = (record + 4);
989
990 *out = (len >> 8);
991 *(out + 1) = (len & 0xff);
992}
993
994/* Writes the 16-bit stream id <id> at address <record> */
995static inline void fcgi_set_record_id(void *record, uint16_t id)
996{
997 uint8_t *out = (record + 2);
998
999 *out = (id >> 8);
1000 *(out + 1) = (id & 0xff);
1001}
1002
1003/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
1004 * its connection if the stream was not yet closed. Please use this exclusively
1005 * before closing a stream to ensure stream count is well maintained.
1006 */
1007static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
1008{
1009 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001010 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001011 fstrm->fconn->nb_streams--;
1012 if (!fstrm->id)
1013 fstrm->fconn->nb_reserved--;
1014 if (fstrm->cs) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001015 if (!(fstrm->endp->flags & CS_EP_EOS) && !b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001016 fcgi_strm_notify_recv(fstrm);
1017 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001018 fstrm->state = FCGI_SS_CLOSED;
1019 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1020 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001022}
1023
1024/* Detaches a FCGI stream from its FCGI connection and releases it to the
1025 * fcgi_strm pool.
1026 */
1027static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1028{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001029 struct connection *conn = fstrm->fconn->conn;
1030
1031 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1032
Christopher Faulet99eff652019-08-11 23:11:30 +02001033 fcgi_strm_close(fstrm);
1034 eb32_delete(&fstrm->by_id);
1035 if (b_size(&fstrm->rxbuf)) {
1036 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001037 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001038 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001039 if (fstrm->subs)
1040 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001041 /* There's no need to explicitly call unsubscribe here, the only
1042 * reference left would be in the fconn send_list/fctl_list, and if
1043 * we're in it, we're getting out anyway
1044 */
1045 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001046 tasklet_free(fstrm->shut_tl);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001047 BUG_ON(fstrm->endp && !(fstrm->endp->flags & CS_EP_ORPHAN));
1048 cs_endpoint_free(fstrm->endp);
Christopher Faulet99eff652019-08-11 23:11:30 +02001049 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001050
1051 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001052}
1053
1054/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1055 * stream tree. In case of error, nothing is added and NULL is returned. The
1056 * causes of errors can be any failed memory allocation. The caller is
1057 * responsible for checking if the connection may support an extra stream prior
1058 * to calling this function.
1059 */
1060static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1061{
1062 struct fcgi_strm *fstrm;
1063
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001064 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1065
Christopher Faulet99eff652019-08-11 23:11:30 +02001066 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +01001067 if (!fstrm) {
1068 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 +02001069 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +01001070 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001071
Willy Tarreau7aad7032020-01-16 17:20:57 +01001072 fstrm->shut_tl = tasklet_new();
1073 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001074 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 +02001075 pool_free(pool_head_fcgi_strm, fstrm);
1076 goto out;
1077 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001078 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001079 fstrm->shut_tl->process = fcgi_deferred_shut;
1080 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001081 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001082 fstrm->fconn = fconn;
1083 fstrm->cs = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001084 fstrm->endp = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001085 fstrm->flags = FCGI_SF_NONE;
1086 fstrm->proto_status = 0;
1087 fstrm->state = FCGI_SS_IDLE;
1088 fstrm->rxbuf = BUF_NULL;
1089
1090 h1m_init_res(&fstrm->h1m);
1091 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1092 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1093
1094 fstrm->by_id.key = fstrm->id = id;
1095 if (id > 0)
1096 fconn->max_id = id;
1097 else
1098 fconn->nb_reserved++;
1099
1100 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1101 fconn->nb_streams++;
1102 fconn->stream_cnt++;
1103
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001104 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001105 return fstrm;
1106
1107 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001108 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 +02001109 return NULL;
1110}
1111
1112/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1113 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1114 * highest possible stream ID was reached.
1115 */
1116static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1117 struct session *sess)
1118{
1119 struct fcgi_strm *fstrm = NULL;
1120
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001121 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1122 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001123 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 +02001124 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001125 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001126
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001127 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001128 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 +02001129 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001130 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001131
1132 /* Defer choosing the ID until we send the first message to create the stream */
1133 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001134 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001135 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 +02001136 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001137 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001138 cs_attach_mux(cs, fstrm, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001139 fstrm->cs = cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001140 fstrm->endp = cs->endp;
Christopher Faulet99eff652019-08-11 23:11:30 +02001141 fstrm->sess = sess;
Christopher Faulet99eff652019-08-11 23:11:30 +02001142 fconn->nb_cs++;
1143
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001144 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001145 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001146
1147 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001148 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001150}
1151
Christopher Fauletb041b232022-03-24 10:27:02 +01001152/* Wakes a specific stream and assign its conn_stream some CS_EP_* flags among
1153 * CS_EP_ERR_PENDING and CS_EP_ERROR if needed. The stream's state is
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 * automatically updated accordingly. If the stream is orphaned, it is
1155 * destroyed.
1156 */
1157static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1158{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001159 struct fcgi_conn *fconn = fstrm->fconn;
1160
1161 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1162
Christopher Faulet99eff652019-08-11 23:11:30 +02001163 if (!fstrm->cs) {
1164 /* this stream was already orphaned */
1165 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001166 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001167 return;
1168 }
1169
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001170 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001171 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001172 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001173 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001174 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001175 else if (fstrm->state == FCGI_SS_HLOC)
1176 fcgi_strm_close(fstrm);
1177 }
1178
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001179 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001180 fstrm->endp->flags |= CS_EP_ERR_PENDING;
1181 if (fstrm->endp->flags & CS_EP_EOS)
1182 fstrm->endp->flags |= CS_EP_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001183
1184 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001185 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001186 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1187 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001188 }
1189
1190 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001191
1192 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001193}
1194
1195/* Wakes unassigned streams (ID == 0) attached to the connection. */
1196static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1197{
1198 struct eb32_node *node;
1199 struct fcgi_strm *fstrm;
1200
1201 node = eb32_lookup(&fconn->streams_by_id, 0);
1202 while (node) {
1203 fstrm = container_of(node, struct fcgi_strm, by_id);
1204 if (fstrm->id > 0)
1205 break;
1206 node = eb32_next(node);
1207 fcgi_strm_wake_one_stream(fstrm);
1208 }
1209}
1210
1211/* Wakes the streams attached to the connection, whose id is greater than <last>
1212 * or unassigned.
1213 */
1214static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1215{
1216 struct eb32_node *node;
1217 struct fcgi_strm *fstrm;
1218
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001219 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1220
Christopher Faulet99eff652019-08-11 23:11:30 +02001221 /* Wake all streams with ID > last */
1222 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1223 while (node) {
1224 fstrm = container_of(node, struct fcgi_strm, by_id);
1225 node = eb32_next(node);
1226 fcgi_strm_wake_one_stream(fstrm);
1227 }
1228 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001229
1230 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001231}
1232
1233static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1234 struct htx *htx, struct htx_sl *sl,
1235 struct fcgi_strm_params *params)
1236{
1237 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
Christopher Faulet908628c2022-03-25 16:43:49 +01001238 const struct sockaddr_storage *src = (cs_check(fstrm->cs) ? conn_src(fconn->conn) : si_src(cs_opposite(fstrm->cs)->si));
1239 const struct sockaddr_storage *dst = (cs_check(fstrm->cs) ? conn_dst(fconn->conn) : si_dst(cs_opposite(fstrm->cs)->si));
Christopher Faulet99eff652019-08-11 23:11:30 +02001240 struct ist p;
1241
1242 if (!sl)
1243 goto error;
1244
1245 if (!(params->mask & FCGI_SP_DOC_ROOT))
1246 params->docroot = fconn->app->docroot;
1247
1248 if (!(params->mask & FCGI_SP_REQ_METH)) {
1249 p = htx_sl_req_meth(sl);
1250 params->meth = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001251 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001252 }
1253 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001254 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001255 params->uri = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001256 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001257 }
1258 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1259 p = htx_sl_req_vsn(sl);
1260 params->vsn = ist2(b_tail(params->p), p.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001261 chunk_istcat(params->p, p);
Christopher Faulet99eff652019-08-11 23:11:30 +02001262 }
1263 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1264 char *end;
1265 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001266 if (dst)
1267 port = get_host_port(dst);
Christopher Faulet99eff652019-08-11 23:11:30 +02001268 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1269 if (!end)
1270 goto error;
1271 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1272 params->p->data += params->srv_port.len;
1273 }
1274 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1275 /* If no Host header found, use the server address to fill
1276 * srv_name */
1277 if (!istlen(params->srv_name)) {
1278 char *ptr = NULL;
1279
Christopher Faulet568008d2021-10-25 07:56:51 +02001280 if (dst)
1281 if (addr_to_str(dst, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001282 ptr = b_tail(params->p);
1283 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001284 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001285 params->p->data += params->srv_name.len;
1286 }
1287 }
1288 }
1289 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1290 char *ptr = NULL;
1291
Christopher Faulet568008d2021-10-25 07:56:51 +02001292 if (src)
1293 if (addr_to_str(src, b_tail(params->p), b_room(params->p)) != -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001294 ptr = b_tail(params->p);
1295 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001296 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001297 params->p->data += params->rem_addr.len;
1298 }
1299 }
1300 if (!(params->mask & FCGI_SP_REM_PORT)) {
1301 char *end;
1302 int port = 0;
Christopher Faulet568008d2021-10-25 07:56:51 +02001303 if (src)
1304 port = get_host_port(src);
Christopher Faulet99eff652019-08-11 23:11:30 +02001305 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1306 if (!end)
1307 goto error;
1308 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1309 params->p->data += params->rem_port.len;
1310 }
1311 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1312 struct htx_blk *blk;
1313 enum htx_blk_type type;
1314 char *end;
1315 size_t len = 0;
1316
1317 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1318 type = htx_get_blk_type(blk);
1319
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001320 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001321 break;
1322 if (type == HTX_BLK_DATA)
1323 len += htx_get_blksz(blk);
1324 }
1325 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1326 if (!end)
1327 goto error;
1328 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1329 params->p->data += params->cont_len.len;
1330 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001331
Christopher Faulet99eff652019-08-11 23:11:30 +02001332 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001333 if (cli_conn)
Willy Tarreau1057bee2021-10-06 11:38:44 +02001334 params->https = conn_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001335 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001336
Christopher Faulet99eff652019-08-11 23:11:30 +02001337 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1338 /* one of scriptname, pathinfo or query_string is no set */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001339 struct http_uri_parser parser = http_uri_parser_init(params->uri);
1340 struct ist path = http_parse_path(&parser);
Christopher Faulet99eff652019-08-11 23:11:30 +02001341 int len;
1342
Christopher Faulet99eff652019-08-11 23:11:30 +02001343 /* No scrit_name set but no valid path ==> error */
1344 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1345 goto error;
1346
Christopher Faulet99eff652019-08-11 23:11:30 +02001347 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001348 if (!(params->mask & FCGI_SP_REQ_QS)) {
1349 struct ist qs = istfind(path, '?');
1350
1351 /* Update the path length */
1352 path.len -= qs.len;
1353
1354 /* Set the query-string skipping the '?', if any */
1355 if (istlen(qs))
1356 params->qs = istnext(qs);
1357 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001358
1359 /* If the script_name is set, don't try to deduce the path_info
1360 * too. The opposite is not true.
1361 */
1362 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1363 params->mask |= FCGI_SP_PATH_INFO;
1364 goto end;
1365 }
1366
Christopher Faulet0f17a442020-07-23 15:44:37 +02001367 /* Decode the path. it must first be copied to keep the URI
1368 * untouched.
1369 */
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001370 chunk_istcat(params->p, path);
Christopher Faulet0f17a442020-07-23 15:44:37 +02001371 path.ptr = b_tail(params->p) - path.len;
1372 len = url_decode(ist0(path), 0);
1373 if (len < 0)
1374 goto error;
1375 path.len = len;
1376
Christopher Faulet99eff652019-08-11 23:11:30 +02001377 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001378 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001379
1380 /* If there is no regex to match the pathinfo, just to the last
1381 * part and see if the index must be used.
1382 */
1383 if (!fconn->app->pathinfo_re)
1384 goto check_index;
1385
Christopher Faulet28cb3662020-02-14 14:47:37 +01001386 /* If some special characters are found in the decoded path (\n
Ilya Shipitsin01881082021-08-07 14:41:56 +05001387 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001388 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001389 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001390 * case.
1391 */
1392 if (istchr(path, '\n') || istchr(path, '\0'))
1393 goto error;
1394
Christopher Faulet99eff652019-08-11 23:11:30 +02001395 /* The regex does not match, just to the last part and see if
1396 * the index must be used.
1397 */
1398 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1399 goto check_index;
1400
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001401 /* We must have at least 1 capture for the script name,
1402 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001403 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001404 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001405 goto check_index;
1406
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001407 /* Finally we can set the script_name and the path_info. The
1408 * path_info is set if not already defined, and if it was
1409 * captured
1410 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001411 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001412 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1413 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001414
1415 check_index:
1416 len = params->scriptname.len;
1417 /* the script_name if finished by a '/' so we can add the index
1418 * part, if any.
1419 */
1420 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1421 struct ist sn = params->scriptname;
1422
1423 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +01001424 chunk_istcat(params->p, sn);
Tim Duesterhus77508502022-03-15 13:11:06 +01001425 chunk_istcat(params->p, fconn->app->index);
Christopher Faulet99eff652019-08-11 23:11:30 +02001426 }
1427 }
1428
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001429 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1430 params->srv_soft = ist2(b_tail(params->p), 0);
1431 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1432 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1433 }
1434
Christopher Faulet99eff652019-08-11 23:11:30 +02001435 end:
1436 return 1;
1437 error:
1438 return 0;
1439}
1440
1441static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1442 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1443{
1444 struct fcgi_param p;
1445
1446 if (params->mask & flag)
1447 return 1;
1448
1449 chunk_reset(&trash);
1450
1451 switch (flag) {
1452 case FCGI_SP_CGI_GATEWAY:
1453 p.n = ist("GATEWAY_INTERFACE");
1454 p.v = ist("CGI/1.1");
1455 goto encode;
1456 case FCGI_SP_DOC_ROOT:
1457 p.n = ist("DOCUMENT_ROOT");
1458 p.v = params->docroot;
1459 goto encode;
1460 case FCGI_SP_SCRIPT_NAME:
1461 p.n = ist("SCRIPT_NAME");
1462 p.v = params->scriptname;
1463 goto encode;
1464 case FCGI_SP_PATH_INFO:
1465 p.n = ist("PATH_INFO");
1466 p.v = params->pathinfo;
1467 goto encode;
1468 case FCGI_SP_REQ_URI:
1469 p.n = ist("REQUEST_URI");
1470 p.v = params->uri;
1471 goto encode;
1472 case FCGI_SP_REQ_METH:
1473 p.n = ist("REQUEST_METHOD");
1474 p.v = params->meth;
1475 goto encode;
1476 case FCGI_SP_REQ_QS:
1477 p.n = ist("QUERY_STRING");
1478 p.v = params->qs;
1479 goto encode;
1480 case FCGI_SP_SRV_NAME:
1481 p.n = ist("SERVER_NAME");
1482 p.v = params->srv_name;
1483 goto encode;
1484 case FCGI_SP_SRV_PORT:
1485 p.n = ist("SERVER_PORT");
1486 p.v = params->srv_port;
1487 goto encode;
1488 case FCGI_SP_SRV_PROTO:
1489 p.n = ist("SERVER_PROTOCOL");
1490 p.v = params->vsn;
1491 goto encode;
1492 case FCGI_SP_REM_ADDR:
1493 p.n = ist("REMOTE_ADDR");
1494 p.v = params->rem_addr;
1495 goto encode;
1496 case FCGI_SP_REM_PORT:
1497 p.n = ist("REMOTE_PORT");
1498 p.v = params->rem_port;
1499 goto encode;
1500 case FCGI_SP_SCRIPT_FILE:
1501 p.n = ist("SCRIPT_FILENAME");
Tim Duesterhus77508502022-03-15 13:11:06 +01001502 chunk_istcat(&trash, params->docroot);
1503 chunk_istcat(&trash, params->scriptname);
Christopher Faulet99eff652019-08-11 23:11:30 +02001504 p.v = ist2(b_head(&trash), b_data(&trash));
1505 goto encode;
1506 case FCGI_SP_PATH_TRANS:
1507 if (!istlen(params->pathinfo))
1508 goto skip;
1509 p.n = ist("PATH_TRANSLATED");
Tim Duesterhus77508502022-03-15 13:11:06 +01001510 chunk_istcat(&trash, params->docroot);
1511 chunk_istcat(&trash, params->pathinfo);
Christopher Faulet99eff652019-08-11 23:11:30 +02001512 p.v = ist2(b_head(&trash), b_data(&trash));
1513 goto encode;
1514 case FCGI_SP_CONT_LEN:
1515 p.n = ist("CONTENT_LENGTH");
1516 p.v = params->cont_len;
1517 goto encode;
1518 case FCGI_SP_HTTPS:
1519 if (!params->https)
1520 goto skip;
1521 p.n = ist("HTTPS");
1522 p.v = ist("on");
1523 goto encode;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001524 case FCGI_SP_SRV_SOFT:
1525 p.n = ist("SERVER_SOFTWARE");
1526 p.v = params->srv_soft;
1527 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001528 default:
1529 goto skip;
1530 }
1531
1532 encode:
1533 if (!istlen(p.v))
1534 goto skip;
1535 if (!fcgi_encode_param(outbuf, &p))
1536 return 0;
1537 skip:
1538 params->mask |= flag;
1539 return 1;
1540}
1541
1542/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1543 * anything. It is highly unexpected, but if the record is larger than a buffer
1544 * and cannot be encoded in one time, an error is triggered and the connection is
1545 * closed. GET_VALUES record cannot be split.
1546 */
1547static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1548{
1549 struct buffer outbuf;
1550 struct buffer *mbuf;
1551 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1552 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001553 int ret = 0;
1554
1555 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001556
1557 mbuf = br_tail(fconn->mbuf);
1558 retry:
1559 if (!fcgi_get_buf(fconn, mbuf)) {
1560 fconn->flags |= FCGI_CF_MUX_MALLOC;
1561 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001562 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1563 ret = 0;
1564 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001565 }
1566
1567 while (1) {
1568 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001569 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001570 break;
1571 realign_again:
1572 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1573 }
1574
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001575 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001576 goto full;
1577
1578 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1579 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001580 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1581 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001582
1583 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1584 * handled by HAProxy.
1585 */
1586 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1587 goto full;
1588
1589 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001590 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 +01001591 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001592 b_add(mbuf, outbuf.data);
1593 ret = 1;
1594
1595 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001596 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001597 return ret;
1598 full:
1599 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001600 if (!b_data(mbuf)) {
1601 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 +02001602 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001603 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001604
1605 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1606 goto retry;
1607 fconn->flags |= FCGI_CF_MUX_MFULL;
1608 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001609 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001610 ret = 0;
1611 goto end;
1612 fail:
1613 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001614 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1615 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1616 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001617}
1618
1619/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1620 * couldn't do anything. It is highly unexpected, but if the record is larger
1621 * than a buffer and cannot be decoded in one time, an error is triggered and
1622 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1623 */
1624static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1625{
1626 struct buffer inbuf;
1627 struct buffer *dbuf;
1628 size_t offset;
1629
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001630 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1631
Christopher Faulet99eff652019-08-11 23:11:30 +02001632 dbuf = &fconn->dbuf;
1633
1634 /* Record too large to be fully decoded */
1635 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1636 goto fail;
1637
1638 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001639 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1640 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001641 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001642 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001643
1644 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1645 /* Realign the dmux buffer if the record wraps. It is unexpected
1646 * at this stage because it should be the first record received
1647 * from the FCGI application.
1648 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01001649 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001650 }
1651
1652 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1653
1654 for (offset = 0; offset < b_data(&inbuf); ) {
1655 struct fcgi_param p;
1656 size_t ret;
1657
1658 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1659 if (!ret) {
1660 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001661 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 +02001662 goto fail;
1663 }
1664 offset += ret;
1665
1666 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001667 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001668 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 +02001669 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001670 }
1671 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001672 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 +02001673 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001674 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001675 }
1676 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1677 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001678 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 +02001679 }
1680 /*
1681 * Ignore all other params
1682 */
1683 }
1684
1685 /* Reset the number of concurrent streams supported if the FCGI
1686 * application does not support connection multiplexing
1687 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001688 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001689 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001690 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1691 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001692
1693 /* We must be sure to have read exactly the announced record length, no
1694 * more no less
1695 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001696 if (offset != fconn->drl) {
1697 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 +02001698 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001699 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001700
Willy Tarreau022e5e52020-09-10 09:33:15 +02001701 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 +02001702 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1703 fconn->drl = 0;
1704 fconn->drp = 0;
1705 fconn->state = FCGI_CS_RECORD_H;
1706 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001707 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1708 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001709 return 1;
1710 fail:
1711 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001712 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1713 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 +02001714 return 0;
1715}
1716
1717/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1718 * excluded, as the streams which already received the end-of-stream. It returns
1719 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1720 */
1721static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1722{
1723 struct eb32_node *node;
1724 struct fcgi_strm *fstrm;
1725
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001726 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1727
Christopher Faulet99eff652019-08-11 23:11:30 +02001728 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1729 while (node) {
1730 fstrm = container_of(node, struct fcgi_strm, by_id);
1731 node = eb32_next(node);
1732 if (fstrm->state != FCGI_SS_CLOSED &&
1733 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1734 !fcgi_strm_send_abort(fconn, fstrm))
1735 return 0;
1736 }
1737 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001738 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1739 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001740 return 1;
1741}
1742
1743/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1744 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1745 * space to proceed. It is small enough to be encoded in an empty buffer.
1746 */
1747static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1748{
1749 struct buffer outbuf;
1750 struct buffer *mbuf;
1751 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1752 int ret;
1753
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001754 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1755
Christopher Faulet99eff652019-08-11 23:11:30 +02001756 mbuf = br_tail(fconn->mbuf);
1757 retry:
1758 if (!fcgi_get_buf(fconn, mbuf)) {
1759 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001760 fstrm->flags |= FCGI_SF_BLK_MROOM;
1761 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1762 ret = 0;
1763 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001764 }
1765
1766 while (1) {
1767 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001768 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001769 break;
1770 realign_again:
1771 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1772 }
1773
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001774 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001775 goto full;
1776
1777 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1778 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001779 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001780 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001781 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001782
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001783 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1784 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001785 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001786 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001787 if (!fcgi_encode_begin_request(&outbuf, &rec))
1788 goto full;
1789
1790 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001791 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 +02001792 b_add(mbuf, outbuf.data);
1793 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1794 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001795 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001796 ret = 1;
1797
1798 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001799 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001800 return ret;
1801 full:
1802 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1803 goto retry;
1804 fconn->flags |= FCGI_CF_MUX_MFULL;
1805 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001806 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 +02001807 ret = 0;
1808 goto end;
1809}
1810
1811/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1812 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1813 * space to proceed. It is small enough to be encoded in an empty buffer.
1814 */
1815static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1816 enum fcgi_record_type rtype)
1817{
1818 struct buffer outbuf;
1819 struct buffer *mbuf;
1820 int ret;
1821
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001822 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001823 mbuf = br_tail(fconn->mbuf);
1824 retry:
1825 if (!fcgi_get_buf(fconn, mbuf)) {
1826 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001827 fstrm->flags |= FCGI_SF_BLK_MROOM;
1828 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1829 ret = 0;
1830 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001831 }
1832
1833 while (1) {
1834 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001835 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001836 break;
1837 realign_again:
1838 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1839 }
1840
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001841 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001842 goto full;
1843
1844 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1845 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001846 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001847 outbuf.area[1] = rtype;
1848 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001849 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001850
1851 /* commit the record */
1852 b_add(mbuf, outbuf.data);
1853 ret = 1;
1854
1855 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001856 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 return ret;
1858 full:
1859 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1860 goto retry;
1861 fconn->flags |= FCGI_CF_MUX_MFULL;
1862 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001863 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 +02001864 ret = 0;
1865 goto end;
1866}
1867
1868
1869/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1870 * marks the end of params.
1871 */
1872static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1873{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001874 int ret;
1875
1876 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1877 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001878 if (ret) {
1879 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001880 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 +01001881 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001882 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001883}
1884
1885/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1886 * marks the end of input. On success, all the request was successfully sent.
1887 */
1888static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1889{
1890 int ret;
1891
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001892 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001893 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001894 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001895 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001896 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 +02001897 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1898 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1899 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001900 return ret;
1901}
1902
1903/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1904 * stops the request processing.
1905 */
1906static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1907{
1908 int ret;
1909
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001910 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001911 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001912 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001913 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001914 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 +02001915 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1916 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1917 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001918 return ret;
1919}
1920
1921/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1922 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1923 * several records are sent. However, a K/V param cannot be split between 2
1924 * records.
1925 */
1926static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1927 struct htx *htx)
1928{
1929 struct buffer outbuf;
1930 struct buffer *mbuf;
1931 struct htx_blk *blk;
1932 struct htx_sl *sl = NULL;
1933 struct fcgi_strm_params params;
1934 size_t total = 0;
1935
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001936 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1937
Christopher Faulet99eff652019-08-11 23:11:30 +02001938 memset(&params, 0, sizeof(params));
1939 params.p = get_trash_chunk();
1940
1941 mbuf = br_tail(fconn->mbuf);
1942 retry:
1943 if (!fcgi_get_buf(fconn, mbuf)) {
1944 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001945 fstrm->flags |= FCGI_SF_BLK_MROOM;
1946 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1947 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001948 }
1949
1950 while (1) {
1951 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001952 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001953 break;
1954 realign_again:
1955 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1956 }
1957
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001958 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001959 goto full;
1960
1961 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1962 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001963 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001964 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001965 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001966
1967 blk = htx_get_head_blk(htx);
1968 while (blk) {
1969 enum htx_blk_type type;
1970 uint32_t size = htx_get_blksz(blk);
1971 struct fcgi_param p;
1972
1973 type = htx_get_blk_type(blk);
1974 switch (type) {
1975 case HTX_BLK_REQ_SL:
1976 sl = htx_get_blk_ptr(htx, blk);
1977 if (sl->info.req.meth == HTTP_METH_HEAD)
1978 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1979 if (sl->flags & HTX_SL_F_VER_11)
1980 fstrm->h1m.flags |= H1_MF_VER_11;
1981 break;
1982
1983 case HTX_BLK_HDR:
1984 p.n = htx_get_blk_name(htx, blk);
1985 p.v = htx_get_blk_value(htx, blk);
1986
1987 if (istmatch(p.n, ist(":fcgi-"))) {
Tim Duesterhusa6a32792022-03-05 00:52:45 +01001988 p.n = istadv(p.n, 6);
Christopher Faulet99eff652019-08-11 23:11:30 +02001989 if (isteq(p.n, ist("gateway_interface")))
1990 params.mask |= FCGI_SP_CGI_GATEWAY;
1991 else if (isteq(p.n, ist("document_root"))) {
1992 params.mask |= FCGI_SP_DOC_ROOT;
1993 params.docroot = p.v;
1994 }
1995 else if (isteq(p.n, ist("script_name"))) {
1996 params.mask |= FCGI_SP_SCRIPT_NAME;
1997 params.scriptname = p.v;
1998 }
1999 else if (isteq(p.n, ist("path_info"))) {
2000 params.mask |= FCGI_SP_PATH_INFO;
2001 params.pathinfo = p.v;
2002 }
2003 else if (isteq(p.n, ist("request_uri"))) {
2004 params.mask |= FCGI_SP_REQ_URI;
2005 params.uri = p.v;
2006 }
2007 else if (isteq(p.n, ist("request_meth")))
2008 params.mask |= FCGI_SP_REQ_METH;
2009 else if (isteq(p.n, ist("query_string")))
2010 params.mask |= FCGI_SP_REQ_QS;
2011 else if (isteq(p.n, ist("server_name")))
2012 params.mask |= FCGI_SP_SRV_NAME;
2013 else if (isteq(p.n, ist("server_port")))
2014 params.mask |= FCGI_SP_SRV_PORT;
2015 else if (isteq(p.n, ist("server_protocol")))
2016 params.mask |= FCGI_SP_SRV_PROTO;
2017 else if (isteq(p.n, ist("remote_addr")))
2018 params.mask |= FCGI_SP_REM_ADDR;
2019 else if (isteq(p.n, ist("remote_port")))
2020 params.mask |= FCGI_SP_REM_PORT;
2021 else if (isteq(p.n, ist("script_filename")))
2022 params.mask |= FCGI_SP_SCRIPT_FILE;
2023 else if (isteq(p.n, ist("path_translated")))
2024 params.mask |= FCGI_SP_PATH_TRANS;
2025 else if (isteq(p.n, ist("https")))
2026 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002027 else if (isteq(p.n, ist("server_software")))
2028 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02002029 }
2030 else if (isteq(p.n, ist("content-length"))) {
2031 p.n = ist("CONTENT_LENGTH");
2032 params.mask |= FCGI_SP_CONT_LEN;
2033 }
2034 else if (isteq(p.n, ist("content-type")))
2035 p.n = ist("CONTENT_TYPE");
2036 else {
Tim Duesterhus98f05f62022-03-05 00:52:44 +01002037 struct ist n;
2038
Christopher Faulet99eff652019-08-11 23:11:30 +02002039 if (isteq(p.n, ist("host")))
2040 params.srv_name = p.v;
Christopher Fauletf56e8462021-09-28 10:56:36 +02002041 else if (isteq(p.n, ist("te"))) {
2042 /* "te" may only be sent with "trailers" if this value
2043 * is present, otherwise it must be deleted.
2044 */
2045 p.v = istist(p.v, ist("trailers"));
2046 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
2047 break;
2048 p.v = ist("trailers");
2049 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002050
Christopher Faulet67d58092019-10-02 10:51:38 +02002051 /* Skip header if same name is used to add the server name */
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002052 if (isttest(fconn->proxy->server_id_hdr_name) && isteq(p.n, fconn->proxy->server_id_hdr_name))
Christopher Faulet67d58092019-10-02 10:51:38 +02002053 break;
2054
Tim Duesterhus98f05f62022-03-05 00:52:44 +01002055 n = ist2(trash.area, 0);
2056 istcat(&n, ist("http_"), trash.size);
2057 istcat(&n, p.n, trash.size);
2058 p.n = n;
Christopher Faulet99eff652019-08-11 23:11:30 +02002059 }
2060
2061 if (!fcgi_encode_param(&outbuf, &p)) {
2062 if (b_space_wraps(mbuf))
2063 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002064 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002065 goto full;
2066 goto done;
2067 }
2068 break;
2069
2070 case HTX_BLK_EOH:
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002071 if (isttest(fconn->proxy->server_id_hdr_name)) {
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002072 struct server *srv = objt_server(fconn->conn->target);
2073
2074 if (!srv)
2075 goto done;
2076
Tim Duesterhusb4b03772022-03-05 00:52:43 +01002077 p.n = ist2(trash.area, 0);
2078 istcat(&p.n, ist("http_"), trash.size);
2079 istcat(&p.n, fconn->proxy->server_id_hdr_name, trash.size);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002080 p.v = ist(srv->id);
2081
2082 if (!fcgi_encode_param(&outbuf, &p)) {
2083 if (b_space_wraps(mbuf))
2084 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002085 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002086 goto full;
2087 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002088 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002089 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002090 goto done;
2091
2092 default:
2093 break;
2094 }
2095 total += size;
2096 blk = htx_remove_blk(htx, blk);
2097 }
2098
2099 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002100 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2101 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002102 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002103 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002104
2105 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2106 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2107 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2108 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2109 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2110 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2111 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2112 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2113 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2114 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2115 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2116 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2117 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2118 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2119 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002120 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002121 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2122 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002123 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002124 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002125
2126 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002127 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});
2128 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002129 b_add(mbuf, outbuf.data);
2130
2131 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002132 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002133 return total;
2134 full:
2135 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2136 goto retry;
2137 fconn->flags |= FCGI_CF_MUX_MFULL;
2138 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002139 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 +02002140 if (total)
2141 goto error;
2142 goto end;
2143
2144 error:
2145 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002146 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 +02002147 fcgi_strm_error(fstrm);
2148 goto end;
2149}
2150
2151/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2152 * anything. STDIN records contain the request body.
2153 */
2154static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2155 struct htx *htx, size_t count, struct buffer *buf)
2156{
2157 struct buffer outbuf;
2158 struct buffer *mbuf;
2159 struct htx_blk *blk;
2160 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002161 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002162 size_t total = 0;
2163
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002164 extra_bytes = 0;
2165
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002166 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002167 if (!count)
2168 goto end;
2169
2170 mbuf = br_tail(fconn->mbuf);
2171 retry:
2172 if (!fcgi_get_buf(fconn, mbuf)) {
2173 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002174 fstrm->flags |= FCGI_SF_BLK_MROOM;
2175 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2176 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002177 }
2178
2179 /* Perform some optimizations to reduce the number of buffer copies.
2180 * First, if the mux's buffer is empty and the htx area contains exactly
2181 * one data block of the same size as the requested count, and this
2182 * count fits within the record size, then it's possible to simply swap
2183 * the caller's buffer with the mux's output buffer and adjust offsets
2184 * and length to match the entire DATA HTX block in the middle. In this
2185 * case we perform a true zero-copy operation from end-to-end. This is
2186 * the situation that happens all the time with large files. Second, if
2187 * this is not possible, but the mux's output buffer is empty, we still
2188 * have an opportunity to avoid the copy to the intermediary buffer, by
2189 * making the intermediary buffer's area point to the output buffer's
2190 * area. In this case we want to skip the HTX header to make sure that
2191 * copies remain aligned and that this operation remains possible all
2192 * the time. This goes for headers, data blocks and any data extracted
2193 * from the HTX blocks.
2194 */
2195 blk = htx_get_head_blk(htx);
2196 if (!blk)
2197 goto end;
2198 type = htx_get_blk_type(blk);
2199 size = htx_get_blksz(blk);
2200 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2201 void *old_area = mbuf->area;
2202
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002203 /* Last block of the message: Reserve the size for the empty stdin record */
2204 if (htx->flags & HTX_FL_EOM)
2205 extra_bytes = FCGI_RECORD_HEADER_SZ;
2206
Christopher Faulet99eff652019-08-11 23:11:30 +02002207 if (b_data(mbuf)) {
2208 /* Too bad there are data left there. We're willing to memcpy/memmove
2209 * up to 1/4 of the buffer, which means that it's OK to copy a large
2210 * record into a buffer containing few data if it needs to be realigned,
2211 * and that it's also OK to copy few data without realigning. Otherwise
2212 * we'll pretend the mbuf is full and wait for it to become empty.
2213 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002214 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002215 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002217 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002218 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002219 }
2220
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002221 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 +02002222 /* map a FCGI record to the HTX block so that we can put the
2223 * record header there.
2224 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002225 *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 +02002226 outbuf.area = b_head(mbuf);
2227
2228 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002229 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002230 fcgi_set_record_id(outbuf.area, fstrm->id);
2231 fcgi_set_record_size(outbuf.area, size);
2232
2233 /* and exchange with our old area */
2234 buf->area = old_area;
2235 buf->data = buf->head = 0;
2236 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002237
2238 htx = (struct htx *)buf->area;
2239 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002240 goto end;
2241 }
2242
2243 copy:
2244 while (1) {
2245 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002246 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002247 break;
2248 realign_again:
2249 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2250 }
2251
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002252 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002253 goto full;
2254
2255 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2256 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002257 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002258 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002259 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002260
2261 blk = htx_get_head_blk(htx);
2262 while (blk && count) {
2263 enum htx_blk_type type = htx_get_blk_type(blk);
2264 uint32_t size = htx_get_blksz(blk);
2265 struct ist v;
2266
2267 switch (type) {
2268 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002269 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 +02002270 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002271
2272 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2273 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2274
2275 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002276 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002277 extra_bytes = 0;
2278 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002279
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002280 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002281 /* It doesn't fit at once. If it at least fits once split and
2282 * the amount of data to move is low, let's defragment the
2283 * buffer now.
2284 */
2285 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002286 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002287 b_data(mbuf) <= MAX_DATA_REALIGN)
2288 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002289 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002290 }
2291 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002292 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002293 goto full;
2294 goto done;
2295 }
2296 if (v.len != size) {
2297 total += v.len;
2298 count -= v.len;
2299 htx_cut_data_blk(htx, blk, v.len);
2300 goto done;
2301 }
2302 break;
2303
Christopher Faulet99eff652019-08-11 23:11:30 +02002304 default:
2305 break;
2306 }
2307 total += size;
2308 count -= size;
2309 blk = htx_remove_blk(htx, blk);
2310 }
2311
2312 done:
2313 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002314 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});
2315 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002316 b_add(mbuf, outbuf.data);
2317
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002318 /* Send the empty stding here to finish the message */
2319 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2320 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2321 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2322 /* bytes already reserved for this record. It should not fail */
2323 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002324 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 +01002325 fcgi_strm_error(fstrm);
2326 }
2327 }
2328
Christopher Faulet99eff652019-08-11 23:11:30 +02002329 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002331 return total;
2332 full:
2333 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2334 goto retry;
2335 fconn->flags |= FCGI_CF_MUX_MFULL;
2336 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002337 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 +02002338 goto end;
2339}
2340
2341/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2342 * anything. STDOUT records contain the entire response. All the content is
2343 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2344 */
2345static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2346{
2347 struct buffer *dbuf;
2348 size_t ret;
2349 size_t max;
2350
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002351 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2352
Christopher Faulet99eff652019-08-11 23:11:30 +02002353 dbuf = &fconn->dbuf;
2354
2355 /* Only padding remains */
2356 if (fconn->state == FCGI_CS_RECORD_P)
2357 goto end_transfer;
2358
2359 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2360 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2361 buf_room_for_htx_data(dbuf))
2362 goto fail; // incomplete record
2363
2364 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2365 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002366 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2367 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002368 }
2369
2370 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2371 max = buf_room_for_htx_data(&fstrm->rxbuf);
2372 if (!b_data(&fstrm->rxbuf))
2373 fstrm->rxbuf.head = sizeof(struct htx);
2374 if (max > fconn->drl)
2375 max = fconn->drl;
2376
2377 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2378 if (!ret)
2379 goto fail;
2380 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002381 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2382 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 +02002383
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002384 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002385 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002386 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2387 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002388
2389 if (fconn->drl)
2390 goto fail;
2391
2392 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002393 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002394 fconn->drl += fconn->drp;
2395 fconn->drp = 0;
2396 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2397 b_del(&fconn->dbuf, ret);
2398 fconn->drl -= ret;
2399 if (fconn->drl)
2400 goto fail;
2401
2402 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002403 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2404 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002405 return 1;
2406 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002407 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 +02002408 return 0;
2409}
2410
2411
2412/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2413 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002414 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002415 */
2416static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2417{
2418 int ret;
2419
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2421
Christopher Faulet99eff652019-08-11 23:11:30 +02002422 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002423 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002424 fconn->drl += fconn->drp;
2425 fconn->drp = 0;
2426 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2427 b_del(&fconn->dbuf, ret);
2428 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002429 if (fconn->drl) {
2430 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 +02002431 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002432 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002433 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002434 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002435 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 +02002436 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);
2437 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002438 return 1;
2439}
2440
2441/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2442 * anything.
2443 */
2444static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2445{
2446 struct buffer *dbuf;
2447 struct buffer tag;
2448 size_t ret;
2449
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002450 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002451 dbuf = &fconn->dbuf;
2452
2453 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002454 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002455 goto end_transfer;
2456
2457 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2458 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2459 buf_room_for_htx_data(dbuf))
2460 goto fail; // incomplete record
2461
2462 chunk_reset(&trash);
2463 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2464 if (!ret)
2465 goto fail;
2466 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002467 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 +02002468
2469 trash.area[ret] = '\n';
2470 trash.area[ret+1] = '\0';
2471 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002472 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002473
2474 if (fconn->drl)
2475 goto fail;
2476
2477 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002478 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002479 fconn->drl += fconn->drp;
2480 fconn->drp = 0;
2481 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2482 b_del(&fconn->dbuf, ret);
2483 fconn->drl -= ret;
2484 if (fconn->drl)
2485 goto fail;
2486 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002487 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2488 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002489 return 1;
2490 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002491 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 +02002492 return 0;
2493}
2494
2495/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2496 * anything. If the empty STDOUT record is not already received, this one marks
2497 * the end of the response. It is highly unexpected, but if the record is larger
2498 * than a buffer and cannot be decoded in one time, an error is triggered and
2499 * the connection is closed. END_REQUEST record cannot be split.
2500 */
2501static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2502{
2503 struct buffer inbuf;
2504 struct buffer *dbuf;
2505 struct fcgi_end_request endreq;
2506
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002507 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002508 dbuf = &fconn->dbuf;
2509
2510 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002511 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2512 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 +02002513 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002514 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002515
2516 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002517 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2518 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002519 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002520 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002521
2522 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2523 /* Realign the dmux buffer if the record wraps. It is unexpected
2524 * at this stage because it should be the first record received
2525 * from the FCGI application.
2526 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002527 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002528 }
2529
2530 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2531
Christopher Faulet73518be2021-01-27 12:06:54 +01002532 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2533 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 +02002534 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002535 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002536
2537 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002538 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 +02002539 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 +02002540 fstrm->proto_status = endreq.errcode;
2541 fcgi_strm_close(fstrm);
2542
2543 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2544 fconn->drl = 0;
2545 fconn->drp = 0;
2546 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002547 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2548 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002549 return 1;
2550
2551 fail:
2552 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002553 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 +02002554 return 0;
2555}
2556
2557/* process Rx records to be demultiplexed */
2558static void fcgi_process_demux(struct fcgi_conn *fconn)
2559{
2560 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2561 struct fcgi_header hdr;
2562 int ret;
2563
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002564 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2565
Christopher Faulet99eff652019-08-11 23:11:30 +02002566 if (fconn->state == FCGI_CS_CLOSED)
2567 return;
2568
2569 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002570 if (fconn->state == FCGI_CS_INIT) {
2571 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2572 return;
2573 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002574 if (fconn->state == FCGI_CS_SETTINGS) {
2575 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002576 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002577 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002578 if (!ret) {
2579 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 +02002580 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002581 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002582 b_del(&fconn->dbuf, ret);
2583
2584 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2585 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002586 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 +02002587 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 +02002588 goto fail;
2589 }
2590 goto new_record;
2591 }
2592 }
2593
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002594 /* process as many incoming records as possible below */
2595 while (1) {
2596 if (!b_data(&fconn->dbuf)) {
2597 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2598 break;
2599 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002600
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 if (fconn->state == FCGI_CS_CLOSED) {
2602 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002603 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002604 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002605
2606 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002607 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002608 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2609 if (!ret)
2610 break;
2611 b_del(&fconn->dbuf, ret);
2612
2613 new_record:
2614 fconn->dsi = hdr.id;
2615 fconn->drt = hdr.type;
2616 fconn->drl = hdr.len;
2617 fconn->drp = hdr.padding;
2618 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002619 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 +02002620 }
2621
2622 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2623 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2624
2625 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2626 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002627 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002628 fstrm->state == FCGI_SS_CLOSED ||
2629 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Christopher Fauletb041b232022-03-24 10:27:02 +01002630 (fstrm->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING|CS_EP_EOS)))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002631 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002632 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Fauletb041b232022-03-24 10:27:02 +01002633 fstrm->endp->flags |= CS_EP_RCV_MORE;
Christopher Faulet99eff652019-08-11 23:11:30 +02002634 fcgi_strm_notify_recv(fstrm);
2635 }
2636 fstrm = tmp_fstrm;
2637
2638 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2639 /* ignore all record for closed streams */
2640 goto ignore_record;
2641 }
2642 if (fstrm->state == FCGI_SS_IDLE) {
2643 /* ignore all record for unknown streams */
2644 goto ignore_record;
2645 }
2646
2647 switch (fconn->drt) {
2648 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002649 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 +02002650 ret = fcgi_conn_handle_values_result(fconn);
2651 break;
2652
2653 case FCGI_STDOUT:
2654 if (fstrm->flags & FCGI_SF_ES_RCVD)
2655 goto ignore_record;
2656
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002657 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002658 if (fconn->drl)
2659 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2660 else
2661 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2662 break;
2663
2664 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002665 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002666 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2667 break;
2668
2669 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002670 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 +02002671 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2672 break;
2673
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002674 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002675 default:
2676 ignore_record:
2677 /* drop records that we ignore. They may be
2678 * larger than the buffer so we drain all of
2679 * their contents until we reach the end.
2680 */
2681 fconn->state = FCGI_CS_RECORD_P;
2682 fconn->drl += fconn->drp;
2683 fconn->drp = 0;
2684 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002685 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002686 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002687 b_del(&fconn->dbuf, ret);
2688 fconn->drl -= ret;
2689 ret = (fconn->drl == 0);
2690 }
2691
2692 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002693 if (ret <= 0) {
2694 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002695 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002696 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002697
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002698 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002699 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002700 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2701 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002702 }
2703
2704 fail:
2705 /* we can go here on missing data, blocked response or error */
2706 if (fstrm && fstrm->cs &&
2707 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002708 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002709 fstrm->state == FCGI_SS_CLOSED ||
2710 (fstrm->flags & FCGI_SF_ES_RCVD) ||
Christopher Fauletb041b232022-03-24 10:27:02 +01002711 (fstrm->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING|CS_EP_EOS)))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002712 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002713 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Fauletb041b232022-03-24 10:27:02 +01002714 fstrm->endp->flags |= CS_EP_RCV_MORE;
Christopher Faulet99eff652019-08-11 23:11:30 +02002715 fcgi_strm_notify_recv(fstrm);
2716 }
2717
2718 fcgi_conn_restart_reading(fconn, 0);
2719}
2720
2721/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2722 * the end.
2723 */
2724static int fcgi_process_mux(struct fcgi_conn *fconn)
2725{
2726 struct fcgi_strm *fstrm, *fstrm_back;
2727
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002728 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2729
Christopher Faulet99eff652019-08-11 23:11:30 +02002730 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2731 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2732 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2733 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002734 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 +02002735 fcgi_wake_unassigned_streams(fconn);
2736 goto mux;
2737 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002738 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002739 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2740 goto fail;
2741 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002742 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 +02002743 }
2744 /* need to wait for the other side */
2745 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002746 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002747 }
2748
2749 mux:
2750 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2751 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2752 break;
2753
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002754 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002755 continue;
2756
Willy Tarreau7aad7032020-01-16 17:20:57 +01002757 /* If the sender changed his mind and unsubscribed, let's just
2758 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002759 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002760 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2761 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002762 LIST_DEL_INIT(&fstrm->send_list);
2763 continue;
2764 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002765
2766 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002767 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2768 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002769 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002770 tasklet_wakeup(fstrm->subs->tasklet);
2771 fstrm->subs->events &= ~SUB_RETRY_SEND;
2772 if (!fstrm->subs->events)
2773 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002774 } else {
2775 /* it's the shut request that was queued */
2776 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2777 tasklet_wakeup(fstrm->shut_tl);
2778 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002779 }
2780
2781 fail:
2782 if (fconn->state == FCGI_CS_CLOSED) {
2783 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2784 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002785 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2786 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002787 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002788 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002789 }
2790 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002791
2792 done:
2793 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002794 return 1;
2795}
2796
2797
2798/* Attempt to read data, and subscribe if none available.
2799 * The function returns 1 if data has been received, otherwise zero.
2800 */
2801static int fcgi_recv(struct fcgi_conn *fconn)
2802{
2803 struct connection *conn = fconn->conn;
2804 struct buffer *buf;
2805 int max;
2806 size_t ret;
2807
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002808 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2809
2810 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2811 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002812 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002813 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002814
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002815 if (!fcgi_recv_allowed(fconn)) {
2816 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002817 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002818 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002819
2820 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2821 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002822 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002823 fconn->flags |= FCGI_CF_DEM_DALLOC;
2824 return 0;
2825 }
2826
Christopher Faulet99eff652019-08-11 23:11:30 +02002827 if (!b_data(buf)) {
2828 /* try to pre-align the buffer like the
2829 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002830 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002831 * HTX block to alias it upon recv. We cannot use the
2832 * head because rcv_buf() will realign the buffer if
2833 * it's empty. Thus we cheat and pretend we already
2834 * have a few bytes there.
2835 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002836 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2837 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002838 }
2839 else
2840 max = buf_room_for_htx_data(buf);
2841
2842 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2843
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002844 if (max && !ret && fcgi_recv_allowed(fconn)) {
2845 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002846 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002847 }
2848 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002849 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002850
2851 if (!b_data(buf)) {
2852 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002853 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002854 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2855 }
2856
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002857 if (ret == max) {
2858 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002859 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002860 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002861
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002862 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002863 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2864}
2865
2866
2867/* Try to send data if possible.
2868 * The function returns 1 if data have been sent, otherwise zero.
2869 */
2870static int fcgi_send(struct fcgi_conn *fconn)
2871{
2872 struct connection *conn = fconn->conn;
2873 int done;
2874 int sent = 0;
2875
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002876 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2877
2878 if (conn->flags & CO_FL_ERROR) {
2879 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002880 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002881 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002882
2883
Willy Tarreau911db9b2020-01-23 16:27:54 +01002884 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002885 /* a handshake was requested */
2886 goto schedule;
2887 }
2888
2889 /* This loop is quite simple : it tries to fill as much as it can from
2890 * pending streams into the existing buffer until it's reportedly full
2891 * or the end of send requests is reached. Then it tries to send this
2892 * buffer's contents out, marks it not full if at least one byte could
2893 * be sent, and tries again.
2894 *
2895 * The snd_buf() function normally takes a "flags" argument which may
2896 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2897 * data immediately comes and CO_SFL_STREAMER to indicate that the
2898 * connection is streaming lots of data (used to increase TLS record
2899 * size at the expense of latency). The former can be sent any time
2900 * there's a buffer full flag, as it indicates at least one stream
2901 * attempted to send and failed so there are pending data. An
2902 * alternative would be to set it as long as there's an active stream
2903 * but that would be problematic for ACKs until we have an absolute
2904 * guarantee that all waiters have at least one byte to send. The
2905 * latter should possibly not be set for now.
2906 */
2907
2908 done = 0;
2909 while (!done) {
2910 unsigned int flags = 0;
2911 unsigned int released = 0;
2912 struct buffer *buf;
2913
2914 /* fill as much as we can into the current buffer */
2915 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2916 done = fcgi_process_mux(fconn);
2917
2918 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2919 done = 1; // we won't go further without extra buffers
2920
2921 if (conn->flags & CO_FL_ERROR)
2922 break;
2923
2924 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2925 flags |= CO_SFL_MSG_MORE;
2926
2927 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2928 if (b_data(buf)) {
2929 int ret;
2930
2931 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2932 if (!ret) {
2933 done = 1;
2934 break;
2935 }
2936 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002937 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002938 b_del(buf, ret);
2939 if (b_data(buf)) {
2940 done = 1;
2941 break;
2942 }
2943 }
2944 b_free(buf);
2945 released++;
2946 }
2947
2948 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002949 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002950
2951 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002952 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2953 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002954 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2955 }
2956
2957 if (conn->flags & CO_FL_SOCK_WR_SH) {
2958 /* output closed, nothing to send, clear the buffer to release it */
2959 b_reset(br_tail(fconn->mbuf));
2960 }
2961 /* We're not full anymore, so we can wake any task that are waiting
2962 * for us.
2963 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002964 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002965 struct fcgi_strm *fstrm;
2966
2967 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2968 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2969 break;
2970
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002971 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002972 continue;
2973
Willy Tarreau7aad7032020-01-16 17:20:57 +01002974 /* If the sender changed his mind and unsubscribed, let's just
2975 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002976 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002977 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2978 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002979 LIST_DEL_INIT(&fstrm->send_list);
2980 continue;
2981 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002982
2983 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002984 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002985 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002986 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002987 tasklet_wakeup(fstrm->subs->tasklet);
2988 fstrm->subs->events &= ~SUB_RETRY_SEND;
2989 if (!fstrm->subs->events)
2990 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002991 } else {
2992 /* it's the shut request that was queued */
2993 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2994 tasklet_wakeup(fstrm->shut_tl);
2995 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002996 }
2997 }
2998 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002999 if (!br_data(fconn->mbuf)) {
3000 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003001 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003002 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003003schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003004 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
3005 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003006 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003007 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003008
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003009 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003010 return sent;
3011}
3012
3013/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003014struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003015{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003016 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003017 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003018 struct tasklet *tl = (struct tasklet *)t;
3019 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02003020 int ret = 0;
3021
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003022 if (state & TASK_F_USR1) {
3023 /* the tasklet was idling on an idle connection, it might have
3024 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003025 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003026 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3027 if (tl->context == NULL) {
3028 /* The connection has been taken over by another thread,
3029 * we're no longer responsible for it, so just free the
3030 * tasklet, and do nothing.
3031 */
3032 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3033 tasklet_free(tl);
3034 return NULL;
3035 }
3036 conn = fconn->conn;
3037 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003038
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003039 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3040 if (conn_in_list)
3041 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003042
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003043 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3044 } else {
3045 /* we're certain the connection was not in an idle list */
3046 conn = fconn->conn;
3047 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
3048 conn_in_list = 0;
3049 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003050
Christopher Faulet99eff652019-08-11 23:11:30 +02003051 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3052 ret = fcgi_send(fconn);
3053 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3054 ret |= fcgi_recv(fconn);
3055 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003056 ret = fcgi_process(fconn);
3057
3058 /* If we were in an idle list, we want to add it back into it,
3059 * unless fcgi_process() returned -1, which mean it has destroyed
3060 * the connection (testing !ret is enough, if fcgi_process() wasn't
3061 * called then ret will be 0 anyway.
3062 */
Willy Tarreau74163142021-03-13 11:30:19 +01003063 if (ret < 0)
3064 t = NULL;
3065
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003066 if (!ret && conn_in_list) {
3067 struct server *srv = objt_server(conn->target);
3068
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003069 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003070 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003071 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003072 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003073 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003074 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003075 }
Willy Tarreau74163142021-03-13 11:30:19 +01003076 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02003077}
3078
3079/* callback called on any event by the connection handler.
3080 * It applies changes and returns zero, or < 0 if it wants immediate
3081 * destruction of the connection (which normally doesn not happen in FCGI).
3082 */
3083static int fcgi_process(struct fcgi_conn *fconn)
3084{
3085 struct connection *conn = fconn->conn;
3086
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003087 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3088
Christopher Faulet99eff652019-08-11 23:11:30 +02003089 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3090 fcgi_process_demux(fconn);
3091
3092 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3093 b_reset(&fconn->dbuf);
3094
3095 if (buf_room_for_htx_data(&fconn->dbuf))
3096 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3097 }
3098 fcgi_send(fconn);
3099
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003100 if (unlikely(fconn->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003101 /* frontend is stopping, reload likely in progress, let's try
3102 * to announce a graceful shutdown if not yet done. We don't
3103 * care if it fails, it will be tried again later.
3104 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003105 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 +02003106 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3107 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3108 fcgi_conn_send_aborts(fconn);
3109 }
3110 }
3111
3112 /*
3113 * If we received early data, and the handshake is done, wake
3114 * any stream that was waiting for it.
3115 */
3116 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003117 (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 +02003118 struct eb32_node *node;
3119 struct fcgi_strm *fstrm;
3120
3121 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3122 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3123
3124 while (node) {
3125 fstrm = container_of(node, struct fcgi_strm, by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01003126 if (fstrm->cs && fstrm->endp->flags & CS_EP_WAIT_FOR_HS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003127 fcgi_strm_notify_recv(fstrm);
3128 node = eb32_next(node);
3129 }
3130 }
3131
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003132 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003133 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3134 eb_is_empty(&fconn->streams_by_id)) {
3135 fcgi_wake_some_streams(fconn, 0);
3136
3137 if (eb_is_empty(&fconn->streams_by_id)) {
3138 /* no more stream, kill the connection now */
3139 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003140 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003141 return -1;
3142 }
3143 }
3144
3145 if (!b_data(&fconn->dbuf))
3146 fcgi_release_buf(fconn, &fconn->dbuf);
3147
3148 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3149 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3150 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3151 fcgi_release_mbuf(fconn);
3152
3153 if (fconn->task) {
3154 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3155 task_queue(fconn->task);
3156 }
3157
3158 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003159 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003160 return 0;
3161}
3162
3163
3164/* wake-up function called by the connection layer (mux_ops.wake) */
3165static int fcgi_wake(struct connection *conn)
3166{
3167 struct fcgi_conn *fconn = conn->ctx;
3168
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003169 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003170 return (fcgi_process(fconn));
3171}
3172
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003173
3174static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3175{
3176 int ret = 0;
3177 switch (mux_ctl) {
3178 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003179 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003180 ret |= MUX_STATUS_READY;
3181 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003182 case MUX_EXIT_STATUS:
3183 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003184 default:
3185 return -1;
3186 }
3187}
3188
Christopher Faulet99eff652019-08-11 23:11:30 +02003189/* Connection timeout management. The principle is that if there's no receipt
3190 * nor sending for a certain amount of time, the connection is closed. If the
3191 * MUX buffer still has lying data or is not allocatable, the connection is
3192 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003193 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003194 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003195struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003196{
3197 struct fcgi_conn *fconn = context;
3198 int expired = tick_is_expired(t->expire, now_ms);
3199
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003200 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3201
Willy Tarreau60814ff2020-06-30 11:19:23 +02003202 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003203 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003204
3205 /* Somebody already stole the connection from us, so we should not
3206 * free it, we just have to free the task.
3207 */
3208 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003209 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003210 fconn = NULL;
3211 goto do_leave;
3212 }
3213
Willy Tarreau60814ff2020-06-30 11:19:23 +02003214 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003215 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003216 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3217 return t;
3218 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003219
Willy Tarreau60814ff2020-06-30 11:19:23 +02003220 /* We're about to destroy the connection, so make sure nobody attempts
3221 * to steal it from us.
3222 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003223 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003224 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003225
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003226 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003227 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003228
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003229do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003230 task_destroy(t);
3231
3232 if (!fconn) {
3233 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003234 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003235 return NULL;
3236 }
3237
3238 fconn->task = NULL;
3239 fconn->state = FCGI_CS_CLOSED;
3240 fcgi_wake_some_streams(fconn, 0);
3241
3242 if (br_data(fconn->mbuf)) {
3243 /* don't even try to send aborts, the buffer is stuck */
3244 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3245 goto end;
3246 }
3247
3248 /* try to send but no need to insist */
3249 if (!fcgi_conn_send_aborts(fconn))
3250 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3251
3252 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3253 conn_xprt_ready(fconn->conn)) {
3254 unsigned int released = 0;
3255 struct buffer *buf;
3256
3257 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3258 if (b_data(buf)) {
3259 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3260 buf, b_data(buf), 0);
3261 if (!ret)
3262 break;
3263 b_del(buf, ret);
3264 if (b_data(buf))
3265 break;
3266 b_free(buf);
3267 released++;
3268 }
3269 }
3270
3271 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003272 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003273 }
3274
3275 end:
3276 /* either we can release everything now or it will be done later once
3277 * the last stream closes.
3278 */
3279 if (eb_is_empty(&fconn->streams_by_id))
3280 fcgi_release(fconn);
3281
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003282 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003283 return NULL;
3284}
3285
3286
3287/*******************************************/
3288/* functions below are used by the streams */
3289/*******************************************/
3290
3291/* Append the description of what is present in error snapshot <es> into <out>.
3292 * The description must be small enough to always fit in a buffer. The output
3293 * buffer may be the trash so the trash must not be used inside this function.
3294 */
3295static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3296{
3297 chunk_appendf(out,
3298 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3299 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3300 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3301 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3302 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3303 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3304}
3305/*
3306 * Capture a bad response and archive it in the proxy's structure. By default
3307 * it tries to report the error position as h1m->err_pos. However if this one is
3308 * not set, it will then report h1m->next, which is the last known parsing
3309 * point. The function is able to deal with wrapping buffers. It always displays
3310 * buffers as a contiguous area starting at buf->p. The direction is determined
3311 * thanks to the h1m's flags.
3312 */
3313static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3314 struct h1m *h1m, struct buffer *buf)
3315{
3316 struct session *sess = fstrm->sess;
3317 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003318 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003319 union error_snapshot_ctx ctx;
3320
Christopher Fauletf835dea2021-12-21 14:35:17 +01003321 if (fstrm->cs && cs_strm(fstrm->cs)) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003322 if (sess == NULL)
Christopher Faulet693b23b2022-02-28 09:09:05 +01003323 sess = __cs_strm(fstrm->cs)->sess;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003324 if (!(h1m->flags & H1_MF_RESP))
Christopher Faulet693b23b2022-02-28 09:09:05 +01003325 other_end = __cs_strm(fstrm->cs)->be;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003326 else
3327 other_end = sess->fe;
3328 } else
3329 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003330 /* http-specific part now */
3331 ctx.h1.state = h1m->state;
3332 ctx.h1.c_flags = fconn->flags;
3333 ctx.h1.s_flags = fstrm->flags;
3334 ctx.h1.m_flags = h1m->flags;
3335 ctx.h1.m_clen = h1m->curr_len;
3336 ctx.h1.m_blen = h1m->body_len;
3337
3338 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3339 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3340 &ctx, fcgi_show_error_snapshot);
3341}
3342
3343static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3344 struct buffer *buf, size_t *ofs, size_t max)
3345{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003346 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003347
Willy Tarreau022e5e52020-09-10 09:33:15 +02003348 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 +02003349 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003350 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003351 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 +02003352 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003353 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 +02003354 fcgi_strm_error(fstrm);
3355 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3356 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003357 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003358 goto end;
3359 }
3360
Christopher Fauletda3adeb2021-09-28 09:50:07 +02003361 /* Reject any message with an unknown transfer-encoding. In fact if any
3362 * encoding other than "chunked". A 422-Unprocessable-Content is
3363 * returned for an invalid request, a 502-Bad-Gateway for an invalid
3364 * response.
3365 */
3366 if (h1m->flags & H1_MF_TE_OTHER) {
3367 htx->flags |= HTX_FL_PARSING_ERROR;
3368 TRACE_ERROR("Unknown transfer-encoding", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3369 fcgi_strm_error(fstrm);
3370 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3371 ret = 0;
3372 goto end;
3373 }
3374
Christopher Faulet99eff652019-08-11 23:11:30 +02003375 *ofs += ret;
3376 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003377 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 +02003378 return ret;
3379
3380}
3381
Christopher Fauletaf542632019-10-01 21:52:49 +02003382static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003383 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3384{
Christopher Fauletde471a42021-02-01 16:37:28 +01003385 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003386
Willy Tarreau022e5e52020-09-10 09:33:15 +02003387 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 +02003388 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003389 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003390 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 +02003391 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003392 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 +02003393 fcgi_strm_error(fstrm);
3394 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3395 }
3396 goto end;
3397 }
3398 *ofs += ret;
3399 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003400 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 +02003401 return ret;
3402}
3403
3404static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3405 struct buffer *buf, size_t *ofs, size_t max)
3406{
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003407 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003408
Willy Tarreau022e5e52020-09-10 09:33:15 +02003409 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 +02003410 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003411 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003412 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 +02003413 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003414 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 +02003415 fcgi_strm_error(fstrm);
3416 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3417 }
Christopher Fauletd9fc1282022-03-28 15:37:01 +02003418 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003419 goto end;
3420 }
3421 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003423 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 +02003424 return ret;
3425}
3426
Christopher Faulet99eff652019-08-11 23:11:30 +02003427static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3428{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003429 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003430 struct htx *htx;
3431 struct h1m *h1m = &fstrm->h1m;
3432 size_t ret, data, total = 0;
3433
3434 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3436
Christopher Faulet99eff652019-08-11 23:11:30 +02003437 data = htx->data;
3438 if (fstrm->state == FCGI_SS_ERROR)
3439 goto end;
3440
3441 do {
3442 size_t used = htx_used_space(htx);
3443
3444 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003445 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003446 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3447 if (!ret)
3448 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003449
3450 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3451
Christopher Faulet99eff652019-08-11 23:11:30 +02003452 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3453 struct htx_blk *blk = htx_get_head_blk(htx);
3454 struct htx_sl *sl;
3455
3456 if (!blk)
3457 break;
3458 sl = htx_get_blk_ptr(htx, blk);
3459 sl->flags |= HTX_SL_F_XFER_LEN;
3460 htx->extra = 0;
3461 }
3462 }
3463 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003464 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003465 fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003466
3467 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3468 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3469 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003470 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003471 h1m->state = H1_MSG_DONE;
3472 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3473 }
3474
Christopher Faulet16a524c2021-02-02 21:16:03 +01003475 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003476 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003477
3478 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 +02003479 }
3480 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003481 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003482 fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01003483 if (h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003484 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003485
Christopher Faulet76014fd2019-12-10 11:47:22 +01003486 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 +02003487 }
3488 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003489 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 +02003490 if (b_data(&fstrm->rxbuf) > total) {
3491 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003492 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003493 fcgi_strm_error(fstrm);
3494 }
3495 break;
3496 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003497 else {
3498 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003499 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003500 fcgi_strm_error(fstrm);
3501 break;
3502 }
3503
3504 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003505 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003506
3507 if (fstrm->state == FCGI_SS_ERROR) {
3508 b_reset(&fstrm->rxbuf);
3509 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003510 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003511 return 0;
3512 }
3513
3514 b_del(&fstrm->rxbuf, total);
3515
3516 end:
3517 htx_to_buf(htx, buf);
3518 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003519 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003520 return ret;
3521}
3522
3523/*
3524 * Attach a new stream to a connection
3525 * (Used for outgoing connections)
3526 */
Christopher Faulete00ad352021-12-16 14:44:31 +01003527static int fcgi_attach(struct connection *conn, struct conn_stream *cs, struct session *sess)
Christopher Faulet99eff652019-08-11 23:11:30 +02003528{
Christopher Faulet99eff652019-08-11 23:11:30 +02003529 struct fcgi_strm *fstrm;
3530 struct fcgi_conn *fconn = conn->ctx;
3531
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003532 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003533 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
Christopher Faulete00ad352021-12-16 14:44:31 +01003534 if (!fstrm)
Christopher Faulet73518be2021-01-27 12:06:54 +01003535 goto err;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003536
3537 /* the connection is not idle anymore, let's mark this */
3538 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003539 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003540
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003541 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulete00ad352021-12-16 14:44:31 +01003542 return 0;
Christopher Faulet73518be2021-01-27 12:06:54 +01003543
3544 err:
3545 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulete00ad352021-12-16 14:44:31 +01003546 return -1;
Christopher Faulet99eff652019-08-11 23:11:30 +02003547}
3548
3549/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3550 * We have to scan because we may have some orphan streams. It might be
3551 * beneficial to scan backwards from the end to reduce the likeliness to find
3552 * orphans.
3553 */
3554static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3555{
3556 struct fcgi_conn *fconn = conn->ctx;
3557 struct fcgi_strm *fstrm;
3558 struct eb32_node *node;
3559
3560 node = eb32_first(&fconn->streams_by_id);
3561 while (node) {
3562 fstrm = container_of(node, struct fcgi_strm, by_id);
3563 if (fstrm->cs)
3564 return fstrm->cs;
3565 node = eb32_next(node);
3566 }
3567 return NULL;
3568}
3569
3570/*
3571 * Destroy the mux and the associated connection, if it is no longer used
3572 */
3573static void fcgi_destroy(void *ctx)
3574{
3575 struct fcgi_conn *fconn = ctx;
3576
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003577 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003578 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3579 fcgi_release(fconn);
3580}
3581
3582/*
3583 * Detach the stream from the connection and possibly release the connection.
3584 */
3585static void fcgi_detach(struct conn_stream *cs)
3586{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003587 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003588 struct fcgi_conn *fconn;
3589 struct session *sess;
3590
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003591 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3592
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003593 if (!fstrm) {
3594 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003595 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003596 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003597
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003598 /* there's no txbuf so we're certain no to be able to send anything */
3599 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003600
3601 sess = fstrm->sess;
3602 fconn = fstrm->fconn;
3603 fstrm->cs = NULL;
3604 fconn->nb_cs--;
3605
3606 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3607 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3608 fconn->streams_limit = 1;
3609 }
3610 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3611 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3612 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3613 fconn->state = FCGI_CS_CLOSED;
3614 }
3615
3616 /* this stream may be blocked waiting for some data to leave, so orphan
3617 * it in this case.
3618 */
Christopher Faulet897d6122021-12-17 17:28:35 +01003619 if (!(fconn->conn->flags & CO_FL_ERROR) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003620 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003621 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003622 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003623 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003624 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003625 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003626
3627 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3628 /* unblock the connection if it was blocked on this stream. */
3629 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3630 fcgi_conn_restart_reading(fconn, 1);
3631 }
3632
3633 fcgi_strm_destroy(fstrm);
3634
3635 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003636 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003637 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003638 /* Add the connection in the session serverlist, if not already done */
3639 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3640 fconn->conn->owner = NULL;
3641 if (eb_is_empty(&fconn->streams_by_id)) {
3642 /* let's kill the connection right away */
3643 fconn->conn->mux->destroy(fconn);
3644 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3645 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003646 }
3647 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003648 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003649 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3650 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003651 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003652 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003653 }
3654 }
3655 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003656 else {
3657 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003658 /* If the connection is owned by the session, first remove it
3659 * from its list
3660 */
3661 if (fconn->conn->owner) {
3662 session_unown_conn(fconn->conn->owner, fconn->conn);
3663 fconn->conn->owner = NULL;
3664 }
3665
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003666 /* mark that the tasklet may lose its context to another thread and
3667 * that the handler needs to check it under the idle conns lock.
3668 */
3669 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003670 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3671
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003672 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003673 /* The server doesn't want it, let's kill the connection right away */
3674 fconn->conn->mux->destroy(fconn);
3675 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3676 return;
3677 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003678 /* At this point, the connection has been added to the
3679 * server idle list, so another thread may already have
3680 * hijacked it, so we can't do anything with it.
3681 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3683 return;
3684 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003685 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003686 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02003687 !LIST_INLIST(&fconn->conn->session_list)) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003688 ebmb_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003689 &fconn->conn->hash_node->node,
3690 sizeof(fconn->conn->hash_node->hash));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003691 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003692 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 }
3694
3695 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003696 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003697 */
3698 if (fcgi_conn_is_dead(fconn)) {
3699 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003700 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003701 fcgi_release(fconn);
3702 }
3703 else if (fconn->task) {
3704 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3705 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003706 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003707 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003708 else
3709 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003710}
3711
3712
3713/* Performs a synchronous or asynchronous shutr(). */
3714static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3715{
3716 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003717
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003718 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3719
Christopher Faulet99eff652019-08-11 23:11:30 +02003720 if (fstrm->state == FCGI_SS_CLOSED)
3721 goto done;
3722
3723 /* a connstream may require us to immediately kill the whole connection
3724 * for example because of a "tcp-request content reject" rule that is
3725 * normally used to limit abuse.
3726 */
3727 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003728 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3729 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003730 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003731 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003733 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003734 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3735 !fcgi_strm_send_abort(fconn, fstrm))
3736 goto add_to_list;
3737 }
3738
3739 fcgi_strm_close(fstrm);
3740
3741 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3742 tasklet_wakeup(fconn->wait_event.tasklet);
3743 done:
3744 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003745 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003746 return;
3747
3748 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003749 /* Let the handler know we want to shutr, and add ourselves to the
3750 * send list if not yet done. fcgi_deferred_shut() will be
3751 * automatically called via the shut_tl tasklet when there's room
3752 * again.
3753 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003754 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003755 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003756 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 }
3758 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003759 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003760 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003761 return;
3762}
3763
3764/* Performs a synchronous or asynchronous shutw(). */
3765static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3766{
3767 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003768
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003769 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3770
Christopher Faulet99eff652019-08-11 23:11:30 +02003771 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3772 goto done;
3773
3774 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3775 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3776 !fcgi_strm_send_abort(fconn, fstrm))
3777 goto add_to_list;
3778
3779 if (fstrm->state == FCGI_SS_HREM)
3780 fcgi_strm_close(fstrm);
3781 else
3782 fstrm->state = FCGI_SS_HLOC;
3783 } else {
3784 /* a connstream may require us to immediately kill the whole connection
3785 * for example because of a "tcp-request content reject" rule that is
3786 * normally used to limit abuse.
3787 */
3788 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003789 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3790 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003791 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003792 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003793
3794 fcgi_strm_close(fstrm);
3795 }
3796
3797 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3798 tasklet_wakeup(fconn->wait_event.tasklet);
3799 done:
3800 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003801 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003802 return;
3803
3804 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003805 /* Let the handler know we want to shutr, and add ourselves to the
3806 * send list if not yet done. fcgi_deferred_shut() will be
3807 * automatically called via the shut_tl tasklet when there's room
3808 * again.
3809 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003810 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003811 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003812 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003813 }
3814 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003815 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003816 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003817 return;
3818}
3819
Willy Tarreau7aad7032020-01-16 17:20:57 +01003820/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003821 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003822 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003823 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003824struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003825{
3826 struct fcgi_strm *fstrm = ctx;
3827 struct fcgi_conn *fconn = fstrm->fconn;
3828
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003829 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3830
Willy Tarreau7aad7032020-01-16 17:20:57 +01003831 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3832 /* some data processing remains to be done first */
3833 goto end;
3834 }
3835
Christopher Faulet99eff652019-08-11 23:11:30 +02003836 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3837 fcgi_do_shutw(fstrm);
3838
3839 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3840 fcgi_do_shutr(fstrm);
3841
3842 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3843 /* We're done trying to send, remove ourself from the send_list */
3844 LIST_DEL_INIT(&fstrm->send_list);
3845
3846 if (!fstrm->cs) {
3847 fcgi_strm_destroy(fstrm);
3848 if (fcgi_conn_is_dead(fconn))
3849 fcgi_release(fconn);
3850 }
3851 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003852 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003853 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003854 return NULL;
3855}
3856
3857/* shutr() called by the conn_stream (mux_ops.shutr) */
3858static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3859{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003860 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003861
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003862 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulete9e48202022-03-22 18:13:29 +01003863 if (cs->endp->flags & CS_EP_KILL_CONN)
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 fstrm->flags |= FCGI_SF_KILL_CONN;
3865
3866 if (!mode)
3867 return;
3868
3869 fcgi_do_shutr(fstrm);
3870}
3871
3872/* shutw() called by the conn_stream (mux_ops.shutw) */
3873static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3874{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003875 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003876
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003877 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulete9e48202022-03-22 18:13:29 +01003878 if (cs->endp->flags & CS_EP_KILL_CONN)
Christopher Faulet99eff652019-08-11 23:11:30 +02003879 fstrm->flags |= FCGI_SF_KILL_CONN;
3880
3881 fcgi_do_shutw(fstrm);
3882}
3883
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003884/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3885 * event subscriber <es> is not allowed to change from a previous call as long
3886 * as at least one event is still subscribed. The <event_type> must only be a
3887 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003888 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003889static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003890{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003891 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 struct fcgi_conn *fconn = fstrm->fconn;
3893
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003894 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003895 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003896
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003897 es->events |= event_type;
3898 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003899
3900 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003901 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003902
Christopher Faulet99eff652019-08-11 23:11:30 +02003903 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003904 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003905 if (!LIST_INLIST(&fstrm->send_list))
3906 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003907 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003908 return 0;
3909}
3910
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003911/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3912 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3913 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003914 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003915static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003916{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003917 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003918 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003919
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003920 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003921 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003922
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003923 es->events &= ~event_type;
3924 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003925 fstrm->subs = NULL;
3926
3927 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003928 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003929
Christopher Faulet99eff652019-08-11 23:11:30 +02003930 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003931 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003932 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003933 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3934 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003935 }
3936 return 0;
3937}
3938
Christopher Faulet564e39c2021-09-21 15:50:55 +02003939/* Called from the upper layer, to receive data
3940 *
3941 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3942 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3943 * means the caller wants to flush input data (from the mux buffer and the
3944 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3945 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3946 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3947 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3948 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3949 * copy as much data as possible.
3950 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003951static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3952{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003953 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003954 struct fcgi_conn *fconn = fstrm->fconn;
3955 size_t ret = 0;
3956
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003957 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3958
Christopher Faulet99eff652019-08-11 23:11:30 +02003959 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3960 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003961 else
3962 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003963
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003964 if (b_data(&fstrm->rxbuf))
Christopher Fauletb041b232022-03-24 10:27:02 +01003965 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Christopher Faulet99eff652019-08-11 23:11:30 +02003966 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01003967 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003968 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01003969 cs->endp->flags |= CS_EP_EOI;
Christopher Faulet99eff652019-08-11 23:11:30 +02003970 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
Christopher Fauletb041b232022-03-24 10:27:02 +01003971 cs->endp->flags |= CS_EP_EOS;
Christopher Faulet99eff652019-08-11 23:11:30 +02003972 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003973 if (fcgi_conn_read0_pending(fconn))
Christopher Fauletb041b232022-03-24 10:27:02 +01003974 cs->endp->flags |= CS_EP_EOS;
3975 if (cs->endp->flags & CS_EP_ERR_PENDING)
3976 cs->endp->flags |= CS_EP_ERROR;
Christopher Faulet99eff652019-08-11 23:11:30 +02003977 fcgi_release_buf(fconn, &fstrm->rxbuf);
3978 }
3979
3980 if (ret && fconn->dsi == fstrm->id) {
3981 /* demux is blocking on this stream's buffer */
3982 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3983 fcgi_conn_restart_reading(fconn, 1);
3984 }
3985
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003986 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003987 return ret;
3988}
3989
3990
Christopher Faulet99eff652019-08-11 23:11:30 +02003991/* Called from the upper layer, to send data from buffer <buf> for no more than
3992 * <count> bytes. Returns the number of bytes effectively sent. Some status
3993 * flags may be updated on the conn_stream.
3994 */
3995static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3996{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01003997 struct fcgi_strm *fstrm = __cs_mux(cs);
Christopher Faulet99eff652019-08-11 23:11:30 +02003998 struct fcgi_conn *fconn = fstrm->fconn;
3999 size_t total = 0;
4000 size_t ret;
4001 struct htx *htx = NULL;
4002 struct htx_sl *sl;
4003 struct htx_blk *blk;
4004 uint32_t bsize;
4005
Willy Tarreau022e5e52020-09-10 09:33:15 +02004006 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004007
Christopher Faulet99eff652019-08-11 23:11:30 +02004008 /* If we were not just woken because we wanted to send but couldn't,
4009 * and there's somebody else that is waiting to send, do nothing,
4010 * we will subscribe later and be put at the end of the list
4011 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01004012 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004013 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 +02004014 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004015 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01004016 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02004017
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004018 if (fconn->state < FCGI_CS_RECORD_H) {
4019 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02004020 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004022
4023 htx = htxbuf(buf);
4024 if (fstrm->id == 0) {
4025 int32_t id = fcgi_conn_get_next_sid(fconn);
4026
4027 if (id < 0) {
4028 fcgi_strm_close(fstrm);
Christopher Fauletb041b232022-03-24 10:27:02 +01004029 cs->endp->flags |= CS_EP_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004030 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 +02004031 return 0;
4032 }
4033
4034 eb32_delete(&fstrm->by_id);
4035 fstrm->by_id.key = fstrm->id = id;
4036 fconn->max_id = id;
4037 fconn->nb_reserved--;
4038 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
4039
4040
4041 /* Check if length of the body is known or if the message is
4042 * full. Otherwise, the request is invalid.
4043 */
4044 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004045 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02004046 htx->flags |= HTX_FL_PARSING_ERROR;
4047 fcgi_strm_error(fstrm);
4048 goto done;
4049 }
4050 }
4051
4052 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004053 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 +02004054 if (!fcgi_strm_send_begin_request(fconn, fstrm))
4055 goto done;
4056 }
4057
4058 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
4059 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
4060
Christopher Fauletfe410d62020-05-19 15:13:00 +02004061 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02004062 count && !htx_is_empty(htx)) {
4063 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02004064 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02004065 bsize = htx_get_blksz(blk);
4066
4067 switch (htx_get_blk_type(blk)) {
4068 case HTX_BLK_REQ_SL:
4069 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004070 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 +02004071 ret = fcgi_strm_send_params(fconn, fstrm, htx);
4072 if (!ret) {
4073 goto done;
4074 }
4075 total += ret;
4076 count -= ret;
4077 break;
4078
4079 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004080 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4081 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4082 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4083 if (!ret)
4084 goto done;
4085 }
4086 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4087 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4088 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4089 if (!ret)
4090 goto done;
4091 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004092 goto remove_blk;
4093
4094 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004095 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 +02004096 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4097 if (ret > 0) {
4098 htx = htx_from_buf(buf);
4099 total += ret;
4100 count -= ret;
4101 if (ret < bsize)
4102 goto done;
4103 }
4104 break;
4105
Christopher Faulet99eff652019-08-11 23:11:30 +02004106 default:
4107 remove_blk:
4108 htx_remove_blk(htx, blk);
4109 total += bsize;
4110 count -= bsize;
4111 break;
4112 }
4113 }
4114
4115 done:
4116 if (fstrm->state >= FCGI_SS_HLOC) {
4117 /* trim any possibly pending data after we close (extra CR-LF,
4118 * unprocessed trailers, abnormal extra data, ...)
4119 */
4120 total += count;
4121 count = 0;
4122 }
4123
4124 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004125 TRACE_DEVEL("reporting error to the app-layer stream", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_ERR|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02004126 cs_set_error(cs);
4127 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4128 fcgi_strm_close(fstrm);
4129 }
4130
4131 if (htx)
4132 htx_to_buf(htx, buf);
4133
Christopher Faulet99eff652019-08-11 23:11:30 +02004134 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004135 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4136 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 +02004137 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004138 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004139
4140 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004141 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4142 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004143 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004144
4145 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004146 return total;
4147}
4148
4149/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004150static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004151{
4152 struct fcgi_conn *fconn = conn->ctx;
4153 struct fcgi_strm *fstrm = NULL;
4154 struct eb32_node *node;
4155 int send_cnt = 0;
4156 int tree_cnt = 0;
4157 int orph_cnt = 0;
4158 struct buffer *hmbuf, *tmbuf;
4159
4160 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004161 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004162
4163 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4164 send_cnt++;
4165
4166 fstrm = NULL;
4167 node = eb32_first(&fconn->streams_by_id);
4168 while (node) {
4169 fstrm = container_of(node, struct fcgi_strm, by_id);
4170 tree_cnt++;
4171 if (!fstrm->cs)
4172 orph_cnt++;
4173 node = eb32_next(node);
4174 }
4175
4176 hmbuf = br_head(fconn->mbuf);
4177 tmbuf = br_tail(fconn->mbuf);
4178 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4179 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4180 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4181 fconn->state, fconn->max_id, fconn->flags,
4182 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4183 fconn->wait_event.events, fconn->dsi,
4184 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4185 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4186 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4187 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4188 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4189 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4190 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4191
4192 if (fstrm) {
4193 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4194 fstrm, fstrm->id, fstrm->flags,
4195 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4196 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4197 fstrm->cs);
4198 if (fstrm->cs)
Christopher Fauletf835dea2021-12-21 14:35:17 +01004199 chunk_appendf(msg, " .cs.flg=0x%08x .cs.app=%p",
4200 fstrm->cs->flags, fstrm->cs->app);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004201 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4202 if (fstrm->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004203 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4204 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4205 fstrm->subs->tasklet->calls,
4206 fstrm->subs->tasklet->context);
4207 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4208 chunk_appendf(&trash, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004209 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004210 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004211 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004212}
4213
4214/* Migrate the the connection to the current thread.
4215 * Return 0 if successful, non-zero otherwise.
4216 * Expected to be called with the old thread lock held.
4217 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004218static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004219{
4220 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004221 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004222
4223 if (fd_takeover(conn->handle.fd, conn) != 0)
4224 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004225
4226 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4227 /* We failed to takeover the xprt, even if the connection may
4228 * still be valid, flag it as error'd, as we have already
4229 * taken over the fd, and wake the tasklet, so that it will
4230 * destroy it.
4231 */
4232 conn->flags |= CO_FL_ERROR;
4233 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4234 return -1;
4235 }
4236
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004237 if (fcgi->wait_event.events)
4238 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4239 fcgi->wait_event.events, &fcgi->wait_event);
4240 /* To let the tasklet know it should free itself, and do nothing else,
4241 * set its context to NULL;
4242 */
4243 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004244 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004245
4246 task = fcgi->task;
4247 if (task) {
4248 task->context = NULL;
4249 fcgi->task = NULL;
4250 __ha_barrier_store();
4251 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004252
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004253 fcgi->task = task_new_here();
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004254 if (!fcgi->task) {
4255 fcgi_release(fcgi);
4256 return -1;
4257 }
4258 fcgi->task->process = fcgi_timeout_task;
4259 fcgi->task->context = fcgi;
4260 }
4261 fcgi->wait_event.tasklet = tasklet_new();
4262 if (!fcgi->wait_event.tasklet) {
4263 fcgi_release(fcgi);
4264 return -1;
4265 }
4266 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4267 fcgi->wait_event.tasklet->context = fcgi;
4268 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4269 SUB_RETRY_RECV, &fcgi->wait_event);
4270
4271 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004272}
4273
4274/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004275/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004276/****************************************/
4277
4278/* The mux operations */
4279static const struct mux_ops mux_fcgi_ops = {
4280 .init = fcgi_init,
4281 .wake = fcgi_wake,
4282 .attach = fcgi_attach,
4283 .get_first_cs = fcgi_get_first_cs,
4284 .detach = fcgi_detach,
4285 .destroy = fcgi_destroy,
4286 .avail_streams = fcgi_avail_streams,
4287 .used_streams = fcgi_used_streams,
4288 .rcv_buf = fcgi_rcv_buf,
4289 .snd_buf = fcgi_snd_buf,
4290 .subscribe = fcgi_subscribe,
4291 .unsubscribe = fcgi_unsubscribe,
4292 .shutr = fcgi_shutr,
4293 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004294 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004295 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004296 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004297 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004298 .name = "FCGI",
4299};
4300
4301
4302/* this mux registers FCGI proto */
4303static struct mux_proto_list mux_proto_fcgi =
4304{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4305
4306INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4307
4308/*
4309 * Local variables:
4310 * c-indent-level: 8
4311 * c-basic-offset: 8
4312 * End:
4313 */