blob: 46ee5eddf5961451900be11d863a414214fd62b6 [file] [log] [blame]
Christopher Faulet99eff652019-08-11 23:11:30 +02001/*
2 * FastCGI mux-demux for connections
3 *
4 * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/ist.h>
Willy Tarreau63617db2021-10-06 18:23:40 +020014#include <import/eb32tree.h>
15#include <import/ebmbtree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020019#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020020#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020021#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020023#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020024#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020027#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreauc5396bd2021-05-08 20:28:54 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020032#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020035#include <haproxy/trace.h>
Christopher Faulet5cd0e522021-06-11 13:34:42 +020036#include <haproxy/version.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020037
Willy Tarreaub2551052020-06-09 09:07:15 +020038
Christopher Faulet99eff652019-08-11 23:11:30 +020039/* FCGI Connection flags (32 bits) */
40#define FCGI_CF_NONE 0x00000000
41
42/* Flags indicating why writing to the mux is blockes */
43#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
44#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
45#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
46
47/* Flags indicating why writing to the demux is blocked.
48 * The first two ones directly affect the ability for the mux to receive data
49 * from the connection. The other ones affect the mux's ability to demux
50 * received data.
51 */
52#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
53#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
54#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
55#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
56#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
57#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
58#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
59
60/* Other flags */
61#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
62#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
63#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
64#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 +020065#define FCGI_CF_KEEP_CONN 0x00001000 /* HAProxy is responsible to close the connection */
Christopher Faulet99eff652019-08-11 23:11:30 +020066#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
67
68/* FCGI connection state (fcgi_conn->state) */
69enum fcgi_conn_st {
70 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
71 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
72 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
73 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
74 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
75 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
76 FCGI_CS_ENTRIES
77} __attribute__((packed));
78
79/* 32 buffers: one for the ring's root, rest for the mbuf itself */
80#define FCGI_C_MBUF_CNT 32
81
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010082/* Size for a record header (also size of empty record) */
83#define FCGI_RECORD_HEADER_SZ 8
84
Christopher Faulet99eff652019-08-11 23:11:30 +020085/* FCGI connection descriptor */
86struct fcgi_conn {
87 struct connection *conn;
88
89 enum fcgi_conn_st state; /* FCGI connection state */
90 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
91 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
92 uint32_t flags; /* Connection flags: FCGI_CF_* */
93
94 int16_t dsi; /* dmux stream ID (<0 = idle ) */
95 uint16_t drl; /* demux record length (if dsi >= 0) */
96 uint8_t drt; /* demux record type (if dsi >= 0) */
97 uint8_t drp; /* demux record padding (if dsi >= 0) */
98
99 struct buffer dbuf; /* demux buffer */
100 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
101
102 int timeout; /* idle timeout duration in ticks */
103 int shut_timeout; /* idle timeout duration in ticks after shutdown */
104 unsigned int nb_streams; /* number of streams in the tree */
105 unsigned int nb_cs; /* number of attached conn_streams */
106 unsigned int nb_reserved; /* number of reserved streams */
107 unsigned int stream_cnt; /* total number of streams seen */
108
109 struct proxy *proxy; /* the proxy this connection was created for */
110 struct fcgi_app *app; /* FCGI application used by this mux */
111 struct task *task; /* timeout management task */
112 struct eb_root streams_by_id; /* all active streams by their ID */
113
114 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200115
116 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
117 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
118};
119
120
121/* FCGI stream state, in fcgi_strm->state */
122enum fcgi_strm_st {
123 FCGI_SS_IDLE = 0,
124 FCGI_SS_OPEN,
125 FCGI_SS_HREM, // half-closed(remote)
126 FCGI_SS_HLOC, // half-closed(local)
127 FCGI_SS_ERROR,
128 FCGI_SS_CLOSED,
129 FCGI_SS_ENTRIES
130} __attribute__((packed));
131
132
133/* FCGI stream flags (32 bits) */
134#define FCGI_SF_NONE 0x00000000
135#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100136#define FCGI_SF_ES_SENT 0x00000002 /* end-of-stream sent (empty STDIN record) */
137#define FCGI_SF_EP_SENT 0x00000004 /* end-of-param sent (empty PARAMS record) */
138#define FCGI_SF_ABRT_SENT 0x00000008 /* abort sent (ABORT_REQUEST record) */
Christopher Faulet99eff652019-08-11 23:11:30 +0200139
140/* Stream flags indicating the reason the stream is blocked */
141#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
142#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
143#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
144
145#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
146#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100147#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200148
149#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
150#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
151#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
152
Christopher Faulet99eff652019-08-11 23:11:30 +0200153
154/* FCGI stream descriptor */
155struct fcgi_strm {
156 struct conn_stream *cs;
157 struct session *sess;
158 struct fcgi_conn *fconn;
159
160 int32_t id; /* stream ID */
161
162 uint32_t flags; /* Connection flags: FCGI_SF_* */
163 enum fcgi_strm_st state; /* FCGI stream state */
164 int proto_status; /* FCGI_PS_* */
165
166 struct h1m h1m; /* response parser state for H1 */
167
168 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
169
170 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100171 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200172 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100173 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 +0200174};
175
176/* Flags representing all default FCGI parameters */
177#define FCGI_SP_CGI_GATEWAY 0x00000001
178#define FCGI_SP_DOC_ROOT 0x00000002
179#define FCGI_SP_SCRIPT_NAME 0x00000004
180#define FCGI_SP_PATH_INFO 0x00000008
181#define FCGI_SP_REQ_URI 0x00000010
182#define FCGI_SP_REQ_METH 0x00000020
183#define FCGI_SP_REQ_QS 0x00000040
184#define FCGI_SP_SRV_PORT 0x00000080
185#define FCGI_SP_SRV_PROTO 0x00000100
186#define FCGI_SP_SRV_NAME 0x00000200
187#define FCGI_SP_REM_ADDR 0x00000400
188#define FCGI_SP_REM_PORT 0x00000800
189#define FCGI_SP_SCRIPT_FILE 0x00001000
190#define FCGI_SP_PATH_TRANS 0x00002000
191#define FCGI_SP_CONT_LEN 0x00004000
192#define FCGI_SP_HTTPS 0x00008000
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200193#define FCGI_SP_SRV_SOFT 0x00010000
194#define FCGI_SP_MASK 0x0001FFFF
Christopher Faulet99eff652019-08-11 23:11:30 +0200195#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
196
197/* FCGI parameters used when PARAMS record is sent */
198struct fcgi_strm_params {
199 uint32_t mask;
200 struct ist docroot;
201 struct ist scriptname;
202 struct ist pathinfo;
203 struct ist meth;
204 struct ist uri;
205 struct ist vsn;
206 struct ist qs;
207 struct ist srv_name;
208 struct ist srv_port;
209 struct ist rem_addr;
210 struct ist rem_port;
211 struct ist cont_len;
Christopher Faulet5cd0e522021-06-11 13:34:42 +0200212 struct ist srv_soft;
Christopher Faulet99eff652019-08-11 23:11:30 +0200213 int https;
214 struct buffer *p;
215};
216
217/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
218#define MAX_DATA_REALIGN 1024
219
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200220/* trace source and events */
221static void fcgi_trace(enum trace_level level, uint64_t mask,
222 const struct trace_source *src,
223 const struct ist where, const struct ist func,
224 const void *a1, const void *a2, const void *a3, const void *a4);
225
226/* The event representation is split like this :
227 * fconn - internal FCGI connection
228 * fstrm - internal FCGI stream
229 * strm - application layer
230 * rx - data receipt
231 * tx - data transmission
232 * rsp - response parsing
233 */
234static const struct trace_event fcgi_trace_events[] = {
235#define FCGI_EV_FCONN_NEW (1ULL << 0)
236 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
237#define FCGI_EV_FCONN_RECV (1ULL << 1)
238 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
239#define FCGI_EV_FCONN_SEND (1ULL << 2)
240 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
241#define FCGI_EV_FCONN_BLK (1ULL << 3)
242 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
243#define FCGI_EV_FCONN_WAKE (1ULL << 4)
244 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
245#define FCGI_EV_FCONN_END (1ULL << 5)
246 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
247#define FCGI_EV_FCONN_ERR (1ULL << 6)
248 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
249
250#define FCGI_EV_RX_FHDR (1ULL << 7)
251 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
252#define FCGI_EV_RX_RECORD (1ULL << 8)
253 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
254#define FCGI_EV_RX_EOI (1ULL << 9)
255 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
256#define FCGI_EV_RX_GETVAL (1ULL << 10)
257 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
258#define FCGI_EV_RX_STDOUT (1ULL << 11)
259 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
260#define FCGI_EV_RX_STDERR (1ULL << 12)
261 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
262#define FCGI_EV_RX_ENDREQ (1ULL << 13)
263 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
264
265#define FCGI_EV_TX_RECORD (1ULL << 14)
266 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
267#define FCGI_EV_TX_EOI (1ULL << 15)
268 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
269#define FCGI_EV_TX_BEGREQ (1ULL << 16)
270 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
271#define FCGI_EV_TX_GETVAL (1ULL << 17)
272 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
273#define FCGI_EV_TX_PARAMS (1ULL << 18)
274 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
275#define FCGI_EV_TX_STDIN (1ULL << 19)
276 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
277#define FCGI_EV_TX_ABORT (1ULL << 20)
278 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
279
280#define FCGI_EV_RSP_DATA (1ULL << 21)
281 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
282#define FCGI_EV_RSP_EOM (1ULL << 22)
283 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
284#define FCGI_EV_RSP_HDRS (1ULL << 23)
285 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
286#define FCGI_EV_RSP_BODY (1ULL << 24)
287 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
288#define FCGI_EV_RSP_TLRS (1ULL << 25)
289 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
290
291#define FCGI_EV_FSTRM_NEW (1ULL << 26)
292 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
293#define FCGI_EV_FSTRM_BLK (1ULL << 27)
294 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
295#define FCGI_EV_FSTRM_END (1ULL << 28)
296 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
297#define FCGI_EV_FSTRM_ERR (1ULL << 29)
298 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
299
300#define FCGI_EV_STRM_NEW (1ULL << 30)
301 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
302#define FCGI_EV_STRM_RECV (1ULL << 31)
303 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
304#define FCGI_EV_STRM_SEND (1ULL << 32)
305 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
306#define FCGI_EV_STRM_FULL (1ULL << 33)
307 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
308#define FCGI_EV_STRM_WAKE (1ULL << 34)
309 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
310#define FCGI_EV_STRM_SHUT (1ULL << 35)
311 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
312#define FCGI_EV_STRM_END (1ULL << 36)
313 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
314#define FCGI_EV_STRM_ERR (1ULL << 37)
315 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
316
317 { }
318};
319
320static const struct name_desc fcgi_trace_lockon_args[4] = {
321 /* arg1 */ { /* already used by the connection */ },
322 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
323 /* arg3 */ { },
324 /* arg4 */ { }
325};
326
327
328static const struct name_desc fcgi_trace_decoding[] = {
329#define FCGI_VERB_CLEAN 1
330 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
331#define FCGI_VERB_MINIMAL 2
332 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
333#define FCGI_VERB_SIMPLE 3
334 { .name="simple", .desc="add request/response status line or htx info when available" },
335#define FCGI_VERB_ADVANCED 4
336 { .name="advanced", .desc="add header fields or record decoding when available" },
337#define FCGI_VERB_COMPLETE 5
338 { .name="complete", .desc="add full data dump when available" },
339 { /* end */ }
340};
341
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200342static struct trace_source trace_fcgi __read_mostly = {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200343 .name = IST("fcgi"),
344 .desc = "FastCGI multiplexer",
345 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
346 .default_cb = fcgi_trace,
347 .known_events = fcgi_trace_events,
348 .lockon_args = fcgi_trace_lockon_args,
349 .decoding = fcgi_trace_decoding,
350 .report_events = ~0, // report everything by default
351};
352
353#define TRACE_SOURCE &trace_fcgi
354INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
355
Christopher Faulet99eff652019-08-11 23:11:30 +0200356/* FCGI connection and stream pools */
357DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
358DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
359
Willy Tarreau144f84a2021-03-02 16:09:26 +0100360struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200361static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau691d5032021-01-20 14:55:01 +0100362/* fcgi_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100363struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200364static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100365struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200366static 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 +0200367static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
368static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200369static void fcgi_strm_alert(struct fcgi_strm *fstrm);
370static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
371
372/* a dmumy management stream */
373static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
374 .cs = NULL,
375 .fconn = NULL,
376 .state = FCGI_SS_CLOSED,
377 .flags = FCGI_SF_NONE,
378 .id = 0,
379};
380
381/* and a dummy idle stream for use with any unknown stream */
382static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
383 .cs = NULL,
384 .fconn = NULL,
385 .state = FCGI_SS_IDLE,
386 .flags = FCGI_SF_NONE,
387 .id = 0,
388};
389
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200390/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
391static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
392{
393 switch (st) {
394 case FCGI_CS_INIT : return "INI";
395 case FCGI_CS_SETTINGS : return "STG";
396 case FCGI_CS_RECORD_H : return "RDH";
397 case FCGI_CS_RECORD_D : return "RDD";
398 case FCGI_CS_RECORD_P : return "RDP";
399 case FCGI_CS_CLOSED : return "CLO";
400 default : return "???";
401 }
402}
403
404/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
405static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
406{
407 switch (st) {
408 case FCGI_SS_IDLE : return "IDL";
409 case FCGI_SS_OPEN : return "OPN";
410 case FCGI_SS_HREM : return "RCL";
411 case FCGI_SS_HLOC : return "HCL";
412 case FCGI_SS_ERROR : return "ERR";
413 case FCGI_SS_CLOSED : return "CLO";
414 default : return "???";
415 }
416}
417
418
419/* the FCGI traces always expect that arg1, if non-null, is of type connection
420 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
421 * and that arg3, if non-null, is a htx for rx/tx headers.
422 */
423static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
424 const struct ist where, const struct ist func,
425 const void *a1, const void *a2, const void *a3, const void *a4)
426{
427 const struct connection *conn = a1;
428 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
429 const struct fcgi_strm *fstrm = a2;
430 const struct htx *htx = a3;
431 const size_t *val = a4;
432
433 if (!fconn)
434 fconn = (fstrm ? fstrm->fconn : NULL);
435
436 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
437 return;
438
439 /* Display the response state if fstrm is defined */
440 if (fstrm)
441 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
442
443 if (src->verbosity == FCGI_VERB_CLEAN)
444 return;
445
446 /* Display the value to the 4th argument (level > STATE) */
447 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100448 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200449
450 /* Display status-line if possible (verbosity > MINIMAL) */
451 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
452 const struct htx_blk *blk = htx_get_head_blk(htx);
453 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
454 enum htx_blk_type type = htx_get_blk_type(blk);
455
456 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
457 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
458 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
459 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
460 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
461 }
462
463 /* Display fconn info and, if defined, fstrm info */
464 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
465 if (fstrm)
466 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
467
468 if (!fstrm || fstrm->id <= 0)
469 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
470 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
471 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
472
473 if (src->verbosity == FCGI_VERB_MINIMAL)
474 return;
475
476 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
477 if (src->level > TRACE_LEVEL_USER) {
478 if (src->verbosity == FCGI_VERB_COMPLETE ||
479 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
480 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
481 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
482 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
483 if (src->verbosity == FCGI_VERB_COMPLETE ||
484 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
485 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
486 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
487
488 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
489 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
490 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
491 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
492 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
493 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
494 }
495
496 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
497 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
498 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
499 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
500 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
501 }
502
503 /* Display htx info if defined (level > USER) */
504 if (src->level > TRACE_LEVEL_USER && htx) {
505 int full = 0;
506
507 /* Full htx info (level > STATE && verbosity > SIMPLE) */
508 if (src->level > TRACE_LEVEL_STATE) {
509 if (src->verbosity == FCGI_VERB_COMPLETE)
510 full = 1;
511 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
512 full = 1;
513 }
514
515 chunk_memcat(&trace_buf, "\n\t", 2);
516 htx_dump(&trace_buf, htx, full);
517 }
518}
Christopher Faulet99eff652019-08-11 23:11:30 +0200519
520/*****************************************************/
521/* functions below are for dynamic buffer management */
522/*****************************************************/
523
524/* Indicates whether or not the we may call the fcgi_recv() function to attempt
525 * to receive data into the buffer and/or demux pending data. The condition is
526 * a bit complex due to some API limits for now. The rules are the following :
527 * - if an error or a shutdown was detected on the connection and the buffer
528 * is empty, we must not attempt to receive
529 * - if the demux buf failed to be allocated, we must not try to receive and
530 * we know there is nothing pending
531 * - if no flag indicates a blocking condition, we may attempt to receive,
532 * regardless of whether the demux buffer is full or not, so that only
533 * de demux part decides whether or not to block. This is needed because
534 * the connection API indeed prevents us from re-enabling receipt that is
535 * already enabled in a polled state, so we must always immediately stop
536 * as soon as the demux can't proceed so as never to hit an end of read
537 * with data pending in the buffers.
538 * - otherwise must may not attempt
539 */
540static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
541{
542 if (b_data(&fconn->dbuf) == 0 &&
543 (fconn->state == FCGI_CS_CLOSED ||
544 fconn->conn->flags & CO_FL_ERROR ||
545 conn_xprt_read0_pending(fconn->conn)))
546 return 0;
547
548 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
549 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
550 return 1;
551
552 return 0;
553}
554
555/* Restarts reading on the connection if it was not enabled */
556static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
557{
558 if (!fcgi_recv_allowed(fconn))
559 return;
560 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
561 (fconn->wait_event.events & SUB_RETRY_RECV))
562 return;
563 tasklet_wakeup(fconn->wait_event.tasklet);
564}
565
566
567/* Tries to grab a buffer and to re-enable processing on mux <target>. The
568 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
569 * the allocation succeeds, in which case the connection is woken up, or 0 if
570 * it's impossible to wake up and we prefer to be woken up later.
571 */
572static int fcgi_buf_available(void *target)
573{
574 struct fcgi_conn *fconn = target;
575 struct fcgi_strm *fstrm;
576
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100577 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200578 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 +0200579 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
580 fcgi_conn_restart_reading(fconn, 1);
581 return 1;
582 }
583
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100584 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200585 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 +0200586 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200587 if (fconn->flags & FCGI_CF_DEM_MROOM) {
588 fconn->flags &= ~FCGI_CF_DEM_MROOM;
589 fcgi_conn_restart_reading(fconn, 1);
590 }
591 return 1;
592 }
593
594 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
595 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100596 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200597 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 +0200598 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
599 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200600 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200601 return 1;
602 }
603
604 return 0;
605}
606
607static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
608{
609 struct buffer *buf = NULL;
610
Willy Tarreau2b718102021-04-21 07:32:39 +0200611 if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100612 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200613 fconn->buf_wait.target = fconn;
614 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200615 LIST_APPEND(&th_ctx->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200616 }
617 return buf;
618}
619
620static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
621{
622 if (bptr->size) {
623 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100624 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200625 }
626}
627
628static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
629{
630 struct buffer *buf;
631 unsigned int count = 0;
632
633 while (b_size(buf = br_head_pick(fconn->mbuf))) {
634 b_free(buf);
635 count++;
636 }
637 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100638 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200639}
640
641/* Returns the number of allocatable outgoing streams for the connection taking
642 * the number reserved streams into account.
643 */
644static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
645{
646 int ret;
647
648 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
649 if (ret < 0)
650 ret = 0;
651 return ret;
652}
653
654/* Returns the number of streams in use on a connection to figure if it's
655 * idle or not. We check nb_cs and not nb_streams as the caller will want
656 * to know if it was the last one after a detach().
657 */
658static int fcgi_used_streams(struct connection *conn)
659{
660 struct fcgi_conn *fconn = conn->ctx;
661
662 return fconn->nb_cs;
663}
664
665/* Returns the number of concurrent streams available on the connection */
666static int fcgi_avail_streams(struct connection *conn)
667{
668 struct server *srv = objt_server(conn->target);
669 struct fcgi_conn *fconn = conn->ctx;
670 int ret1, ret2;
671
672 /* Don't open new stream if the connection is closed */
673 if (fconn->state == FCGI_CS_CLOSED)
674 return 0;
675
676 /* May be negative if this setting has changed */
677 ret1 = (fconn->streams_limit - fconn->nb_streams);
678
679 /* we must also consider the limit imposed by stream IDs */
680 ret2 = fcgi_streams_left(fconn);
681 ret1 = MIN(ret1, ret2);
682 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
683 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
684 ret1 = MIN(ret1, ret2);
685 }
686 return ret1;
687}
688
689/*****************************************************************/
690/* functions below are dedicated to the mux setup and management */
691/*****************************************************************/
692
693/* Initializes the mux once it's attached. Only outgoing connections are
694 * supported. So the context is already initialized before installing the
695 * mux. <input> is always used as Input buffer and may contain data. It is the
696 * caller responsibility to not reuse it anymore. Returns < 0 on error.
697 */
698static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
699 struct buffer *input)
700{
701 struct fcgi_conn *fconn;
702 struct fcgi_strm *fstrm;
703 struct fcgi_app *app = get_px_fcgi_app(px);
704 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200705 void *conn_ctx = conn->ctx;
706
707 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200708
Christopher Faulet73518be2021-01-27 12:06:54 +0100709 if (!app) {
710 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 +0200711 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100712 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200713
714 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100715 if (!fconn) {
716 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200717 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100718 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200719
720 fconn->shut_timeout = fconn->timeout = px->timeout.server;
721 if (tick_isset(px->timeout.serverfin))
722 fconn->shut_timeout = px->timeout.serverfin;
723
724 fconn->flags = FCGI_CF_NONE;
725
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500726 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200727 if (app->flags & FCGI_APP_FL_KEEP_CONN)
728 fconn->flags |= FCGI_CF_KEEP_CONN;
729 if (app->flags & FCGI_APP_FL_GET_VALUES)
730 fconn->flags |= FCGI_CF_GET_VALUES;
731 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
732 fconn->flags |= FCGI_CF_MPXS_CONNS;
733
734 fconn->proxy = px;
735 fconn->app = app;
736 fconn->task = NULL;
737 if (tick_isset(fconn->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200738 t = task_new_here();
Christopher Faulet73518be2021-01-27 12:06:54 +0100739 if (!t) {
740 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 +0200741 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100742 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200743
744 fconn->task = t;
745 t->process = fcgi_timeout_task;
746 t->context = fconn;
747 t->expire = tick_add(now_ms, fconn->timeout);
748 }
749
750 fconn->wait_event.tasklet = tasklet_new();
751 if (!fconn->wait_event.tasklet)
752 goto fail;
753 fconn->wait_event.tasklet->process = fcgi_io_cb;
754 fconn->wait_event.tasklet->context = fconn;
755 fconn->wait_event.events = 0;
756
757 /* Initialise the context. */
758 fconn->state = FCGI_CS_INIT;
759 fconn->conn = conn;
760 fconn->streams_limit = app->maxreqs;
761 fconn->max_id = -1;
762 fconn->nb_streams = 0;
763 fconn->nb_cs = 0;
764 fconn->nb_reserved = 0;
765 fconn->stream_cnt = 0;
766
767 fconn->dbuf = *input;
768 fconn->dsi = -1;
769
770 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
771 fconn->streams_by_id = EB_ROOT;
772 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100773 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200774
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200775 conn->ctx = fconn;
776
Christopher Faulet99eff652019-08-11 23:11:30 +0200777 if (t)
778 task_queue(t);
779
780 /* FIXME: this is temporary, for outgoing connections we need to
781 * immediately allocate a stream until the code is modified so that the
782 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200783 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200784 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200785 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200786 if (!fstrm)
787 goto fail;
788
Christopher Faulet99eff652019-08-11 23:11:30 +0200789
790 /* Repare to read something */
791 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200792 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200793 return 0;
794
795 fail:
796 task_destroy(t);
797 if (fconn->wait_event.tasklet)
798 tasklet_free(fconn->wait_event.tasklet);
799 pool_free(pool_head_fcgi_conn, fconn);
800 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200801 conn->ctx = conn_ctx; // restore saved ctx
802 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200803 return -1;
804}
805
806/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
807 * -1 if no more is allocatable.
808 */
809static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
810{
811 int32_t id = (fconn->max_id + 1) | 1;
812
813 if ((id & 0x80000000U))
814 id = -1;
815 return id;
816}
817
818/* Returns the stream associated with id <id> or NULL if not found */
819static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
820{
821 struct eb32_node *node;
822
823 if (id == 0)
824 return (struct fcgi_strm *)fcgi_mgmt_stream;
825
826 if (id > fconn->max_id)
827 return (struct fcgi_strm *)fcgi_unknown_stream;
828
829 node = eb32_lookup(&fconn->streams_by_id, id);
830 if (!node)
831 return (struct fcgi_strm *)fcgi_unknown_stream;
832 return container_of(node, struct fcgi_strm, by_id);
833}
834
835
836/* Release function. This one should be called to free all resources allocated
837 * to the mux.
838 */
839static void fcgi_release(struct fcgi_conn *fconn)
840{
William Dauchy477757c2020-08-07 22:19:23 +0200841 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200842
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200843 TRACE_POINT(FCGI_EV_FCONN_END);
844
Christopher Faulet99eff652019-08-11 23:11:30 +0200845 if (fconn) {
846 /* The connection must be attached to this mux to be released */
847 if (fconn->conn && fconn->conn->ctx == fconn)
848 conn = fconn->conn;
849
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200850 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
851
Willy Tarreau2b718102021-04-21 07:32:39 +0200852 if (LIST_INLIST(&fconn->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +0100853 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200854
855 fcgi_release_buf(fconn, &fconn->dbuf);
856 fcgi_release_mbuf(fconn);
857
858 if (fconn->task) {
859 fconn->task->context = NULL;
860 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
861 fconn->task = NULL;
862 }
863 if (fconn->wait_event.tasklet)
864 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200865 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200866 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
867 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200868
869 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200870 }
871
872 if (conn) {
873 conn->mux = NULL;
874 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200875 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200876
877 conn_stop_tracking(conn);
878 conn_full_close(conn);
879 if (conn->destroy_cb)
880 conn->destroy_cb(conn);
881 conn_free(conn);
882 }
883}
884
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200885/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
886 * pending on the connection AND if there is no more data in the demux
887 * buffer. The function returns 1 to report a read0 or 0 otherwise.
888 */
889static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
890{
891 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
892 return 1;
893 return 0;
894}
895
Christopher Faulet99eff652019-08-11 23:11:30 +0200896
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500897/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200898static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
899{
900 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
901 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
902 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
903 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
904 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
905 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
906 conn_xprt_read0_pending(fconn->conn))))
907 return 1;
908 return 0;
909}
910
911
912/********************************************************/
913/* functions below are for the FCGI protocol processing */
914/********************************************************/
915
Christopher Faulet99eff652019-08-11 23:11:30 +0200916/* Marks an error on the stream. */
917static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
918{
919 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200920 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
921 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200922 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200923 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
924 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200925 if (fstrm->cs)
926 cs_set_error(fstrm->cs);
927 }
928}
929
930/* Attempts to notify the data layer of recv availability */
931static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
932{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100933 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200934 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100935 tasklet_wakeup(fstrm->subs->tasklet);
936 fstrm->subs->events &= ~SUB_RETRY_RECV;
937 if (!fstrm->subs->events)
938 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200939 }
940}
941
942/* Attempts to notify the data layer of send availability */
943static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
944{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100945 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200946 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100947 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100948 tasklet_wakeup(fstrm->subs->tasklet);
949 fstrm->subs->events &= ~SUB_RETRY_SEND;
950 if (!fstrm->subs->events)
951 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200952 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100953 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
954 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
955 tasklet_wakeup(fstrm->shut_tl);
956 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200957}
958
959/* Alerts the data layer, trying to wake it up by all means, following
960 * this sequence :
961 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
962 * for recv
963 * - if its subscribed to send, then it's woken up for send
964 * - if it was subscribed to neither, its ->wake() callback is called
965 * It is safe to call this function with a closed stream which doesn't have a
966 * conn_stream anymore.
967 */
968static void fcgi_strm_alert(struct fcgi_strm *fstrm)
969{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200970 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100971 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100972 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200973 fcgi_strm_notify_recv(fstrm);
974 fcgi_strm_notify_send(fstrm);
975 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200976 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
977 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200978 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200979 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200980}
981
982/* Writes the 16-bit record size <len> at address <record> */
983static inline void fcgi_set_record_size(void *record, uint16_t len)
984{
985 uint8_t *out = (record + 4);
986
987 *out = (len >> 8);
988 *(out + 1) = (len & 0xff);
989}
990
991/* Writes the 16-bit stream id <id> at address <record> */
992static inline void fcgi_set_record_id(void *record, uint16_t id)
993{
994 uint8_t *out = (record + 2);
995
996 *out = (id >> 8);
997 *(out + 1) = (id & 0xff);
998}
999
1000/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
1001 * its connection if the stream was not yet closed. Please use this exclusively
1002 * before closing a stream to ensure stream count is well maintained.
1003 */
1004static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
1005{
1006 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001007 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001008 fstrm->fconn->nb_streams--;
1009 if (!fstrm->id)
1010 fstrm->fconn->nb_reserved--;
1011 if (fstrm->cs) {
1012 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1013 fcgi_strm_notify_recv(fstrm);
1014 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001015 fstrm->state = FCGI_SS_CLOSED;
1016 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1017 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001018 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001019}
1020
1021/* Detaches a FCGI stream from its FCGI connection and releases it to the
1022 * fcgi_strm pool.
1023 */
1024static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1025{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001026 struct connection *conn = fstrm->fconn->conn;
1027
1028 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1029
Christopher Faulet99eff652019-08-11 23:11:30 +02001030 fcgi_strm_close(fstrm);
1031 eb32_delete(&fstrm->by_id);
1032 if (b_size(&fstrm->rxbuf)) {
1033 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001034 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001035 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001036 if (fstrm->subs)
1037 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001038 /* There's no need to explicitly call unsubscribe here, the only
1039 * reference left would be in the fconn send_list/fctl_list, and if
1040 * we're in it, we're getting out anyway
1041 */
1042 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001043 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001044 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001045
1046 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001047}
1048
1049/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1050 * stream tree. In case of error, nothing is added and NULL is returned. The
1051 * causes of errors can be any failed memory allocation. The caller is
1052 * responsible for checking if the connection may support an extra stream prior
1053 * to calling this function.
1054 */
1055static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1056{
1057 struct fcgi_strm *fstrm;
1058
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001059 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1060
Christopher Faulet99eff652019-08-11 23:11:30 +02001061 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +01001062 if (!fstrm) {
1063 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 +02001064 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +01001065 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001066
Willy Tarreau7aad7032020-01-16 17:20:57 +01001067 fstrm->shut_tl = tasklet_new();
1068 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001069 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 +02001070 pool_free(pool_head_fcgi_strm, fstrm);
1071 goto out;
1072 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001073 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001074 fstrm->shut_tl->process = fcgi_deferred_shut;
1075 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001076 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001077 fstrm->fconn = fconn;
1078 fstrm->cs = NULL;
1079 fstrm->flags = FCGI_SF_NONE;
1080 fstrm->proto_status = 0;
1081 fstrm->state = FCGI_SS_IDLE;
1082 fstrm->rxbuf = BUF_NULL;
1083
1084 h1m_init_res(&fstrm->h1m);
1085 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1086 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1087
1088 fstrm->by_id.key = fstrm->id = id;
1089 if (id > 0)
1090 fconn->max_id = id;
1091 else
1092 fconn->nb_reserved++;
1093
1094 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1095 fconn->nb_streams++;
1096 fconn->stream_cnt++;
1097
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001098 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001099 return fstrm;
1100
1101 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001102 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 +02001103 return NULL;
1104}
1105
1106/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1107 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1108 * highest possible stream ID was reached.
1109 */
1110static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1111 struct session *sess)
1112{
1113 struct fcgi_strm *fstrm = NULL;
1114
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1116 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001117 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 +02001118 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001119 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001120
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001121 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001122 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 +02001123 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001124 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001125
1126 /* Defer choosing the ID until we send the first message to create the stream */
1127 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001128 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001129 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 +02001130 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001131 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001132
1133 fstrm->cs = cs;
1134 fstrm->sess = sess;
1135 cs->ctx = fstrm;
1136 fconn->nb_cs++;
1137
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001138 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001139 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001140
1141 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001142 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 +02001143 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001144}
1145
1146/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1147 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1148 * automatically updated accordingly. If the stream is orphaned, it is
1149 * destroyed.
1150 */
1151static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1152{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001153 struct fcgi_conn *fconn = fstrm->fconn;
1154
1155 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1156
Christopher Faulet99eff652019-08-11 23:11:30 +02001157 if (!fstrm->cs) {
1158 /* this stream was already orphaned */
1159 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001161 return;
1162 }
1163
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001164 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001165 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001166 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001167 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001168 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001169 else if (fstrm->state == FCGI_SS_HLOC)
1170 fcgi_strm_close(fstrm);
1171 }
1172
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001173 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001174 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1175 if (fstrm->cs->flags & CS_FL_EOS)
1176 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001177
1178 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001179 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001180 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1181 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001182 }
1183
1184 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001185
1186 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001187}
1188
1189/* Wakes unassigned streams (ID == 0) attached to the connection. */
1190static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1191{
1192 struct eb32_node *node;
1193 struct fcgi_strm *fstrm;
1194
1195 node = eb32_lookup(&fconn->streams_by_id, 0);
1196 while (node) {
1197 fstrm = container_of(node, struct fcgi_strm, by_id);
1198 if (fstrm->id > 0)
1199 break;
1200 node = eb32_next(node);
1201 fcgi_strm_wake_one_stream(fstrm);
1202 }
1203}
1204
1205/* Wakes the streams attached to the connection, whose id is greater than <last>
1206 * or unassigned.
1207 */
1208static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1209{
1210 struct eb32_node *node;
1211 struct fcgi_strm *fstrm;
1212
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001213 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1214
Christopher Faulet99eff652019-08-11 23:11:30 +02001215 /* Wake all streams with ID > last */
1216 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1217 while (node) {
1218 fstrm = container_of(node, struct fcgi_strm, by_id);
1219 node = eb32_next(node);
1220 fcgi_strm_wake_one_stream(fstrm);
1221 }
1222 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001223
1224 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001225}
1226
1227static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1228 struct htx *htx, struct htx_sl *sl,
1229 struct fcgi_strm_params *params)
1230{
1231 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1232 struct ist p;
1233
1234 if (!sl)
1235 goto error;
1236
1237 if (!(params->mask & FCGI_SP_DOC_ROOT))
1238 params->docroot = fconn->app->docroot;
1239
1240 if (!(params->mask & FCGI_SP_REQ_METH)) {
1241 p = htx_sl_req_meth(sl);
1242 params->meth = ist2(b_tail(params->p), p.len);
1243 chunk_memcat(params->p, p.ptr, p.len);
1244 }
1245 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletfb38c912021-04-26 09:38:55 +02001246 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001247 params->uri = ist2(b_tail(params->p), p.len);
1248 chunk_memcat(params->p, p.ptr, p.len);
1249 }
1250 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1251 p = htx_sl_req_vsn(sl);
1252 params->vsn = ist2(b_tail(params->p), p.len);
1253 chunk_memcat(params->p, p.ptr, p.len);
1254 }
1255 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1256 char *end;
1257 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001258 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001259 port = get_host_port(cli_conn->dst);
1260 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1261 if (!end)
1262 goto error;
1263 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1264 params->p->data += params->srv_port.len;
1265 }
1266 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1267 /* If no Host header found, use the server address to fill
1268 * srv_name */
1269 if (!istlen(params->srv_name)) {
1270 char *ptr = NULL;
1271
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001272 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001273 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1274 ptr = b_tail(params->p);
1275 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001276 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001277 params->p->data += params->srv_name.len;
1278 }
1279 }
1280 }
1281 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1282 char *ptr = NULL;
1283
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001284 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001285 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1286 ptr = b_tail(params->p);
1287 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001288 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001289 params->p->data += params->rem_addr.len;
1290 }
1291 }
1292 if (!(params->mask & FCGI_SP_REM_PORT)) {
1293 char *end;
1294 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001295 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001296 port = get_host_port(cli_conn->src);
1297 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1298 if (!end)
1299 goto error;
1300 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1301 params->p->data += params->rem_port.len;
1302 }
1303 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1304 struct htx_blk *blk;
1305 enum htx_blk_type type;
1306 char *end;
1307 size_t len = 0;
1308
1309 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1310 type = htx_get_blk_type(blk);
1311
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001312 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001313 break;
1314 if (type == HTX_BLK_DATA)
1315 len += htx_get_blksz(blk);
1316 }
1317 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1318 if (!end)
1319 goto error;
1320 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1321 params->p->data += params->cont_len.len;
1322 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001323
Christopher Faulet99eff652019-08-11 23:11:30 +02001324 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001325 if (cli_conn)
Willy Tarreau1057bee2021-10-06 11:38:44 +02001326 params->https = conn_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001327 }
Willy Tarreaud2ae3852021-10-06 11:40:11 +02001328
Christopher Faulet99eff652019-08-11 23:11:30 +02001329 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1330 /* one of scriptname, pathinfo or query_string is no set */
Amaury Denoyellec453f952021-07-06 11:40:12 +02001331 struct http_uri_parser parser = http_uri_parser_init(params->uri);
1332 struct ist path = http_parse_path(&parser);
Christopher Faulet99eff652019-08-11 23:11:30 +02001333 int len;
1334
Christopher Faulet99eff652019-08-11 23:11:30 +02001335 /* No scrit_name set but no valid path ==> error */
1336 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1337 goto error;
1338
Christopher Faulet99eff652019-08-11 23:11:30 +02001339 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001340 if (!(params->mask & FCGI_SP_REQ_QS)) {
1341 struct ist qs = istfind(path, '?');
1342
1343 /* Update the path length */
1344 path.len -= qs.len;
1345
1346 /* Set the query-string skipping the '?', if any */
1347 if (istlen(qs))
1348 params->qs = istnext(qs);
1349 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001350
1351 /* If the script_name is set, don't try to deduce the path_info
1352 * too. The opposite is not true.
1353 */
1354 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1355 params->mask |= FCGI_SP_PATH_INFO;
1356 goto end;
1357 }
1358
Christopher Faulet0f17a442020-07-23 15:44:37 +02001359 /* Decode the path. it must first be copied to keep the URI
1360 * untouched.
1361 */
1362 chunk_memcat(params->p, path.ptr, path.len);
1363 path.ptr = b_tail(params->p) - path.len;
1364 len = url_decode(ist0(path), 0);
1365 if (len < 0)
1366 goto error;
1367 path.len = len;
1368
Christopher Faulet99eff652019-08-11 23:11:30 +02001369 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001370 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001371
1372 /* If there is no regex to match the pathinfo, just to the last
1373 * part and see if the index must be used.
1374 */
1375 if (!fconn->app->pathinfo_re)
1376 goto check_index;
1377
Christopher Faulet28cb3662020-02-14 14:47:37 +01001378 /* If some special characters are found in the decoded path (\n
Ilya Shipitsin01881082021-08-07 14:41:56 +05001379 * or \0), the PATH_INFO regex cannot match. This is theoretically
Christopher Faulet28cb3662020-02-14 14:47:37 +01001380 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001381 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001382 * case.
1383 */
1384 if (istchr(path, '\n') || istchr(path, '\0'))
1385 goto error;
1386
Christopher Faulet99eff652019-08-11 23:11:30 +02001387 /* The regex does not match, just to the last part and see if
1388 * the index must be used.
1389 */
1390 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1391 goto check_index;
1392
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001393 /* We must have at least 1 capture for the script name,
1394 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001395 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001396 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001397 goto check_index;
1398
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001399 /* Finally we can set the script_name and the path_info. The
1400 * path_info is set if not already defined, and if it was
1401 * captured
1402 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001403 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001404 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1405 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001406
1407 check_index:
1408 len = params->scriptname.len;
1409 /* the script_name if finished by a '/' so we can add the index
1410 * part, if any.
1411 */
1412 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1413 struct ist sn = params->scriptname;
1414
1415 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1416 chunk_memcat(params->p, sn.ptr, sn.len);
1417 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1418 }
1419 }
1420
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001421 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1422 params->srv_soft = ist2(b_tail(params->p), 0);
1423 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1424 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1425 }
1426
Christopher Faulet99eff652019-08-11 23:11:30 +02001427 end:
1428 return 1;
1429 error:
1430 return 0;
1431}
1432
1433static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1434 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1435{
1436 struct fcgi_param p;
1437
1438 if (params->mask & flag)
1439 return 1;
1440
1441 chunk_reset(&trash);
1442
1443 switch (flag) {
1444 case FCGI_SP_CGI_GATEWAY:
1445 p.n = ist("GATEWAY_INTERFACE");
1446 p.v = ist("CGI/1.1");
1447 goto encode;
1448 case FCGI_SP_DOC_ROOT:
1449 p.n = ist("DOCUMENT_ROOT");
1450 p.v = params->docroot;
1451 goto encode;
1452 case FCGI_SP_SCRIPT_NAME:
1453 p.n = ist("SCRIPT_NAME");
1454 p.v = params->scriptname;
1455 goto encode;
1456 case FCGI_SP_PATH_INFO:
1457 p.n = ist("PATH_INFO");
1458 p.v = params->pathinfo;
1459 goto encode;
1460 case FCGI_SP_REQ_URI:
1461 p.n = ist("REQUEST_URI");
1462 p.v = params->uri;
1463 goto encode;
1464 case FCGI_SP_REQ_METH:
1465 p.n = ist("REQUEST_METHOD");
1466 p.v = params->meth;
1467 goto encode;
1468 case FCGI_SP_REQ_QS:
1469 p.n = ist("QUERY_STRING");
1470 p.v = params->qs;
1471 goto encode;
1472 case FCGI_SP_SRV_NAME:
1473 p.n = ist("SERVER_NAME");
1474 p.v = params->srv_name;
1475 goto encode;
1476 case FCGI_SP_SRV_PORT:
1477 p.n = ist("SERVER_PORT");
1478 p.v = params->srv_port;
1479 goto encode;
1480 case FCGI_SP_SRV_PROTO:
1481 p.n = ist("SERVER_PROTOCOL");
1482 p.v = params->vsn;
1483 goto encode;
1484 case FCGI_SP_REM_ADDR:
1485 p.n = ist("REMOTE_ADDR");
1486 p.v = params->rem_addr;
1487 goto encode;
1488 case FCGI_SP_REM_PORT:
1489 p.n = ist("REMOTE_PORT");
1490 p.v = params->rem_port;
1491 goto encode;
1492 case FCGI_SP_SCRIPT_FILE:
1493 p.n = ist("SCRIPT_FILENAME");
1494 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1495 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1496 p.v = ist2(b_head(&trash), b_data(&trash));
1497 goto encode;
1498 case FCGI_SP_PATH_TRANS:
1499 if (!istlen(params->pathinfo))
1500 goto skip;
1501 p.n = ist("PATH_TRANSLATED");
1502 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1503 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1504 p.v = ist2(b_head(&trash), b_data(&trash));
1505 goto encode;
1506 case FCGI_SP_CONT_LEN:
1507 p.n = ist("CONTENT_LENGTH");
1508 p.v = params->cont_len;
1509 goto encode;
1510 case FCGI_SP_HTTPS:
1511 if (!params->https)
1512 goto skip;
1513 p.n = ist("HTTPS");
1514 p.v = ist("on");
1515 goto encode;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02001516 case FCGI_SP_SRV_SOFT:
1517 p.n = ist("SERVER_SOFTWARE");
1518 p.v = params->srv_soft;
1519 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001520 default:
1521 goto skip;
1522 }
1523
1524 encode:
1525 if (!istlen(p.v))
1526 goto skip;
1527 if (!fcgi_encode_param(outbuf, &p))
1528 return 0;
1529 skip:
1530 params->mask |= flag;
1531 return 1;
1532}
1533
1534/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1535 * anything. It is highly unexpected, but if the record is larger than a buffer
1536 * and cannot be encoded in one time, an error is triggered and the connection is
1537 * closed. GET_VALUES record cannot be split.
1538 */
1539static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1540{
1541 struct buffer outbuf;
1542 struct buffer *mbuf;
1543 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1544 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001545 int ret = 0;
1546
1547 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001548
1549 mbuf = br_tail(fconn->mbuf);
1550 retry:
1551 if (!fcgi_get_buf(fconn, mbuf)) {
1552 fconn->flags |= FCGI_CF_MUX_MALLOC;
1553 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001554 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1555 ret = 0;
1556 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001557 }
1558
1559 while (1) {
1560 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001561 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001562 break;
1563 realign_again:
1564 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1565 }
1566
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001567 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001568 goto full;
1569
1570 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1571 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001572 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1573 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001574
1575 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1576 * handled by HAProxy.
1577 */
1578 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1579 goto full;
1580
1581 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001582 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 +01001583 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001584 b_add(mbuf, outbuf.data);
1585 ret = 1;
1586
1587 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001588 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001589 return ret;
1590 full:
1591 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001592 if (!b_data(mbuf)) {
1593 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 +02001594 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001595 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001596
1597 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1598 goto retry;
1599 fconn->flags |= FCGI_CF_MUX_MFULL;
1600 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001601 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001602 ret = 0;
1603 goto end;
1604 fail:
1605 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001606 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1607 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1608 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001609}
1610
1611/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1612 * couldn't do anything. It is highly unexpected, but if the record is larger
1613 * than a buffer and cannot be decoded in one time, an error is triggered and
1614 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1615 */
1616static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1617{
1618 struct buffer inbuf;
1619 struct buffer *dbuf;
1620 size_t offset;
1621
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001622 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1623
Christopher Faulet99eff652019-08-11 23:11:30 +02001624 dbuf = &fconn->dbuf;
1625
1626 /* Record too large to be fully decoded */
1627 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1628 goto fail;
1629
1630 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001631 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1632 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001633 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001634 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001635
1636 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1637 /* Realign the dmux buffer if the record wraps. It is unexpected
1638 * at this stage because it should be the first record received
1639 * from the FCGI application.
1640 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01001641 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001642 }
1643
1644 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1645
1646 for (offset = 0; offset < b_data(&inbuf); ) {
1647 struct fcgi_param p;
1648 size_t ret;
1649
1650 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1651 if (!ret) {
1652 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001653 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 +02001654 goto fail;
1655 }
1656 offset += ret;
1657
1658 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001659 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001660 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 +02001661 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001662 }
1663 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001664 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 +02001665 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001666 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001667 }
1668 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1669 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001670 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 +02001671 }
1672 /*
1673 * Ignore all other params
1674 */
1675 }
1676
1677 /* Reset the number of concurrent streams supported if the FCGI
1678 * application does not support connection multiplexing
1679 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001680 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001681 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001682 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1683 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001684
1685 /* We must be sure to have read exactly the announced record length, no
1686 * more no less
1687 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001688 if (offset != fconn->drl) {
1689 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 +02001690 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001691 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001692
Willy Tarreau022e5e52020-09-10 09:33:15 +02001693 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 +02001694 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1695 fconn->drl = 0;
1696 fconn->drp = 0;
1697 fconn->state = FCGI_CS_RECORD_H;
1698 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001699 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1700 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001701 return 1;
1702 fail:
1703 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001704 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1705 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 +02001706 return 0;
1707}
1708
1709/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1710 * excluded, as the streams which already received the end-of-stream. It returns
1711 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1712 */
1713static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1714{
1715 struct eb32_node *node;
1716 struct fcgi_strm *fstrm;
1717
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001718 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1719
Christopher Faulet99eff652019-08-11 23:11:30 +02001720 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1721 while (node) {
1722 fstrm = container_of(node, struct fcgi_strm, by_id);
1723 node = eb32_next(node);
1724 if (fstrm->state != FCGI_SS_CLOSED &&
1725 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1726 !fcgi_strm_send_abort(fconn, fstrm))
1727 return 0;
1728 }
1729 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001730 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1731 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001732 return 1;
1733}
1734
1735/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1736 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1737 * space to proceed. It is small enough to be encoded in an empty buffer.
1738 */
1739static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1740{
1741 struct buffer outbuf;
1742 struct buffer *mbuf;
1743 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1744 int ret;
1745
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001746 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1747
Christopher Faulet99eff652019-08-11 23:11:30 +02001748 mbuf = br_tail(fconn->mbuf);
1749 retry:
1750 if (!fcgi_get_buf(fconn, mbuf)) {
1751 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001752 fstrm->flags |= FCGI_SF_BLK_MROOM;
1753 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1754 ret = 0;
1755 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001756 }
1757
1758 while (1) {
1759 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001760 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001761 break;
1762 realign_again:
1763 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1764 }
1765
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001766 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001767 goto full;
1768
1769 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1770 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001771 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001772 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001773 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001774
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001775 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1776 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001778 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001779 if (!fcgi_encode_begin_request(&outbuf, &rec))
1780 goto full;
1781
1782 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001783 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 +02001784 b_add(mbuf, outbuf.data);
1785 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1786 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001787 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001788 ret = 1;
1789
1790 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001791 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001792 return ret;
1793 full:
1794 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1795 goto retry;
1796 fconn->flags |= FCGI_CF_MUX_MFULL;
1797 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001798 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 +02001799 ret = 0;
1800 goto end;
1801}
1802
1803/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1804 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1805 * space to proceed. It is small enough to be encoded in an empty buffer.
1806 */
1807static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1808 enum fcgi_record_type rtype)
1809{
1810 struct buffer outbuf;
1811 struct buffer *mbuf;
1812 int ret;
1813
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001814 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001815 mbuf = br_tail(fconn->mbuf);
1816 retry:
1817 if (!fcgi_get_buf(fconn, mbuf)) {
1818 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001819 fstrm->flags |= FCGI_SF_BLK_MROOM;
1820 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1821 ret = 0;
1822 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001823 }
1824
1825 while (1) {
1826 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001827 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001828 break;
1829 realign_again:
1830 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1831 }
1832
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001833 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001834 goto full;
1835
1836 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1837 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001838 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001839 outbuf.area[1] = rtype;
1840 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001841 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001842
1843 /* commit the record */
1844 b_add(mbuf, outbuf.data);
1845 ret = 1;
1846
1847 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001848 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001849 return ret;
1850 full:
1851 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1852 goto retry;
1853 fconn->flags |= FCGI_CF_MUX_MFULL;
1854 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001855 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 +02001856 ret = 0;
1857 goto end;
1858}
1859
1860
1861/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1862 * marks the end of params.
1863 */
1864static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1865{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001866 int ret;
1867
1868 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1869 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001870 if (ret) {
1871 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001872 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 +01001873 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001874 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001875}
1876
1877/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1878 * marks the end of input. On success, all the request was successfully sent.
1879 */
1880static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1881{
1882 int ret;
1883
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001884 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001885 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001886 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001887 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001888 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 +02001889 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1890 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1891 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001892 return ret;
1893}
1894
1895/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1896 * stops the request processing.
1897 */
1898static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1899{
1900 int ret;
1901
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001902 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001903 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001904 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001905 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001906 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 +02001907 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1908 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1909 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001910 return ret;
1911}
1912
1913/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1914 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1915 * several records are sent. However, a K/V param cannot be split between 2
1916 * records.
1917 */
1918static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1919 struct htx *htx)
1920{
1921 struct buffer outbuf;
1922 struct buffer *mbuf;
1923 struct htx_blk *blk;
1924 struct htx_sl *sl = NULL;
1925 struct fcgi_strm_params params;
1926 size_t total = 0;
1927
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001928 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1929
Christopher Faulet99eff652019-08-11 23:11:30 +02001930 memset(&params, 0, sizeof(params));
1931 params.p = get_trash_chunk();
1932
1933 mbuf = br_tail(fconn->mbuf);
1934 retry:
1935 if (!fcgi_get_buf(fconn, mbuf)) {
1936 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001937 fstrm->flags |= FCGI_SF_BLK_MROOM;
1938 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1939 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001940 }
1941
1942 while (1) {
1943 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001944 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001945 break;
1946 realign_again:
1947 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1948 }
1949
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001950 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001951 goto full;
1952
1953 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1954 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001955 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001956 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001957 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001958
1959 blk = htx_get_head_blk(htx);
1960 while (blk) {
1961 enum htx_blk_type type;
1962 uint32_t size = htx_get_blksz(blk);
1963 struct fcgi_param p;
1964
1965 type = htx_get_blk_type(blk);
1966 switch (type) {
1967 case HTX_BLK_REQ_SL:
1968 sl = htx_get_blk_ptr(htx, blk);
1969 if (sl->info.req.meth == HTTP_METH_HEAD)
1970 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1971 if (sl->flags & HTX_SL_F_VER_11)
1972 fstrm->h1m.flags |= H1_MF_VER_11;
1973 break;
1974
1975 case HTX_BLK_HDR:
1976 p.n = htx_get_blk_name(htx, blk);
1977 p.v = htx_get_blk_value(htx, blk);
1978
1979 if (istmatch(p.n, ist(":fcgi-"))) {
1980 p.n.ptr += 6;
1981 p.n.len -= 6;
1982 if (isteq(p.n, ist("gateway_interface")))
1983 params.mask |= FCGI_SP_CGI_GATEWAY;
1984 else if (isteq(p.n, ist("document_root"))) {
1985 params.mask |= FCGI_SP_DOC_ROOT;
1986 params.docroot = p.v;
1987 }
1988 else if (isteq(p.n, ist("script_name"))) {
1989 params.mask |= FCGI_SP_SCRIPT_NAME;
1990 params.scriptname = p.v;
1991 }
1992 else if (isteq(p.n, ist("path_info"))) {
1993 params.mask |= FCGI_SP_PATH_INFO;
1994 params.pathinfo = p.v;
1995 }
1996 else if (isteq(p.n, ist("request_uri"))) {
1997 params.mask |= FCGI_SP_REQ_URI;
1998 params.uri = p.v;
1999 }
2000 else if (isteq(p.n, ist("request_meth")))
2001 params.mask |= FCGI_SP_REQ_METH;
2002 else if (isteq(p.n, ist("query_string")))
2003 params.mask |= FCGI_SP_REQ_QS;
2004 else if (isteq(p.n, ist("server_name")))
2005 params.mask |= FCGI_SP_SRV_NAME;
2006 else if (isteq(p.n, ist("server_port")))
2007 params.mask |= FCGI_SP_SRV_PORT;
2008 else if (isteq(p.n, ist("server_protocol")))
2009 params.mask |= FCGI_SP_SRV_PROTO;
2010 else if (isteq(p.n, ist("remote_addr")))
2011 params.mask |= FCGI_SP_REM_ADDR;
2012 else if (isteq(p.n, ist("remote_port")))
2013 params.mask |= FCGI_SP_REM_PORT;
2014 else if (isteq(p.n, ist("script_filename")))
2015 params.mask |= FCGI_SP_SCRIPT_FILE;
2016 else if (isteq(p.n, ist("path_translated")))
2017 params.mask |= FCGI_SP_PATH_TRANS;
2018 else if (isteq(p.n, ist("https")))
2019 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002020 else if (isteq(p.n, ist("server_software")))
2021 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02002022 }
2023 else if (isteq(p.n, ist("content-length"))) {
2024 p.n = ist("CONTENT_LENGTH");
2025 params.mask |= FCGI_SP_CONT_LEN;
2026 }
2027 else if (isteq(p.n, ist("content-type")))
2028 p.n = ist("CONTENT_TYPE");
2029 else {
2030 if (isteq(p.n, ist("host")))
2031 params.srv_name = p.v;
Christopher Fauletf56e8462021-09-28 10:56:36 +02002032 else if (isteq(p.n, ist("te"))) {
2033 /* "te" may only be sent with "trailers" if this value
2034 * is present, otherwise it must be deleted.
2035 */
2036 p.v = istist(p.v, ist("trailers"));
2037 if (!isttest(p.v) || (p.v.len > 8 && p.v.ptr[8] != ','))
2038 break;
2039 p.v = ist("trailers");
2040 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002041
Christopher Faulet67d58092019-10-02 10:51:38 +02002042 /* Skip header if same name is used to add the server name */
2043 if (fconn->proxy->server_id_hdr_name &&
2044 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2045 break;
2046
Christopher Faulet99eff652019-08-11 23:11:30 +02002047 memcpy(trash.area, "http_", 5);
2048 memcpy(trash.area+5, p.n.ptr, p.n.len);
2049 p.n = ist2(trash.area, p.n.len+5);
2050 }
2051
2052 if (!fcgi_encode_param(&outbuf, &p)) {
2053 if (b_space_wraps(mbuf))
2054 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002055 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002056 goto full;
2057 goto done;
2058 }
2059 break;
2060
2061 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002062 if (fconn->proxy->server_id_hdr_name) {
2063 struct server *srv = objt_server(fconn->conn->target);
2064
2065 if (!srv)
2066 goto done;
2067
2068 memcpy(trash.area, "http_", 5);
2069 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2070 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2071 p.v = ist(srv->id);
2072
2073 if (!fcgi_encode_param(&outbuf, &p)) {
2074 if (b_space_wraps(mbuf))
2075 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002076 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002077 goto full;
2078 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002079 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002080 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002081 goto done;
2082
2083 default:
2084 break;
2085 }
2086 total += size;
2087 blk = htx_remove_blk(htx, blk);
2088 }
2089
2090 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002091 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2092 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002093 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002094 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002095
2096 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2097 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2098 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2099 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2100 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2101 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2102 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2103 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2104 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2105 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2106 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2107 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2108 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2109 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2110 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet5cd0e522021-06-11 13:34:42 +02002111 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002112 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2113 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002114 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002115 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002116
2117 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002118 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});
2119 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002120 b_add(mbuf, outbuf.data);
2121
2122 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002123 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002124 return total;
2125 full:
2126 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2127 goto retry;
2128 fconn->flags |= FCGI_CF_MUX_MFULL;
2129 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002130 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 +02002131 if (total)
2132 goto error;
2133 goto end;
2134
2135 error:
2136 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002137 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 +02002138 fcgi_strm_error(fstrm);
2139 goto end;
2140}
2141
2142/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2143 * anything. STDIN records contain the request body.
2144 */
2145static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2146 struct htx *htx, size_t count, struct buffer *buf)
2147{
2148 struct buffer outbuf;
2149 struct buffer *mbuf;
2150 struct htx_blk *blk;
2151 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002152 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002153 size_t total = 0;
2154
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002155 extra_bytes = 0;
2156
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002157 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002158 if (!count)
2159 goto end;
2160
2161 mbuf = br_tail(fconn->mbuf);
2162 retry:
2163 if (!fcgi_get_buf(fconn, mbuf)) {
2164 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002165 fstrm->flags |= FCGI_SF_BLK_MROOM;
2166 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2167 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002168 }
2169
2170 /* Perform some optimizations to reduce the number of buffer copies.
2171 * First, if the mux's buffer is empty and the htx area contains exactly
2172 * one data block of the same size as the requested count, and this
2173 * count fits within the record size, then it's possible to simply swap
2174 * the caller's buffer with the mux's output buffer and adjust offsets
2175 * and length to match the entire DATA HTX block in the middle. In this
2176 * case we perform a true zero-copy operation from end-to-end. This is
2177 * the situation that happens all the time with large files. Second, if
2178 * this is not possible, but the mux's output buffer is empty, we still
2179 * have an opportunity to avoid the copy to the intermediary buffer, by
2180 * making the intermediary buffer's area point to the output buffer's
2181 * area. In this case we want to skip the HTX header to make sure that
2182 * copies remain aligned and that this operation remains possible all
2183 * the time. This goes for headers, data blocks and any data extracted
2184 * from the HTX blocks.
2185 */
2186 blk = htx_get_head_blk(htx);
2187 if (!blk)
2188 goto end;
2189 type = htx_get_blk_type(blk);
2190 size = htx_get_blksz(blk);
2191 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2192 void *old_area = mbuf->area;
2193
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002194 /* Last block of the message: Reserve the size for the empty stdin record */
2195 if (htx->flags & HTX_FL_EOM)
2196 extra_bytes = FCGI_RECORD_HEADER_SZ;
2197
Christopher Faulet99eff652019-08-11 23:11:30 +02002198 if (b_data(mbuf)) {
2199 /* Too bad there are data left there. We're willing to memcpy/memmove
2200 * up to 1/4 of the buffer, which means that it's OK to copy a large
2201 * record into a buffer containing few data if it needs to be realigned,
2202 * and that it's also OK to copy few data without realigning. Otherwise
2203 * we'll pretend the mbuf is full and wait for it to become empty.
2204 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002205 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002206 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002207 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002208 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002209 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002210 }
2211
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002212 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 +02002213 /* map a FCGI record to the HTX block so that we can put the
2214 * record header there.
2215 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 *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 +02002217 outbuf.area = b_head(mbuf);
2218
2219 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002220 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002221 fcgi_set_record_id(outbuf.area, fstrm->id);
2222 fcgi_set_record_size(outbuf.area, size);
2223
2224 /* and exchange with our old area */
2225 buf->area = old_area;
2226 buf->data = buf->head = 0;
2227 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002228
2229 htx = (struct htx *)buf->area;
2230 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002231 goto end;
2232 }
2233
2234 copy:
2235 while (1) {
2236 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002237 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002238 break;
2239 realign_again:
2240 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2241 }
2242
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002243 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002244 goto full;
2245
2246 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2247 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002248 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002249 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002250 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002251
2252 blk = htx_get_head_blk(htx);
2253 while (blk && count) {
2254 enum htx_blk_type type = htx_get_blk_type(blk);
2255 uint32_t size = htx_get_blksz(blk);
2256 struct ist v;
2257
2258 switch (type) {
2259 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002260 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 +02002261 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002262
2263 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2264 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2265
2266 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002267 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002268 extra_bytes = 0;
2269 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002270
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002271 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002272 /* It doesn't fit at once. If it at least fits once split and
2273 * the amount of data to move is low, let's defragment the
2274 * buffer now.
2275 */
2276 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002277 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002278 b_data(mbuf) <= MAX_DATA_REALIGN)
2279 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002280 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002281 }
2282 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002283 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002284 goto full;
2285 goto done;
2286 }
2287 if (v.len != size) {
2288 total += v.len;
2289 count -= v.len;
2290 htx_cut_data_blk(htx, blk, v.len);
2291 goto done;
2292 }
2293 break;
2294
Christopher Faulet99eff652019-08-11 23:11:30 +02002295 default:
2296 break;
2297 }
2298 total += size;
2299 count -= size;
2300 blk = htx_remove_blk(htx, blk);
2301 }
2302
2303 done:
2304 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002305 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});
2306 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002307 b_add(mbuf, outbuf.data);
2308
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002309 /* Send the empty stding here to finish the message */
2310 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2311 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2312 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2313 /* bytes already reserved for this record. It should not fail */
2314 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002315 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 +01002316 fcgi_strm_error(fstrm);
2317 }
2318 }
2319
Christopher Faulet99eff652019-08-11 23:11:30 +02002320 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002321 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002322 return total;
2323 full:
2324 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2325 goto retry;
2326 fconn->flags |= FCGI_CF_MUX_MFULL;
2327 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002328 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 +02002329 goto end;
2330}
2331
2332/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2333 * anything. STDOUT records contain the entire response. All the content is
2334 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2335 */
2336static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2337{
2338 struct buffer *dbuf;
2339 size_t ret;
2340 size_t max;
2341
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002342 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2343
Christopher Faulet99eff652019-08-11 23:11:30 +02002344 dbuf = &fconn->dbuf;
2345
2346 /* Only padding remains */
2347 if (fconn->state == FCGI_CS_RECORD_P)
2348 goto end_transfer;
2349
2350 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2351 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2352 buf_room_for_htx_data(dbuf))
2353 goto fail; // incomplete record
2354
2355 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2356 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002357 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2358 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002359 }
2360
2361 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2362 max = buf_room_for_htx_data(&fstrm->rxbuf);
2363 if (!b_data(&fstrm->rxbuf))
2364 fstrm->rxbuf.head = sizeof(struct htx);
2365 if (max > fconn->drl)
2366 max = fconn->drl;
2367
2368 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2369 if (!ret)
2370 goto fail;
2371 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002372 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2373 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 +02002374
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002375 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002376 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002377 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2378 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002379
2380 if (fconn->drl)
2381 goto fail;
2382
2383 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002384 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002385 fconn->drl += fconn->drp;
2386 fconn->drp = 0;
2387 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2388 b_del(&fconn->dbuf, ret);
2389 fconn->drl -= ret;
2390 if (fconn->drl)
2391 goto fail;
2392
2393 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002394 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2395 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002396 return 1;
2397 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002398 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 +02002399 return 0;
2400}
2401
2402
2403/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2404 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002405 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002406 */
2407static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2408{
2409 int ret;
2410
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002411 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2412
Christopher Faulet99eff652019-08-11 23:11:30 +02002413 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002414 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002415 fconn->drl += fconn->drp;
2416 fconn->drp = 0;
2417 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2418 b_del(&fconn->dbuf, ret);
2419 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 if (fconn->drl) {
2421 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 +02002422 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002423 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002424 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002425 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002426 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 +02002427 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);
2428 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002429 return 1;
2430}
2431
2432/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2433 * anything.
2434 */
2435static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2436{
2437 struct buffer *dbuf;
2438 struct buffer tag;
2439 size_t ret;
2440
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002441 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002442 dbuf = &fconn->dbuf;
2443
2444 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002445 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002446 goto end_transfer;
2447
2448 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2449 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2450 buf_room_for_htx_data(dbuf))
2451 goto fail; // incomplete record
2452
2453 chunk_reset(&trash);
2454 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2455 if (!ret)
2456 goto fail;
2457 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002458 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 +02002459
2460 trash.area[ret] = '\n';
2461 trash.area[ret+1] = '\0';
2462 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002463 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002464
2465 if (fconn->drl)
2466 goto fail;
2467
2468 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002469 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002470 fconn->drl += fconn->drp;
2471 fconn->drp = 0;
2472 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2473 b_del(&fconn->dbuf, ret);
2474 fconn->drl -= ret;
2475 if (fconn->drl)
2476 goto fail;
2477 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002478 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2479 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002480 return 1;
2481 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002482 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 +02002483 return 0;
2484}
2485
2486/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2487 * anything. If the empty STDOUT record is not already received, this one marks
2488 * the end of the response. It is highly unexpected, but if the record is larger
2489 * than a buffer and cannot be decoded in one time, an error is triggered and
2490 * the connection is closed. END_REQUEST record cannot be split.
2491 */
2492static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2493{
2494 struct buffer inbuf;
2495 struct buffer *dbuf;
2496 struct fcgi_end_request endreq;
2497
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002498 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002499 dbuf = &fconn->dbuf;
2500
2501 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002502 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2503 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 +02002504 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002505 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002506
2507 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002508 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2509 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002510 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002511 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002512
2513 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2514 /* Realign the dmux buffer if the record wraps. It is unexpected
2515 * at this stage because it should be the first record received
2516 * from the FCGI application.
2517 */
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002518 b_slow_realign_ofs(dbuf, trash.area, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002519 }
2520
2521 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2522
Christopher Faulet73518be2021-01-27 12:06:54 +01002523 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2524 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 +02002525 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002526 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002527
2528 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002529 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 +02002530 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 +02002531 fstrm->proto_status = endreq.errcode;
2532 fcgi_strm_close(fstrm);
2533
2534 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2535 fconn->drl = 0;
2536 fconn->drp = 0;
2537 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002538 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2539 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002540 return 1;
2541
2542 fail:
2543 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002544 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 +02002545 return 0;
2546}
2547
2548/* process Rx records to be demultiplexed */
2549static void fcgi_process_demux(struct fcgi_conn *fconn)
2550{
2551 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2552 struct fcgi_header hdr;
2553 int ret;
2554
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002555 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2556
Christopher Faulet99eff652019-08-11 23:11:30 +02002557 if (fconn->state == FCGI_CS_CLOSED)
2558 return;
2559
2560 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002561 if (fconn->state == FCGI_CS_INIT) {
2562 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2563 return;
2564 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002565 if (fconn->state == FCGI_CS_SETTINGS) {
2566 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002567 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002568 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002569 if (!ret) {
2570 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 +02002571 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002572 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002573 b_del(&fconn->dbuf, ret);
2574
2575 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2576 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002577 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 +02002578 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 +02002579 goto fail;
2580 }
2581 goto new_record;
2582 }
2583 }
2584
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002585 /* process as many incoming records as possible below */
2586 while (1) {
2587 if (!b_data(&fconn->dbuf)) {
2588 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2589 break;
2590 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002591
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002592 if (fconn->state == FCGI_CS_CLOSED) {
2593 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002594 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002595 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002596
2597 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002598 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002599 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2600 if (!ret)
2601 break;
2602 b_del(&fconn->dbuf, ret);
2603
2604 new_record:
2605 fconn->dsi = hdr.id;
2606 fconn->drt = hdr.type;
2607 fconn->drl = hdr.len;
2608 fconn->drp = hdr.padding;
2609 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002610 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 +02002611 }
2612
2613 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2614 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2615
2616 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2617 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002618 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002619 fstrm->state == FCGI_SS_CLOSED ||
2620 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2621 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2622 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002623 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 +02002624 fstrm->cs->flags |= CS_FL_RCV_MORE;
2625 fcgi_strm_notify_recv(fstrm);
2626 }
2627 fstrm = tmp_fstrm;
2628
2629 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2630 /* ignore all record for closed streams */
2631 goto ignore_record;
2632 }
2633 if (fstrm->state == FCGI_SS_IDLE) {
2634 /* ignore all record for unknown streams */
2635 goto ignore_record;
2636 }
2637
2638 switch (fconn->drt) {
2639 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002640 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 +02002641 ret = fcgi_conn_handle_values_result(fconn);
2642 break;
2643
2644 case FCGI_STDOUT:
2645 if (fstrm->flags & FCGI_SF_ES_RCVD)
2646 goto ignore_record;
2647
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002648 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002649 if (fconn->drl)
2650 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2651 else
2652 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2653 break;
2654
2655 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002656 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002657 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2658 break;
2659
2660 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002661 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 +02002662 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2663 break;
2664
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002665 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002666 default:
2667 ignore_record:
2668 /* drop records that we ignore. They may be
2669 * larger than the buffer so we drain all of
2670 * their contents until we reach the end.
2671 */
2672 fconn->state = FCGI_CS_RECORD_P;
2673 fconn->drl += fconn->drp;
2674 fconn->drp = 0;
2675 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002676 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 b_del(&fconn->dbuf, ret);
2679 fconn->drl -= ret;
2680 ret = (fconn->drl == 0);
2681 }
2682
2683 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002684 if (ret <= 0) {
2685 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002686 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002687 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002688
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002689 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002691 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2692 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002693 }
2694
2695 fail:
2696 /* we can go here on missing data, blocked response or error */
2697 if (fstrm && fstrm->cs &&
2698 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002699 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002700 fstrm->state == FCGI_SS_CLOSED ||
2701 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2702 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2703 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002704 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 +02002705 fstrm->cs->flags |= CS_FL_RCV_MORE;
2706 fcgi_strm_notify_recv(fstrm);
2707 }
2708
2709 fcgi_conn_restart_reading(fconn, 0);
2710}
2711
2712/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2713 * the end.
2714 */
2715static int fcgi_process_mux(struct fcgi_conn *fconn)
2716{
2717 struct fcgi_strm *fstrm, *fstrm_back;
2718
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002719 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2720
Christopher Faulet99eff652019-08-11 23:11:30 +02002721 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2722 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2723 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2724 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002725 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 +02002726 fcgi_wake_unassigned_streams(fconn);
2727 goto mux;
2728 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002729 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002730 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2731 goto fail;
2732 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002733 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 +02002734 }
2735 /* need to wait for the other side */
2736 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002737 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002738 }
2739
2740 mux:
2741 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2742 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2743 break;
2744
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002745 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002746 continue;
2747
Willy Tarreau7aad7032020-01-16 17:20:57 +01002748 /* If the sender changed his mind and unsubscribed, let's just
2749 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002750 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002751 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2752 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002753 LIST_DEL_INIT(&fstrm->send_list);
2754 continue;
2755 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002756
2757 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002758 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2759 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002760 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002761 tasklet_wakeup(fstrm->subs->tasklet);
2762 fstrm->subs->events &= ~SUB_RETRY_SEND;
2763 if (!fstrm->subs->events)
2764 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002765 } else {
2766 /* it's the shut request that was queued */
2767 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2768 tasklet_wakeup(fstrm->shut_tl);
2769 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002770 }
2771
2772 fail:
2773 if (fconn->state == FCGI_CS_CLOSED) {
2774 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2775 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002776 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2777 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002778 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002779 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002780 }
2781 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002782
2783 done:
2784 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002785 return 1;
2786}
2787
2788
2789/* Attempt to read data, and subscribe if none available.
2790 * The function returns 1 if data has been received, otherwise zero.
2791 */
2792static int fcgi_recv(struct fcgi_conn *fconn)
2793{
2794 struct connection *conn = fconn->conn;
2795 struct buffer *buf;
2796 int max;
2797 size_t ret;
2798
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002799 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2800
2801 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2802 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002803 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002804 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002805
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002806 if (!fcgi_recv_allowed(fconn)) {
2807 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002808 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002809 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002810
2811 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2812 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002813 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002814 fconn->flags |= FCGI_CF_DEM_DALLOC;
2815 return 0;
2816 }
2817
Christopher Faulet99eff652019-08-11 23:11:30 +02002818 if (!b_data(buf)) {
2819 /* try to pre-align the buffer like the
2820 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002821 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002822 * HTX block to alias it upon recv. We cannot use the
2823 * head because rcv_buf() will realign the buffer if
2824 * it's empty. Thus we cheat and pretend we already
2825 * have a few bytes there.
2826 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002827 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2828 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002829 }
2830 else
2831 max = buf_room_for_htx_data(buf);
2832
2833 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2834
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002835 if (max && !ret && fcgi_recv_allowed(fconn)) {
2836 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002837 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002838 }
2839 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002840 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002841
2842 if (!b_data(buf)) {
2843 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002844 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002845 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2846 }
2847
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002848 if (ret == max) {
2849 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002850 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002851 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002852
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002853 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002854 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2855}
2856
2857
2858/* Try to send data if possible.
2859 * The function returns 1 if data have been sent, otherwise zero.
2860 */
2861static int fcgi_send(struct fcgi_conn *fconn)
2862{
2863 struct connection *conn = fconn->conn;
2864 int done;
2865 int sent = 0;
2866
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002867 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2868
2869 if (conn->flags & CO_FL_ERROR) {
2870 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002871 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002872 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002873
2874
Willy Tarreau911db9b2020-01-23 16:27:54 +01002875 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002876 /* a handshake was requested */
2877 goto schedule;
2878 }
2879
2880 /* This loop is quite simple : it tries to fill as much as it can from
2881 * pending streams into the existing buffer until it's reportedly full
2882 * or the end of send requests is reached. Then it tries to send this
2883 * buffer's contents out, marks it not full if at least one byte could
2884 * be sent, and tries again.
2885 *
2886 * The snd_buf() function normally takes a "flags" argument which may
2887 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2888 * data immediately comes and CO_SFL_STREAMER to indicate that the
2889 * connection is streaming lots of data (used to increase TLS record
2890 * size at the expense of latency). The former can be sent any time
2891 * there's a buffer full flag, as it indicates at least one stream
2892 * attempted to send and failed so there are pending data. An
2893 * alternative would be to set it as long as there's an active stream
2894 * but that would be problematic for ACKs until we have an absolute
2895 * guarantee that all waiters have at least one byte to send. The
2896 * latter should possibly not be set for now.
2897 */
2898
2899 done = 0;
2900 while (!done) {
2901 unsigned int flags = 0;
2902 unsigned int released = 0;
2903 struct buffer *buf;
2904
2905 /* fill as much as we can into the current buffer */
2906 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2907 done = fcgi_process_mux(fconn);
2908
2909 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2910 done = 1; // we won't go further without extra buffers
2911
2912 if (conn->flags & CO_FL_ERROR)
2913 break;
2914
2915 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2916 flags |= CO_SFL_MSG_MORE;
2917
2918 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2919 if (b_data(buf)) {
2920 int ret;
2921
2922 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2923 if (!ret) {
2924 done = 1;
2925 break;
2926 }
2927 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002928 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002929 b_del(buf, ret);
2930 if (b_data(buf)) {
2931 done = 1;
2932 break;
2933 }
2934 }
2935 b_free(buf);
2936 released++;
2937 }
2938
2939 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002940 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002941
2942 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002943 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2944 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002945 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2946 }
2947
2948 if (conn->flags & CO_FL_SOCK_WR_SH) {
2949 /* output closed, nothing to send, clear the buffer to release it */
2950 b_reset(br_tail(fconn->mbuf));
2951 }
2952 /* We're not full anymore, so we can wake any task that are waiting
2953 * for us.
2954 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002955 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002956 struct fcgi_strm *fstrm;
2957
2958 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2959 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2960 break;
2961
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002962 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002963 continue;
2964
Willy Tarreau7aad7032020-01-16 17:20:57 +01002965 /* If the sender changed his mind and unsubscribed, let's just
2966 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002967 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002968 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2969 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002970 LIST_DEL_INIT(&fstrm->send_list);
2971 continue;
2972 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002973
2974 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002975 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002976 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002977 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002978 tasklet_wakeup(fstrm->subs->tasklet);
2979 fstrm->subs->events &= ~SUB_RETRY_SEND;
2980 if (!fstrm->subs->events)
2981 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002982 } else {
2983 /* it's the shut request that was queued */
2984 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2985 tasklet_wakeup(fstrm->shut_tl);
2986 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002987 }
2988 }
2989 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002990 if (!br_data(fconn->mbuf)) {
2991 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002992 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002993 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002994schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002995 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2996 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002997 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002998 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002999
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003000 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003001 return sent;
3002}
3003
3004/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003005struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003006{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003007 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003008 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003009 struct tasklet *tl = (struct tasklet *)t;
3010 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02003011 int ret = 0;
3012
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003013 if (state & TASK_F_USR1) {
3014 /* the tasklet was idling on an idle connection, it might have
3015 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003016 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003017 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3018 if (tl->context == NULL) {
3019 /* The connection has been taken over by another thread,
3020 * we're no longer responsible for it, so just free the
3021 * tasklet, and do nothing.
3022 */
3023 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3024 tasklet_free(tl);
3025 return NULL;
3026 }
3027 conn = fconn->conn;
3028 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003029
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003030 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3031 if (conn_in_list)
3032 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003033
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003034 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3035 } else {
3036 /* we're certain the connection was not in an idle list */
3037 conn = fconn->conn;
3038 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
3039 conn_in_list = 0;
3040 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003041
Christopher Faulet99eff652019-08-11 23:11:30 +02003042 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3043 ret = fcgi_send(fconn);
3044 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3045 ret |= fcgi_recv(fconn);
3046 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003047 ret = fcgi_process(fconn);
3048
3049 /* If we were in an idle list, we want to add it back into it,
3050 * unless fcgi_process() returned -1, which mean it has destroyed
3051 * the connection (testing !ret is enough, if fcgi_process() wasn't
3052 * called then ret will be 0 anyway.
3053 */
Willy Tarreau74163142021-03-13 11:30:19 +01003054 if (ret < 0)
3055 t = NULL;
3056
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003057 if (!ret && conn_in_list) {
3058 struct server *srv = objt_server(conn->target);
3059
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003060 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003061 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003062 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003063 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003064 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003065 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003066 }
Willy Tarreau74163142021-03-13 11:30:19 +01003067 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02003068}
3069
3070/* callback called on any event by the connection handler.
3071 * It applies changes and returns zero, or < 0 if it wants immediate
3072 * destruction of the connection (which normally doesn not happen in FCGI).
3073 */
3074static int fcgi_process(struct fcgi_conn *fconn)
3075{
3076 struct connection *conn = fconn->conn;
3077
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003078 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3079
Christopher Faulet99eff652019-08-11 23:11:30 +02003080 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3081 fcgi_process_demux(fconn);
3082
3083 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3084 b_reset(&fconn->dbuf);
3085
3086 if (buf_room_for_htx_data(&fconn->dbuf))
3087 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3088 }
3089 fcgi_send(fconn);
3090
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003091 if (unlikely(fconn->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003092 /* frontend is stopping, reload likely in progress, let's try
3093 * to announce a graceful shutdown if not yet done. We don't
3094 * care if it fails, it will be tried again later.
3095 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003096 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 +02003097 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3098 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3099 fcgi_conn_send_aborts(fconn);
3100 }
3101 }
3102
3103 /*
3104 * If we received early data, and the handshake is done, wake
3105 * any stream that was waiting for it.
3106 */
3107 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003108 (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 +02003109 struct eb32_node *node;
3110 struct fcgi_strm *fstrm;
3111
3112 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3113 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3114
3115 while (node) {
3116 fstrm = container_of(node, struct fcgi_strm, by_id);
3117 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3118 fcgi_strm_notify_recv(fstrm);
3119 node = eb32_next(node);
3120 }
3121 }
3122
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003123 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003124 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3125 eb_is_empty(&fconn->streams_by_id)) {
3126 fcgi_wake_some_streams(fconn, 0);
3127
3128 if (eb_is_empty(&fconn->streams_by_id)) {
3129 /* no more stream, kill the connection now */
3130 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003131 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003132 return -1;
3133 }
3134 }
3135
3136 if (!b_data(&fconn->dbuf))
3137 fcgi_release_buf(fconn, &fconn->dbuf);
3138
3139 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3140 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3141 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3142 fcgi_release_mbuf(fconn);
3143
3144 if (fconn->task) {
3145 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3146 task_queue(fconn->task);
3147 }
3148
3149 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003150 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003151 return 0;
3152}
3153
3154
3155/* wake-up function called by the connection layer (mux_ops.wake) */
3156static int fcgi_wake(struct connection *conn)
3157{
3158 struct fcgi_conn *fconn = conn->ctx;
3159
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003160 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003161 return (fcgi_process(fconn));
3162}
3163
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003164
3165static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3166{
3167 int ret = 0;
3168 switch (mux_ctl) {
3169 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003170 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003171 ret |= MUX_STATUS_READY;
3172 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003173 case MUX_EXIT_STATUS:
3174 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003175 default:
3176 return -1;
3177 }
3178}
3179
Christopher Faulet99eff652019-08-11 23:11:30 +02003180/* Connection timeout management. The principle is that if there's no receipt
3181 * nor sending for a certain amount of time, the connection is closed. If the
3182 * MUX buffer still has lying data or is not allocatable, the connection is
3183 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003184 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003185 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003186struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003187{
3188 struct fcgi_conn *fconn = context;
3189 int expired = tick_is_expired(t->expire, now_ms);
3190
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003191 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3192
Willy Tarreau60814ff2020-06-30 11:19:23 +02003193 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003194 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003195
3196 /* Somebody already stole the connection from us, so we should not
3197 * free it, we just have to free the task.
3198 */
3199 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003200 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003201 fconn = NULL;
3202 goto do_leave;
3203 }
3204
Willy Tarreau60814ff2020-06-30 11:19:23 +02003205 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003206 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003207 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3208 return t;
3209 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003210
Willy Tarreau60814ff2020-06-30 11:19:23 +02003211 /* We're about to destroy the connection, so make sure nobody attempts
3212 * to steal it from us.
3213 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003214 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003215 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003216
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003217 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003218 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003219
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003220do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003221 task_destroy(t);
3222
3223 if (!fconn) {
3224 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003225 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003226 return NULL;
3227 }
3228
3229 fconn->task = NULL;
3230 fconn->state = FCGI_CS_CLOSED;
3231 fcgi_wake_some_streams(fconn, 0);
3232
3233 if (br_data(fconn->mbuf)) {
3234 /* don't even try to send aborts, the buffer is stuck */
3235 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3236 goto end;
3237 }
3238
3239 /* try to send but no need to insist */
3240 if (!fcgi_conn_send_aborts(fconn))
3241 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3242
3243 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3244 conn_xprt_ready(fconn->conn)) {
3245 unsigned int released = 0;
3246 struct buffer *buf;
3247
3248 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3249 if (b_data(buf)) {
3250 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3251 buf, b_data(buf), 0);
3252 if (!ret)
3253 break;
3254 b_del(buf, ret);
3255 if (b_data(buf))
3256 break;
3257 b_free(buf);
3258 released++;
3259 }
3260 }
3261
3262 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003263 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003264 }
3265
3266 end:
3267 /* either we can release everything now or it will be done later once
3268 * the last stream closes.
3269 */
3270 if (eb_is_empty(&fconn->streams_by_id))
3271 fcgi_release(fconn);
3272
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003273 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003274 return NULL;
3275}
3276
3277
3278/*******************************************/
3279/* functions below are used by the streams */
3280/*******************************************/
3281
3282/* Append the description of what is present in error snapshot <es> into <out>.
3283 * The description must be small enough to always fit in a buffer. The output
3284 * buffer may be the trash so the trash must not be used inside this function.
3285 */
3286static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3287{
3288 chunk_appendf(out,
3289 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3290 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3291 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3292 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3293 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3294 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3295}
3296/*
3297 * Capture a bad response and archive it in the proxy's structure. By default
3298 * it tries to report the error position as h1m->err_pos. However if this one is
3299 * not set, it will then report h1m->next, which is the last known parsing
3300 * point. The function is able to deal with wrapping buffers. It always displays
3301 * buffers as a contiguous area starting at buf->p. The direction is determined
3302 * thanks to the h1m's flags.
3303 */
3304static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3305 struct h1m *h1m, struct buffer *buf)
3306{
3307 struct session *sess = fstrm->sess;
3308 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003309 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 union error_snapshot_ctx ctx;
3311
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003312 if (fstrm->cs && fstrm->cs->data) {
3313 if (sess == NULL)
3314 sess = si_strm(fstrm->cs->data)->sess;
3315 if (!(h1m->flags & H1_MF_RESP))
3316 other_end = si_strm(fstrm->cs->data)->be;
3317 else
3318 other_end = sess->fe;
3319 } else
3320 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003321 /* http-specific part now */
3322 ctx.h1.state = h1m->state;
3323 ctx.h1.c_flags = fconn->flags;
3324 ctx.h1.s_flags = fstrm->flags;
3325 ctx.h1.m_flags = h1m->flags;
3326 ctx.h1.m_clen = h1m->curr_len;
3327 ctx.h1.m_blen = h1m->body_len;
3328
3329 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3330 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3331 &ctx, fcgi_show_error_snapshot);
3332}
3333
3334static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3335 struct buffer *buf, size_t *ofs, size_t max)
3336{
Christopher Fauletde471a42021-02-01 16:37:28 +01003337 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003338
Willy Tarreau022e5e52020-09-10 09:33:15 +02003339 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 +02003340 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3341 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003342 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 +02003343 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003344 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 +02003345 fcgi_strm_error(fstrm);
3346 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3347 }
3348 goto end;
3349 }
3350
Christopher Fauletda3adeb2021-09-28 09:50:07 +02003351 /* Reject any message with an unknown transfer-encoding. In fact if any
3352 * encoding other than "chunked". A 422-Unprocessable-Content is
3353 * returned for an invalid request, a 502-Bad-Gateway for an invalid
3354 * response.
3355 */
3356 if (h1m->flags & H1_MF_TE_OTHER) {
3357 htx->flags |= HTX_FL_PARSING_ERROR;
3358 TRACE_ERROR("Unknown transfer-encoding", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3359 fcgi_strm_error(fstrm);
3360 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3361 ret = 0;
3362 goto end;
3363 }
3364
Christopher Faulet99eff652019-08-11 23:11:30 +02003365 *ofs += ret;
3366 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003367 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 +02003368 return ret;
3369
3370}
3371
Christopher Fauletaf542632019-10-01 21:52:49 +02003372static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003373 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3374{
Christopher Fauletde471a42021-02-01 16:37:28 +01003375 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003376
Willy Tarreau022e5e52020-09-10 09:33:15 +02003377 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 +02003378 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003379 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003380 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 +02003381 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003382 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 +02003383 fcgi_strm_error(fstrm);
3384 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3385 }
3386 goto end;
3387 }
3388 *ofs += ret;
3389 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003390 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 +02003391 return ret;
3392}
3393
3394static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3395 struct buffer *buf, size_t *ofs, size_t max)
3396{
Christopher Fauletde471a42021-02-01 16:37:28 +01003397 size_t ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003398
Willy Tarreau022e5e52020-09-10 09:33:15 +02003399 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 +02003400 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003401 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003402 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 +02003403 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003404 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 +02003405 fcgi_strm_error(fstrm);
3406 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3407 }
3408 goto end;
3409 }
3410 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003411 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003412 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 +02003413 return ret;
3414}
3415
Christopher Faulet99eff652019-08-11 23:11:30 +02003416static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3417{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003418 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003419 struct htx *htx;
3420 struct h1m *h1m = &fstrm->h1m;
3421 size_t ret, data, total = 0;
3422
3423 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003424 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3425
Christopher Faulet99eff652019-08-11 23:11:30 +02003426 data = htx->data;
3427 if (fstrm->state == FCGI_SS_ERROR)
3428 goto end;
3429
3430 do {
3431 size_t used = htx_used_space(htx);
3432
3433 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003434 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003435 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3436 if (!ret)
3437 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003438
3439 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3440
Christopher Faulet99eff652019-08-11 23:11:30 +02003441 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3442 struct htx_blk *blk = htx_get_head_blk(htx);
3443 struct htx_sl *sl;
3444
3445 if (!blk)
3446 break;
3447 sl = htx_get_blk_ptr(htx, blk);
3448 sl->flags |= HTX_SL_F_XFER_LEN;
3449 htx->extra = 0;
3450 }
3451 }
3452 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003453 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003454 fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003455
3456 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3457 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3458 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003459 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003460 h1m->state = H1_MSG_DONE;
3461 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3462 }
3463
Christopher Faulet16a524c2021-02-02 21:16:03 +01003464 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet99eff652019-08-11 23:11:30 +02003465 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003466
3467 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 +02003468 }
3469 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003470 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Fauletbf774302021-06-02 12:04:40 +02003471 fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01003472 if (h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003473 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003474
Christopher Faulet76014fd2019-12-10 11:47:22 +01003475 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 +02003476 }
3477 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003478 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 +02003479 if (b_data(&fstrm->rxbuf) > total) {
3480 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003481 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003482 fcgi_strm_error(fstrm);
3483 }
3484 break;
3485 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003486 else {
3487 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003488 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 fcgi_strm_error(fstrm);
3490 break;
3491 }
3492
3493 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003494 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003495
3496 if (fstrm->state == FCGI_SS_ERROR) {
3497 b_reset(&fstrm->rxbuf);
3498 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003499 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003500 return 0;
3501 }
3502
3503 b_del(&fstrm->rxbuf, total);
3504
3505 end:
3506 htx_to_buf(htx, buf);
3507 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003508 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003509 return ret;
3510}
3511
3512/*
3513 * Attach a new stream to a connection
3514 * (Used for outgoing connections)
3515 */
3516static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3517{
3518 struct conn_stream *cs;
3519 struct fcgi_strm *fstrm;
3520 struct fcgi_conn *fconn = conn->ctx;
3521
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003522 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003523 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003524 if (!cs) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003525 TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3526 goto err;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003527 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003528 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3529 if (!fstrm) {
3530 cs_free(cs);
Christopher Faulet73518be2021-01-27 12:06:54 +01003531 goto err;
Christopher Faulet99eff652019-08-11 23:11:30 +02003532 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003533
3534 /* the connection is not idle anymore, let's mark this */
3535 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003536 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003537
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003538 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003539 return cs;
Christopher Faulet73518be2021-01-27 12:06:54 +01003540
3541 err:
3542 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3543 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003544}
3545
3546/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3547 * We have to scan because we may have some orphan streams. It might be
3548 * beneficial to scan backwards from the end to reduce the likeliness to find
3549 * orphans.
3550 */
3551static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3552{
3553 struct fcgi_conn *fconn = conn->ctx;
3554 struct fcgi_strm *fstrm;
3555 struct eb32_node *node;
3556
3557 node = eb32_first(&fconn->streams_by_id);
3558 while (node) {
3559 fstrm = container_of(node, struct fcgi_strm, by_id);
3560 if (fstrm->cs)
3561 return fstrm->cs;
3562 node = eb32_next(node);
3563 }
3564 return NULL;
3565}
3566
3567/*
3568 * Destroy the mux and the associated connection, if it is no longer used
3569 */
3570static void fcgi_destroy(void *ctx)
3571{
3572 struct fcgi_conn *fconn = ctx;
3573
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003574 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003575 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3576 fcgi_release(fconn);
3577}
3578
3579/*
3580 * Detach the stream from the connection and possibly release the connection.
3581 */
3582static void fcgi_detach(struct conn_stream *cs)
3583{
3584 struct fcgi_strm *fstrm = cs->ctx;
3585 struct fcgi_conn *fconn;
3586 struct session *sess;
3587
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003588 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3589
Christopher Faulet99eff652019-08-11 23:11:30 +02003590 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003591 if (!fstrm) {
3592 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003593 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003594 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003595
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003596 /* there's no txbuf so we're certain no to be able to send anything */
3597 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003598
3599 sess = fstrm->sess;
3600 fconn = fstrm->fconn;
3601 fstrm->cs = NULL;
3602 fconn->nb_cs--;
3603
3604 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3605 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3606 fconn->streams_limit = 1;
3607 }
3608 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3609 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3610 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3611 fconn->state = FCGI_CS_CLOSED;
3612 }
3613
3614 /* this stream may be blocked waiting for some data to leave, so orphan
3615 * it in this case.
3616 */
3617 if (!(cs->conn->flags & CO_FL_ERROR) &&
3618 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003619 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003620 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003621 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003622 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003623 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003624
3625 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3626 /* unblock the connection if it was blocked on this stream. */
3627 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3628 fcgi_conn_restart_reading(fconn, 1);
3629 }
3630
3631 fcgi_strm_destroy(fstrm);
3632
3633 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003634 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003635 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003636 /* Add the connection in the session serverlist, if not already done */
3637 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3638 fconn->conn->owner = NULL;
3639 if (eb_is_empty(&fconn->streams_by_id)) {
3640 /* let's kill the connection right away */
3641 fconn->conn->mux->destroy(fconn);
3642 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3643 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003644 }
3645 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003646 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003647 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3648 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003649 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003650 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003651 }
3652 }
3653 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003654 else {
3655 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003656 /* If the connection is owned by the session, first remove it
3657 * from its list
3658 */
3659 if (fconn->conn->owner) {
3660 session_unown_conn(fconn->conn->owner, fconn->conn);
3661 fconn->conn->owner = NULL;
3662 }
3663
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003664 /* mark that the tasklet may lose its context to another thread and
3665 * that the handler needs to check it under the idle conns lock.
3666 */
3667 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003668 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3669
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003670 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003671 /* The server doesn't want it, let's kill the connection right away */
3672 fconn->conn->mux->destroy(fconn);
3673 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3674 return;
3675 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003676 /* At this point, the connection has been added to the
3677 * server idle list, so another thread may already have
3678 * hijacked it, so we can't do anything with it.
3679 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003680 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3681 return;
3682 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003683 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003684 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
Willy Tarreau2b718102021-04-21 07:32:39 +02003685 !LIST_INLIST(&fconn->conn->session_list)) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003686 ebmb_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003687 &fconn->conn->hash_node->node,
3688 sizeof(fconn->conn->hash_node->hash));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003689 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003690 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003691 }
3692
3693 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003694 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003695 */
3696 if (fcgi_conn_is_dead(fconn)) {
3697 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003698 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003699 fcgi_release(fconn);
3700 }
3701 else if (fconn->task) {
3702 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3703 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003704 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003706 else
3707 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003708}
3709
3710
3711/* Performs a synchronous or asynchronous shutr(). */
3712static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3713{
3714 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003715
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003716 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3717
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 if (fstrm->state == FCGI_SS_CLOSED)
3719 goto done;
3720
3721 /* a connstream may require us to immediately kill the whole connection
3722 * for example because of a "tcp-request content reject" rule that is
3723 * normally used to limit abuse.
3724 */
3725 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003726 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3727 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003728 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003729 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003730 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003731 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3733 !fcgi_strm_send_abort(fconn, fstrm))
3734 goto add_to_list;
3735 }
3736
3737 fcgi_strm_close(fstrm);
3738
3739 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3740 tasklet_wakeup(fconn->wait_event.tasklet);
3741 done:
3742 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003743 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003744 return;
3745
3746 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003747 /* Let the handler know we want to shutr, and add ourselves to the
3748 * send list if not yet done. fcgi_deferred_shut() will be
3749 * automatically called via the shut_tl tasklet when there's room
3750 * again.
3751 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003752 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003753 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003754 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003755 }
3756 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003758 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003759 return;
3760}
3761
3762/* Performs a synchronous or asynchronous shutw(). */
3763static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3764{
3765 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003766
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003767 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3768
Christopher Faulet99eff652019-08-11 23:11:30 +02003769 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3770 goto done;
3771
3772 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3773 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3774 !fcgi_strm_send_abort(fconn, fstrm))
3775 goto add_to_list;
3776
3777 if (fstrm->state == FCGI_SS_HREM)
3778 fcgi_strm_close(fstrm);
3779 else
3780 fstrm->state = FCGI_SS_HLOC;
3781 } else {
3782 /* a connstream may require us to immediately kill the whole connection
3783 * for example because of a "tcp-request content reject" rule that is
3784 * normally used to limit abuse.
3785 */
3786 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003787 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3788 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003789 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003790 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003791
3792 fcgi_strm_close(fstrm);
3793 }
3794
3795 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3796 tasklet_wakeup(fconn->wait_event.tasklet);
3797 done:
3798 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003799 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003800 return;
3801
3802 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003803 /* Let the handler know we want to shutr, and add ourselves to the
3804 * send list if not yet done. fcgi_deferred_shut() will be
3805 * automatically called via the shut_tl tasklet when there's room
3806 * again.
3807 */
Willy Tarreau2b718102021-04-21 07:32:39 +02003808 if (!LIST_INLIST(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003809 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003810 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003811 }
3812 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003813 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003814 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003815 return;
3816}
3817
Willy Tarreau7aad7032020-01-16 17:20:57 +01003818/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003819 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003820 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003821 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003822struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003823{
3824 struct fcgi_strm *fstrm = ctx;
3825 struct fcgi_conn *fconn = fstrm->fconn;
3826
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003827 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3828
Willy Tarreau7aad7032020-01-16 17:20:57 +01003829 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3830 /* some data processing remains to be done first */
3831 goto end;
3832 }
3833
Christopher Faulet99eff652019-08-11 23:11:30 +02003834 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3835 fcgi_do_shutw(fstrm);
3836
3837 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3838 fcgi_do_shutr(fstrm);
3839
3840 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3841 /* We're done trying to send, remove ourself from the send_list */
3842 LIST_DEL_INIT(&fstrm->send_list);
3843
3844 if (!fstrm->cs) {
3845 fcgi_strm_destroy(fstrm);
3846 if (fcgi_conn_is_dead(fconn))
3847 fcgi_release(fconn);
3848 }
3849 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003850 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003851 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003852 return NULL;
3853}
3854
3855/* shutr() called by the conn_stream (mux_ops.shutr) */
3856static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3857{
3858 struct fcgi_strm *fstrm = cs->ctx;
3859
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003860 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003861 if (cs->flags & CS_FL_KILL_CONN)
3862 fstrm->flags |= FCGI_SF_KILL_CONN;
3863
3864 if (!mode)
3865 return;
3866
3867 fcgi_do_shutr(fstrm);
3868}
3869
3870/* shutw() called by the conn_stream (mux_ops.shutw) */
3871static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3872{
3873 struct fcgi_strm *fstrm = cs->ctx;
3874
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003875 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003876 if (cs->flags & CS_FL_KILL_CONN)
3877 fstrm->flags |= FCGI_SF_KILL_CONN;
3878
3879 fcgi_do_shutw(fstrm);
3880}
3881
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003882/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3883 * event subscriber <es> is not allowed to change from a previous call as long
3884 * as at least one event is still subscribed. The <event_type> must only be a
3885 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003886 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003887static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003888{
Christopher Faulet99eff652019-08-11 23:11:30 +02003889 struct fcgi_strm *fstrm = cs->ctx;
3890 struct fcgi_conn *fconn = fstrm->fconn;
3891
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003892 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003893 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003894
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003895 es->events |= event_type;
3896 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003897
3898 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003899 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003900
Christopher Faulet99eff652019-08-11 23:11:30 +02003901 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003902 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau2b718102021-04-21 07:32:39 +02003903 if (!LIST_INLIST(&fstrm->send_list))
3904 LIST_APPEND(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003905 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003906 return 0;
3907}
3908
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003909/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3910 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3911 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003912 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003913static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003914{
Christopher Faulet99eff652019-08-11 23:11:30 +02003915 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003916 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003917
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003918 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003919 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003920
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003921 es->events &= ~event_type;
3922 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003923 fstrm->subs = NULL;
3924
3925 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003926 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003927
Christopher Faulet99eff652019-08-11 23:11:30 +02003928 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003929 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003930 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003931 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3932 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003933 }
3934 return 0;
3935}
3936
Christopher Faulet564e39c2021-09-21 15:50:55 +02003937/* Called from the upper layer, to receive data
3938 *
3939 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3940 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3941 * means the caller wants to flush input data (from the mux buffer and the
3942 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3943 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3944 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3945 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3946 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3947 * copy as much data as possible.
3948 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003949static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3950{
3951 struct fcgi_strm *fstrm = cs->ctx;
3952 struct fcgi_conn *fconn = fstrm->fconn;
3953 size_t ret = 0;
3954
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003955 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3956
Christopher Faulet99eff652019-08-11 23:11:30 +02003957 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3958 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003959 else
3960 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003961
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003962 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003963 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3964 else {
3965 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003966 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003967 cs->flags |= CS_FL_EOI;
3968 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3969 cs->flags |= CS_FL_EOS;
3970 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003971 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003972 cs->flags |= CS_FL_EOS;
3973 if (cs->flags & CS_FL_ERR_PENDING)
3974 cs->flags |= CS_FL_ERROR;
3975 fcgi_release_buf(fconn, &fstrm->rxbuf);
3976 }
3977
3978 if (ret && fconn->dsi == fstrm->id) {
3979 /* demux is blocking on this stream's buffer */
3980 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3981 fcgi_conn_restart_reading(fconn, 1);
3982 }
3983
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003984 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003985 return ret;
3986}
3987
3988
Christopher Faulet99eff652019-08-11 23:11:30 +02003989/* Called from the upper layer, to send data from buffer <buf> for no more than
3990 * <count> bytes. Returns the number of bytes effectively sent. Some status
3991 * flags may be updated on the conn_stream.
3992 */
3993static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3994{
3995 struct fcgi_strm *fstrm = cs->ctx;
3996 struct fcgi_conn *fconn = fstrm->fconn;
3997 size_t total = 0;
3998 size_t ret;
3999 struct htx *htx = NULL;
4000 struct htx_sl *sl;
4001 struct htx_blk *blk;
4002 uint32_t bsize;
4003
Willy Tarreau022e5e52020-09-10 09:33:15 +02004004 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004005
Christopher Faulet99eff652019-08-11 23:11:30 +02004006 /* If we were not just woken because we wanted to send but couldn't,
4007 * and there's somebody else that is waiting to send, do nothing,
4008 * we will subscribe later and be put at the end of the list
4009 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01004010 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004011 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 +02004012 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004013 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01004014 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02004015
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004016 if (fconn->state < FCGI_CS_RECORD_H) {
4017 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02004018 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004019 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004020
4021 htx = htxbuf(buf);
4022 if (fstrm->id == 0) {
4023 int32_t id = fcgi_conn_get_next_sid(fconn);
4024
4025 if (id < 0) {
4026 fcgi_strm_close(fstrm);
4027 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004028 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 +02004029 return 0;
4030 }
4031
4032 eb32_delete(&fstrm->by_id);
4033 fstrm->by_id.key = fstrm->id = id;
4034 fconn->max_id = id;
4035 fconn->nb_reserved--;
4036 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
4037
4038
4039 /* Check if length of the body is known or if the message is
4040 * full. Otherwise, the request is invalid.
4041 */
4042 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004043 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02004044 htx->flags |= HTX_FL_PARSING_ERROR;
4045 fcgi_strm_error(fstrm);
4046 goto done;
4047 }
4048 }
4049
4050 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004051 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 +02004052 if (!fcgi_strm_send_begin_request(fconn, fstrm))
4053 goto done;
4054 }
4055
4056 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
4057 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
4058
Christopher Fauletfe410d62020-05-19 15:13:00 +02004059 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02004060 count && !htx_is_empty(htx)) {
4061 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02004062 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02004063 bsize = htx_get_blksz(blk);
4064
4065 switch (htx_get_blk_type(blk)) {
4066 case HTX_BLK_REQ_SL:
4067 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004068 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 +02004069 ret = fcgi_strm_send_params(fconn, fstrm, htx);
4070 if (!ret) {
4071 goto done;
4072 }
4073 total += ret;
4074 count -= ret;
4075 break;
4076
4077 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004078 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4079 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4080 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4081 if (!ret)
4082 goto done;
4083 }
4084 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4085 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4086 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4087 if (!ret)
4088 goto done;
4089 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004090 goto remove_blk;
4091
4092 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004093 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 +02004094 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4095 if (ret > 0) {
4096 htx = htx_from_buf(buf);
4097 total += ret;
4098 count -= ret;
4099 if (ret < bsize)
4100 goto done;
4101 }
4102 break;
4103
Christopher Faulet99eff652019-08-11 23:11:30 +02004104 default:
4105 remove_blk:
4106 htx_remove_blk(htx, blk);
4107 total += bsize;
4108 count -= bsize;
4109 break;
4110 }
4111 }
4112
4113 done:
4114 if (fstrm->state >= FCGI_SS_HLOC) {
4115 /* trim any possibly pending data after we close (extra CR-LF,
4116 * unprocessed trailers, abnormal extra data, ...)
4117 */
4118 total += count;
4119 count = 0;
4120 }
4121
4122 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004123 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 +02004124 cs_set_error(cs);
4125 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4126 fcgi_strm_close(fstrm);
4127 }
4128
4129 if (htx)
4130 htx_to_buf(htx, buf);
4131
Christopher Faulet99eff652019-08-11 23:11:30 +02004132 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004133 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4134 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 +02004135 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004136 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004137
4138 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004139 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4140 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004141 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004142
4143 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004144 return total;
4145}
4146
4147/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004148static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004149{
4150 struct fcgi_conn *fconn = conn->ctx;
4151 struct fcgi_strm *fstrm = NULL;
4152 struct eb32_node *node;
4153 int send_cnt = 0;
4154 int tree_cnt = 0;
4155 int orph_cnt = 0;
4156 struct buffer *hmbuf, *tmbuf;
4157
4158 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004159 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004160
4161 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4162 send_cnt++;
4163
4164 fstrm = NULL;
4165 node = eb32_first(&fconn->streams_by_id);
4166 while (node) {
4167 fstrm = container_of(node, struct fcgi_strm, by_id);
4168 tree_cnt++;
4169 if (!fstrm->cs)
4170 orph_cnt++;
4171 node = eb32_next(node);
4172 }
4173
4174 hmbuf = br_head(fconn->mbuf);
4175 tmbuf = br_tail(fconn->mbuf);
4176 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4177 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4178 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4179 fconn->state, fconn->max_id, fconn->flags,
4180 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4181 fconn->wait_event.events, fconn->dsi,
4182 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4183 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4184 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4185 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4186 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4187 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4188 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4189
4190 if (fstrm) {
4191 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4192 fstrm, fstrm->id, fstrm->flags,
4193 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4194 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4195 fstrm->cs);
4196 if (fstrm->cs)
4197 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4198 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004199 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4200 if (fstrm->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004201 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4202 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4203 fstrm->subs->tasklet->calls,
4204 fstrm->subs->tasklet->context);
4205 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4206 chunk_appendf(&trash, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004207 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004208 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004209 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004210}
4211
4212/* Migrate the the connection to the current thread.
4213 * Return 0 if successful, non-zero otherwise.
4214 * Expected to be called with the old thread lock held.
4215 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004216static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004217{
4218 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004219 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004220
4221 if (fd_takeover(conn->handle.fd, conn) != 0)
4222 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004223
4224 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4225 /* We failed to takeover the xprt, even if the connection may
4226 * still be valid, flag it as error'd, as we have already
4227 * taken over the fd, and wake the tasklet, so that it will
4228 * destroy it.
4229 */
4230 conn->flags |= CO_FL_ERROR;
4231 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4232 return -1;
4233 }
4234
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004235 if (fcgi->wait_event.events)
4236 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4237 fcgi->wait_event.events, &fcgi->wait_event);
4238 /* To let the tasklet know it should free itself, and do nothing else,
4239 * set its context to NULL;
4240 */
4241 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004242 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004243
4244 task = fcgi->task;
4245 if (task) {
4246 task->context = NULL;
4247 fcgi->task = NULL;
4248 __ha_barrier_store();
4249 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004250
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004251 fcgi->task = task_new_here();
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004252 if (!fcgi->task) {
4253 fcgi_release(fcgi);
4254 return -1;
4255 }
4256 fcgi->task->process = fcgi_timeout_task;
4257 fcgi->task->context = fcgi;
4258 }
4259 fcgi->wait_event.tasklet = tasklet_new();
4260 if (!fcgi->wait_event.tasklet) {
4261 fcgi_release(fcgi);
4262 return -1;
4263 }
4264 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4265 fcgi->wait_event.tasklet->context = fcgi;
4266 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4267 SUB_RETRY_RECV, &fcgi->wait_event);
4268
4269 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004270}
4271
4272/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004273/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004274/****************************************/
4275
4276/* The mux operations */
4277static const struct mux_ops mux_fcgi_ops = {
4278 .init = fcgi_init,
4279 .wake = fcgi_wake,
4280 .attach = fcgi_attach,
4281 .get_first_cs = fcgi_get_first_cs,
4282 .detach = fcgi_detach,
4283 .destroy = fcgi_destroy,
4284 .avail_streams = fcgi_avail_streams,
4285 .used_streams = fcgi_used_streams,
4286 .rcv_buf = fcgi_rcv_buf,
4287 .snd_buf = fcgi_snd_buf,
4288 .subscribe = fcgi_subscribe,
4289 .unsubscribe = fcgi_unsubscribe,
4290 .shutr = fcgi_shutr,
4291 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004292 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004293 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004294 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004295 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004296 .name = "FCGI",
4297};
4298
4299
4300/* this mux registers FCGI proto */
4301static struct mux_proto_list mux_proto_fcgi =
4302{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4303
4304INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4305
4306/*
4307 * Local variables:
4308 * c-indent-level: 8
4309 * c-basic-offset: 8
4310 * End:
4311 */