blob: 83c57a42d86cc35f03ec4bbbf83b2443708ff435 [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>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020018#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020019#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020022#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020023#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020024#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020025#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020026#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020027#include <haproxy/net_helper.h>
Willy Tarreauc5396bd2021-05-08 20:28:54 +020028#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020029#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session-t.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020031#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020034#include <haproxy/trace.h>
Christopher Fauletb2a50292021-06-11 13:34:42 +020035#include <haproxy/version.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020036
Willy Tarreaub2551052020-06-09 09:07:15 +020037
Christopher Faulet99eff652019-08-11 23:11:30 +020038/* FCGI Connection flags (32 bits) */
39#define FCGI_CF_NONE 0x00000000
40
41/* Flags indicating why writing to the mux is blockes */
42#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
43#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
44#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
45
46/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
51#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
52#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
53#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
54#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
55#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
56#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
57#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
58
59/* Other flags */
60#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
61#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
62#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
63#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 +020064#define FCGI_CF_KEEP_CONN 0x00001000 /* HAProxy is responsible to close the connection */
Christopher Faulet99eff652019-08-11 23:11:30 +020065#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
66
67/* FCGI connection state (fcgi_conn->state) */
68enum fcgi_conn_st {
69 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
70 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
71 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
72 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
73 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
74 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
75 FCGI_CS_ENTRIES
76} __attribute__((packed));
77
78/* 32 buffers: one for the ring's root, rest for the mbuf itself */
79#define FCGI_C_MBUF_CNT 32
80
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010081/* Size for a record header (also size of empty record) */
82#define FCGI_RECORD_HEADER_SZ 8
83
Christopher Faulet99eff652019-08-11 23:11:30 +020084/* FCGI connection descriptor */
85struct fcgi_conn {
86 struct connection *conn;
87
88 enum fcgi_conn_st state; /* FCGI connection state */
89 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
90 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
91 uint32_t flags; /* Connection flags: FCGI_CF_* */
92
93 int16_t dsi; /* dmux stream ID (<0 = idle ) */
94 uint16_t drl; /* demux record length (if dsi >= 0) */
95 uint8_t drt; /* demux record type (if dsi >= 0) */
96 uint8_t drp; /* demux record padding (if dsi >= 0) */
97
98 struct buffer dbuf; /* demux buffer */
99 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
100
101 int timeout; /* idle timeout duration in ticks */
102 int shut_timeout; /* idle timeout duration in ticks after shutdown */
103 unsigned int nb_streams; /* number of streams in the tree */
104 unsigned int nb_cs; /* number of attached conn_streams */
105 unsigned int nb_reserved; /* number of reserved streams */
106 unsigned int stream_cnt; /* total number of streams seen */
107
108 struct proxy *proxy; /* the proxy this connection was created for */
109 struct fcgi_app *app; /* FCGI application used by this mux */
110 struct task *task; /* timeout management task */
111 struct eb_root streams_by_id; /* all active streams by their ID */
112
113 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200114
115 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
116 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
117};
118
119
120/* FCGI stream state, in fcgi_strm->state */
121enum fcgi_strm_st {
122 FCGI_SS_IDLE = 0,
123 FCGI_SS_OPEN,
124 FCGI_SS_HREM, // half-closed(remote)
125 FCGI_SS_HLOC, // half-closed(local)
126 FCGI_SS_ERROR,
127 FCGI_SS_CLOSED,
128 FCGI_SS_ENTRIES
129} __attribute__((packed));
130
131
132/* FCGI stream flags (32 bits) */
133#define FCGI_SF_NONE 0x00000000
134#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100135#define FCGI_SF_ES_SENT 0x00000002 /* end-of-stream sent (empty STDIN record) */
136#define FCGI_SF_EP_SENT 0x00000004 /* end-of-param sent (empty PARAMS record) */
137#define FCGI_SF_ABRT_SENT 0x00000008 /* abort sent (ABORT_REQUEST record) */
Christopher Faulet99eff652019-08-11 23:11:30 +0200138
139/* Stream flags indicating the reason the stream is blocked */
140#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
141#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
142#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
143
144#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
145#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100146#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200147
148#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
149#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
150#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
151
Christopher Faulet99eff652019-08-11 23:11:30 +0200152
153/* FCGI stream descriptor */
154struct fcgi_strm {
155 struct conn_stream *cs;
156 struct session *sess;
157 struct fcgi_conn *fconn;
158
159 int32_t id; /* stream ID */
160
161 uint32_t flags; /* Connection flags: FCGI_SF_* */
162 enum fcgi_strm_st state; /* FCGI stream state */
163 int proto_status; /* FCGI_PS_* */
164
165 struct h1m h1m; /* response parser state for H1 */
166
167 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
168
169 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100170 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200171 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100172 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 +0200173};
174
175/* Flags representing all default FCGI parameters */
176#define FCGI_SP_CGI_GATEWAY 0x00000001
177#define FCGI_SP_DOC_ROOT 0x00000002
178#define FCGI_SP_SCRIPT_NAME 0x00000004
179#define FCGI_SP_PATH_INFO 0x00000008
180#define FCGI_SP_REQ_URI 0x00000010
181#define FCGI_SP_REQ_METH 0x00000020
182#define FCGI_SP_REQ_QS 0x00000040
183#define FCGI_SP_SRV_PORT 0x00000080
184#define FCGI_SP_SRV_PROTO 0x00000100
185#define FCGI_SP_SRV_NAME 0x00000200
186#define FCGI_SP_REM_ADDR 0x00000400
187#define FCGI_SP_REM_PORT 0x00000800
188#define FCGI_SP_SCRIPT_FILE 0x00001000
189#define FCGI_SP_PATH_TRANS 0x00002000
190#define FCGI_SP_CONT_LEN 0x00004000
191#define FCGI_SP_HTTPS 0x00008000
Christopher Fauletb2a50292021-06-11 13:34:42 +0200192#define FCGI_SP_SRV_SOFT 0x00010000
193#define FCGI_SP_MASK 0x0001FFFF
Christopher Faulet99eff652019-08-11 23:11:30 +0200194#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
195
196/* FCGI parameters used when PARAMS record is sent */
197struct fcgi_strm_params {
198 uint32_t mask;
199 struct ist docroot;
200 struct ist scriptname;
201 struct ist pathinfo;
202 struct ist meth;
203 struct ist uri;
204 struct ist vsn;
205 struct ist qs;
206 struct ist srv_name;
207 struct ist srv_port;
208 struct ist rem_addr;
209 struct ist rem_port;
210 struct ist cont_len;
Christopher Fauletb2a50292021-06-11 13:34:42 +0200211 struct ist srv_soft;
Christopher Faulet99eff652019-08-11 23:11:30 +0200212 int https;
213 struct buffer *p;
214};
215
216/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
217#define MAX_DATA_REALIGN 1024
218
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200219/* trace source and events */
220static void fcgi_trace(enum trace_level level, uint64_t mask,
221 const struct trace_source *src,
222 const struct ist where, const struct ist func,
223 const void *a1, const void *a2, const void *a3, const void *a4);
224
225/* The event representation is split like this :
226 * fconn - internal FCGI connection
227 * fstrm - internal FCGI stream
228 * strm - application layer
229 * rx - data receipt
230 * tx - data transmission
231 * rsp - response parsing
232 */
233static const struct trace_event fcgi_trace_events[] = {
234#define FCGI_EV_FCONN_NEW (1ULL << 0)
235 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
236#define FCGI_EV_FCONN_RECV (1ULL << 1)
237 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
238#define FCGI_EV_FCONN_SEND (1ULL << 2)
239 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
240#define FCGI_EV_FCONN_BLK (1ULL << 3)
241 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
242#define FCGI_EV_FCONN_WAKE (1ULL << 4)
243 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
244#define FCGI_EV_FCONN_END (1ULL << 5)
245 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
246#define FCGI_EV_FCONN_ERR (1ULL << 6)
247 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
248
249#define FCGI_EV_RX_FHDR (1ULL << 7)
250 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
251#define FCGI_EV_RX_RECORD (1ULL << 8)
252 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
253#define FCGI_EV_RX_EOI (1ULL << 9)
254 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
255#define FCGI_EV_RX_GETVAL (1ULL << 10)
256 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
257#define FCGI_EV_RX_STDOUT (1ULL << 11)
258 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
259#define FCGI_EV_RX_STDERR (1ULL << 12)
260 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
261#define FCGI_EV_RX_ENDREQ (1ULL << 13)
262 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
263
264#define FCGI_EV_TX_RECORD (1ULL << 14)
265 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
266#define FCGI_EV_TX_EOI (1ULL << 15)
267 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
268#define FCGI_EV_TX_BEGREQ (1ULL << 16)
269 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
270#define FCGI_EV_TX_GETVAL (1ULL << 17)
271 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
272#define FCGI_EV_TX_PARAMS (1ULL << 18)
273 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
274#define FCGI_EV_TX_STDIN (1ULL << 19)
275 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
276#define FCGI_EV_TX_ABORT (1ULL << 20)
277 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
278
279#define FCGI_EV_RSP_DATA (1ULL << 21)
280 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
281#define FCGI_EV_RSP_EOM (1ULL << 22)
282 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
283#define FCGI_EV_RSP_HDRS (1ULL << 23)
284 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
285#define FCGI_EV_RSP_BODY (1ULL << 24)
286 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
287#define FCGI_EV_RSP_TLRS (1ULL << 25)
288 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
289
290#define FCGI_EV_FSTRM_NEW (1ULL << 26)
291 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
292#define FCGI_EV_FSTRM_BLK (1ULL << 27)
293 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
294#define FCGI_EV_FSTRM_END (1ULL << 28)
295 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
296#define FCGI_EV_FSTRM_ERR (1ULL << 29)
297 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
298
299#define FCGI_EV_STRM_NEW (1ULL << 30)
300 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
301#define FCGI_EV_STRM_RECV (1ULL << 31)
302 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
303#define FCGI_EV_STRM_SEND (1ULL << 32)
304 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
305#define FCGI_EV_STRM_FULL (1ULL << 33)
306 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
307#define FCGI_EV_STRM_WAKE (1ULL << 34)
308 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
309#define FCGI_EV_STRM_SHUT (1ULL << 35)
310 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
311#define FCGI_EV_STRM_END (1ULL << 36)
312 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
313#define FCGI_EV_STRM_ERR (1ULL << 37)
314 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
315
316 { }
317};
318
319static const struct name_desc fcgi_trace_lockon_args[4] = {
320 /* arg1 */ { /* already used by the connection */ },
321 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
322 /* arg3 */ { },
323 /* arg4 */ { }
324};
325
326
327static const struct name_desc fcgi_trace_decoding[] = {
328#define FCGI_VERB_CLEAN 1
329 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
330#define FCGI_VERB_MINIMAL 2
331 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
332#define FCGI_VERB_SIMPLE 3
333 { .name="simple", .desc="add request/response status line or htx info when available" },
334#define FCGI_VERB_ADVANCED 4
335 { .name="advanced", .desc="add header fields or record decoding when available" },
336#define FCGI_VERB_COMPLETE 5
337 { .name="complete", .desc="add full data dump when available" },
338 { /* end */ }
339};
340
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200341static struct trace_source trace_fcgi __read_mostly = {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200342 .name = IST("fcgi"),
343 .desc = "FastCGI multiplexer",
344 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
345 .default_cb = fcgi_trace,
346 .known_events = fcgi_trace_events,
347 .lockon_args = fcgi_trace_lockon_args,
348 .decoding = fcgi_trace_decoding,
349 .report_events = ~0, // report everything by default
350};
351
352#define TRACE_SOURCE &trace_fcgi
353INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
354
Christopher Faulet99eff652019-08-11 23:11:30 +0200355/* FCGI connection and stream pools */
356DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
357DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
358
Willy Tarreau144f84a2021-03-02 16:09:26 +0100359struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200360static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau691d5032021-01-20 14:55:01 +0100361/* fcgi_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100362struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200363static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100364struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200365static 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 +0200366static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
367static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200368static void fcgi_strm_alert(struct fcgi_strm *fstrm);
369static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
370
371/* a dmumy management stream */
372static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
373 .cs = NULL,
374 .fconn = NULL,
375 .state = FCGI_SS_CLOSED,
376 .flags = FCGI_SF_NONE,
377 .id = 0,
378};
379
380/* and a dummy idle stream for use with any unknown stream */
381static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
382 .cs = NULL,
383 .fconn = NULL,
384 .state = FCGI_SS_IDLE,
385 .flags = FCGI_SF_NONE,
386 .id = 0,
387};
388
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200389/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
390static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
391{
392 switch (st) {
393 case FCGI_CS_INIT : return "INI";
394 case FCGI_CS_SETTINGS : return "STG";
395 case FCGI_CS_RECORD_H : return "RDH";
396 case FCGI_CS_RECORD_D : return "RDD";
397 case FCGI_CS_RECORD_P : return "RDP";
398 case FCGI_CS_CLOSED : return "CLO";
399 default : return "???";
400 }
401}
402
403/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
404static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
405{
406 switch (st) {
407 case FCGI_SS_IDLE : return "IDL";
408 case FCGI_SS_OPEN : return "OPN";
409 case FCGI_SS_HREM : return "RCL";
410 case FCGI_SS_HLOC : return "HCL";
411 case FCGI_SS_ERROR : return "ERR";
412 case FCGI_SS_CLOSED : return "CLO";
413 default : return "???";
414 }
415}
416
417
418/* the FCGI traces always expect that arg1, if non-null, is of type connection
419 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
420 * and that arg3, if non-null, is a htx for rx/tx headers.
421 */
422static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
423 const struct ist where, const struct ist func,
424 const void *a1, const void *a2, const void *a3, const void *a4)
425{
426 const struct connection *conn = a1;
427 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
428 const struct fcgi_strm *fstrm = a2;
429 const struct htx *htx = a3;
430 const size_t *val = a4;
431
432 if (!fconn)
433 fconn = (fstrm ? fstrm->fconn : NULL);
434
435 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
436 return;
437
438 /* Display the response state if fstrm is defined */
439 if (fstrm)
440 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
441
442 if (src->verbosity == FCGI_VERB_CLEAN)
443 return;
444
445 /* Display the value to the 4th argument (level > STATE) */
446 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100447 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200448
449 /* Display status-line if possible (verbosity > MINIMAL) */
450 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
451 const struct htx_blk *blk = htx_get_head_blk(htx);
452 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
453 enum htx_blk_type type = htx_get_blk_type(blk);
454
455 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
456 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
457 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
458 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
459 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
460 }
461
462 /* Display fconn info and, if defined, fstrm info */
463 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
464 if (fstrm)
465 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
466
467 if (!fstrm || fstrm->id <= 0)
468 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
469 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
470 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
471
472 if (src->verbosity == FCGI_VERB_MINIMAL)
473 return;
474
475 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
476 if (src->level > TRACE_LEVEL_USER) {
477 if (src->verbosity == FCGI_VERB_COMPLETE ||
478 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
479 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
480 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
481 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
482 if (src->verbosity == FCGI_VERB_COMPLETE ||
483 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
484 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
485 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
486
487 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
488 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
489 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
490 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
491 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
492 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
493 }
494
495 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
496 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
497 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
498 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
499 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
500 }
501
502 /* Display htx info if defined (level > USER) */
503 if (src->level > TRACE_LEVEL_USER && htx) {
504 int full = 0;
505
506 /* Full htx info (level > STATE && verbosity > SIMPLE) */
507 if (src->level > TRACE_LEVEL_STATE) {
508 if (src->verbosity == FCGI_VERB_COMPLETE)
509 full = 1;
510 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
511 full = 1;
512 }
513
514 chunk_memcat(&trace_buf, "\n\t", 2);
515 htx_dump(&trace_buf, htx, full);
516 }
517}
Christopher Faulet99eff652019-08-11 23:11:30 +0200518
519/*****************************************************/
520/* functions below are for dynamic buffer management */
521/*****************************************************/
522
523/* Indicates whether or not the we may call the fcgi_recv() function to attempt
524 * to receive data into the buffer and/or demux pending data. The condition is
525 * a bit complex due to some API limits for now. The rules are the following :
526 * - if an error or a shutdown was detected on the connection and the buffer
527 * is empty, we must not attempt to receive
528 * - if the demux buf failed to be allocated, we must not try to receive and
529 * we know there is nothing pending
530 * - if no flag indicates a blocking condition, we may attempt to receive,
531 * regardless of whether the demux buffer is full or not, so that only
532 * de demux part decides whether or not to block. This is needed because
533 * the connection API indeed prevents us from re-enabling receipt that is
534 * already enabled in a polled state, so we must always immediately stop
535 * as soon as the demux can't proceed so as never to hit an end of read
536 * with data pending in the buffers.
537 * - otherwise must may not attempt
538 */
539static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
540{
541 if (b_data(&fconn->dbuf) == 0 &&
542 (fconn->state == FCGI_CS_CLOSED ||
543 fconn->conn->flags & CO_FL_ERROR ||
544 conn_xprt_read0_pending(fconn->conn)))
545 return 0;
546
547 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
548 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
549 return 1;
550
551 return 0;
552}
553
554/* Restarts reading on the connection if it was not enabled */
555static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
556{
557 if (!fcgi_recv_allowed(fconn))
558 return;
559 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
560 (fconn->wait_event.events & SUB_RETRY_RECV))
561 return;
562 tasklet_wakeup(fconn->wait_event.tasklet);
563}
564
565
566/* Tries to grab a buffer and to re-enable processing on mux <target>. The
567 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
568 * the allocation succeeds, in which case the connection is woken up, or 0 if
569 * it's impossible to wake up and we prefer to be woken up later.
570 */
571static int fcgi_buf_available(void *target)
572{
573 struct fcgi_conn *fconn = target;
574 struct fcgi_strm *fstrm;
575
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100576 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200577 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 +0200578 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
579 fcgi_conn_restart_reading(fconn, 1);
580 return 1;
581 }
582
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100583 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200584 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 +0200585 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200586 if (fconn->flags & FCGI_CF_DEM_MROOM) {
587 fconn->flags &= ~FCGI_CF_DEM_MROOM;
588 fcgi_conn_restart_reading(fconn, 1);
589 }
590 return 1;
591 }
592
593 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
594 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100595 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200596 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 +0200597 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
598 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200599 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200600 return 1;
601 }
602
603 return 0;
604}
605
606static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
607{
608 struct buffer *buf = NULL;
609
Willy Tarreau2b718102021-04-21 07:32:39 +0200610 if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100611 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200612 fconn->buf_wait.target = fconn;
613 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau2b718102021-04-21 07:32:39 +0200614 LIST_APPEND(&ti->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200615 }
616 return buf;
617}
618
619static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
620{
621 if (bptr->size) {
622 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100623 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200624 }
625}
626
627static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
628{
629 struct buffer *buf;
630 unsigned int count = 0;
631
632 while (b_size(buf = br_head_pick(fconn->mbuf))) {
633 b_free(buf);
634 count++;
635 }
636 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100637 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200638}
639
640/* Returns the number of allocatable outgoing streams for the connection taking
641 * the number reserved streams into account.
642 */
643static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
644{
645 int ret;
646
647 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
648 if (ret < 0)
649 ret = 0;
650 return ret;
651}
652
653/* Returns the number of streams in use on a connection to figure if it's
654 * idle or not. We check nb_cs and not nb_streams as the caller will want
655 * to know if it was the last one after a detach().
656 */
657static int fcgi_used_streams(struct connection *conn)
658{
659 struct fcgi_conn *fconn = conn->ctx;
660
661 return fconn->nb_cs;
662}
663
664/* Returns the number of concurrent streams available on the connection */
665static int fcgi_avail_streams(struct connection *conn)
666{
667 struct server *srv = objt_server(conn->target);
668 struct fcgi_conn *fconn = conn->ctx;
669 int ret1, ret2;
670
671 /* Don't open new stream if the connection is closed */
672 if (fconn->state == FCGI_CS_CLOSED)
673 return 0;
674
675 /* May be negative if this setting has changed */
676 ret1 = (fconn->streams_limit - fconn->nb_streams);
677
678 /* we must also consider the limit imposed by stream IDs */
679 ret2 = fcgi_streams_left(fconn);
680 ret1 = MIN(ret1, ret2);
681 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
682 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
683 ret1 = MIN(ret1, ret2);
684 }
685 return ret1;
686}
687
688/*****************************************************************/
689/* functions below are dedicated to the mux setup and management */
690/*****************************************************************/
691
692/* Initializes the mux once it's attached. Only outgoing connections are
693 * supported. So the context is already initialized before installing the
694 * mux. <input> is always used as Input buffer and may contain data. It is the
695 * caller responsibility to not reuse it anymore. Returns < 0 on error.
696 */
697static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
698 struct buffer *input)
699{
700 struct fcgi_conn *fconn;
701 struct fcgi_strm *fstrm;
702 struct fcgi_app *app = get_px_fcgi_app(px);
703 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200704 void *conn_ctx = conn->ctx;
705
706 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200707
Christopher Faulet73518be2021-01-27 12:06:54 +0100708 if (!app) {
709 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 +0200710 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100711 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200712
713 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100714 if (!fconn) {
715 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200716 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100717 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200718
719 fconn->shut_timeout = fconn->timeout = px->timeout.server;
720 if (tick_isset(px->timeout.serverfin))
721 fconn->shut_timeout = px->timeout.serverfin;
722
723 fconn->flags = FCGI_CF_NONE;
724
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500725 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200726 if (app->flags & FCGI_APP_FL_KEEP_CONN)
727 fconn->flags |= FCGI_CF_KEEP_CONN;
728 if (app->flags & FCGI_APP_FL_GET_VALUES)
729 fconn->flags |= FCGI_CF_GET_VALUES;
730 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
731 fconn->flags |= FCGI_CF_MPXS_CONNS;
732
733 fconn->proxy = px;
734 fconn->app = app;
735 fconn->task = NULL;
736 if (tick_isset(fconn->timeout)) {
737 t = task_new(tid_bit);
Christopher Faulet73518be2021-01-27 12:06:54 +0100738 if (!t) {
739 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 +0200740 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100741 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200742
743 fconn->task = t;
744 t->process = fcgi_timeout_task;
745 t->context = fconn;
746 t->expire = tick_add(now_ms, fconn->timeout);
747 }
748
749 fconn->wait_event.tasklet = tasklet_new();
750 if (!fconn->wait_event.tasklet)
751 goto fail;
752 fconn->wait_event.tasklet->process = fcgi_io_cb;
753 fconn->wait_event.tasklet->context = fconn;
754 fconn->wait_event.events = 0;
755
756 /* Initialise the context. */
757 fconn->state = FCGI_CS_INIT;
758 fconn->conn = conn;
759 fconn->streams_limit = app->maxreqs;
760 fconn->max_id = -1;
761 fconn->nb_streams = 0;
762 fconn->nb_cs = 0;
763 fconn->nb_reserved = 0;
764 fconn->stream_cnt = 0;
765
766 fconn->dbuf = *input;
767 fconn->dsi = -1;
768
769 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
770 fconn->streams_by_id = EB_ROOT;
771 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100772 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200773
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200774 conn->ctx = fconn;
775
Christopher Faulet99eff652019-08-11 23:11:30 +0200776 if (t)
777 task_queue(t);
778
779 /* FIXME: this is temporary, for outgoing connections we need to
780 * immediately allocate a stream until the code is modified so that the
781 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200782 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200783 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200784 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200785 if (!fstrm)
786 goto fail;
787
Christopher Faulet99eff652019-08-11 23:11:30 +0200788
789 /* Repare to read something */
790 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200791 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200792 return 0;
793
794 fail:
795 task_destroy(t);
796 if (fconn->wait_event.tasklet)
797 tasklet_free(fconn->wait_event.tasklet);
798 pool_free(pool_head_fcgi_conn, fconn);
799 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200800 conn->ctx = conn_ctx; // restore saved ctx
801 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200802 return -1;
803}
804
805/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
806 * -1 if no more is allocatable.
807 */
808static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
809{
810 int32_t id = (fconn->max_id + 1) | 1;
811
812 if ((id & 0x80000000U))
813 id = -1;
814 return id;
815}
816
817/* Returns the stream associated with id <id> or NULL if not found */
818static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
819{
820 struct eb32_node *node;
821
822 if (id == 0)
823 return (struct fcgi_strm *)fcgi_mgmt_stream;
824
825 if (id > fconn->max_id)
826 return (struct fcgi_strm *)fcgi_unknown_stream;
827
828 node = eb32_lookup(&fconn->streams_by_id, id);
829 if (!node)
830 return (struct fcgi_strm *)fcgi_unknown_stream;
831 return container_of(node, struct fcgi_strm, by_id);
832}
833
834
835/* Release function. This one should be called to free all resources allocated
836 * to the mux.
837 */
838static void fcgi_release(struct fcgi_conn *fconn)
839{
William Dauchy477757c2020-08-07 22:19:23 +0200840 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200841
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200842 TRACE_POINT(FCGI_EV_FCONN_END);
843
Christopher Faulet99eff652019-08-11 23:11:30 +0200844 if (fconn) {
845 /* The connection must be attached to this mux to be released */
846 if (fconn->conn && fconn->conn->ctx == fconn)
847 conn = fconn->conn;
848
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200849 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
850
Willy Tarreau2b718102021-04-21 07:32:39 +0200851 if (LIST_INLIST(&fconn->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +0100852 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200853
854 fcgi_release_buf(fconn, &fconn->dbuf);
855 fcgi_release_mbuf(fconn);
856
857 if (fconn->task) {
858 fconn->task->context = NULL;
859 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
860 fconn->task = NULL;
861 }
862 if (fconn->wait_event.tasklet)
863 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200864 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200865 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
866 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200867
868 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200869 }
870
871 if (conn) {
872 conn->mux = NULL;
873 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200874 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200875
876 conn_stop_tracking(conn);
877 conn_full_close(conn);
878 if (conn->destroy_cb)
879 conn->destroy_cb(conn);
880 conn_free(conn);
881 }
882}
883
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200884/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
885 * pending on the connection AND if there is no more data in the demux
886 * buffer. The function returns 1 to report a read0 or 0 otherwise.
887 */
888static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
889{
890 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
891 return 1;
892 return 0;
893}
894
Christopher Faulet99eff652019-08-11 23:11:30 +0200895
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500896/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200897static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
898{
899 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
900 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
901 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
902 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
903 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
904 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
905 conn_xprt_read0_pending(fconn->conn))))
906 return 1;
907 return 0;
908}
909
910
911/********************************************************/
912/* functions below are for the FCGI protocol processing */
913/********************************************************/
914
Christopher Faulet99eff652019-08-11 23:11:30 +0200915/* Marks an error on the stream. */
916static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
917{
918 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200919 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
920 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200921 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200922 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
923 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200924 if (fstrm->cs)
925 cs_set_error(fstrm->cs);
926 }
927}
928
929/* Attempts to notify the data layer of recv availability */
930static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
931{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100932 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200933 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100934 tasklet_wakeup(fstrm->subs->tasklet);
935 fstrm->subs->events &= ~SUB_RETRY_RECV;
936 if (!fstrm->subs->events)
937 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200938 }
939}
940
941/* Attempts to notify the data layer of send availability */
942static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
943{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100944 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200945 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100946 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100947 tasklet_wakeup(fstrm->subs->tasklet);
948 fstrm->subs->events &= ~SUB_RETRY_SEND;
949 if (!fstrm->subs->events)
950 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200951 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100952 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
953 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
954 tasklet_wakeup(fstrm->shut_tl);
955 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200956}
957
958/* Alerts the data layer, trying to wake it up by all means, following
959 * this sequence :
960 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
961 * for recv
962 * - if its subscribed to send, then it's woken up for send
963 * - if it was subscribed to neither, its ->wake() callback is called
964 * It is safe to call this function with a closed stream which doesn't have a
965 * conn_stream anymore.
966 */
967static void fcgi_strm_alert(struct fcgi_strm *fstrm)
968{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200969 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100970 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100971 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200972 fcgi_strm_notify_recv(fstrm);
973 fcgi_strm_notify_send(fstrm);
974 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200975 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
976 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200977 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200978 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200979}
980
981/* Writes the 16-bit record size <len> at address <record> */
982static inline void fcgi_set_record_size(void *record, uint16_t len)
983{
984 uint8_t *out = (record + 4);
985
986 *out = (len >> 8);
987 *(out + 1) = (len & 0xff);
988}
989
990/* Writes the 16-bit stream id <id> at address <record> */
991static inline void fcgi_set_record_id(void *record, uint16_t id)
992{
993 uint8_t *out = (record + 2);
994
995 *out = (id >> 8);
996 *(out + 1) = (id & 0xff);
997}
998
999/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
1000 * its connection if the stream was not yet closed. Please use this exclusively
1001 * before closing a stream to ensure stream count is well maintained.
1002 */
1003static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
1004{
1005 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001006 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001007 fstrm->fconn->nb_streams--;
1008 if (!fstrm->id)
1009 fstrm->fconn->nb_reserved--;
1010 if (fstrm->cs) {
1011 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1012 fcgi_strm_notify_recv(fstrm);
1013 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001014 fstrm->state = FCGI_SS_CLOSED;
1015 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1016 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001017 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001018}
1019
1020/* Detaches a FCGI stream from its FCGI connection and releases it to the
1021 * fcgi_strm pool.
1022 */
1023static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1024{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001025 struct connection *conn = fstrm->fconn->conn;
1026
1027 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1028
Christopher Faulet99eff652019-08-11 23:11:30 +02001029 fcgi_strm_close(fstrm);
1030 eb32_delete(&fstrm->by_id);
1031 if (b_size(&fstrm->rxbuf)) {
1032 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001033 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001034 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001035 if (fstrm->subs)
1036 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001037 /* There's no need to explicitly call unsubscribe here, the only
1038 * reference left would be in the fconn send_list/fctl_list, and if
1039 * we're in it, we're getting out anyway
1040 */
1041 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001042 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001043 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001044
1045 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001046}
1047
1048/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1049 * stream tree. In case of error, nothing is added and NULL is returned. The
1050 * causes of errors can be any failed memory allocation. The caller is
1051 * responsible for checking if the connection may support an extra stream prior
1052 * to calling this function.
1053 */
1054static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1055{
1056 struct fcgi_strm *fstrm;
1057
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001058 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1059
Christopher Faulet99eff652019-08-11 23:11:30 +02001060 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +01001061 if (!fstrm) {
1062 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 +02001063 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +01001064 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001065
Willy Tarreau7aad7032020-01-16 17:20:57 +01001066 fstrm->shut_tl = tasklet_new();
1067 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001068 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 +02001069 pool_free(pool_head_fcgi_strm, fstrm);
1070 goto out;
1071 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001072 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001073 fstrm->shut_tl->process = fcgi_deferred_shut;
1074 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001075 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001076 fstrm->fconn = fconn;
1077 fstrm->cs = NULL;
1078 fstrm->flags = FCGI_SF_NONE;
1079 fstrm->proto_status = 0;
1080 fstrm->state = FCGI_SS_IDLE;
1081 fstrm->rxbuf = BUF_NULL;
1082
1083 h1m_init_res(&fstrm->h1m);
1084 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1085 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1086
1087 fstrm->by_id.key = fstrm->id = id;
1088 if (id > 0)
1089 fconn->max_id = id;
1090 else
1091 fconn->nb_reserved++;
1092
1093 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1094 fconn->nb_streams++;
1095 fconn->stream_cnt++;
1096
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001097 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001098 return fstrm;
1099
1100 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001101 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 +02001102 return NULL;
1103}
1104
1105/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1106 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1107 * highest possible stream ID was reached.
1108 */
1109static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1110 struct session *sess)
1111{
1112 struct fcgi_strm *fstrm = NULL;
1113
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001114 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1115 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001116 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 +02001117 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001118 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001119
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001120 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001121 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 +02001122 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001123 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001124
1125 /* Defer choosing the ID until we send the first message to create the stream */
1126 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001127 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001128 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 +02001129 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001130 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001131
1132 fstrm->cs = cs;
1133 fstrm->sess = sess;
1134 cs->ctx = fstrm;
1135 fconn->nb_cs++;
1136
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001137 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001138 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001139
1140 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001141 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 +02001142 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001143}
1144
1145/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1146 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1147 * automatically updated accordingly. If the stream is orphaned, it is
1148 * destroyed.
1149 */
1150static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1151{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001152 struct fcgi_conn *fconn = fstrm->fconn;
1153
1154 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1155
Christopher Faulet99eff652019-08-11 23:11:30 +02001156 if (!fstrm->cs) {
1157 /* this stream was already orphaned */
1158 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001159 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001160 return;
1161 }
1162
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001163 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001166 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001167 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001168 else if (fstrm->state == FCGI_SS_HLOC)
1169 fcgi_strm_close(fstrm);
1170 }
1171
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001172 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001173 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1174 if (fstrm->cs->flags & CS_FL_EOS)
1175 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001176
1177 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001178 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001179 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1180 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001181 }
1182
1183 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001184
1185 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001186}
1187
1188/* Wakes unassigned streams (ID == 0) attached to the connection. */
1189static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1190{
1191 struct eb32_node *node;
1192 struct fcgi_strm *fstrm;
1193
1194 node = eb32_lookup(&fconn->streams_by_id, 0);
1195 while (node) {
1196 fstrm = container_of(node, struct fcgi_strm, by_id);
1197 if (fstrm->id > 0)
1198 break;
1199 node = eb32_next(node);
1200 fcgi_strm_wake_one_stream(fstrm);
1201 }
1202}
1203
1204/* Wakes the streams attached to the connection, whose id is greater than <last>
1205 * or unassigned.
1206 */
1207static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1208{
1209 struct eb32_node *node;
1210 struct fcgi_strm *fstrm;
1211
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001212 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1213
Christopher Faulet99eff652019-08-11 23:11:30 +02001214 /* Wake all streams with ID > last */
1215 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1216 while (node) {
1217 fstrm = container_of(node, struct fcgi_strm, by_id);
1218 node = eb32_next(node);
1219 fcgi_strm_wake_one_stream(fstrm);
1220 }
1221 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001222
1223 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001224}
1225
1226static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1227 struct htx *htx, struct htx_sl *sl,
1228 struct fcgi_strm_params *params)
1229{
1230 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1231 struct ist p;
1232
1233 if (!sl)
1234 goto error;
1235
1236 if (!(params->mask & FCGI_SP_DOC_ROOT))
1237 params->docroot = fconn->app->docroot;
1238
1239 if (!(params->mask & FCGI_SP_REQ_METH)) {
1240 p = htx_sl_req_meth(sl);
1241 params->meth = ist2(b_tail(params->p), p.len);
1242 chunk_memcat(params->p, p.ptr, p.len);
1243 }
1244 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001245 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001246 params->uri = ist2(b_tail(params->p), p.len);
1247 chunk_memcat(params->p, p.ptr, p.len);
1248 }
1249 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1250 p = htx_sl_req_vsn(sl);
1251 params->vsn = ist2(b_tail(params->p), p.len);
1252 chunk_memcat(params->p, p.ptr, p.len);
1253 }
1254 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1255 char *end;
1256 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001257 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001258 port = get_host_port(cli_conn->dst);
1259 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1260 if (!end)
1261 goto error;
1262 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1263 params->p->data += params->srv_port.len;
1264 }
1265 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1266 /* If no Host header found, use the server address to fill
1267 * srv_name */
1268 if (!istlen(params->srv_name)) {
1269 char *ptr = NULL;
1270
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001271 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001272 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1273 ptr = b_tail(params->p);
1274 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001275 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001276 params->p->data += params->srv_name.len;
1277 }
1278 }
1279 }
1280 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1281 char *ptr = NULL;
1282
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001283 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001284 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1285 ptr = b_tail(params->p);
1286 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001287 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001288 params->p->data += params->rem_addr.len;
1289 }
1290 }
1291 if (!(params->mask & FCGI_SP_REM_PORT)) {
1292 char *end;
1293 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001294 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001295 port = get_host_port(cli_conn->src);
1296 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1297 if (!end)
1298 goto error;
1299 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1300 params->p->data += params->rem_port.len;
1301 }
1302 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1303 struct htx_blk *blk;
1304 enum htx_blk_type type;
1305 char *end;
1306 size_t len = 0;
1307
1308 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1309 type = htx_get_blk_type(blk);
1310
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001311 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001312 break;
1313 if (type == HTX_BLK_DATA)
1314 len += htx_get_blksz(blk);
1315 }
1316 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1317 if (!end)
1318 goto error;
1319 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1320 params->p->data += params->cont_len.len;
1321 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001322#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001323 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001324 if (cli_conn)
1325 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001326 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001327#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001328 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1329 /* one of scriptname, pathinfo or query_string is no set */
1330 struct ist path = http_get_path(params->uri);
1331 int len;
1332
Christopher Faulet99eff652019-08-11 23:11:30 +02001333 /* No scrit_name set but no valid path ==> error */
1334 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1335 goto error;
1336
Christopher Faulet99eff652019-08-11 23:11:30 +02001337 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001338 if (!(params->mask & FCGI_SP_REQ_QS)) {
1339 struct ist qs = istfind(path, '?');
1340
1341 /* Update the path length */
1342 path.len -= qs.len;
1343
1344 /* Set the query-string skipping the '?', if any */
1345 if (istlen(qs))
1346 params->qs = istnext(qs);
1347 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001348
1349 /* If the script_name is set, don't try to deduce the path_info
1350 * too. The opposite is not true.
1351 */
1352 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1353 params->mask |= FCGI_SP_PATH_INFO;
1354 goto end;
1355 }
1356
Christopher Faulet0f17a442020-07-23 15:44:37 +02001357 /* Decode the path. it must first be copied to keep the URI
1358 * untouched.
1359 */
1360 chunk_memcat(params->p, path.ptr, path.len);
1361 path.ptr = b_tail(params->p) - path.len;
1362 len = url_decode(ist0(path), 0);
1363 if (len < 0)
1364 goto error;
1365 path.len = len;
1366
Christopher Faulet99eff652019-08-11 23:11:30 +02001367 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001368 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001369
1370 /* If there is no regex to match the pathinfo, just to the last
1371 * part and see if the index must be used.
1372 */
1373 if (!fconn->app->pathinfo_re)
1374 goto check_index;
1375
Christopher Faulet28cb3662020-02-14 14:47:37 +01001376 /* If some special characters are found in the decoded path (\n
Ilya Shipitsinc6ecf562021-08-07 14:41:56 +05001377 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001378 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001379 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001380 * case.
1381 */
1382 if (istchr(path, '\n') || istchr(path, '\0'))
1383 goto error;
1384
Christopher Faulet99eff652019-08-11 23:11:30 +02001385 /* The regex does not match, just to the last part and see if
1386 * the index must be used.
1387 */
1388 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1389 goto check_index;
1390
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001391 /* We must have at least 1 capture for the script name,
1392 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001393 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001394 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001395 goto check_index;
1396
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001397 /* Finally we can set the script_name and the path_info. The
1398 * path_info is set if not already defined, and if it was
1399 * captured
1400 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001401 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001402 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1403 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001404
1405 check_index:
1406 len = params->scriptname.len;
1407 /* the script_name if finished by a '/' so we can add the index
1408 * part, if any.
1409 */
1410 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1411 struct ist sn = params->scriptname;
1412
1413 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1414 chunk_memcat(params->p, sn.ptr, sn.len);
1415 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1416 }
1417 }
1418
Christopher Fauletb2a50292021-06-11 13:34:42 +02001419 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1420 params->srv_soft = ist2(b_tail(params->p), 0);
1421 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1422 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1423 }
1424
Christopher Faulet99eff652019-08-11 23:11:30 +02001425 end:
1426 return 1;
1427 error:
1428 return 0;
1429}
1430
1431static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1432 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1433{
1434 struct fcgi_param p;
1435
1436 if (params->mask & flag)
1437 return 1;
1438
1439 chunk_reset(&trash);
1440
1441 switch (flag) {
1442 case FCGI_SP_CGI_GATEWAY:
1443 p.n = ist("GATEWAY_INTERFACE");
1444 p.v = ist("CGI/1.1");
1445 goto encode;
1446 case FCGI_SP_DOC_ROOT:
1447 p.n = ist("DOCUMENT_ROOT");
1448 p.v = params->docroot;
1449 goto encode;
1450 case FCGI_SP_SCRIPT_NAME:
1451 p.n = ist("SCRIPT_NAME");
1452 p.v = params->scriptname;
1453 goto encode;
1454 case FCGI_SP_PATH_INFO:
1455 p.n = ist("PATH_INFO");
1456 p.v = params->pathinfo;
1457 goto encode;
1458 case FCGI_SP_REQ_URI:
1459 p.n = ist("REQUEST_URI");
1460 p.v = params->uri;
1461 goto encode;
1462 case FCGI_SP_REQ_METH:
1463 p.n = ist("REQUEST_METHOD");
1464 p.v = params->meth;
1465 goto encode;
1466 case FCGI_SP_REQ_QS:
1467 p.n = ist("QUERY_STRING");
1468 p.v = params->qs;
1469 goto encode;
1470 case FCGI_SP_SRV_NAME:
1471 p.n = ist("SERVER_NAME");
1472 p.v = params->srv_name;
1473 goto encode;
1474 case FCGI_SP_SRV_PORT:
1475 p.n = ist("SERVER_PORT");
1476 p.v = params->srv_port;
1477 goto encode;
1478 case FCGI_SP_SRV_PROTO:
1479 p.n = ist("SERVER_PROTOCOL");
1480 p.v = params->vsn;
1481 goto encode;
1482 case FCGI_SP_REM_ADDR:
1483 p.n = ist("REMOTE_ADDR");
1484 p.v = params->rem_addr;
1485 goto encode;
1486 case FCGI_SP_REM_PORT:
1487 p.n = ist("REMOTE_PORT");
1488 p.v = params->rem_port;
1489 goto encode;
1490 case FCGI_SP_SCRIPT_FILE:
1491 p.n = ist("SCRIPT_FILENAME");
1492 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1493 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1494 p.v = ist2(b_head(&trash), b_data(&trash));
1495 goto encode;
1496 case FCGI_SP_PATH_TRANS:
1497 if (!istlen(params->pathinfo))
1498 goto skip;
1499 p.n = ist("PATH_TRANSLATED");
1500 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1501 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1502 p.v = ist2(b_head(&trash), b_data(&trash));
1503 goto encode;
1504 case FCGI_SP_CONT_LEN:
1505 p.n = ist("CONTENT_LENGTH");
1506 p.v = params->cont_len;
1507 goto encode;
1508 case FCGI_SP_HTTPS:
1509 if (!params->https)
1510 goto skip;
1511 p.n = ist("HTTPS");
1512 p.v = ist("on");
1513 goto encode;
Christopher Fauletb2a50292021-06-11 13:34:42 +02001514 case FCGI_SP_SRV_SOFT:
1515 p.n = ist("SERVER_SOFTWARE");
1516 p.v = params->srv_soft;
1517 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001518 default:
1519 goto skip;
1520 }
1521
1522 encode:
1523 if (!istlen(p.v))
1524 goto skip;
1525 if (!fcgi_encode_param(outbuf, &p))
1526 return 0;
1527 skip:
1528 params->mask |= flag;
1529 return 1;
1530}
1531
1532/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1533 * anything. It is highly unexpected, but if the record is larger than a buffer
1534 * and cannot be encoded in one time, an error is triggered and the connection is
1535 * closed. GET_VALUES record cannot be split.
1536 */
1537static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1538{
1539 struct buffer outbuf;
1540 struct buffer *mbuf;
1541 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1542 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001543 int ret = 0;
1544
1545 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001546
1547 mbuf = br_tail(fconn->mbuf);
1548 retry:
1549 if (!fcgi_get_buf(fconn, mbuf)) {
1550 fconn->flags |= FCGI_CF_MUX_MALLOC;
1551 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001552 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1553 ret = 0;
1554 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001555 }
1556
1557 while (1) {
1558 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001559 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001560 break;
1561 realign_again:
1562 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1563 }
1564
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001565 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001566 goto full;
1567
1568 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1569 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001570 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1571 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001572
1573 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1574 * handled by HAProxy.
1575 */
1576 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1577 goto full;
1578
1579 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001580 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 +01001581 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001582 b_add(mbuf, outbuf.data);
1583 ret = 1;
1584
1585 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001586 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001587 return ret;
1588 full:
1589 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001590 if (!b_data(mbuf)) {
1591 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 +02001592 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001593 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001594
1595 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1596 goto retry;
1597 fconn->flags |= FCGI_CF_MUX_MFULL;
1598 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001599 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001600 ret = 0;
1601 goto end;
1602 fail:
1603 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001604 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1605 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1606 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001607}
1608
1609/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1610 * couldn't do anything. It is highly unexpected, but if the record is larger
1611 * than a buffer and cannot be decoded in one time, an error is triggered and
1612 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1613 */
1614static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1615{
1616 struct buffer inbuf;
1617 struct buffer *dbuf;
1618 size_t offset;
1619
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001620 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1621
Christopher Faulet99eff652019-08-11 23:11:30 +02001622 dbuf = &fconn->dbuf;
1623
1624 /* Record too large to be fully decoded */
1625 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1626 goto fail;
1627
1628 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001629 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1630 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001631 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001632 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001633
1634 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1635 /* Realign the dmux buffer if the record wraps. It is unexpected
1636 * at this stage because it should be the first record received
1637 * from the FCGI application.
1638 */
1639 b_slow_realign(dbuf, trash.area, 0);
1640 }
1641
1642 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1643
1644 for (offset = 0; offset < b_data(&inbuf); ) {
1645 struct fcgi_param p;
1646 size_t ret;
1647
1648 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1649 if (!ret) {
1650 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001651 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 +02001652 goto fail;
1653 }
1654 offset += ret;
1655
1656 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001657 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001658 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 +02001659 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001660 }
1661 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001662 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 +02001663 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001664 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001665 }
1666 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1667 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001668 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 +02001669 }
1670 /*
1671 * Ignore all other params
1672 */
1673 }
1674
1675 /* Reset the number of concurrent streams supported if the FCGI
1676 * application does not support connection multiplexing
1677 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001678 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001679 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001680 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1681 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001682
1683 /* We must be sure to have read exactly the announced record length, no
1684 * more no less
1685 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001686 if (offset != fconn->drl) {
1687 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 +02001688 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001689 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001690
Willy Tarreau022e5e52020-09-10 09:33:15 +02001691 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 +02001692 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1693 fconn->drl = 0;
1694 fconn->drp = 0;
1695 fconn->state = FCGI_CS_RECORD_H;
1696 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001697 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1698 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001699 return 1;
1700 fail:
1701 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001702 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1703 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 +02001704 return 0;
1705}
1706
1707/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1708 * excluded, as the streams which already received the end-of-stream. It returns
1709 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1710 */
1711static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1712{
1713 struct eb32_node *node;
1714 struct fcgi_strm *fstrm;
1715
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001716 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1717
Christopher Faulet99eff652019-08-11 23:11:30 +02001718 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1719 while (node) {
1720 fstrm = container_of(node, struct fcgi_strm, by_id);
1721 node = eb32_next(node);
1722 if (fstrm->state != FCGI_SS_CLOSED &&
1723 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1724 !fcgi_strm_send_abort(fconn, fstrm))
1725 return 0;
1726 }
1727 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001728 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1729 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001730 return 1;
1731}
1732
1733/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1734 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1735 * space to proceed. It is small enough to be encoded in an empty buffer.
1736 */
1737static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1738{
1739 struct buffer outbuf;
1740 struct buffer *mbuf;
1741 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1742 int ret;
1743
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001744 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1745
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 mbuf = br_tail(fconn->mbuf);
1747 retry:
1748 if (!fcgi_get_buf(fconn, mbuf)) {
1749 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001750 fstrm->flags |= FCGI_SF_BLK_MROOM;
1751 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1752 ret = 0;
1753 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001754 }
1755
1756 while (1) {
1757 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001758 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001759 break;
1760 realign_again:
1761 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1762 }
1763
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001764 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001765 goto full;
1766
1767 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1768 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001769 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001770 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001771 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001772
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001773 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1774 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001775 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001776 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 if (!fcgi_encode_begin_request(&outbuf, &rec))
1778 goto full;
1779
1780 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001781 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 +02001782 b_add(mbuf, outbuf.data);
1783 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1784 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001785 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001786 ret = 1;
1787
1788 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001789 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001790 return ret;
1791 full:
1792 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1793 goto retry;
1794 fconn->flags |= FCGI_CF_MUX_MFULL;
1795 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001796 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 +02001797 ret = 0;
1798 goto end;
1799}
1800
1801/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1802 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1803 * space to proceed. It is small enough to be encoded in an empty buffer.
1804 */
1805static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1806 enum fcgi_record_type rtype)
1807{
1808 struct buffer outbuf;
1809 struct buffer *mbuf;
1810 int ret;
1811
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001812 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001813 mbuf = br_tail(fconn->mbuf);
1814 retry:
1815 if (!fcgi_get_buf(fconn, mbuf)) {
1816 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001817 fstrm->flags |= FCGI_SF_BLK_MROOM;
1818 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1819 ret = 0;
1820 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001821 }
1822
1823 while (1) {
1824 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001825 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001826 break;
1827 realign_again:
1828 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1829 }
1830
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001831 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001832 goto full;
1833
1834 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1835 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001836 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001837 outbuf.area[1] = rtype;
1838 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001839 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001840
1841 /* commit the record */
1842 b_add(mbuf, outbuf.data);
1843 ret = 1;
1844
1845 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001846 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001847 return ret;
1848 full:
1849 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1850 goto retry;
1851 fconn->flags |= FCGI_CF_MUX_MFULL;
1852 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001853 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 +02001854 ret = 0;
1855 goto end;
1856}
1857
1858
1859/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1860 * marks the end of params.
1861 */
1862static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1863{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001864 int ret;
1865
1866 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1867 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001868 if (ret) {
1869 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001870 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 +01001871 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001872 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001873}
1874
1875/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1876 * marks the end of input. On success, all the request was successfully sent.
1877 */
1878static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1879{
1880 int ret;
1881
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001882 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001883 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001884 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001885 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001886 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 +02001887 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1888 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1889 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001890 return ret;
1891}
1892
1893/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1894 * stops the request processing.
1895 */
1896static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1897{
1898 int ret;
1899
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001900 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001901 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001902 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001903 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001904 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 +02001905 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1906 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1907 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001908 return ret;
1909}
1910
1911/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1912 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1913 * several records are sent. However, a K/V param cannot be split between 2
1914 * records.
1915 */
1916static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1917 struct htx *htx)
1918{
1919 struct buffer outbuf;
1920 struct buffer *mbuf;
1921 struct htx_blk *blk;
1922 struct htx_sl *sl = NULL;
1923 struct fcgi_strm_params params;
1924 size_t total = 0;
1925
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001926 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1927
Christopher Faulet99eff652019-08-11 23:11:30 +02001928 memset(&params, 0, sizeof(params));
1929 params.p = get_trash_chunk();
1930
1931 mbuf = br_tail(fconn->mbuf);
1932 retry:
1933 if (!fcgi_get_buf(fconn, mbuf)) {
1934 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001935 fstrm->flags |= FCGI_SF_BLK_MROOM;
1936 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1937 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001938 }
1939
1940 while (1) {
1941 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001942 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001943 break;
1944 realign_again:
1945 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1946 }
1947
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001948 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001949 goto full;
1950
1951 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1952 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001953 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001954 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001955 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001956
1957 blk = htx_get_head_blk(htx);
1958 while (blk) {
1959 enum htx_blk_type type;
1960 uint32_t size = htx_get_blksz(blk);
1961 struct fcgi_param p;
1962
1963 type = htx_get_blk_type(blk);
1964 switch (type) {
1965 case HTX_BLK_REQ_SL:
1966 sl = htx_get_blk_ptr(htx, blk);
1967 if (sl->info.req.meth == HTTP_METH_HEAD)
1968 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1969 if (sl->flags & HTX_SL_F_VER_11)
1970 fstrm->h1m.flags |= H1_MF_VER_11;
1971 break;
1972
1973 case HTX_BLK_HDR:
1974 p.n = htx_get_blk_name(htx, blk);
1975 p.v = htx_get_blk_value(htx, blk);
1976
1977 if (istmatch(p.n, ist(":fcgi-"))) {
1978 p.n.ptr += 6;
1979 p.n.len -= 6;
1980 if (isteq(p.n, ist("gateway_interface")))
1981 params.mask |= FCGI_SP_CGI_GATEWAY;
1982 else if (isteq(p.n, ist("document_root"))) {
1983 params.mask |= FCGI_SP_DOC_ROOT;
1984 params.docroot = p.v;
1985 }
1986 else if (isteq(p.n, ist("script_name"))) {
1987 params.mask |= FCGI_SP_SCRIPT_NAME;
1988 params.scriptname = p.v;
1989 }
1990 else if (isteq(p.n, ist("path_info"))) {
1991 params.mask |= FCGI_SP_PATH_INFO;
1992 params.pathinfo = p.v;
1993 }
1994 else if (isteq(p.n, ist("request_uri"))) {
1995 params.mask |= FCGI_SP_REQ_URI;
1996 params.uri = p.v;
1997 }
1998 else if (isteq(p.n, ist("request_meth")))
1999 params.mask |= FCGI_SP_REQ_METH;
2000 else if (isteq(p.n, ist("query_string")))
2001 params.mask |= FCGI_SP_REQ_QS;
2002 else if (isteq(p.n, ist("server_name")))
2003 params.mask |= FCGI_SP_SRV_NAME;
2004 else if (isteq(p.n, ist("server_port")))
2005 params.mask |= FCGI_SP_SRV_PORT;
2006 else if (isteq(p.n, ist("server_protocol")))
2007 params.mask |= FCGI_SP_SRV_PROTO;
2008 else if (isteq(p.n, ist("remote_addr")))
2009 params.mask |= FCGI_SP_REM_ADDR;
2010 else if (isteq(p.n, ist("remote_port")))
2011 params.mask |= FCGI_SP_REM_PORT;
2012 else if (isteq(p.n, ist("script_filename")))
2013 params.mask |= FCGI_SP_SCRIPT_FILE;
2014 else if (isteq(p.n, ist("path_translated")))
2015 params.mask |= FCGI_SP_PATH_TRANS;
2016 else if (isteq(p.n, ist("https")))
2017 params.mask |= FCGI_SP_HTTPS;
Christopher Fauletb2a50292021-06-11 13:34:42 +02002018 else if (isteq(p.n, ist("server_software")))
2019 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02002020 }
2021 else if (isteq(p.n, ist("content-length"))) {
2022 p.n = ist("CONTENT_LENGTH");
2023 params.mask |= FCGI_SP_CONT_LEN;
2024 }
2025 else if (isteq(p.n, ist("content-type")))
2026 p.n = ist("CONTENT_TYPE");
2027 else {
2028 if (isteq(p.n, ist("host")))
2029 params.srv_name = p.v;
Christopher Fauletf9bb8d02021-09-28 10:56:36 +02002030 else if (isteq(p.n, ist("te"))) {
2031 /* "te" may only be sent with "trailers" if this value
2032 * is present, otherwise it must be deleted.
2033 */
2034 p.v = istist(p.v, ist("trailers"));
2035 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
2036 break;
2037 p.v = ist("trailers");
2038 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002039
Christopher Faulet67d58092019-10-02 10:51:38 +02002040 /* Skip header if same name is used to add the server name */
2041 if (fconn->proxy->server_id_hdr_name &&
2042 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2043 break;
2044
Christopher Faulet99eff652019-08-11 23:11:30 +02002045 memcpy(trash.area, "http_", 5);
2046 memcpy(trash.area+5, p.n.ptr, p.n.len);
2047 p.n = ist2(trash.area, p.n.len+5);
2048 }
2049
2050 if (!fcgi_encode_param(&outbuf, &p)) {
2051 if (b_space_wraps(mbuf))
2052 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002053 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002054 goto full;
2055 goto done;
2056 }
2057 break;
2058
2059 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002060 if (fconn->proxy->server_id_hdr_name) {
2061 struct server *srv = objt_server(fconn->conn->target);
2062
2063 if (!srv)
2064 goto done;
2065
2066 memcpy(trash.area, "http_", 5);
2067 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2068 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2069 p.v = ist(srv->id);
2070
2071 if (!fcgi_encode_param(&outbuf, &p)) {
2072 if (b_space_wraps(mbuf))
2073 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002074 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002075 goto full;
2076 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002077 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002078 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002079 goto done;
2080
2081 default:
2082 break;
2083 }
2084 total += size;
2085 blk = htx_remove_blk(htx, blk);
2086 }
2087
2088 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002089 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2090 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002091 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002092 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002093
2094 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2095 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2096 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2097 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2098 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2099 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2100 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2101 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2102 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2103 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2104 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2105 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2106 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2107 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2108 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Fauletb2a50292021-06-11 13:34:42 +02002109 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002110 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2111 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002112 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002113 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002114
2115 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002116 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});
2117 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002118 b_add(mbuf, outbuf.data);
2119
2120 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002121 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002122 return total;
2123 full:
2124 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2125 goto retry;
2126 fconn->flags |= FCGI_CF_MUX_MFULL;
2127 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002128 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 +02002129 if (total)
2130 goto error;
2131 goto end;
2132
2133 error:
2134 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002135 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 +02002136 fcgi_strm_error(fstrm);
2137 goto end;
2138}
2139
2140/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2141 * anything. STDIN records contain the request body.
2142 */
2143static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2144 struct htx *htx, size_t count, struct buffer *buf)
2145{
2146 struct buffer outbuf;
2147 struct buffer *mbuf;
2148 struct htx_blk *blk;
2149 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002150 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002151 size_t total = 0;
2152
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002153 extra_bytes = 0;
2154
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002155 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002156 if (!count)
2157 goto end;
2158
2159 mbuf = br_tail(fconn->mbuf);
2160 retry:
2161 if (!fcgi_get_buf(fconn, mbuf)) {
2162 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002163 fstrm->flags |= FCGI_SF_BLK_MROOM;
2164 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2165 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002166 }
2167
2168 /* Perform some optimizations to reduce the number of buffer copies.
2169 * First, if the mux's buffer is empty and the htx area contains exactly
2170 * one data block of the same size as the requested count, and this
2171 * count fits within the record size, then it's possible to simply swap
2172 * the caller's buffer with the mux's output buffer and adjust offsets
2173 * and length to match the entire DATA HTX block in the middle. In this
2174 * case we perform a true zero-copy operation from end-to-end. This is
2175 * the situation that happens all the time with large files. Second, if
2176 * this is not possible, but the mux's output buffer is empty, we still
2177 * have an opportunity to avoid the copy to the intermediary buffer, by
2178 * making the intermediary buffer's area point to the output buffer's
2179 * area. In this case we want to skip the HTX header to make sure that
2180 * copies remain aligned and that this operation remains possible all
2181 * the time. This goes for headers, data blocks and any data extracted
2182 * from the HTX blocks.
2183 */
2184 blk = htx_get_head_blk(htx);
2185 if (!blk)
2186 goto end;
2187 type = htx_get_blk_type(blk);
2188 size = htx_get_blksz(blk);
2189 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2190 void *old_area = mbuf->area;
2191
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002192 /* Last block of the message: Reserve the size for the empty stdin record */
2193 if (htx->flags & HTX_FL_EOM)
2194 extra_bytes = FCGI_RECORD_HEADER_SZ;
2195
Christopher Faulet99eff652019-08-11 23:11:30 +02002196 if (b_data(mbuf)) {
2197 /* Too bad there are data left there. We're willing to memcpy/memmove
2198 * up to 1/4 of the buffer, which means that it's OK to copy a large
2199 * record into a buffer containing few data if it needs to be realigned,
2200 * and that it's also OK to copy few data without realigning. Otherwise
2201 * we'll pretend the mbuf is full and wait for it to become empty.
2202 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002203 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002204 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002205 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002206 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002207 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002208 }
2209
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002210 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 +02002211 /* map a FCGI record to the HTX block so that we can put the
2212 * record header there.
2213 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002214 *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 +02002215 outbuf.area = b_head(mbuf);
2216
2217 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002218 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002219 fcgi_set_record_id(outbuf.area, fstrm->id);
2220 fcgi_set_record_size(outbuf.area, size);
2221
2222 /* and exchange with our old area */
2223 buf->area = old_area;
2224 buf->data = buf->head = 0;
2225 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002226
2227 htx = (struct htx *)buf->area;
2228 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002229 goto end;
2230 }
2231
2232 copy:
2233 while (1) {
2234 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002235 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002236 break;
2237 realign_again:
2238 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2239 }
2240
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002241 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002242 goto full;
2243
2244 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2245 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002246 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002247 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002248 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002249
2250 blk = htx_get_head_blk(htx);
2251 while (blk && count) {
2252 enum htx_blk_type type = htx_get_blk_type(blk);
2253 uint32_t size = htx_get_blksz(blk);
2254 struct ist v;
2255
2256 switch (type) {
2257 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002258 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 +02002259 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002260
2261 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2262 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2263
2264 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002265 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002266 extra_bytes = 0;
2267 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002268
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002269 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002270 /* It doesn't fit at once. If it at least fits once split and
2271 * the amount of data to move is low, let's defragment the
2272 * buffer now.
2273 */
2274 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002275 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002276 b_data(mbuf) <= MAX_DATA_REALIGN)
2277 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002278 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002279 }
2280 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002281 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002282 goto full;
2283 goto done;
2284 }
2285 if (v.len != size) {
2286 total += v.len;
2287 count -= v.len;
2288 htx_cut_data_blk(htx, blk, v.len);
2289 goto done;
2290 }
2291 break;
2292
Christopher Faulet99eff652019-08-11 23:11:30 +02002293 default:
2294 break;
2295 }
2296 total += size;
2297 count -= size;
2298 blk = htx_remove_blk(htx, blk);
2299 }
2300
2301 done:
2302 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002303 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});
2304 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002305 b_add(mbuf, outbuf.data);
2306
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002307 /* Send the empty stding here to finish the message */
2308 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2309 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2310 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2311 /* bytes already reserved for this record. It should not fail */
2312 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002313 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 +01002314 fcgi_strm_error(fstrm);
2315 }
2316 }
2317
Christopher Faulet99eff652019-08-11 23:11:30 +02002318 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002319 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002320 return total;
2321 full:
2322 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2323 goto retry;
2324 fconn->flags |= FCGI_CF_MUX_MFULL;
2325 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002326 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 +02002327 goto end;
2328}
2329
2330/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2331 * anything. STDOUT records contain the entire response. All the content is
2332 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2333 */
2334static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2335{
2336 struct buffer *dbuf;
2337 size_t ret;
2338 size_t max;
2339
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002340 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2341
Christopher Faulet99eff652019-08-11 23:11:30 +02002342 dbuf = &fconn->dbuf;
2343
2344 /* Only padding remains */
2345 if (fconn->state == FCGI_CS_RECORD_P)
2346 goto end_transfer;
2347
2348 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2349 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2350 buf_room_for_htx_data(dbuf))
2351 goto fail; // incomplete record
2352
2353 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2354 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002355 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2356 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002357 }
2358
2359 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2360 max = buf_room_for_htx_data(&fstrm->rxbuf);
2361 if (!b_data(&fstrm->rxbuf))
2362 fstrm->rxbuf.head = sizeof(struct htx);
2363 if (max > fconn->drl)
2364 max = fconn->drl;
2365
2366 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2367 if (!ret)
2368 goto fail;
2369 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002370 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2371 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 +02002372
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002373 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002374 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002375 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2376 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002377
2378 if (fconn->drl)
2379 goto fail;
2380
2381 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002382 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002383 fconn->drl += fconn->drp;
2384 fconn->drp = 0;
2385 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2386 b_del(&fconn->dbuf, ret);
2387 fconn->drl -= ret;
2388 if (fconn->drl)
2389 goto fail;
2390
2391 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2393 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002394 return 1;
2395 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002397 return 0;
2398}
2399
2400
2401/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2402 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002403 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002404 */
2405static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2406{
2407 int ret;
2408
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002409 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2410
Christopher Faulet99eff652019-08-11 23:11:30 +02002411 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002413 fconn->drl += fconn->drp;
2414 fconn->drp = 0;
2415 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2416 b_del(&fconn->dbuf, ret);
2417 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002418 if (fconn->drl) {
2419 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 +02002420 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002421 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002422 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002423 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002424 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 +02002425 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);
2426 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002427 return 1;
2428}
2429
2430/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2431 * anything.
2432 */
2433static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2434{
2435 struct buffer *dbuf;
2436 struct buffer tag;
2437 size_t ret;
2438
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002439 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002440 dbuf = &fconn->dbuf;
2441
2442 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002443 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002444 goto end_transfer;
2445
2446 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2447 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2448 buf_room_for_htx_data(dbuf))
2449 goto fail; // incomplete record
2450
2451 chunk_reset(&trash);
2452 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2453 if (!ret)
2454 goto fail;
2455 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002456 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 +02002457
2458 trash.area[ret] = '\n';
2459 trash.area[ret+1] = '\0';
2460 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002461 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002462
2463 if (fconn->drl)
2464 goto fail;
2465
2466 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002467 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002468 fconn->drl += fconn->drp;
2469 fconn->drp = 0;
2470 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2471 b_del(&fconn->dbuf, ret);
2472 fconn->drl -= ret;
2473 if (fconn->drl)
2474 goto fail;
2475 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002476 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2477 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002478 return 1;
2479 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002480 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 +02002481 return 0;
2482}
2483
2484/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2485 * anything. If the empty STDOUT record is not already received, this one marks
2486 * the end of the response. It is highly unexpected, but if the record is larger
2487 * than a buffer and cannot be decoded in one time, an error is triggered and
2488 * the connection is closed. END_REQUEST record cannot be split.
2489 */
2490static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2491{
2492 struct buffer inbuf;
2493 struct buffer *dbuf;
2494 struct fcgi_end_request endreq;
2495
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002496 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002497 dbuf = &fconn->dbuf;
2498
2499 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002500 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2501 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 +02002502 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002503 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002504
2505 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002506 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2507 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002508 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002509 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002510
2511 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2512 /* Realign the dmux buffer if the record wraps. It is unexpected
2513 * at this stage because it should be the first record received
2514 * from the FCGI application.
2515 */
2516 b_slow_realign(dbuf, trash.area, 0);
2517 }
2518
2519 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2520
Christopher Faulet73518be2021-01-27 12:06:54 +01002521 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2522 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 +02002523 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002524 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002525
2526 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002527 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 +02002528 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 +02002529 fstrm->proto_status = endreq.errcode;
2530 fcgi_strm_close(fstrm);
2531
2532 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2533 fconn->drl = 0;
2534 fconn->drp = 0;
2535 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002536 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2537 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002538 return 1;
2539
2540 fail:
2541 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002542 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 +02002543 return 0;
2544}
2545
2546/* process Rx records to be demultiplexed */
2547static void fcgi_process_demux(struct fcgi_conn *fconn)
2548{
2549 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2550 struct fcgi_header hdr;
2551 int ret;
2552
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002553 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2554
Christopher Faulet99eff652019-08-11 23:11:30 +02002555 if (fconn->state == FCGI_CS_CLOSED)
2556 return;
2557
2558 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002559 if (fconn->state == FCGI_CS_INIT) {
2560 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2561 return;
2562 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002563 if (fconn->state == FCGI_CS_SETTINGS) {
2564 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002565 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002566 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002567 if (!ret) {
2568 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 +02002569 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002570 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002571 b_del(&fconn->dbuf, ret);
2572
2573 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2574 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002575 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 +02002576 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 +02002577 goto fail;
2578 }
2579 goto new_record;
2580 }
2581 }
2582
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002583 /* process as many incoming records as possible below */
2584 while (1) {
2585 if (!b_data(&fconn->dbuf)) {
2586 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2587 break;
2588 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002589
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002590 if (fconn->state == FCGI_CS_CLOSED) {
2591 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002592 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002593 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002594
2595 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002597 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2598 if (!ret)
2599 break;
2600 b_del(&fconn->dbuf, ret);
2601
2602 new_record:
2603 fconn->dsi = hdr.id;
2604 fconn->drt = hdr.type;
2605 fconn->drl = hdr.len;
2606 fconn->drp = hdr.padding;
2607 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002608 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 +02002609 }
2610
2611 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2612 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2613
2614 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2615 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002616 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002617 fstrm->state == FCGI_SS_CLOSED ||
2618 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2619 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2620 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002621 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002622 fstrm->cs->flags |= CS_FL_RCV_MORE;
2623 fcgi_strm_notify_recv(fstrm);
2624 }
2625 fstrm = tmp_fstrm;
2626
2627 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2628 /* ignore all record for closed streams */
2629 goto ignore_record;
2630 }
2631 if (fstrm->state == FCGI_SS_IDLE) {
2632 /* ignore all record for unknown streams */
2633 goto ignore_record;
2634 }
2635
2636 switch (fconn->drt) {
2637 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 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 +02002639 ret = fcgi_conn_handle_values_result(fconn);
2640 break;
2641
2642 case FCGI_STDOUT:
2643 if (fstrm->flags & FCGI_SF_ES_RCVD)
2644 goto ignore_record;
2645
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002646 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002647 if (fconn->drl)
2648 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2649 else
2650 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2651 break;
2652
2653 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002654 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002655 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2656 break;
2657
2658 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002659 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 +02002660 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2661 break;
2662
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002663 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002664 default:
2665 ignore_record:
2666 /* drop records that we ignore. They may be
2667 * larger than the buffer so we drain all of
2668 * their contents until we reach the end.
2669 */
2670 fconn->state = FCGI_CS_RECORD_P;
2671 fconn->drl += fconn->drp;
2672 fconn->drp = 0;
2673 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002674 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002675 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002676 b_del(&fconn->dbuf, ret);
2677 fconn->drl -= ret;
2678 ret = (fconn->drl == 0);
2679 }
2680
2681 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002682 if (ret <= 0) {
2683 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002684 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002685 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002686
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002687 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002688 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002689 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2690 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002691 }
2692
2693 fail:
2694 /* we can go here on missing data, blocked response or error */
2695 if (fstrm && fstrm->cs &&
2696 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002697 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002698 fstrm->state == FCGI_SS_CLOSED ||
2699 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2700 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2701 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002702 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002703 fstrm->cs->flags |= CS_FL_RCV_MORE;
2704 fcgi_strm_notify_recv(fstrm);
2705 }
2706
2707 fcgi_conn_restart_reading(fconn, 0);
2708}
2709
2710/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2711 * the end.
2712 */
2713static int fcgi_process_mux(struct fcgi_conn *fconn)
2714{
2715 struct fcgi_strm *fstrm, *fstrm_back;
2716
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002717 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2718
Christopher Faulet99eff652019-08-11 23:11:30 +02002719 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2720 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2721 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2722 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002723 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 +02002724 fcgi_wake_unassigned_streams(fconn);
2725 goto mux;
2726 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002728 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2729 goto fail;
2730 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002731 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 +02002732 }
2733 /* need to wait for the other side */
2734 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002735 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002736 }
2737
2738 mux:
2739 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2740 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2741 break;
2742
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002743 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002744 continue;
2745
Willy Tarreau7aad7032020-01-16 17:20:57 +01002746 /* If the sender changed his mind and unsubscribed, let's just
2747 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002748 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002749 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2750 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 LIST_DEL_INIT(&fstrm->send_list);
2752 continue;
2753 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002754
2755 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002756 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2757 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002758 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002759 tasklet_wakeup(fstrm->subs->tasklet);
2760 fstrm->subs->events &= ~SUB_RETRY_SEND;
2761 if (!fstrm->subs->events)
2762 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002763 } else {
2764 /* it's the shut request that was queued */
2765 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2766 tasklet_wakeup(fstrm->shut_tl);
2767 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002768 }
2769
2770 fail:
2771 if (fconn->state == FCGI_CS_CLOSED) {
2772 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2773 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002774 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2775 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002776 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002778 }
2779 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002780
2781 done:
2782 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002783 return 1;
2784}
2785
2786
2787/* Attempt to read data, and subscribe if none available.
2788 * The function returns 1 if data has been received, otherwise zero.
2789 */
2790static int fcgi_recv(struct fcgi_conn *fconn)
2791{
2792 struct connection *conn = fconn->conn;
2793 struct buffer *buf;
2794 int max;
2795 size_t ret;
2796
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002797 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2798
2799 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2800 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002801 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002802 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002803
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002804 if (!fcgi_recv_allowed(fconn)) {
2805 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002806 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002807 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002808
2809 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2810 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002811 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002812 fconn->flags |= FCGI_CF_DEM_DALLOC;
2813 return 0;
2814 }
2815
Christopher Faulet99eff652019-08-11 23:11:30 +02002816 if (!b_data(buf)) {
2817 /* try to pre-align the buffer like the
2818 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002819 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002820 * HTX block to alias it upon recv. We cannot use the
2821 * head because rcv_buf() will realign the buffer if
2822 * it's empty. Thus we cheat and pretend we already
2823 * have a few bytes there.
2824 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002825 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2826 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002827 }
2828 else
2829 max = buf_room_for_htx_data(buf);
2830
2831 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2832
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002833 if (max && !ret && fcgi_recv_allowed(fconn)) {
2834 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002835 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002836 }
2837 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002838 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002839
2840 if (!b_data(buf)) {
2841 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002842 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002843 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2844 }
2845
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002846 if (ret == max) {
2847 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002848 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002849 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002850
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002851 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002852 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2853}
2854
2855
2856/* Try to send data if possible.
2857 * The function returns 1 if data have been sent, otherwise zero.
2858 */
2859static int fcgi_send(struct fcgi_conn *fconn)
2860{
2861 struct connection *conn = fconn->conn;
2862 int done;
2863 int sent = 0;
2864
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002865 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2866
2867 if (conn->flags & CO_FL_ERROR) {
2868 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002870 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002871
2872
Willy Tarreau911db9b2020-01-23 16:27:54 +01002873 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002874 /* a handshake was requested */
2875 goto schedule;
2876 }
2877
2878 /* This loop is quite simple : it tries to fill as much as it can from
2879 * pending streams into the existing buffer until it's reportedly full
2880 * or the end of send requests is reached. Then it tries to send this
2881 * buffer's contents out, marks it not full if at least one byte could
2882 * be sent, and tries again.
2883 *
2884 * The snd_buf() function normally takes a "flags" argument which may
2885 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2886 * data immediately comes and CO_SFL_STREAMER to indicate that the
2887 * connection is streaming lots of data (used to increase TLS record
2888 * size at the expense of latency). The former can be sent any time
2889 * there's a buffer full flag, as it indicates at least one stream
2890 * attempted to send and failed so there are pending data. An
2891 * alternative would be to set it as long as there's an active stream
2892 * but that would be problematic for ACKs until we have an absolute
2893 * guarantee that all waiters have at least one byte to send. The
2894 * latter should possibly not be set for now.
2895 */
2896
2897 done = 0;
2898 while (!done) {
2899 unsigned int flags = 0;
2900 unsigned int released = 0;
2901 struct buffer *buf;
2902
2903 /* fill as much as we can into the current buffer */
2904 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2905 done = fcgi_process_mux(fconn);
2906
2907 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2908 done = 1; // we won't go further without extra buffers
2909
2910 if (conn->flags & CO_FL_ERROR)
2911 break;
2912
2913 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2914 flags |= CO_SFL_MSG_MORE;
2915
2916 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2917 if (b_data(buf)) {
2918 int ret;
2919
2920 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2921 if (!ret) {
2922 done = 1;
2923 break;
2924 }
2925 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002926 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002927 b_del(buf, ret);
2928 if (b_data(buf)) {
2929 done = 1;
2930 break;
2931 }
2932 }
2933 b_free(buf);
2934 released++;
2935 }
2936
2937 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002938 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002939
2940 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002941 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2942 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002943 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2944 }
2945
2946 if (conn->flags & CO_FL_SOCK_WR_SH) {
2947 /* output closed, nothing to send, clear the buffer to release it */
2948 b_reset(br_tail(fconn->mbuf));
2949 }
2950 /* We're not full anymore, so we can wake any task that are waiting
2951 * for us.
2952 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002953 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002954 struct fcgi_strm *fstrm;
2955
2956 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2957 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2958 break;
2959
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002960 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002961 continue;
2962
Willy Tarreau7aad7032020-01-16 17:20:57 +01002963 /* If the sender changed his mind and unsubscribed, let's just
2964 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002965 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002966 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2967 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002968 LIST_DEL_INIT(&fstrm->send_list);
2969 continue;
2970 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002971
2972 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002973 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002974 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002975 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002976 tasklet_wakeup(fstrm->subs->tasklet);
2977 fstrm->subs->events &= ~SUB_RETRY_SEND;
2978 if (!fstrm->subs->events)
2979 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002980 } else {
2981 /* it's the shut request that was queued */
2982 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2983 tasklet_wakeup(fstrm->shut_tl);
2984 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002985 }
2986 }
2987 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002988 if (!br_data(fconn->mbuf)) {
2989 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002990 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002991 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002992schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002993 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2994 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002995 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002996 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002997
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002998 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002999 return sent;
3000}
3001
3002/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003003struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003004{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003005 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003006 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003007 struct tasklet *tl = (struct tasklet *)t;
3008 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02003009 int ret = 0;
3010
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003011 if (state & TASK_F_USR1) {
3012 /* the tasklet was idling on an idle connection, it might have
3013 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003014 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003015 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3016 if (tl->context == NULL) {
3017 /* The connection has been taken over by another thread,
3018 * we're no longer responsible for it, so just free the
3019 * tasklet, and do nothing.
3020 */
3021 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3022 tasklet_free(tl);
3023 return NULL;
3024 }
3025 conn = fconn->conn;
3026 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003027
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003028 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3029 if (conn_in_list)
3030 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003031
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003032 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3033 } else {
3034 /* we're certain the connection was not in an idle list */
3035 conn = fconn->conn;
3036 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
3037 conn_in_list = 0;
3038 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003039
Christopher Faulet99eff652019-08-11 23:11:30 +02003040 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3041 ret = fcgi_send(fconn);
3042 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3043 ret |= fcgi_recv(fconn);
3044 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003045 ret = fcgi_process(fconn);
3046
3047 /* If we were in an idle list, we want to add it back into it,
3048 * unless fcgi_process() returned -1, which mean it has destroyed
3049 * the connection (testing !ret is enough, if fcgi_process() wasn't
3050 * called then ret will be 0 anyway.
3051 */
Willy Tarreau74163142021-03-13 11:30:19 +01003052 if (ret < 0)
3053 t = NULL;
3054
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003055 if (!ret && conn_in_list) {
3056 struct server *srv = objt_server(conn->target);
3057
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003058 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003059 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003060 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003061 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003062 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003063 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003064 }
Willy Tarreau74163142021-03-13 11:30:19 +01003065 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02003066}
3067
3068/* callback called on any event by the connection handler.
3069 * It applies changes and returns zero, or < 0 if it wants immediate
3070 * destruction of the connection (which normally doesn not happen in FCGI).
3071 */
3072static int fcgi_process(struct fcgi_conn *fconn)
3073{
3074 struct connection *conn = fconn->conn;
3075
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003076 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3077
Christopher Faulet99eff652019-08-11 23:11:30 +02003078 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3079 fcgi_process_demux(fconn);
3080
3081 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3082 b_reset(&fconn->dbuf);
3083
3084 if (buf_room_for_htx_data(&fconn->dbuf))
3085 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3086 }
3087 fcgi_send(fconn);
3088
Willy Tarreauc3914d42020-09-24 08:39:22 +02003089 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003090 /* frontend is stopping, reload likely in progress, let's try
3091 * to announce a graceful shutdown if not yet done. We don't
3092 * care if it fails, it will be tried again later.
3093 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003094 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 +02003095 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3096 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3097 fcgi_conn_send_aborts(fconn);
3098 }
3099 }
3100
3101 /*
3102 * If we received early data, and the handshake is done, wake
3103 * any stream that was waiting for it.
3104 */
3105 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003106 (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 +02003107 struct eb32_node *node;
3108 struct fcgi_strm *fstrm;
3109
3110 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3111 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3112
3113 while (node) {
3114 fstrm = container_of(node, struct fcgi_strm, by_id);
3115 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3116 fcgi_strm_notify_recv(fstrm);
3117 node = eb32_next(node);
3118 }
3119 }
3120
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003121 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003122 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3123 eb_is_empty(&fconn->streams_by_id)) {
3124 fcgi_wake_some_streams(fconn, 0);
3125
3126 if (eb_is_empty(&fconn->streams_by_id)) {
3127 /* no more stream, kill the connection now */
3128 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003129 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003130 return -1;
3131 }
3132 }
3133
3134 if (!b_data(&fconn->dbuf))
3135 fcgi_release_buf(fconn, &fconn->dbuf);
3136
3137 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3138 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3139 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3140 fcgi_release_mbuf(fconn);
3141
3142 if (fconn->task) {
3143 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3144 task_queue(fconn->task);
3145 }
3146
3147 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003148 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003149 return 0;
3150}
3151
3152
3153/* wake-up function called by the connection layer (mux_ops.wake) */
3154static int fcgi_wake(struct connection *conn)
3155{
3156 struct fcgi_conn *fconn = conn->ctx;
3157
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003158 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003159 return (fcgi_process(fconn));
3160}
3161
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003162
3163static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3164{
3165 int ret = 0;
3166 switch (mux_ctl) {
3167 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003168 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003169 ret |= MUX_STATUS_READY;
3170 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003171 case MUX_EXIT_STATUS:
3172 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003173 default:
3174 return -1;
3175 }
3176}
3177
Christopher Faulet99eff652019-08-11 23:11:30 +02003178/* Connection timeout management. The principle is that if there's no receipt
3179 * nor sending for a certain amount of time, the connection is closed. If the
3180 * MUX buffer still has lying data or is not allocatable, the connection is
3181 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003182 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003183 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003184struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003185{
3186 struct fcgi_conn *fconn = context;
3187 int expired = tick_is_expired(t->expire, now_ms);
3188
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003189 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3190
Willy Tarreau60814ff2020-06-30 11:19:23 +02003191 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003192 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003193
3194 /* Somebody already stole the connection from us, so we should not
3195 * free it, we just have to free the task.
3196 */
3197 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003198 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003199 fconn = NULL;
3200 goto do_leave;
3201 }
3202
Willy Tarreau60814ff2020-06-30 11:19:23 +02003203 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003204 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003205 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3206 return t;
3207 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003208
Willy Tarreau60814ff2020-06-30 11:19:23 +02003209 /* We're about to destroy the connection, so make sure nobody attempts
3210 * to steal it from us.
3211 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003212 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003213 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003214
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 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003217
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003218do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003219 task_destroy(t);
3220
3221 if (!fconn) {
3222 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003223 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003224 return NULL;
3225 }
3226
3227 fconn->task = NULL;
3228 fconn->state = FCGI_CS_CLOSED;
3229 fcgi_wake_some_streams(fconn, 0);
3230
3231 if (br_data(fconn->mbuf)) {
3232 /* don't even try to send aborts, the buffer is stuck */
3233 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3234 goto end;
3235 }
3236
3237 /* try to send but no need to insist */
3238 if (!fcgi_conn_send_aborts(fconn))
3239 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3240
3241 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3242 conn_xprt_ready(fconn->conn)) {
3243 unsigned int released = 0;
3244 struct buffer *buf;
3245
3246 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3247 if (b_data(buf)) {
3248 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3249 buf, b_data(buf), 0);
3250 if (!ret)
3251 break;
3252 b_del(buf, ret);
3253 if (b_data(buf))
3254 break;
3255 b_free(buf);
3256 released++;
3257 }
3258 }
3259
3260 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003261 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003262 }
3263
3264 end:
3265 /* either we can release everything now or it will be done later once
3266 * the last stream closes.
3267 */
3268 if (eb_is_empty(&fconn->streams_by_id))
3269 fcgi_release(fconn);
3270
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003271 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003272 return NULL;
3273}
3274
3275
3276/*******************************************/
3277/* functions below are used by the streams */
3278/*******************************************/
3279
3280/* Append the description of what is present in error snapshot <es> into <out>.
3281 * The description must be small enough to always fit in a buffer. The output
3282 * buffer may be the trash so the trash must not be used inside this function.
3283 */
3284static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3285{
3286 chunk_appendf(out,
3287 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3288 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3289 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3290 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3291 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3292 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3293}
3294/*
3295 * Capture a bad response and archive it in the proxy's structure. By default
3296 * it tries to report the error position as h1m->err_pos. However if this one is
3297 * not set, it will then report h1m->next, which is the last known parsing
3298 * point. The function is able to deal with wrapping buffers. It always displays
3299 * buffers as a contiguous area starting at buf->p. The direction is determined
3300 * thanks to the h1m's flags.
3301 */
3302static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3303 struct h1m *h1m, struct buffer *buf)
3304{
3305 struct session *sess = fstrm->sess;
3306 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003307 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003308 union error_snapshot_ctx ctx;
3309
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003310 if (fstrm->cs && fstrm->cs->data) {
3311 if (sess == NULL)
3312 sess = si_strm(fstrm->cs->data)->sess;
3313 if (!(h1m->flags & H1_MF_RESP))
3314 other_end = si_strm(fstrm->cs->data)->be;
3315 else
3316 other_end = sess->fe;
3317 } else
3318 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003319 /* http-specific part now */
3320 ctx.h1.state = h1m->state;
3321 ctx.h1.c_flags = fconn->flags;
3322 ctx.h1.s_flags = fstrm->flags;
3323 ctx.h1.m_flags = h1m->flags;
3324 ctx.h1.m_clen = h1m->curr_len;
3325 ctx.h1.m_blen = h1m->body_len;
3326
3327 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3328 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3329 &ctx, fcgi_show_error_snapshot);
3330}
3331
3332static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3333 struct buffer *buf, size_t *ofs, size_t max)
3334{
3335 int ret;
3336
Willy Tarreau022e5e52020-09-10 09:33:15 +02003337 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 +02003338 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
Christopher Fauletd04f5302022-03-28 15:37:01 +02003339 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003340 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 +02003341 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003342 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 +02003343 fcgi_strm_error(fstrm);
3344 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3345 }
Christopher Fauletd04f5302022-03-28 15:37:01 +02003346 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003347 goto end;
3348 }
3349
3350 *ofs += ret;
3351 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003352 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 +02003353 return ret;
3354
3355}
3356
Christopher Fauletaf542632019-10-01 21:52:49 +02003357static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003358 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3359{
3360 int ret;
3361
Willy Tarreau022e5e52020-09-10 09:33:15 +02003362 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 +02003363 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003364 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003365 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 +02003366 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003367 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 +02003368 fcgi_strm_error(fstrm);
3369 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3370 }
3371 goto end;
3372 }
3373 *ofs += ret;
3374 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003375 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 +02003376 return ret;
3377}
3378
3379static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3380 struct buffer *buf, size_t *ofs, size_t max)
3381{
3382 int ret;
3383
Willy Tarreau022e5e52020-09-10 09:33:15 +02003384 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 +02003385 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Fauletd04f5302022-03-28 15:37:01 +02003386 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003387 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 +02003388 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003389 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 +02003390 fcgi_strm_error(fstrm);
3391 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3392 }
Christopher Fauletd04f5302022-03-28 15:37:01 +02003393 ret = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02003394 goto end;
3395 }
3396 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003397 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003398 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 +02003399 return ret;
3400}
3401
Christopher Faulet99eff652019-08-11 23:11:30 +02003402static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3403{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003404 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003405 struct htx *htx;
3406 struct h1m *h1m = &fstrm->h1m;
3407 size_t ret, data, total = 0;
3408
3409 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003410 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3411
Christopher Faulet99eff652019-08-11 23:11:30 +02003412 data = htx->data;
3413 if (fstrm->state == FCGI_SS_ERROR)
3414 goto end;
3415
3416 do {
3417 size_t used = htx_used_space(htx);
3418
3419 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003420 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003421 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3422 if (!ret)
3423 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003424
3425 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3426
Christopher Faulet99eff652019-08-11 23:11:30 +02003427 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3428 struct htx_blk *blk = htx_get_head_blk(htx);
3429 struct htx_sl *sl;
3430
3431 if (!blk)
3432 break;
3433 sl = htx_get_blk_ptr(htx, blk);
3434 sl->flags |= HTX_SL_F_XFER_LEN;
3435 htx->extra = 0;
3436 }
3437 }
3438 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003439 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003440 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003441
3442 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3443 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3444 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauleta7a92f12022-05-05 09:24:52 +02003445 if (htx_is_empty(htx) && !htx_add_endof(htx, HTX_BLK_EOT))
3446 break;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003447 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003448 h1m->state = H1_MSG_DONE;
3449 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3450 }
3451
Christopher Faulet76014fd2019-12-10 11:47:22 +01003452 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003453 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003454
3455 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 +02003456 }
3457 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003458 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3459 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3460 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003461 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003462
Christopher Faulet76014fd2019-12-10 11:47:22 +01003463 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 +02003464 }
3465 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003466 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 +02003467 if (b_data(&fstrm->rxbuf) > total) {
3468 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003469 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003470 fcgi_strm_error(fstrm);
3471 }
3472 break;
3473 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003474 else {
3475 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003476 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003477 fcgi_strm_error(fstrm);
3478 break;
3479 }
3480
3481 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003482 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003483
3484 if (fstrm->state == FCGI_SS_ERROR) {
3485 b_reset(&fstrm->rxbuf);
3486 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003487 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003488 return 0;
3489 }
3490
3491 b_del(&fstrm->rxbuf, total);
3492
3493 end:
3494 htx_to_buf(htx, buf);
3495 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003496 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003497 return ret;
3498}
3499
3500/*
3501 * Attach a new stream to a connection
3502 * (Used for outgoing connections)
3503 */
3504static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3505{
3506 struct conn_stream *cs;
3507 struct fcgi_strm *fstrm;
3508 struct fcgi_conn *fconn = conn->ctx;
3509
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003510 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003511 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003512 if (!cs) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003513 TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3514 goto err;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003515 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003516 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3517 if (!fstrm) {
3518 cs_free(cs);
Christopher Faulet73518be2021-01-27 12:06:54 +01003519 goto err;
Christopher Faulet99eff652019-08-11 23:11:30 +02003520 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003521
3522 /* the connection is not idle anymore, let's mark this */
3523 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003524 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003525
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003526 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003527 return cs;
Christopher Faulet73518be2021-01-27 12:06:54 +01003528
3529 err:
3530 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3531 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003532}
3533
3534/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3535 * We have to scan because we may have some orphan streams. It might be
3536 * beneficial to scan backwards from the end to reduce the likeliness to find
3537 * orphans.
3538 */
3539static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3540{
3541 struct fcgi_conn *fconn = conn->ctx;
3542 struct fcgi_strm *fstrm;
3543 struct eb32_node *node;
3544
3545 node = eb32_first(&fconn->streams_by_id);
3546 while (node) {
3547 fstrm = container_of(node, struct fcgi_strm, by_id);
3548 if (fstrm->cs)
3549 return fstrm->cs;
3550 node = eb32_next(node);
3551 }
3552 return NULL;
3553}
3554
3555/*
3556 * Destroy the mux and the associated connection, if it is no longer used
3557 */
3558static void fcgi_destroy(void *ctx)
3559{
3560 struct fcgi_conn *fconn = ctx;
3561
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003562 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003563 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3564 fcgi_release(fconn);
3565}
3566
3567/*
3568 * Detach the stream from the connection and possibly release the connection.
3569 */
3570static void fcgi_detach(struct conn_stream *cs)
3571{
3572 struct fcgi_strm *fstrm = cs->ctx;
3573 struct fcgi_conn *fconn;
3574 struct session *sess;
3575
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003576 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3577
Christopher Faulet99eff652019-08-11 23:11:30 +02003578 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003579 if (!fstrm) {
3580 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003581 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003582 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003583
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003584 /* there's no txbuf so we're certain no to be able to send anything */
3585 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003586
3587 sess = fstrm->sess;
3588 fconn = fstrm->fconn;
3589 fstrm->cs = NULL;
3590 fconn->nb_cs--;
3591
3592 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3593 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3594 fconn->streams_limit = 1;
3595 }
3596 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3597 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3598 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3599 fconn->state = FCGI_CS_CLOSED;
3600 }
3601
3602 /* this stream may be blocked waiting for some data to leave, so orphan
3603 * it in this case.
3604 */
3605 if (!(cs->conn->flags & CO_FL_ERROR) &&
3606 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003607 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003608 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003609 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003610 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003611 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003612
3613 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3614 /* unblock the connection if it was blocked on this stream. */
3615 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3616 fcgi_conn_restart_reading(fconn, 1);
3617 }
3618
3619 fcgi_strm_destroy(fstrm);
3620
3621 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003622 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003623 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003624 /* Add the connection in the session serverlist, if not already done */
3625 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3626 fconn->conn->owner = NULL;
3627 if (eb_is_empty(&fconn->streams_by_id)) {
3628 /* let's kill the connection right away */
3629 fconn->conn->mux->destroy(fconn);
3630 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3631 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003632 }
3633 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003634 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003635 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3636 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003637 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003638 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003639 }
3640 }
3641 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003642 else {
3643 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003644 /* If the connection is owned by the session, first remove it
3645 * from its list
3646 */
3647 if (fconn->conn->owner) {
3648 session_unown_conn(fconn->conn->owner, fconn->conn);
3649 fconn->conn->owner = NULL;
3650 }
3651
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003652 /* mark that the tasklet may lose its context to another thread and
3653 * that the handler needs to check it under the idle conns lock.
3654 */
3655 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003656 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3657
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003658 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003659 /* The server doesn't want it, let's kill the connection right away */
3660 fconn->conn->mux->destroy(fconn);
3661 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3662 return;
3663 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003664 /* At this point, the connection has been added to the
3665 * server idle list, so another thread may already have
3666 * hijacked it, so we can't do anything with it.
3667 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003668 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3669 return;
3670 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003671 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003672 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02003673 !LIST_INLIST(&fconn->conn->session_list)) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003674 ebmb_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003675 &fconn->conn->hash_node->node,
3676 sizeof(fconn->conn->hash_node->hash));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003677 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003678 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003679 }
3680
3681 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003683 */
3684 if (fcgi_conn_is_dead(fconn)) {
3685 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003686 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003687 fcgi_release(fconn);
3688 }
3689 else if (fconn->task) {
3690 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3691 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003692 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003694 else
3695 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003696}
3697
3698
3699/* Performs a synchronous or asynchronous shutr(). */
3700static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3701{
3702 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003703
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003704 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3705
Christopher Faulet99eff652019-08-11 23:11:30 +02003706 if (fstrm->state == FCGI_SS_CLOSED)
3707 goto done;
3708
3709 /* a connstream may require us to immediately kill the whole connection
3710 * for example because of a "tcp-request content reject" rule that is
3711 * normally used to limit abuse.
3712 */
3713 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003714 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3715 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003717 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003719 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003720 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3721 !fcgi_strm_send_abort(fconn, fstrm))
3722 goto add_to_list;
3723 }
3724
3725 fcgi_strm_close(fstrm);
3726
3727 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3728 tasklet_wakeup(fconn->wait_event.tasklet);
3729 done:
3730 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003731 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 return;
3733
3734 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003735 /* Let the handler know we want to shutr, and add ourselves to the
3736 * send list if not yet done. fcgi_deferred_shut() will be
3737 * automatically called via the shut_tl tasklet when there's room
3738 * again.
3739 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003740 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003741 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003742 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003743 }
3744 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003745 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003746 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003747 return;
3748}
3749
3750/* Performs a synchronous or asynchronous shutw(). */
3751static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3752{
3753 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003754
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003755 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3756
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3758 goto done;
3759
3760 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3761 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3762 !fcgi_strm_send_abort(fconn, fstrm))
3763 goto add_to_list;
3764
3765 if (fstrm->state == FCGI_SS_HREM)
3766 fcgi_strm_close(fstrm);
3767 else
3768 fstrm->state = FCGI_SS_HLOC;
3769 } else {
3770 /* a connstream may require us to immediately kill the whole connection
3771 * for example because of a "tcp-request content reject" rule that is
3772 * normally used to limit abuse.
3773 */
3774 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003775 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3776 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003777 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003778 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003779
3780 fcgi_strm_close(fstrm);
3781 }
3782
3783 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3784 tasklet_wakeup(fconn->wait_event.tasklet);
3785 done:
3786 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003787 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 return;
3789
3790 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003791 /* Let the handler know we want to shutr, and add ourselves to the
3792 * send list if not yet done. fcgi_deferred_shut() will be
3793 * automatically called via the shut_tl tasklet when there's room
3794 * again.
3795 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003796 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003798 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003799 }
3800 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003801 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003802 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003803 return;
3804}
3805
Willy Tarreau7aad7032020-01-16 17:20:57 +01003806/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003807 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003808 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003809 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003810struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003811{
3812 struct fcgi_strm *fstrm = ctx;
3813 struct fcgi_conn *fconn = fstrm->fconn;
3814
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003815 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3816
Willy Tarreau7aad7032020-01-16 17:20:57 +01003817 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3818 /* some data processing remains to be done first */
3819 goto end;
3820 }
3821
Christopher Faulet99eff652019-08-11 23:11:30 +02003822 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3823 fcgi_do_shutw(fstrm);
3824
3825 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3826 fcgi_do_shutr(fstrm);
3827
3828 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3829 /* We're done trying to send, remove ourself from the send_list */
3830 LIST_DEL_INIT(&fstrm->send_list);
3831
3832 if (!fstrm->cs) {
3833 fcgi_strm_destroy(fstrm);
3834 if (fcgi_conn_is_dead(fconn))
3835 fcgi_release(fconn);
3836 }
3837 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003838 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003839 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003840 return NULL;
3841}
3842
3843/* shutr() called by the conn_stream (mux_ops.shutr) */
3844static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3845{
3846 struct fcgi_strm *fstrm = cs->ctx;
3847
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003848 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003849 if (cs->flags & CS_FL_KILL_CONN)
3850 fstrm->flags |= FCGI_SF_KILL_CONN;
3851
3852 if (!mode)
3853 return;
3854
3855 fcgi_do_shutr(fstrm);
3856}
3857
3858/* shutw() called by the conn_stream (mux_ops.shutw) */
3859static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3860{
3861 struct fcgi_strm *fstrm = cs->ctx;
3862
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003863 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 if (cs->flags & CS_FL_KILL_CONN)
3865 fstrm->flags |= FCGI_SF_KILL_CONN;
3866
3867 fcgi_do_shutw(fstrm);
3868}
3869
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003870/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3871 * event subscriber <es> is not allowed to change from a previous call as long
3872 * as at least one event is still subscribed. The <event_type> must only be a
3873 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003874 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003875static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003876{
Christopher Faulet99eff652019-08-11 23:11:30 +02003877 struct fcgi_strm *fstrm = cs->ctx;
3878 struct fcgi_conn *fconn = fstrm->fconn;
3879
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003880 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003881 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003882
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003883 es->events |= event_type;
3884 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003885
3886 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003887 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003888
Christopher Faulet99eff652019-08-11 23:11:30 +02003889 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003890 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003891 if (!LIST_INLIST(&fstrm->send_list))
3892 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003893 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003894 return 0;
3895}
3896
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003897/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3898 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3899 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003900 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003901static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003902{
Christopher Faulet99eff652019-08-11 23:11:30 +02003903 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003904 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003905
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003906 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003907 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003908
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003909 es->events &= ~event_type;
3910 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003911 fstrm->subs = NULL;
3912
3913 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003914 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003915
Christopher Faulet99eff652019-08-11 23:11:30 +02003916 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003917 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003918 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003919 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3920 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003921 }
3922 return 0;
3923}
3924
Christopher Faulet93a466b2021-09-21 15:50:55 +02003925/* Called from the upper layer, to receive data
3926 *
3927 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3928 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3929 * means the caller wants to flush input data (from the mux buffer and the
3930 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3931 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3932 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3933 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3934 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3935 * copy as much data as possible.
3936 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003937static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3938{
3939 struct fcgi_strm *fstrm = cs->ctx;
3940 struct fcgi_conn *fconn = fstrm->fconn;
3941 size_t ret = 0;
3942
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003943 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3944
Christopher Faulet99eff652019-08-11 23:11:30 +02003945 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3946 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003947 else
3948 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003949
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003950 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003951 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3952 else {
3953 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003954 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003955 cs->flags |= CS_FL_EOI;
3956 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3957 cs->flags |= CS_FL_EOS;
3958 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003959 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003960 cs->flags |= CS_FL_EOS;
3961 if (cs->flags & CS_FL_ERR_PENDING)
3962 cs->flags |= CS_FL_ERROR;
3963 fcgi_release_buf(fconn, &fstrm->rxbuf);
3964 }
3965
3966 if (ret && fconn->dsi == fstrm->id) {
3967 /* demux is blocking on this stream's buffer */
3968 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3969 fcgi_conn_restart_reading(fconn, 1);
3970 }
3971
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003972 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003973 return ret;
3974}
3975
3976
Christopher Faulet99eff652019-08-11 23:11:30 +02003977/* Called from the upper layer, to send data from buffer <buf> for no more than
3978 * <count> bytes. Returns the number of bytes effectively sent. Some status
3979 * flags may be updated on the conn_stream.
3980 */
3981static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3982{
3983 struct fcgi_strm *fstrm = cs->ctx;
3984 struct fcgi_conn *fconn = fstrm->fconn;
3985 size_t total = 0;
3986 size_t ret;
3987 struct htx *htx = NULL;
3988 struct htx_sl *sl;
3989 struct htx_blk *blk;
3990 uint32_t bsize;
3991
Willy Tarreau022e5e52020-09-10 09:33:15 +02003992 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993
Christopher Faulet99eff652019-08-11 23:11:30 +02003994 /* If we were not just woken because we wanted to send but couldn't,
3995 * and there's somebody else that is waiting to send, do nothing,
3996 * we will subscribe later and be put at the end of the list
3997 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003998 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003999 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 +02004000 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004001 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01004002 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02004003
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004004 if (fconn->state < FCGI_CS_RECORD_H) {
4005 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02004006 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004007 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004008
4009 htx = htxbuf(buf);
4010 if (fstrm->id == 0) {
4011 int32_t id = fcgi_conn_get_next_sid(fconn);
4012
4013 if (id < 0) {
4014 fcgi_strm_close(fstrm);
4015 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004016 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 +02004017 return 0;
4018 }
4019
4020 eb32_delete(&fstrm->by_id);
4021 fstrm->by_id.key = fstrm->id = id;
4022 fconn->max_id = id;
4023 fconn->nb_reserved--;
4024 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
4025
4026
4027 /* Check if length of the body is known or if the message is
4028 * full. Otherwise, the request is invalid.
4029 */
4030 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004031 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02004032 htx->flags |= HTX_FL_PARSING_ERROR;
4033 fcgi_strm_error(fstrm);
4034 goto done;
4035 }
4036 }
4037
4038 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004039 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 +02004040 if (!fcgi_strm_send_begin_request(fconn, fstrm))
4041 goto done;
4042 }
4043
4044 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
4045 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
4046
Christopher Fauletfe410d62020-05-19 15:13:00 +02004047 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02004048 count && !htx_is_empty(htx)) {
4049 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02004050 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02004051 bsize = htx_get_blksz(blk);
4052
4053 switch (htx_get_blk_type(blk)) {
4054 case HTX_BLK_REQ_SL:
4055 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004056 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 +02004057 ret = fcgi_strm_send_params(fconn, fstrm, htx);
4058 if (!ret) {
4059 goto done;
4060 }
4061 total += ret;
4062 count -= ret;
4063 break;
4064
4065 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004066 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4067 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4068 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4069 if (!ret)
4070 goto done;
4071 }
4072 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4073 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4074 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4075 if (!ret)
4076 goto done;
4077 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004078 goto remove_blk;
4079
4080 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004081 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 +02004082 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4083 if (ret > 0) {
4084 htx = htx_from_buf(buf);
4085 total += ret;
4086 count -= ret;
4087 if (ret < bsize)
4088 goto done;
4089 }
4090 break;
4091
Christopher Faulet99eff652019-08-11 23:11:30 +02004092 default:
4093 remove_blk:
4094 htx_remove_blk(htx, blk);
4095 total += bsize;
4096 count -= bsize;
4097 break;
4098 }
4099 }
4100
4101 done:
4102 if (fstrm->state >= FCGI_SS_HLOC) {
4103 /* trim any possibly pending data after we close (extra CR-LF,
4104 * unprocessed trailers, abnormal extra data, ...)
4105 */
4106 total += count;
4107 count = 0;
4108 }
4109
4110 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004111 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 +02004112 cs_set_error(cs);
4113 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4114 fcgi_strm_close(fstrm);
4115 }
4116
4117 if (htx)
4118 htx_to_buf(htx, buf);
4119
Christopher Faulet99eff652019-08-11 23:11:30 +02004120 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004121 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4122 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 +02004123 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004124 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004125
4126 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004127 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4128 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004129 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004130
4131 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004132 return total;
4133}
4134
4135/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004136static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004137{
4138 struct fcgi_conn *fconn = conn->ctx;
4139 struct fcgi_strm *fstrm = NULL;
4140 struct eb32_node *node;
4141 int send_cnt = 0;
4142 int tree_cnt = 0;
4143 int orph_cnt = 0;
4144 struct buffer *hmbuf, *tmbuf;
4145
4146 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004147 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004148
4149 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4150 send_cnt++;
4151
4152 fstrm = NULL;
4153 node = eb32_first(&fconn->streams_by_id);
4154 while (node) {
4155 fstrm = container_of(node, struct fcgi_strm, by_id);
4156 tree_cnt++;
4157 if (!fstrm->cs)
4158 orph_cnt++;
4159 node = eb32_next(node);
4160 }
4161
4162 hmbuf = br_head(fconn->mbuf);
4163 tmbuf = br_tail(fconn->mbuf);
4164 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4165 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4166 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4167 fconn->state, fconn->max_id, fconn->flags,
4168 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4169 fconn->wait_event.events, fconn->dsi,
4170 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4171 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4172 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4173 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4174 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4175 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4176 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4177
4178 if (fstrm) {
4179 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4180 fstrm, fstrm->id, fstrm->flags,
4181 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4182 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4183 fstrm->cs);
4184 if (fstrm->cs)
4185 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4186 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreaud3d95512022-09-02 14:22:38 +02004187 chunk_appendf(msg, " .subs=%p", fstrm->subs);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004188 if (fstrm->subs) {
Willy Tarreaud3d95512022-09-02 14:22:38 +02004189 chunk_appendf(msg, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4190 chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=",
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004191 fstrm->subs->tasklet->calls,
4192 fstrm->subs->tasklet->context);
Willy Tarreaud3d95512022-09-02 14:22:38 +02004193 resolve_sym_name(msg, NULL, fstrm->subs->tasklet->process);
4194 chunk_appendf(msg, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004195 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004196 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004197 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004198}
4199
4200/* Migrate the the connection to the current thread.
4201 * Return 0 if successful, non-zero otherwise.
4202 * Expected to be called with the old thread lock held.
4203 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004204static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004205{
4206 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004207 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004208
4209 if (fd_takeover(conn->handle.fd, conn) != 0)
4210 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004211
4212 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4213 /* We failed to takeover the xprt, even if the connection may
4214 * still be valid, flag it as error'd, as we have already
4215 * taken over the fd, and wake the tasklet, so that it will
4216 * destroy it.
4217 */
4218 conn->flags |= CO_FL_ERROR;
4219 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4220 return -1;
4221 }
4222
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004223 if (fcgi->wait_event.events)
4224 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4225 fcgi->wait_event.events, &fcgi->wait_event);
4226 /* To let the tasklet know it should free itself, and do nothing else,
4227 * set its context to NULL;
4228 */
4229 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004230 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004231
4232 task = fcgi->task;
4233 if (task) {
4234 task->context = NULL;
4235 fcgi->task = NULL;
4236 __ha_barrier_store();
4237 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004238
4239 fcgi->task = task_new(tid_bit);
4240 if (!fcgi->task) {
4241 fcgi_release(fcgi);
4242 return -1;
4243 }
4244 fcgi->task->process = fcgi_timeout_task;
4245 fcgi->task->context = fcgi;
4246 }
4247 fcgi->wait_event.tasklet = tasklet_new();
4248 if (!fcgi->wait_event.tasklet) {
4249 fcgi_release(fcgi);
4250 return -1;
4251 }
4252 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4253 fcgi->wait_event.tasklet->context = fcgi;
4254 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4255 SUB_RETRY_RECV, &fcgi->wait_event);
4256
4257 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004258}
4259
4260/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004261/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004262/****************************************/
4263
4264/* The mux operations */
4265static const struct mux_ops mux_fcgi_ops = {
4266 .init = fcgi_init,
4267 .wake = fcgi_wake,
4268 .attach = fcgi_attach,
4269 .get_first_cs = fcgi_get_first_cs,
4270 .detach = fcgi_detach,
4271 .destroy = fcgi_destroy,
4272 .avail_streams = fcgi_avail_streams,
4273 .used_streams = fcgi_used_streams,
4274 .rcv_buf = fcgi_rcv_buf,
4275 .snd_buf = fcgi_snd_buf,
4276 .subscribe = fcgi_subscribe,
4277 .unsubscribe = fcgi_unsubscribe,
4278 .shutr = fcgi_shutr,
4279 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004280 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004281 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004282 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004283 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004284 .name = "FCGI",
4285};
4286
4287
4288/* this mux registers FCGI proto */
4289static struct mux_proto_list mux_proto_fcgi =
4290{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4291
4292INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4293
4294/*
4295 * Local variables:
4296 * c-indent-level: 8
4297 * c-basic-offset: 8
4298 * End:
4299 */