blob: ddb2a60d6bc971695a41ef2ec784563003caf3fd [file] [log] [blame]
Christopher Faulet99eff652019-08-11 23:11:30 +02001/*
2 * FastCGI mux-demux for connections
3 *
4 * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/ist.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020018#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020019#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020022#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020023#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020024#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020025#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020026#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020027#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020028#include <haproxy/proxy-t.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020029#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session-t.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020031#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020034#include <haproxy/trace.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020035
Willy Tarreaub2551052020-06-09 09:07:15 +020036
Christopher Faulet99eff652019-08-11 23:11:30 +020037/* FCGI Connection flags (32 bits) */
38#define FCGI_CF_NONE 0x00000000
39
40/* Flags indicating why writing to the mux is blockes */
41#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
42#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
43#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
44
45/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
50#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
51#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
52#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
53#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
54#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
55#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
56#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
57
58/* Other flags */
59#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
60#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
61#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
62#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
63#define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */
64#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
65
66/* FCGI connection state (fcgi_conn->state) */
67enum fcgi_conn_st {
68 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
69 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
70 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
71 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
72 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
73 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
74 FCGI_CS_ENTRIES
75} __attribute__((packed));
76
77/* 32 buffers: one for the ring's root, rest for the mbuf itself */
78#define FCGI_C_MBUF_CNT 32
79
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010080/* Size for a record header (also size of empty record) */
81#define FCGI_RECORD_HEADER_SZ 8
82
Christopher Faulet99eff652019-08-11 23:11:30 +020083/* FCGI connection descriptor */
84struct fcgi_conn {
85 struct connection *conn;
86
87 enum fcgi_conn_st state; /* FCGI connection state */
88 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
89 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
90 uint32_t flags; /* Connection flags: FCGI_CF_* */
91
92 int16_t dsi; /* dmux stream ID (<0 = idle ) */
93 uint16_t drl; /* demux record length (if dsi >= 0) */
94 uint8_t drt; /* demux record type (if dsi >= 0) */
95 uint8_t drp; /* demux record padding (if dsi >= 0) */
96
97 struct buffer dbuf; /* demux buffer */
98 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
99
100 int timeout; /* idle timeout duration in ticks */
101 int shut_timeout; /* idle timeout duration in ticks after shutdown */
102 unsigned int nb_streams; /* number of streams in the tree */
103 unsigned int nb_cs; /* number of attached conn_streams */
104 unsigned int nb_reserved; /* number of reserved streams */
105 unsigned int stream_cnt; /* total number of streams seen */
106
107 struct proxy *proxy; /* the proxy this connection was created for */
108 struct fcgi_app *app; /* FCGI application used by this mux */
109 struct task *task; /* timeout management task */
110 struct eb_root streams_by_id; /* all active streams by their ID */
111
112 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200113
114 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
115 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
116};
117
118
119/* FCGI stream state, in fcgi_strm->state */
120enum fcgi_strm_st {
121 FCGI_SS_IDLE = 0,
122 FCGI_SS_OPEN,
123 FCGI_SS_HREM, // half-closed(remote)
124 FCGI_SS_HLOC, // half-closed(local)
125 FCGI_SS_ERROR,
126 FCGI_SS_CLOSED,
127 FCGI_SS_ENTRIES
128} __attribute__((packed));
129
130
131/* FCGI stream flags (32 bits) */
132#define FCGI_SF_NONE 0x00000000
133#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100134#define FCGI_SF_ES_SENT 0x00000002 /* end-of-stream sent (empty STDIN record) */
135#define FCGI_SF_EP_SENT 0x00000004 /* end-of-param sent (empty PARAMS record) */
136#define FCGI_SF_ABRT_SENT 0x00000008 /* abort sent (ABORT_REQUEST record) */
Christopher Faulet99eff652019-08-11 23:11:30 +0200137
138/* Stream flags indicating the reason the stream is blocked */
139#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
140#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
141#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
142
143#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
144#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100145#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200146
147#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
148#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
149#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
150
Christopher Faulet99eff652019-08-11 23:11:30 +0200151
152/* FCGI stream descriptor */
153struct fcgi_strm {
154 struct conn_stream *cs;
155 struct session *sess;
156 struct fcgi_conn *fconn;
157
158 int32_t id; /* stream ID */
159
160 uint32_t flags; /* Connection flags: FCGI_SF_* */
161 enum fcgi_strm_st state; /* FCGI stream state */
162 int proto_status; /* FCGI_PS_* */
163
164 struct h1m h1m; /* response parser state for H1 */
165
166 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
167
168 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100169 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200170 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100171 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 +0200172};
173
174/* Flags representing all default FCGI parameters */
175#define FCGI_SP_CGI_GATEWAY 0x00000001
176#define FCGI_SP_DOC_ROOT 0x00000002
177#define FCGI_SP_SCRIPT_NAME 0x00000004
178#define FCGI_SP_PATH_INFO 0x00000008
179#define FCGI_SP_REQ_URI 0x00000010
180#define FCGI_SP_REQ_METH 0x00000020
181#define FCGI_SP_REQ_QS 0x00000040
182#define FCGI_SP_SRV_PORT 0x00000080
183#define FCGI_SP_SRV_PROTO 0x00000100
184#define FCGI_SP_SRV_NAME 0x00000200
185#define FCGI_SP_REM_ADDR 0x00000400
186#define FCGI_SP_REM_PORT 0x00000800
187#define FCGI_SP_SCRIPT_FILE 0x00001000
188#define FCGI_SP_PATH_TRANS 0x00002000
189#define FCGI_SP_CONT_LEN 0x00004000
190#define FCGI_SP_HTTPS 0x00008000
191#define FCGI_SP_MASK 0x0000FFFF
192#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
193
194/* FCGI parameters used when PARAMS record is sent */
195struct fcgi_strm_params {
196 uint32_t mask;
197 struct ist docroot;
198 struct ist scriptname;
199 struct ist pathinfo;
200 struct ist meth;
201 struct ist uri;
202 struct ist vsn;
203 struct ist qs;
204 struct ist srv_name;
205 struct ist srv_port;
206 struct ist rem_addr;
207 struct ist rem_port;
208 struct ist cont_len;
209 int https;
210 struct buffer *p;
211};
212
213/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
214#define MAX_DATA_REALIGN 1024
215
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200216/* trace source and events */
217static void fcgi_trace(enum trace_level level, uint64_t mask,
218 const struct trace_source *src,
219 const struct ist where, const struct ist func,
220 const void *a1, const void *a2, const void *a3, const void *a4);
221
222/* The event representation is split like this :
223 * fconn - internal FCGI connection
224 * fstrm - internal FCGI stream
225 * strm - application layer
226 * rx - data receipt
227 * tx - data transmission
228 * rsp - response parsing
229 */
230static const struct trace_event fcgi_trace_events[] = {
231#define FCGI_EV_FCONN_NEW (1ULL << 0)
232 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
233#define FCGI_EV_FCONN_RECV (1ULL << 1)
234 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
235#define FCGI_EV_FCONN_SEND (1ULL << 2)
236 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
237#define FCGI_EV_FCONN_BLK (1ULL << 3)
238 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
239#define FCGI_EV_FCONN_WAKE (1ULL << 4)
240 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
241#define FCGI_EV_FCONN_END (1ULL << 5)
242 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
243#define FCGI_EV_FCONN_ERR (1ULL << 6)
244 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
245
246#define FCGI_EV_RX_FHDR (1ULL << 7)
247 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
248#define FCGI_EV_RX_RECORD (1ULL << 8)
249 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
250#define FCGI_EV_RX_EOI (1ULL << 9)
251 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
252#define FCGI_EV_RX_GETVAL (1ULL << 10)
253 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
254#define FCGI_EV_RX_STDOUT (1ULL << 11)
255 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
256#define FCGI_EV_RX_STDERR (1ULL << 12)
257 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
258#define FCGI_EV_RX_ENDREQ (1ULL << 13)
259 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
260
261#define FCGI_EV_TX_RECORD (1ULL << 14)
262 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
263#define FCGI_EV_TX_EOI (1ULL << 15)
264 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
265#define FCGI_EV_TX_BEGREQ (1ULL << 16)
266 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
267#define FCGI_EV_TX_GETVAL (1ULL << 17)
268 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
269#define FCGI_EV_TX_PARAMS (1ULL << 18)
270 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
271#define FCGI_EV_TX_STDIN (1ULL << 19)
272 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
273#define FCGI_EV_TX_ABORT (1ULL << 20)
274 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
275
276#define FCGI_EV_RSP_DATA (1ULL << 21)
277 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
278#define FCGI_EV_RSP_EOM (1ULL << 22)
279 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
280#define FCGI_EV_RSP_HDRS (1ULL << 23)
281 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
282#define FCGI_EV_RSP_BODY (1ULL << 24)
283 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
284#define FCGI_EV_RSP_TLRS (1ULL << 25)
285 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
286
287#define FCGI_EV_FSTRM_NEW (1ULL << 26)
288 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
289#define FCGI_EV_FSTRM_BLK (1ULL << 27)
290 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
291#define FCGI_EV_FSTRM_END (1ULL << 28)
292 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
293#define FCGI_EV_FSTRM_ERR (1ULL << 29)
294 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
295
296#define FCGI_EV_STRM_NEW (1ULL << 30)
297 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
298#define FCGI_EV_STRM_RECV (1ULL << 31)
299 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
300#define FCGI_EV_STRM_SEND (1ULL << 32)
301 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
302#define FCGI_EV_STRM_FULL (1ULL << 33)
303 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
304#define FCGI_EV_STRM_WAKE (1ULL << 34)
305 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
306#define FCGI_EV_STRM_SHUT (1ULL << 35)
307 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
308#define FCGI_EV_STRM_END (1ULL << 36)
309 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
310#define FCGI_EV_STRM_ERR (1ULL << 37)
311 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
312
313 { }
314};
315
316static const struct name_desc fcgi_trace_lockon_args[4] = {
317 /* arg1 */ { /* already used by the connection */ },
318 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
319 /* arg3 */ { },
320 /* arg4 */ { }
321};
322
323
324static const struct name_desc fcgi_trace_decoding[] = {
325#define FCGI_VERB_CLEAN 1
326 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
327#define FCGI_VERB_MINIMAL 2
328 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
329#define FCGI_VERB_SIMPLE 3
330 { .name="simple", .desc="add request/response status line or htx info when available" },
331#define FCGI_VERB_ADVANCED 4
332 { .name="advanced", .desc="add header fields or record decoding when available" },
333#define FCGI_VERB_COMPLETE 5
334 { .name="complete", .desc="add full data dump when available" },
335 { /* end */ }
336};
337
338static struct trace_source trace_fcgi = {
339 .name = IST("fcgi"),
340 .desc = "FastCGI multiplexer",
341 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
342 .default_cb = fcgi_trace,
343 .known_events = fcgi_trace_events,
344 .lockon_args = fcgi_trace_lockon_args,
345 .decoding = fcgi_trace_decoding,
346 .report_events = ~0, // report everything by default
347};
348
349#define TRACE_SOURCE &trace_fcgi
350INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
351
Christopher Faulet99eff652019-08-11 23:11:30 +0200352/* FCGI connection and stream pools */
353DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
354DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
355
Willy Tarreau144f84a2021-03-02 16:09:26 +0100356struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200357static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau691d5032021-01-20 14:55:01 +0100358/* fcgi_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100359struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200360static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
Willy Tarreau144f84a2021-03-02 16:09:26 +0100361struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200362static 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 +0200363static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
364static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200365static void fcgi_strm_alert(struct fcgi_strm *fstrm);
366static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
367
368/* a dmumy management stream */
369static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
370 .cs = NULL,
371 .fconn = NULL,
372 .state = FCGI_SS_CLOSED,
373 .flags = FCGI_SF_NONE,
374 .id = 0,
375};
376
377/* and a dummy idle stream for use with any unknown stream */
378static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
379 .cs = NULL,
380 .fconn = NULL,
381 .state = FCGI_SS_IDLE,
382 .flags = FCGI_SF_NONE,
383 .id = 0,
384};
385
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200386/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
387static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
388{
389 switch (st) {
390 case FCGI_CS_INIT : return "INI";
391 case FCGI_CS_SETTINGS : return "STG";
392 case FCGI_CS_RECORD_H : return "RDH";
393 case FCGI_CS_RECORD_D : return "RDD";
394 case FCGI_CS_RECORD_P : return "RDP";
395 case FCGI_CS_CLOSED : return "CLO";
396 default : return "???";
397 }
398}
399
400/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
401static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
402{
403 switch (st) {
404 case FCGI_SS_IDLE : return "IDL";
405 case FCGI_SS_OPEN : return "OPN";
406 case FCGI_SS_HREM : return "RCL";
407 case FCGI_SS_HLOC : return "HCL";
408 case FCGI_SS_ERROR : return "ERR";
409 case FCGI_SS_CLOSED : return "CLO";
410 default : return "???";
411 }
412}
413
414
415/* the FCGI traces always expect that arg1, if non-null, is of type connection
416 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
417 * and that arg3, if non-null, is a htx for rx/tx headers.
418 */
419static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
420 const struct ist where, const struct ist func,
421 const void *a1, const void *a2, const void *a3, const void *a4)
422{
423 const struct connection *conn = a1;
424 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
425 const struct fcgi_strm *fstrm = a2;
426 const struct htx *htx = a3;
427 const size_t *val = a4;
428
429 if (!fconn)
430 fconn = (fstrm ? fstrm->fconn : NULL);
431
432 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
433 return;
434
435 /* Display the response state if fstrm is defined */
436 if (fstrm)
437 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
438
439 if (src->verbosity == FCGI_VERB_CLEAN)
440 return;
441
442 /* Display the value to the 4th argument (level > STATE) */
443 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100444 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200445
446 /* Display status-line if possible (verbosity > MINIMAL) */
447 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
448 const struct htx_blk *blk = htx_get_head_blk(htx);
449 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
450 enum htx_blk_type type = htx_get_blk_type(blk);
451
452 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
453 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
454 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
455 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
456 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
457 }
458
459 /* Display fconn info and, if defined, fstrm info */
460 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
461 if (fstrm)
462 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
463
464 if (!fstrm || fstrm->id <= 0)
465 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
466 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
467 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
468
469 if (src->verbosity == FCGI_VERB_MINIMAL)
470 return;
471
472 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
473 if (src->level > TRACE_LEVEL_USER) {
474 if (src->verbosity == FCGI_VERB_COMPLETE ||
475 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
476 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
477 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
478 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
479 if (src->verbosity == FCGI_VERB_COMPLETE ||
480 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
481 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
482 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
483
484 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
485 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
486 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
487 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
488 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
489 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
490 }
491
492 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
493 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
494 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
495 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
496 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
497 }
498
499 /* Display htx info if defined (level > USER) */
500 if (src->level > TRACE_LEVEL_USER && htx) {
501 int full = 0;
502
503 /* Full htx info (level > STATE && verbosity > SIMPLE) */
504 if (src->level > TRACE_LEVEL_STATE) {
505 if (src->verbosity == FCGI_VERB_COMPLETE)
506 full = 1;
507 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
508 full = 1;
509 }
510
511 chunk_memcat(&trace_buf, "\n\t", 2);
512 htx_dump(&trace_buf, htx, full);
513 }
514}
Christopher Faulet99eff652019-08-11 23:11:30 +0200515
516/*****************************************************/
517/* functions below are for dynamic buffer management */
518/*****************************************************/
519
520/* Indicates whether or not the we may call the fcgi_recv() function to attempt
521 * to receive data into the buffer and/or demux pending data. The condition is
522 * a bit complex due to some API limits for now. The rules are the following :
523 * - if an error or a shutdown was detected on the connection and the buffer
524 * is empty, we must not attempt to receive
525 * - if the demux buf failed to be allocated, we must not try to receive and
526 * we know there is nothing pending
527 * - if no flag indicates a blocking condition, we may attempt to receive,
528 * regardless of whether the demux buffer is full or not, so that only
529 * de demux part decides whether or not to block. This is needed because
530 * the connection API indeed prevents us from re-enabling receipt that is
531 * already enabled in a polled state, so we must always immediately stop
532 * as soon as the demux can't proceed so as never to hit an end of read
533 * with data pending in the buffers.
534 * - otherwise must may not attempt
535 */
536static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
537{
538 if (b_data(&fconn->dbuf) == 0 &&
539 (fconn->state == FCGI_CS_CLOSED ||
540 fconn->conn->flags & CO_FL_ERROR ||
541 conn_xprt_read0_pending(fconn->conn)))
542 return 0;
543
544 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
545 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
546 return 1;
547
548 return 0;
549}
550
551/* Restarts reading on the connection if it was not enabled */
552static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
553{
554 if (!fcgi_recv_allowed(fconn))
555 return;
556 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
557 (fconn->wait_event.events & SUB_RETRY_RECV))
558 return;
559 tasklet_wakeup(fconn->wait_event.tasklet);
560}
561
562
563/* Tries to grab a buffer and to re-enable processing on mux <target>. The
564 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
565 * the allocation succeeds, in which case the connection is woken up, or 0 if
566 * it's impossible to wake up and we prefer to be woken up later.
567 */
568static int fcgi_buf_available(void *target)
569{
570 struct fcgi_conn *fconn = target;
571 struct fcgi_strm *fstrm;
572
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100573 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200574 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 +0200575 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
576 fcgi_conn_restart_reading(fconn, 1);
577 return 1;
578 }
579
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100580 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200581 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 +0200582 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200583 if (fconn->flags & FCGI_CF_DEM_MROOM) {
584 fconn->flags &= ~FCGI_CF_DEM_MROOM;
585 fcgi_conn_restart_reading(fconn, 1);
586 }
587 return 1;
588 }
589
590 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
591 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100592 b_alloc(&fstrm->rxbuf)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200593 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 +0200594 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
595 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200596 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200597 return 1;
598 }
599
600 return 0;
601}
602
603static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
604{
605 struct buffer *buf = NULL;
606
Willy Tarreau90f366b2021-02-20 11:49:49 +0100607 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100608 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200609 fconn->buf_wait.target = fconn;
610 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau90f366b2021-02-20 11:49:49 +0100611 LIST_ADDQ(&ti->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200612 }
613 return buf;
614}
615
616static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
617{
618 if (bptr->size) {
619 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100620 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200621 }
622}
623
624static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
625{
626 struct buffer *buf;
627 unsigned int count = 0;
628
629 while (b_size(buf = br_head_pick(fconn->mbuf))) {
630 b_free(buf);
631 count++;
632 }
633 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100634 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200635}
636
637/* Returns the number of allocatable outgoing streams for the connection taking
638 * the number reserved streams into account.
639 */
640static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
641{
642 int ret;
643
644 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
645 if (ret < 0)
646 ret = 0;
647 return ret;
648}
649
650/* Returns the number of streams in use on a connection to figure if it's
651 * idle or not. We check nb_cs and not nb_streams as the caller will want
652 * to know if it was the last one after a detach().
653 */
654static int fcgi_used_streams(struct connection *conn)
655{
656 struct fcgi_conn *fconn = conn->ctx;
657
658 return fconn->nb_cs;
659}
660
661/* Returns the number of concurrent streams available on the connection */
662static int fcgi_avail_streams(struct connection *conn)
663{
664 struct server *srv = objt_server(conn->target);
665 struct fcgi_conn *fconn = conn->ctx;
666 int ret1, ret2;
667
668 /* Don't open new stream if the connection is closed */
669 if (fconn->state == FCGI_CS_CLOSED)
670 return 0;
671
672 /* May be negative if this setting has changed */
673 ret1 = (fconn->streams_limit - fconn->nb_streams);
674
675 /* we must also consider the limit imposed by stream IDs */
676 ret2 = fcgi_streams_left(fconn);
677 ret1 = MIN(ret1, ret2);
678 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
679 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
680 ret1 = MIN(ret1, ret2);
681 }
682 return ret1;
683}
684
685/*****************************************************************/
686/* functions below are dedicated to the mux setup and management */
687/*****************************************************************/
688
689/* Initializes the mux once it's attached. Only outgoing connections are
690 * supported. So the context is already initialized before installing the
691 * mux. <input> is always used as Input buffer and may contain data. It is the
692 * caller responsibility to not reuse it anymore. Returns < 0 on error.
693 */
694static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
695 struct buffer *input)
696{
697 struct fcgi_conn *fconn;
698 struct fcgi_strm *fstrm;
699 struct fcgi_app *app = get_px_fcgi_app(px);
700 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200701 void *conn_ctx = conn->ctx;
702
703 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200704
Christopher Faulet73518be2021-01-27 12:06:54 +0100705 if (!app) {
706 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 +0200707 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100708 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200709
710 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100711 if (!fconn) {
712 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200713 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100714 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200715
716 fconn->shut_timeout = fconn->timeout = px->timeout.server;
717 if (tick_isset(px->timeout.serverfin))
718 fconn->shut_timeout = px->timeout.serverfin;
719
720 fconn->flags = FCGI_CF_NONE;
721
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500722 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200723 if (app->flags & FCGI_APP_FL_KEEP_CONN)
724 fconn->flags |= FCGI_CF_KEEP_CONN;
725 if (app->flags & FCGI_APP_FL_GET_VALUES)
726 fconn->flags |= FCGI_CF_GET_VALUES;
727 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
728 fconn->flags |= FCGI_CF_MPXS_CONNS;
729
730 fconn->proxy = px;
731 fconn->app = app;
732 fconn->task = NULL;
733 if (tick_isset(fconn->timeout)) {
734 t = task_new(tid_bit);
Christopher Faulet73518be2021-01-27 12:06:54 +0100735 if (!t) {
736 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 +0200737 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100738 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200739
740 fconn->task = t;
741 t->process = fcgi_timeout_task;
742 t->context = fconn;
743 t->expire = tick_add(now_ms, fconn->timeout);
744 }
745
746 fconn->wait_event.tasklet = tasklet_new();
747 if (!fconn->wait_event.tasklet)
748 goto fail;
749 fconn->wait_event.tasklet->process = fcgi_io_cb;
750 fconn->wait_event.tasklet->context = fconn;
751 fconn->wait_event.events = 0;
752
753 /* Initialise the context. */
754 fconn->state = FCGI_CS_INIT;
755 fconn->conn = conn;
756 fconn->streams_limit = app->maxreqs;
757 fconn->max_id = -1;
758 fconn->nb_streams = 0;
759 fconn->nb_cs = 0;
760 fconn->nb_reserved = 0;
761 fconn->stream_cnt = 0;
762
763 fconn->dbuf = *input;
764 fconn->dsi = -1;
765
766 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
767 fconn->streams_by_id = EB_ROOT;
768 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100769 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200770
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200771 conn->ctx = fconn;
772
Christopher Faulet99eff652019-08-11 23:11:30 +0200773 if (t)
774 task_queue(t);
775
776 /* FIXME: this is temporary, for outgoing connections we need to
777 * immediately allocate a stream until the code is modified so that the
778 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200779 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200781 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200782 if (!fstrm)
783 goto fail;
784
Christopher Faulet99eff652019-08-11 23:11:30 +0200785
786 /* Repare to read something */
787 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200788 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200789 return 0;
790
791 fail:
792 task_destroy(t);
793 if (fconn->wait_event.tasklet)
794 tasklet_free(fconn->wait_event.tasklet);
795 pool_free(pool_head_fcgi_conn, fconn);
796 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200797 conn->ctx = conn_ctx; // restore saved ctx
798 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200799 return -1;
800}
801
802/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
803 * -1 if no more is allocatable.
804 */
805static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
806{
807 int32_t id = (fconn->max_id + 1) | 1;
808
809 if ((id & 0x80000000U))
810 id = -1;
811 return id;
812}
813
814/* Returns the stream associated with id <id> or NULL if not found */
815static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
816{
817 struct eb32_node *node;
818
819 if (id == 0)
820 return (struct fcgi_strm *)fcgi_mgmt_stream;
821
822 if (id > fconn->max_id)
823 return (struct fcgi_strm *)fcgi_unknown_stream;
824
825 node = eb32_lookup(&fconn->streams_by_id, id);
826 if (!node)
827 return (struct fcgi_strm *)fcgi_unknown_stream;
828 return container_of(node, struct fcgi_strm, by_id);
829}
830
831
832/* Release function. This one should be called to free all resources allocated
833 * to the mux.
834 */
835static void fcgi_release(struct fcgi_conn *fconn)
836{
William Dauchy477757c2020-08-07 22:19:23 +0200837 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200838
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200839 TRACE_POINT(FCGI_EV_FCONN_END);
840
Christopher Faulet99eff652019-08-11 23:11:30 +0200841 if (fconn) {
842 /* The connection must be attached to this mux to be released */
843 if (fconn->conn && fconn->conn->ctx == fconn)
844 conn = fconn->conn;
845
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200846 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
847
Willy Tarreau90f366b2021-02-20 11:49:49 +0100848 if (LIST_ADDED(&fconn->buf_wait.list))
849 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200850
851 fcgi_release_buf(fconn, &fconn->dbuf);
852 fcgi_release_mbuf(fconn);
853
854 if (fconn->task) {
855 fconn->task->context = NULL;
856 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
857 fconn->task = NULL;
858 }
859 if (fconn->wait_event.tasklet)
860 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200861 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200862 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
863 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200864
865 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200866 }
867
868 if (conn) {
869 conn->mux = NULL;
870 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200871 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200872
873 conn_stop_tracking(conn);
874 conn_full_close(conn);
875 if (conn->destroy_cb)
876 conn->destroy_cb(conn);
877 conn_free(conn);
878 }
879}
880
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200881/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
882 * pending on the connection AND if there is no more data in the demux
883 * buffer. The function returns 1 to report a read0 or 0 otherwise.
884 */
885static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
886{
887 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
888 return 1;
889 return 0;
890}
891
Christopher Faulet99eff652019-08-11 23:11:30 +0200892
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500893/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200894static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
895{
896 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
897 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
898 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
899 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
900 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
901 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
902 conn_xprt_read0_pending(fconn->conn))))
903 return 1;
904 return 0;
905}
906
907
908/********************************************************/
909/* functions below are for the FCGI protocol processing */
910/********************************************************/
911
Christopher Faulet99eff652019-08-11 23:11:30 +0200912/* Marks an error on the stream. */
913static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
914{
915 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200916 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
917 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200918 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200919 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
920 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200921 if (fstrm->cs)
922 cs_set_error(fstrm->cs);
923 }
924}
925
926/* Attempts to notify the data layer of recv availability */
927static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
928{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100929 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200930 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100931 tasklet_wakeup(fstrm->subs->tasklet);
932 fstrm->subs->events &= ~SUB_RETRY_RECV;
933 if (!fstrm->subs->events)
934 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200935 }
936}
937
938/* Attempts to notify the data layer of send availability */
939static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
940{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100941 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200942 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100943 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100944 tasklet_wakeup(fstrm->subs->tasklet);
945 fstrm->subs->events &= ~SUB_RETRY_SEND;
946 if (!fstrm->subs->events)
947 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200948 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100949 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
950 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
951 tasklet_wakeup(fstrm->shut_tl);
952 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200953}
954
955/* Alerts the data layer, trying to wake it up by all means, following
956 * this sequence :
957 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
958 * for recv
959 * - if its subscribed to send, then it's woken up for send
960 * - if it was subscribed to neither, its ->wake() callback is called
961 * It is safe to call this function with a closed stream which doesn't have a
962 * conn_stream anymore.
963 */
964static void fcgi_strm_alert(struct fcgi_strm *fstrm)
965{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200966 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100967 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100968 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200969 fcgi_strm_notify_recv(fstrm);
970 fcgi_strm_notify_send(fstrm);
971 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200972 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
973 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200974 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200975 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200976}
977
978/* Writes the 16-bit record size <len> at address <record> */
979static inline void fcgi_set_record_size(void *record, uint16_t len)
980{
981 uint8_t *out = (record + 4);
982
983 *out = (len >> 8);
984 *(out + 1) = (len & 0xff);
985}
986
987/* Writes the 16-bit stream id <id> at address <record> */
988static inline void fcgi_set_record_id(void *record, uint16_t id)
989{
990 uint8_t *out = (record + 2);
991
992 *out = (id >> 8);
993 *(out + 1) = (id & 0xff);
994}
995
996/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
997 * its connection if the stream was not yet closed. Please use this exclusively
998 * before closing a stream to ensure stream count is well maintained.
999 */
1000static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
1001{
1002 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001003 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001004 fstrm->fconn->nb_streams--;
1005 if (!fstrm->id)
1006 fstrm->fconn->nb_reserved--;
1007 if (fstrm->cs) {
1008 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1009 fcgi_strm_notify_recv(fstrm);
1010 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001011 fstrm->state = FCGI_SS_CLOSED;
1012 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1013 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001014 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001015}
1016
1017/* Detaches a FCGI stream from its FCGI connection and releases it to the
1018 * fcgi_strm pool.
1019 */
1020static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1021{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001022 struct connection *conn = fstrm->fconn->conn;
1023
1024 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1025
Christopher Faulet99eff652019-08-11 23:11:30 +02001026 fcgi_strm_close(fstrm);
1027 eb32_delete(&fstrm->by_id);
1028 if (b_size(&fstrm->rxbuf)) {
1029 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001030 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001031 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001032 if (fstrm->subs)
1033 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001034 /* There's no need to explicitly call unsubscribe here, the only
1035 * reference left would be in the fconn send_list/fctl_list, and if
1036 * we're in it, we're getting out anyway
1037 */
1038 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001039 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001040 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001041
1042 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001043}
1044
1045/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1046 * stream tree. In case of error, nothing is added and NULL is returned. The
1047 * causes of errors can be any failed memory allocation. The caller is
1048 * responsible for checking if the connection may support an extra stream prior
1049 * to calling this function.
1050 */
1051static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1052{
1053 struct fcgi_strm *fstrm;
1054
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001055 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1056
Christopher Faulet99eff652019-08-11 23:11:30 +02001057 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +01001058 if (!fstrm) {
1059 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 +02001060 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +01001061 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001062
Willy Tarreau7aad7032020-01-16 17:20:57 +01001063 fstrm->shut_tl = tasklet_new();
1064 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001065 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 +02001066 pool_free(pool_head_fcgi_strm, fstrm);
1067 goto out;
1068 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001069 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001070 fstrm->shut_tl->process = fcgi_deferred_shut;
1071 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001072 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001073 fstrm->fconn = fconn;
1074 fstrm->cs = NULL;
1075 fstrm->flags = FCGI_SF_NONE;
1076 fstrm->proto_status = 0;
1077 fstrm->state = FCGI_SS_IDLE;
1078 fstrm->rxbuf = BUF_NULL;
1079
1080 h1m_init_res(&fstrm->h1m);
1081 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1082 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1083
1084 fstrm->by_id.key = fstrm->id = id;
1085 if (id > 0)
1086 fconn->max_id = id;
1087 else
1088 fconn->nb_reserved++;
1089
1090 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1091 fconn->nb_streams++;
1092 fconn->stream_cnt++;
1093
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001094 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001095 return fstrm;
1096
1097 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001098 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 +02001099 return NULL;
1100}
1101
1102/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1103 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1104 * highest possible stream ID was reached.
1105 */
1106static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1107 struct session *sess)
1108{
1109 struct fcgi_strm *fstrm = NULL;
1110
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001111 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1112 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001113 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 +02001114 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001116
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001117 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001118 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 +02001119 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001120 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001121
1122 /* Defer choosing the ID until we send the first message to create the stream */
1123 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001124 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001125 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 +02001126 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001127 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001128
1129 fstrm->cs = cs;
1130 fstrm->sess = sess;
1131 cs->ctx = fstrm;
1132 fconn->nb_cs++;
1133
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001134 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001135 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001136
1137 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001138 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 +02001139 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001140}
1141
1142/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1143 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1144 * automatically updated accordingly. If the stream is orphaned, it is
1145 * destroyed.
1146 */
1147static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1148{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149 struct fcgi_conn *fconn = fstrm->fconn;
1150
1151 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1152
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 if (!fstrm->cs) {
1154 /* this stream was already orphaned */
1155 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001156 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001157 return;
1158 }
1159
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001160 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001161 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001162 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001163 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 else if (fstrm->state == FCGI_SS_HLOC)
1166 fcgi_strm_close(fstrm);
1167 }
1168
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001169 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001170 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1171 if (fstrm->cs->flags & CS_FL_EOS)
1172 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001173
1174 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001175 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001176 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1177 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001178 }
1179
1180 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001181
1182 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001183}
1184
1185/* Wakes unassigned streams (ID == 0) attached to the connection. */
1186static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1187{
1188 struct eb32_node *node;
1189 struct fcgi_strm *fstrm;
1190
1191 node = eb32_lookup(&fconn->streams_by_id, 0);
1192 while (node) {
1193 fstrm = container_of(node, struct fcgi_strm, by_id);
1194 if (fstrm->id > 0)
1195 break;
1196 node = eb32_next(node);
1197 fcgi_strm_wake_one_stream(fstrm);
1198 }
1199}
1200
1201/* Wakes the streams attached to the connection, whose id is greater than <last>
1202 * or unassigned.
1203 */
1204static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1205{
1206 struct eb32_node *node;
1207 struct fcgi_strm *fstrm;
1208
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001209 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1210
Christopher Faulet99eff652019-08-11 23:11:30 +02001211 /* Wake all streams with ID > last */
1212 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1213 while (node) {
1214 fstrm = container_of(node, struct fcgi_strm, by_id);
1215 node = eb32_next(node);
1216 fcgi_strm_wake_one_stream(fstrm);
1217 }
1218 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001219
1220 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001221}
1222
1223static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1224 struct htx *htx, struct htx_sl *sl,
1225 struct fcgi_strm_params *params)
1226{
1227 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1228 struct ist p;
1229
1230 if (!sl)
1231 goto error;
1232
1233 if (!(params->mask & FCGI_SP_DOC_ROOT))
1234 params->docroot = fconn->app->docroot;
1235
1236 if (!(params->mask & FCGI_SP_REQ_METH)) {
1237 p = htx_sl_req_meth(sl);
1238 params->meth = ist2(b_tail(params->p), p.len);
1239 chunk_memcat(params->p, p.ptr, p.len);
1240 }
1241 if (!(params->mask & FCGI_SP_REQ_URI)) {
1242 p = htx_sl_req_uri(sl);
1243 params->uri = ist2(b_tail(params->p), p.len);
1244 chunk_memcat(params->p, p.ptr, p.len);
1245 }
1246 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1247 p = htx_sl_req_vsn(sl);
1248 params->vsn = ist2(b_tail(params->p), p.len);
1249 chunk_memcat(params->p, p.ptr, p.len);
1250 }
1251 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1252 char *end;
1253 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001254 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001255 port = get_host_port(cli_conn->dst);
1256 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1257 if (!end)
1258 goto error;
1259 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1260 params->p->data += params->srv_port.len;
1261 }
1262 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1263 /* If no Host header found, use the server address to fill
1264 * srv_name */
1265 if (!istlen(params->srv_name)) {
1266 char *ptr = NULL;
1267
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001268 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001269 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1270 ptr = b_tail(params->p);
1271 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001272 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001273 params->p->data += params->srv_name.len;
1274 }
1275 }
1276 }
1277 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1278 char *ptr = NULL;
1279
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001280 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001281 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1282 ptr = b_tail(params->p);
1283 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001284 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001285 params->p->data += params->rem_addr.len;
1286 }
1287 }
1288 if (!(params->mask & FCGI_SP_REM_PORT)) {
1289 char *end;
1290 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001291 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001292 port = get_host_port(cli_conn->src);
1293 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1294 if (!end)
1295 goto error;
1296 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1297 params->p->data += params->rem_port.len;
1298 }
1299 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1300 struct htx_blk *blk;
1301 enum htx_blk_type type;
1302 char *end;
1303 size_t len = 0;
1304
1305 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1306 type = htx_get_blk_type(blk);
1307
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001308 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001309 break;
1310 if (type == HTX_BLK_DATA)
1311 len += htx_get_blksz(blk);
1312 }
1313 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1314 if (!end)
1315 goto error;
1316 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1317 params->p->data += params->cont_len.len;
1318 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001319#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001320 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001321 if (cli_conn)
1322 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001323 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001324#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001325 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1326 /* one of scriptname, pathinfo or query_string is no set */
1327 struct ist path = http_get_path(params->uri);
1328 int len;
1329
Christopher Faulet99eff652019-08-11 23:11:30 +02001330 /* No scrit_name set but no valid path ==> error */
1331 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1332 goto error;
1333
Christopher Faulet99eff652019-08-11 23:11:30 +02001334 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001335 if (!(params->mask & FCGI_SP_REQ_QS)) {
1336 struct ist qs = istfind(path, '?');
1337
1338 /* Update the path length */
1339 path.len -= qs.len;
1340
1341 /* Set the query-string skipping the '?', if any */
1342 if (istlen(qs))
1343 params->qs = istnext(qs);
1344 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001345
1346 /* If the script_name is set, don't try to deduce the path_info
1347 * too. The opposite is not true.
1348 */
1349 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1350 params->mask |= FCGI_SP_PATH_INFO;
1351 goto end;
1352 }
1353
Christopher Faulet0f17a442020-07-23 15:44:37 +02001354 /* Decode the path. it must first be copied to keep the URI
1355 * untouched.
1356 */
1357 chunk_memcat(params->p, path.ptr, path.len);
1358 path.ptr = b_tail(params->p) - path.len;
1359 len = url_decode(ist0(path), 0);
1360 if (len < 0)
1361 goto error;
1362 path.len = len;
1363
Christopher Faulet99eff652019-08-11 23:11:30 +02001364 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001365 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001366
1367 /* If there is no regex to match the pathinfo, just to the last
1368 * part and see if the index must be used.
1369 */
1370 if (!fconn->app->pathinfo_re)
1371 goto check_index;
1372
Christopher Faulet28cb3662020-02-14 14:47:37 +01001373 /* If some special characters are found in the decoded path (\n
1374 * or \0), the PATH_INFO regex cannot match. This is theorically
1375 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001376 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001377 * case.
1378 */
1379 if (istchr(path, '\n') || istchr(path, '\0'))
1380 goto error;
1381
Christopher Faulet99eff652019-08-11 23:11:30 +02001382 /* The regex does not match, just to the last part and see if
1383 * the index must be used.
1384 */
1385 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1386 goto check_index;
1387
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001388 /* We must have at least 1 capture for the script name,
1389 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001390 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001391 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001392 goto check_index;
1393
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001394 /* Finally we can set the script_name and the path_info. The
1395 * path_info is set if not already defined, and if it was
1396 * captured
1397 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001398 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001399 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1400 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001401
1402 check_index:
1403 len = params->scriptname.len;
1404 /* the script_name if finished by a '/' so we can add the index
1405 * part, if any.
1406 */
1407 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1408 struct ist sn = params->scriptname;
1409
1410 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1411 chunk_memcat(params->p, sn.ptr, sn.len);
1412 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1413 }
1414 }
1415
1416 end:
1417 return 1;
1418 error:
1419 return 0;
1420}
1421
1422static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1423 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1424{
1425 struct fcgi_param p;
1426
1427 if (params->mask & flag)
1428 return 1;
1429
1430 chunk_reset(&trash);
1431
1432 switch (flag) {
1433 case FCGI_SP_CGI_GATEWAY:
1434 p.n = ist("GATEWAY_INTERFACE");
1435 p.v = ist("CGI/1.1");
1436 goto encode;
1437 case FCGI_SP_DOC_ROOT:
1438 p.n = ist("DOCUMENT_ROOT");
1439 p.v = params->docroot;
1440 goto encode;
1441 case FCGI_SP_SCRIPT_NAME:
1442 p.n = ist("SCRIPT_NAME");
1443 p.v = params->scriptname;
1444 goto encode;
1445 case FCGI_SP_PATH_INFO:
1446 p.n = ist("PATH_INFO");
1447 p.v = params->pathinfo;
1448 goto encode;
1449 case FCGI_SP_REQ_URI:
1450 p.n = ist("REQUEST_URI");
1451 p.v = params->uri;
1452 goto encode;
1453 case FCGI_SP_REQ_METH:
1454 p.n = ist("REQUEST_METHOD");
1455 p.v = params->meth;
1456 goto encode;
1457 case FCGI_SP_REQ_QS:
1458 p.n = ist("QUERY_STRING");
1459 p.v = params->qs;
1460 goto encode;
1461 case FCGI_SP_SRV_NAME:
1462 p.n = ist("SERVER_NAME");
1463 p.v = params->srv_name;
1464 goto encode;
1465 case FCGI_SP_SRV_PORT:
1466 p.n = ist("SERVER_PORT");
1467 p.v = params->srv_port;
1468 goto encode;
1469 case FCGI_SP_SRV_PROTO:
1470 p.n = ist("SERVER_PROTOCOL");
1471 p.v = params->vsn;
1472 goto encode;
1473 case FCGI_SP_REM_ADDR:
1474 p.n = ist("REMOTE_ADDR");
1475 p.v = params->rem_addr;
1476 goto encode;
1477 case FCGI_SP_REM_PORT:
1478 p.n = ist("REMOTE_PORT");
1479 p.v = params->rem_port;
1480 goto encode;
1481 case FCGI_SP_SCRIPT_FILE:
1482 p.n = ist("SCRIPT_FILENAME");
1483 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1484 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1485 p.v = ist2(b_head(&trash), b_data(&trash));
1486 goto encode;
1487 case FCGI_SP_PATH_TRANS:
1488 if (!istlen(params->pathinfo))
1489 goto skip;
1490 p.n = ist("PATH_TRANSLATED");
1491 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1492 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1493 p.v = ist2(b_head(&trash), b_data(&trash));
1494 goto encode;
1495 case FCGI_SP_CONT_LEN:
1496 p.n = ist("CONTENT_LENGTH");
1497 p.v = params->cont_len;
1498 goto encode;
1499 case FCGI_SP_HTTPS:
1500 if (!params->https)
1501 goto skip;
1502 p.n = ist("HTTPS");
1503 p.v = ist("on");
1504 goto encode;
1505 default:
1506 goto skip;
1507 }
1508
1509 encode:
1510 if (!istlen(p.v))
1511 goto skip;
1512 if (!fcgi_encode_param(outbuf, &p))
1513 return 0;
1514 skip:
1515 params->mask |= flag;
1516 return 1;
1517}
1518
1519/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1520 * anything. It is highly unexpected, but if the record is larger than a buffer
1521 * and cannot be encoded in one time, an error is triggered and the connection is
1522 * closed. GET_VALUES record cannot be split.
1523 */
1524static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1525{
1526 struct buffer outbuf;
1527 struct buffer *mbuf;
1528 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1529 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001530 int ret = 0;
1531
1532 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001533
1534 mbuf = br_tail(fconn->mbuf);
1535 retry:
1536 if (!fcgi_get_buf(fconn, mbuf)) {
1537 fconn->flags |= FCGI_CF_MUX_MALLOC;
1538 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001539 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1540 ret = 0;
1541 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001542 }
1543
1544 while (1) {
1545 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001546 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001547 break;
1548 realign_again:
1549 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1550 }
1551
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001552 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001553 goto full;
1554
1555 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1556 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001557 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1558 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001559
1560 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1561 * handled by HAProxy.
1562 */
1563 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1564 goto full;
1565
1566 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001567 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 +01001568 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001569 b_add(mbuf, outbuf.data);
1570 ret = 1;
1571
1572 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001573 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001574 return ret;
1575 full:
1576 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001577 if (!b_data(mbuf)) {
1578 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 +02001579 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001580 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001581
1582 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1583 goto retry;
1584 fconn->flags |= FCGI_CF_MUX_MFULL;
1585 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001586 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001587 ret = 0;
1588 goto end;
1589 fail:
1590 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001591 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1592 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1593 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001594}
1595
1596/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1597 * couldn't do anything. It is highly unexpected, but if the record is larger
1598 * than a buffer and cannot be decoded in one time, an error is triggered and
1599 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1600 */
1601static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1602{
1603 struct buffer inbuf;
1604 struct buffer *dbuf;
1605 size_t offset;
1606
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001607 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1608
Christopher Faulet99eff652019-08-11 23:11:30 +02001609 dbuf = &fconn->dbuf;
1610
1611 /* Record too large to be fully decoded */
1612 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1613 goto fail;
1614
1615 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001616 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1617 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001618 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001619 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001620
1621 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1622 /* Realign the dmux buffer if the record wraps. It is unexpected
1623 * at this stage because it should be the first record received
1624 * from the FCGI application.
1625 */
1626 b_slow_realign(dbuf, trash.area, 0);
1627 }
1628
1629 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1630
1631 for (offset = 0; offset < b_data(&inbuf); ) {
1632 struct fcgi_param p;
1633 size_t ret;
1634
1635 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1636 if (!ret) {
1637 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001638 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 +02001639 goto fail;
1640 }
1641 offset += ret;
1642
1643 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001644 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001645 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 +02001646 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001647 }
1648 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001649 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 +02001650 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001651 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001652 }
1653 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1654 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001655 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 +02001656 }
1657 /*
1658 * Ignore all other params
1659 */
1660 }
1661
1662 /* Reset the number of concurrent streams supported if the FCGI
1663 * application does not support connection multiplexing
1664 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001665 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001666 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001667 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1668 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001669
1670 /* We must be sure to have read exactly the announced record length, no
1671 * more no less
1672 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001673 if (offset != fconn->drl) {
1674 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 +02001675 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001676 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001677
Willy Tarreau022e5e52020-09-10 09:33:15 +02001678 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 +02001679 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1680 fconn->drl = 0;
1681 fconn->drp = 0;
1682 fconn->state = FCGI_CS_RECORD_H;
1683 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001684 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1685 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 return 1;
1687 fail:
1688 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001689 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1690 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 +02001691 return 0;
1692}
1693
1694/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1695 * excluded, as the streams which already received the end-of-stream. It returns
1696 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1697 */
1698static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1699{
1700 struct eb32_node *node;
1701 struct fcgi_strm *fstrm;
1702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001703 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1704
Christopher Faulet99eff652019-08-11 23:11:30 +02001705 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1706 while (node) {
1707 fstrm = container_of(node, struct fcgi_strm, by_id);
1708 node = eb32_next(node);
1709 if (fstrm->state != FCGI_SS_CLOSED &&
1710 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1711 !fcgi_strm_send_abort(fconn, fstrm))
1712 return 0;
1713 }
1714 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001715 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1716 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001717 return 1;
1718}
1719
1720/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1721 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1722 * space to proceed. It is small enough to be encoded in an empty buffer.
1723 */
1724static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1725{
1726 struct buffer outbuf;
1727 struct buffer *mbuf;
1728 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1729 int ret;
1730
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001731 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1732
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 mbuf = br_tail(fconn->mbuf);
1734 retry:
1735 if (!fcgi_get_buf(fconn, mbuf)) {
1736 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001737 fstrm->flags |= FCGI_SF_BLK_MROOM;
1738 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1739 ret = 0;
1740 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001741 }
1742
1743 while (1) {
1744 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001745 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 break;
1747 realign_again:
1748 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1749 }
1750
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001751 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001752 goto full;
1753
1754 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1755 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001756 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001757 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001758 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001759
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001760 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1761 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001762 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001763 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001764 if (!fcgi_encode_begin_request(&outbuf, &rec))
1765 goto full;
1766
1767 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001768 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 +02001769 b_add(mbuf, outbuf.data);
1770 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1771 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001772 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001773 ret = 1;
1774
1775 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001776 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 return ret;
1778 full:
1779 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1780 goto retry;
1781 fconn->flags |= FCGI_CF_MUX_MFULL;
1782 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001783 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 +02001784 ret = 0;
1785 goto end;
1786}
1787
1788/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1789 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1790 * space to proceed. It is small enough to be encoded in an empty buffer.
1791 */
1792static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1793 enum fcgi_record_type rtype)
1794{
1795 struct buffer outbuf;
1796 struct buffer *mbuf;
1797 int ret;
1798
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001799 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001800 mbuf = br_tail(fconn->mbuf);
1801 retry:
1802 if (!fcgi_get_buf(fconn, mbuf)) {
1803 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001804 fstrm->flags |= FCGI_SF_BLK_MROOM;
1805 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1806 ret = 0;
1807 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001808 }
1809
1810 while (1) {
1811 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001812 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001813 break;
1814 realign_again:
1815 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1816 }
1817
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001818 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001819 goto full;
1820
1821 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1822 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001823 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001824 outbuf.area[1] = rtype;
1825 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001826 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001827
1828 /* commit the record */
1829 b_add(mbuf, outbuf.data);
1830 ret = 1;
1831
1832 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001834 return ret;
1835 full:
1836 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1837 goto retry;
1838 fconn->flags |= FCGI_CF_MUX_MFULL;
1839 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001840 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 +02001841 ret = 0;
1842 goto end;
1843}
1844
1845
1846/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1847 * marks the end of params.
1848 */
1849static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1850{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001851 int ret;
1852
1853 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1854 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001855 if (ret) {
1856 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001857 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 +01001858 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001859 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001860}
1861
1862/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1863 * marks the end of input. On success, all the request was successfully sent.
1864 */
1865static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1866{
1867 int ret;
1868
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001869 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001870 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001871 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001872 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001873 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 +02001874 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1875 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1876 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001877 return ret;
1878}
1879
1880/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1881 * stops the request processing.
1882 */
1883static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1884{
1885 int ret;
1886
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001887 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001888 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001889 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001890 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001891 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 +02001892 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1893 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1894 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001895 return ret;
1896}
1897
1898/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1899 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1900 * several records are sent. However, a K/V param cannot be split between 2
1901 * records.
1902 */
1903static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1904 struct htx *htx)
1905{
1906 struct buffer outbuf;
1907 struct buffer *mbuf;
1908 struct htx_blk *blk;
1909 struct htx_sl *sl = NULL;
1910 struct fcgi_strm_params params;
1911 size_t total = 0;
1912
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001913 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1914
Christopher Faulet99eff652019-08-11 23:11:30 +02001915 memset(&params, 0, sizeof(params));
1916 params.p = get_trash_chunk();
1917
1918 mbuf = br_tail(fconn->mbuf);
1919 retry:
1920 if (!fcgi_get_buf(fconn, mbuf)) {
1921 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001922 fstrm->flags |= FCGI_SF_BLK_MROOM;
1923 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1924 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001925 }
1926
1927 while (1) {
1928 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001929 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001930 break;
1931 realign_again:
1932 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1933 }
1934
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001935 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001936 goto full;
1937
1938 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1939 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001940 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001941 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001942 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001943
1944 blk = htx_get_head_blk(htx);
1945 while (blk) {
1946 enum htx_blk_type type;
1947 uint32_t size = htx_get_blksz(blk);
1948 struct fcgi_param p;
1949
1950 type = htx_get_blk_type(blk);
1951 switch (type) {
1952 case HTX_BLK_REQ_SL:
1953 sl = htx_get_blk_ptr(htx, blk);
1954 if (sl->info.req.meth == HTTP_METH_HEAD)
1955 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1956 if (sl->flags & HTX_SL_F_VER_11)
1957 fstrm->h1m.flags |= H1_MF_VER_11;
1958 break;
1959
1960 case HTX_BLK_HDR:
1961 p.n = htx_get_blk_name(htx, blk);
1962 p.v = htx_get_blk_value(htx, blk);
1963
1964 if (istmatch(p.n, ist(":fcgi-"))) {
1965 p.n.ptr += 6;
1966 p.n.len -= 6;
1967 if (isteq(p.n, ist("gateway_interface")))
1968 params.mask |= FCGI_SP_CGI_GATEWAY;
1969 else if (isteq(p.n, ist("document_root"))) {
1970 params.mask |= FCGI_SP_DOC_ROOT;
1971 params.docroot = p.v;
1972 }
1973 else if (isteq(p.n, ist("script_name"))) {
1974 params.mask |= FCGI_SP_SCRIPT_NAME;
1975 params.scriptname = p.v;
1976 }
1977 else if (isteq(p.n, ist("path_info"))) {
1978 params.mask |= FCGI_SP_PATH_INFO;
1979 params.pathinfo = p.v;
1980 }
1981 else if (isteq(p.n, ist("request_uri"))) {
1982 params.mask |= FCGI_SP_REQ_URI;
1983 params.uri = p.v;
1984 }
1985 else if (isteq(p.n, ist("request_meth")))
1986 params.mask |= FCGI_SP_REQ_METH;
1987 else if (isteq(p.n, ist("query_string")))
1988 params.mask |= FCGI_SP_REQ_QS;
1989 else if (isteq(p.n, ist("server_name")))
1990 params.mask |= FCGI_SP_SRV_NAME;
1991 else if (isteq(p.n, ist("server_port")))
1992 params.mask |= FCGI_SP_SRV_PORT;
1993 else if (isteq(p.n, ist("server_protocol")))
1994 params.mask |= FCGI_SP_SRV_PROTO;
1995 else if (isteq(p.n, ist("remote_addr")))
1996 params.mask |= FCGI_SP_REM_ADDR;
1997 else if (isteq(p.n, ist("remote_port")))
1998 params.mask |= FCGI_SP_REM_PORT;
1999 else if (isteq(p.n, ist("script_filename")))
2000 params.mask |= FCGI_SP_SCRIPT_FILE;
2001 else if (isteq(p.n, ist("path_translated")))
2002 params.mask |= FCGI_SP_PATH_TRANS;
2003 else if (isteq(p.n, ist("https")))
2004 params.mask |= FCGI_SP_HTTPS;
2005 }
2006 else if (isteq(p.n, ist("content-length"))) {
2007 p.n = ist("CONTENT_LENGTH");
2008 params.mask |= FCGI_SP_CONT_LEN;
2009 }
2010 else if (isteq(p.n, ist("content-type")))
2011 p.n = ist("CONTENT_TYPE");
2012 else {
2013 if (isteq(p.n, ist("host")))
2014 params.srv_name = p.v;
2015
Christopher Faulet67d58092019-10-02 10:51:38 +02002016 /* Skip header if same name is used to add the server name */
2017 if (fconn->proxy->server_id_hdr_name &&
2018 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2019 break;
2020
Christopher Faulet99eff652019-08-11 23:11:30 +02002021 memcpy(trash.area, "http_", 5);
2022 memcpy(trash.area+5, p.n.ptr, p.n.len);
2023 p.n = ist2(trash.area, p.n.len+5);
2024 }
2025
2026 if (!fcgi_encode_param(&outbuf, &p)) {
2027 if (b_space_wraps(mbuf))
2028 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002029 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002030 goto full;
2031 goto done;
2032 }
2033 break;
2034
2035 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002036 if (fconn->proxy->server_id_hdr_name) {
2037 struct server *srv = objt_server(fconn->conn->target);
2038
2039 if (!srv)
2040 goto done;
2041
2042 memcpy(trash.area, "http_", 5);
2043 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2044 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2045 p.v = ist(srv->id);
2046
2047 if (!fcgi_encode_param(&outbuf, &p)) {
2048 if (b_space_wraps(mbuf))
2049 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002050 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002051 goto full;
2052 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002053 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002054 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002055 goto done;
2056
2057 default:
2058 break;
2059 }
2060 total += size;
2061 blk = htx_remove_blk(htx, blk);
2062 }
2063
2064 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002065 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2066 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002067 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002068 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002069
2070 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2071 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2072 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2073 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2074 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2075 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2076 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2077 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2078 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2079 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2080 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2081 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2082 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2083 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2084 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002085 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2086 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002087 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002088 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002089
2090 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002091 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});
2092 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002093 b_add(mbuf, outbuf.data);
2094
2095 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002096 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002097 return total;
2098 full:
2099 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2100 goto retry;
2101 fconn->flags |= FCGI_CF_MUX_MFULL;
2102 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002103 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 +02002104 if (total)
2105 goto error;
2106 goto end;
2107
2108 error:
2109 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002110 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 +02002111 fcgi_strm_error(fstrm);
2112 goto end;
2113}
2114
2115/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2116 * anything. STDIN records contain the request body.
2117 */
2118static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2119 struct htx *htx, size_t count, struct buffer *buf)
2120{
2121 struct buffer outbuf;
2122 struct buffer *mbuf;
2123 struct htx_blk *blk;
2124 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002125 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002126 size_t total = 0;
2127
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002128 extra_bytes = 0;
2129
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002130 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002131 if (!count)
2132 goto end;
2133
2134 mbuf = br_tail(fconn->mbuf);
2135 retry:
2136 if (!fcgi_get_buf(fconn, mbuf)) {
2137 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002138 fstrm->flags |= FCGI_SF_BLK_MROOM;
2139 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2140 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002141 }
2142
2143 /* Perform some optimizations to reduce the number of buffer copies.
2144 * First, if the mux's buffer is empty and the htx area contains exactly
2145 * one data block of the same size as the requested count, and this
2146 * count fits within the record size, then it's possible to simply swap
2147 * the caller's buffer with the mux's output buffer and adjust offsets
2148 * and length to match the entire DATA HTX block in the middle. In this
2149 * case we perform a true zero-copy operation from end-to-end. This is
2150 * the situation that happens all the time with large files. Second, if
2151 * this is not possible, but the mux's output buffer is empty, we still
2152 * have an opportunity to avoid the copy to the intermediary buffer, by
2153 * making the intermediary buffer's area point to the output buffer's
2154 * area. In this case we want to skip the HTX header to make sure that
2155 * copies remain aligned and that this operation remains possible all
2156 * the time. This goes for headers, data blocks and any data extracted
2157 * from the HTX blocks.
2158 */
2159 blk = htx_get_head_blk(htx);
2160 if (!blk)
2161 goto end;
2162 type = htx_get_blk_type(blk);
2163 size = htx_get_blksz(blk);
2164 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2165 void *old_area = mbuf->area;
2166
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002167 /* Last block of the message: Reserve the size for the empty stdin record */
2168 if (htx->flags & HTX_FL_EOM)
2169 extra_bytes = FCGI_RECORD_HEADER_SZ;
2170
Christopher Faulet99eff652019-08-11 23:11:30 +02002171 if (b_data(mbuf)) {
2172 /* Too bad there are data left there. We're willing to memcpy/memmove
2173 * up to 1/4 of the buffer, which means that it's OK to copy a large
2174 * record into a buffer containing few data if it needs to be realigned,
2175 * and that it's also OK to copy few data without realigning. Otherwise
2176 * we'll pretend the mbuf is full and wait for it to become empty.
2177 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002178 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002179 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002180 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002181 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002182 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002183 }
2184
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002185 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 +02002186 /* map a FCGI record to the HTX block so that we can put the
2187 * record header there.
2188 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002189 *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 +02002190 outbuf.area = b_head(mbuf);
2191
2192 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002193 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002194 fcgi_set_record_id(outbuf.area, fstrm->id);
2195 fcgi_set_record_size(outbuf.area, size);
2196
2197 /* and exchange with our old area */
2198 buf->area = old_area;
2199 buf->data = buf->head = 0;
2200 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002201
2202 htx = (struct htx *)buf->area;
2203 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002204 goto end;
2205 }
2206
2207 copy:
2208 while (1) {
2209 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002210 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002211 break;
2212 realign_again:
2213 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2214 }
2215
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002217 goto full;
2218
2219 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2220 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002221 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002222 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002223 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002224
2225 blk = htx_get_head_blk(htx);
2226 while (blk && count) {
2227 enum htx_blk_type type = htx_get_blk_type(blk);
2228 uint32_t size = htx_get_blksz(blk);
2229 struct ist v;
2230
2231 switch (type) {
2232 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 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 +02002234 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002235
2236 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2237 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2238
2239 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002240 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002241 extra_bytes = 0;
2242 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002243
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002244 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002245 /* It doesn't fit at once. If it at least fits once split and
2246 * the amount of data to move is low, let's defragment the
2247 * buffer now.
2248 */
2249 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002250 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002251 b_data(mbuf) <= MAX_DATA_REALIGN)
2252 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002253 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002254 }
2255 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002256 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002257 goto full;
2258 goto done;
2259 }
2260 if (v.len != size) {
2261 total += v.len;
2262 count -= v.len;
2263 htx_cut_data_blk(htx, blk, v.len);
2264 goto done;
2265 }
2266 break;
2267
Christopher Faulet99eff652019-08-11 23:11:30 +02002268 default:
2269 break;
2270 }
2271 total += size;
2272 count -= size;
2273 blk = htx_remove_blk(htx, blk);
2274 }
2275
2276 done:
2277 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002278 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});
2279 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002280 b_add(mbuf, outbuf.data);
2281
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002282 /* Send the empty stding here to finish the message */
2283 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2284 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2285 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2286 /* bytes already reserved for this record. It should not fail */
2287 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002288 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 +01002289 fcgi_strm_error(fstrm);
2290 }
2291 }
2292
Christopher Faulet99eff652019-08-11 23:11:30 +02002293 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002294 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002295 return total;
2296 full:
2297 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2298 goto retry;
2299 fconn->flags |= FCGI_CF_MUX_MFULL;
2300 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002301 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 +02002302 goto end;
2303}
2304
2305/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2306 * anything. STDOUT records contain the entire response. All the content is
2307 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2308 */
2309static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2310{
2311 struct buffer *dbuf;
2312 size_t ret;
2313 size_t max;
2314
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002315 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2316
Christopher Faulet99eff652019-08-11 23:11:30 +02002317 dbuf = &fconn->dbuf;
2318
2319 /* Only padding remains */
2320 if (fconn->state == FCGI_CS_RECORD_P)
2321 goto end_transfer;
2322
2323 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2324 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2325 buf_room_for_htx_data(dbuf))
2326 goto fail; // incomplete record
2327
2328 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2329 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2331 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002332 }
2333
2334 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2335 max = buf_room_for_htx_data(&fstrm->rxbuf);
2336 if (!b_data(&fstrm->rxbuf))
2337 fstrm->rxbuf.head = sizeof(struct htx);
2338 if (max > fconn->drl)
2339 max = fconn->drl;
2340
2341 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2342 if (!ret)
2343 goto fail;
2344 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002345 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2346 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 +02002347
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002348 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002349 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002350 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2351 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002352
2353 if (fconn->drl)
2354 goto fail;
2355
2356 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002357 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002358 fconn->drl += fconn->drp;
2359 fconn->drp = 0;
2360 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2361 b_del(&fconn->dbuf, ret);
2362 fconn->drl -= ret;
2363 if (fconn->drl)
2364 goto fail;
2365
2366 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002367 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2368 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002369 return 1;
2370 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002371 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 +02002372 return 0;
2373}
2374
2375
2376/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2377 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002378 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002379 */
2380static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2381{
2382 int ret;
2383
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002384 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2385
Christopher Faulet99eff652019-08-11 23:11:30 +02002386 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002387 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002388 fconn->drl += fconn->drp;
2389 fconn->drp = 0;
2390 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2391 b_del(&fconn->dbuf, ret);
2392 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002393 if (fconn->drl) {
2394 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 +02002395 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002397 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002398 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002399 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 +02002400 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);
2401 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002402 return 1;
2403}
2404
2405/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2406 * anything.
2407 */
2408static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2409{
2410 struct buffer *dbuf;
2411 struct buffer tag;
2412 size_t ret;
2413
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002414 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002415 dbuf = &fconn->dbuf;
2416
2417 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002418 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002419 goto end_transfer;
2420
2421 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2422 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2423 buf_room_for_htx_data(dbuf))
2424 goto fail; // incomplete record
2425
2426 chunk_reset(&trash);
2427 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2428 if (!ret)
2429 goto fail;
2430 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002431 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 +02002432
2433 trash.area[ret] = '\n';
2434 trash.area[ret+1] = '\0';
2435 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002436 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002437
2438 if (fconn->drl)
2439 goto fail;
2440
2441 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002442 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002443 fconn->drl += fconn->drp;
2444 fconn->drp = 0;
2445 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2446 b_del(&fconn->dbuf, ret);
2447 fconn->drl -= ret;
2448 if (fconn->drl)
2449 goto fail;
2450 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002451 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2452 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002453 return 1;
2454 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002455 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 +02002456 return 0;
2457}
2458
2459/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2460 * anything. If the empty STDOUT record is not already received, this one marks
2461 * the end of the response. It is highly unexpected, but if the record is larger
2462 * than a buffer and cannot be decoded in one time, an error is triggered and
2463 * the connection is closed. END_REQUEST record cannot be split.
2464 */
2465static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2466{
2467 struct buffer inbuf;
2468 struct buffer *dbuf;
2469 struct fcgi_end_request endreq;
2470
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002472 dbuf = &fconn->dbuf;
2473
2474 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002475 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2476 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 +02002477 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002478 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002479
2480 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2482 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002483 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002484 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002485
2486 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2487 /* Realign the dmux buffer if the record wraps. It is unexpected
2488 * at this stage because it should be the first record received
2489 * from the FCGI application.
2490 */
2491 b_slow_realign(dbuf, trash.area, 0);
2492 }
2493
2494 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2495
Christopher Faulet73518be2021-01-27 12:06:54 +01002496 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2497 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 +02002498 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002499 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002500
2501 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002502 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 +02002503 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 +02002504 fstrm->proto_status = endreq.errcode;
2505 fcgi_strm_close(fstrm);
2506
2507 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2508 fconn->drl = 0;
2509 fconn->drp = 0;
2510 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002511 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2512 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002513 return 1;
2514
2515 fail:
2516 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002517 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 +02002518 return 0;
2519}
2520
2521/* process Rx records to be demultiplexed */
2522static void fcgi_process_demux(struct fcgi_conn *fconn)
2523{
2524 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2525 struct fcgi_header hdr;
2526 int ret;
2527
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002528 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2529
Christopher Faulet99eff652019-08-11 23:11:30 +02002530 if (fconn->state == FCGI_CS_CLOSED)
2531 return;
2532
2533 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002534 if (fconn->state == FCGI_CS_INIT) {
2535 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2536 return;
2537 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002538 if (fconn->state == FCGI_CS_SETTINGS) {
2539 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002540 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002541 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002542 if (!ret) {
2543 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 +02002544 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002545 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002546 b_del(&fconn->dbuf, ret);
2547
2548 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2549 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002550 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 +02002551 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 +02002552 goto fail;
2553 }
2554 goto new_record;
2555 }
2556 }
2557
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002558 /* process as many incoming records as possible below */
2559 while (1) {
2560 if (!b_data(&fconn->dbuf)) {
2561 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2562 break;
2563 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002564
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002565 if (fconn->state == FCGI_CS_CLOSED) {
2566 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002567 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002568 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002569
2570 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002571 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002572 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2573 if (!ret)
2574 break;
2575 b_del(&fconn->dbuf, ret);
2576
2577 new_record:
2578 fconn->dsi = hdr.id;
2579 fconn->drt = hdr.type;
2580 fconn->drl = hdr.len;
2581 fconn->drp = hdr.padding;
2582 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002583 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 +02002584 }
2585
2586 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2587 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2588
2589 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2590 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002591 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002592 fstrm->state == FCGI_SS_CLOSED ||
2593 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2594 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2595 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 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 +02002597 fstrm->cs->flags |= CS_FL_RCV_MORE;
2598 fcgi_strm_notify_recv(fstrm);
2599 }
2600 fstrm = tmp_fstrm;
2601
2602 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2603 /* ignore all record for closed streams */
2604 goto ignore_record;
2605 }
2606 if (fstrm->state == FCGI_SS_IDLE) {
2607 /* ignore all record for unknown streams */
2608 goto ignore_record;
2609 }
2610
2611 switch (fconn->drt) {
2612 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002613 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 +02002614 ret = fcgi_conn_handle_values_result(fconn);
2615 break;
2616
2617 case FCGI_STDOUT:
2618 if (fstrm->flags & FCGI_SF_ES_RCVD)
2619 goto ignore_record;
2620
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002621 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002622 if (fconn->drl)
2623 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2624 else
2625 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2626 break;
2627
2628 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002629 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002630 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2631 break;
2632
2633 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 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 +02002635 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2636 break;
2637
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002639 default:
2640 ignore_record:
2641 /* drop records that we ignore. They may be
2642 * larger than the buffer so we drain all of
2643 * their contents until we reach the end.
2644 */
2645 fconn->state = FCGI_CS_RECORD_P;
2646 fconn->drl += fconn->drp;
2647 fconn->drp = 0;
2648 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002649 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002650 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002651 b_del(&fconn->dbuf, ret);
2652 fconn->drl -= ret;
2653 ret = (fconn->drl == 0);
2654 }
2655
2656 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002657 if (ret <= 0) {
2658 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002659 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002660 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002661
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002662 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002663 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002664 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2665 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002666 }
2667
2668 fail:
2669 /* we can go here on missing data, blocked response or error */
2670 if (fstrm && fstrm->cs &&
2671 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002672 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002673 fstrm->state == FCGI_SS_CLOSED ||
2674 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2675 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2676 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 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 +02002678 fstrm->cs->flags |= CS_FL_RCV_MORE;
2679 fcgi_strm_notify_recv(fstrm);
2680 }
2681
2682 fcgi_conn_restart_reading(fconn, 0);
2683}
2684
2685/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2686 * the end.
2687 */
2688static int fcgi_process_mux(struct fcgi_conn *fconn)
2689{
2690 struct fcgi_strm *fstrm, *fstrm_back;
2691
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002692 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2693
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2695 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2696 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2697 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002698 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 +02002699 fcgi_wake_unassigned_streams(fconn);
2700 goto mux;
2701 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002702 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002703 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2704 goto fail;
2705 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002706 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 +02002707 }
2708 /* need to wait for the other side */
2709 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002710 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002711 }
2712
2713 mux:
2714 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2715 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2716 break;
2717
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002718 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002719 continue;
2720
Willy Tarreau7aad7032020-01-16 17:20:57 +01002721 /* If the sender changed his mind and unsubscribed, let's just
2722 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002723 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002724 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2725 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 LIST_DEL_INIT(&fstrm->send_list);
2727 continue;
2728 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002729
2730 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002731 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2732 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002733 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002734 tasklet_wakeup(fstrm->subs->tasklet);
2735 fstrm->subs->events &= ~SUB_RETRY_SEND;
2736 if (!fstrm->subs->events)
2737 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002738 } else {
2739 /* it's the shut request that was queued */
2740 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2741 tasklet_wakeup(fstrm->shut_tl);
2742 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 }
2744
2745 fail:
2746 if (fconn->state == FCGI_CS_CLOSED) {
2747 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2748 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2750 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002752 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002753 }
2754 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002755
2756 done:
2757 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002758 return 1;
2759}
2760
2761
2762/* Attempt to read data, and subscribe if none available.
2763 * The function returns 1 if data has been received, otherwise zero.
2764 */
2765static int fcgi_recv(struct fcgi_conn *fconn)
2766{
2767 struct connection *conn = fconn->conn;
2768 struct buffer *buf;
2769 int max;
2770 size_t ret;
2771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002772 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2773
2774 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2775 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002776 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002778
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002779 if (!fcgi_recv_allowed(fconn)) {
2780 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002781 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002782 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002783
2784 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2785 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002786 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002787 fconn->flags |= FCGI_CF_DEM_DALLOC;
2788 return 0;
2789 }
2790
Christopher Faulet99eff652019-08-11 23:11:30 +02002791 if (!b_data(buf)) {
2792 /* try to pre-align the buffer like the
2793 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002794 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002795 * HTX block to alias it upon recv. We cannot use the
2796 * head because rcv_buf() will realign the buffer if
2797 * it's empty. Thus we cheat and pretend we already
2798 * have a few bytes there.
2799 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002800 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2801 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002802 }
2803 else
2804 max = buf_room_for_htx_data(buf);
2805
2806 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2807
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002808 if (max && !ret && fcgi_recv_allowed(fconn)) {
2809 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002810 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002811 }
2812 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002813 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002814
2815 if (!b_data(buf)) {
2816 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002817 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002818 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2819 }
2820
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002821 if (ret == max) {
2822 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002823 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002824 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002825
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002826 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002827 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2828}
2829
2830
2831/* Try to send data if possible.
2832 * The function returns 1 if data have been sent, otherwise zero.
2833 */
2834static int fcgi_send(struct fcgi_conn *fconn)
2835{
2836 struct connection *conn = fconn->conn;
2837 int done;
2838 int sent = 0;
2839
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002840 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2841
2842 if (conn->flags & CO_FL_ERROR) {
2843 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002844 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002845 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002846
2847
Willy Tarreau911db9b2020-01-23 16:27:54 +01002848 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002849 /* a handshake was requested */
2850 goto schedule;
2851 }
2852
2853 /* This loop is quite simple : it tries to fill as much as it can from
2854 * pending streams into the existing buffer until it's reportedly full
2855 * or the end of send requests is reached. Then it tries to send this
2856 * buffer's contents out, marks it not full if at least one byte could
2857 * be sent, and tries again.
2858 *
2859 * The snd_buf() function normally takes a "flags" argument which may
2860 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2861 * data immediately comes and CO_SFL_STREAMER to indicate that the
2862 * connection is streaming lots of data (used to increase TLS record
2863 * size at the expense of latency). The former can be sent any time
2864 * there's a buffer full flag, as it indicates at least one stream
2865 * attempted to send and failed so there are pending data. An
2866 * alternative would be to set it as long as there's an active stream
2867 * but that would be problematic for ACKs until we have an absolute
2868 * guarantee that all waiters have at least one byte to send. The
2869 * latter should possibly not be set for now.
2870 */
2871
2872 done = 0;
2873 while (!done) {
2874 unsigned int flags = 0;
2875 unsigned int released = 0;
2876 struct buffer *buf;
2877
2878 /* fill as much as we can into the current buffer */
2879 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2880 done = fcgi_process_mux(fconn);
2881
2882 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2883 done = 1; // we won't go further without extra buffers
2884
2885 if (conn->flags & CO_FL_ERROR)
2886 break;
2887
2888 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2889 flags |= CO_SFL_MSG_MORE;
2890
2891 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2892 if (b_data(buf)) {
2893 int ret;
2894
2895 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2896 if (!ret) {
2897 done = 1;
2898 break;
2899 }
2900 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002901 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002902 b_del(buf, ret);
2903 if (b_data(buf)) {
2904 done = 1;
2905 break;
2906 }
2907 }
2908 b_free(buf);
2909 released++;
2910 }
2911
2912 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002913 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002914
2915 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002916 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2917 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002918 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2919 }
2920
2921 if (conn->flags & CO_FL_SOCK_WR_SH) {
2922 /* output closed, nothing to send, clear the buffer to release it */
2923 b_reset(br_tail(fconn->mbuf));
2924 }
2925 /* We're not full anymore, so we can wake any task that are waiting
2926 * for us.
2927 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002928 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002929 struct fcgi_strm *fstrm;
2930
2931 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2932 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2933 break;
2934
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002935 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002936 continue;
2937
Willy Tarreau7aad7032020-01-16 17:20:57 +01002938 /* If the sender changed his mind and unsubscribed, let's just
2939 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002940 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002941 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2942 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002943 LIST_DEL_INIT(&fstrm->send_list);
2944 continue;
2945 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002946
2947 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002948 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002949 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002950 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002951 tasklet_wakeup(fstrm->subs->tasklet);
2952 fstrm->subs->events &= ~SUB_RETRY_SEND;
2953 if (!fstrm->subs->events)
2954 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002955 } else {
2956 /* it's the shut request that was queued */
2957 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2958 tasklet_wakeup(fstrm->shut_tl);
2959 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002960 }
2961 }
2962 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002963 if (!br_data(fconn->mbuf)) {
2964 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002965 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002966 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002967schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002968 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2969 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002970 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002971 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002972
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002973 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002974 return sent;
2975}
2976
2977/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002978struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02002979{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002980 struct connection *conn;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002981 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002982 struct tasklet *tl = (struct tasklet *)t;
2983 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002984 int ret = 0;
2985
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002986 if (state & TASK_F_USR1) {
2987 /* the tasklet was idling on an idle connection, it might have
2988 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002989 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002990 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2991 if (tl->context == NULL) {
2992 /* The connection has been taken over by another thread,
2993 * we're no longer responsible for it, so just free the
2994 * tasklet, and do nothing.
2995 */
2996 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2997 tasklet_free(tl);
2998 return NULL;
2999 }
3000 conn = fconn->conn;
3001 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003002
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003003 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3004 if (conn_in_list)
3005 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003006
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003007 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3008 } else {
3009 /* we're certain the connection was not in an idle list */
3010 conn = fconn->conn;
3011 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
3012 conn_in_list = 0;
3013 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003014
Christopher Faulet99eff652019-08-11 23:11:30 +02003015 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3016 ret = fcgi_send(fconn);
3017 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3018 ret |= fcgi_recv(fconn);
3019 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003020 ret = fcgi_process(fconn);
3021
3022 /* If we were in an idle list, we want to add it back into it,
3023 * unless fcgi_process() returned -1, which mean it has destroyed
3024 * the connection (testing !ret is enough, if fcgi_process() wasn't
3025 * called then ret will be 0 anyway.
3026 */
Willy Tarreau74163142021-03-13 11:30:19 +01003027 if (ret < 0)
3028 t = NULL;
3029
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003030 if (!ret && conn_in_list) {
3031 struct server *srv = objt_server(conn->target);
3032
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003033 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003034 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003035 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003036 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003037 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003038 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003039 }
Willy Tarreau74163142021-03-13 11:30:19 +01003040 return t;
Christopher Faulet99eff652019-08-11 23:11:30 +02003041}
3042
3043/* callback called on any event by the connection handler.
3044 * It applies changes and returns zero, or < 0 if it wants immediate
3045 * destruction of the connection (which normally doesn not happen in FCGI).
3046 */
3047static int fcgi_process(struct fcgi_conn *fconn)
3048{
3049 struct connection *conn = fconn->conn;
3050
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003051 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3052
Christopher Faulet99eff652019-08-11 23:11:30 +02003053 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3054 fcgi_process_demux(fconn);
3055
3056 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3057 b_reset(&fconn->dbuf);
3058
3059 if (buf_room_for_htx_data(&fconn->dbuf))
3060 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3061 }
3062 fcgi_send(fconn);
3063
Willy Tarreauc3914d42020-09-24 08:39:22 +02003064 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003065 /* frontend is stopping, reload likely in progress, let's try
3066 * to announce a graceful shutdown if not yet done. We don't
3067 * care if it fails, it will be tried again later.
3068 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003069 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 +02003070 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3071 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3072 fcgi_conn_send_aborts(fconn);
3073 }
3074 }
3075
3076 /*
3077 * If we received early data, and the handshake is done, wake
3078 * any stream that was waiting for it.
3079 */
3080 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003081 (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 +02003082 struct eb32_node *node;
3083 struct fcgi_strm *fstrm;
3084
3085 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3086 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3087
3088 while (node) {
3089 fstrm = container_of(node, struct fcgi_strm, by_id);
3090 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3091 fcgi_strm_notify_recv(fstrm);
3092 node = eb32_next(node);
3093 }
3094 }
3095
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003096 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003097 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3098 eb_is_empty(&fconn->streams_by_id)) {
3099 fcgi_wake_some_streams(fconn, 0);
3100
3101 if (eb_is_empty(&fconn->streams_by_id)) {
3102 /* no more stream, kill the connection now */
3103 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003104 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003105 return -1;
3106 }
3107 }
3108
3109 if (!b_data(&fconn->dbuf))
3110 fcgi_release_buf(fconn, &fconn->dbuf);
3111
3112 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3113 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3114 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3115 fcgi_release_mbuf(fconn);
3116
3117 if (fconn->task) {
3118 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3119 task_queue(fconn->task);
3120 }
3121
3122 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003123 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003124 return 0;
3125}
3126
3127
3128/* wake-up function called by the connection layer (mux_ops.wake) */
3129static int fcgi_wake(struct connection *conn)
3130{
3131 struct fcgi_conn *fconn = conn->ctx;
3132
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003133 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003134 return (fcgi_process(fconn));
3135}
3136
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003137
3138static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3139{
3140 int ret = 0;
3141 switch (mux_ctl) {
3142 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003143 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003144 ret |= MUX_STATUS_READY;
3145 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003146 case MUX_EXIT_STATUS:
3147 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003148 default:
3149 return -1;
3150 }
3151}
3152
Christopher Faulet99eff652019-08-11 23:11:30 +02003153/* Connection timeout management. The principle is that if there's no receipt
3154 * nor sending for a certain amount of time, the connection is closed. If the
3155 * MUX buffer still has lying data or is not allocatable, the connection is
3156 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003157 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003158 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003159struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003160{
3161 struct fcgi_conn *fconn = context;
3162 int expired = tick_is_expired(t->expire, now_ms);
3163
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003164 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3165
Willy Tarreau60814ff2020-06-30 11:19:23 +02003166 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003167 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003168
3169 /* Somebody already stole the connection from us, so we should not
3170 * free it, we just have to free the task.
3171 */
3172 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003173 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003174 fconn = NULL;
3175 goto do_leave;
3176 }
3177
Willy Tarreau60814ff2020-06-30 11:19:23 +02003178 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003179 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003180 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3181 return t;
3182 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003183
Willy Tarreau60814ff2020-06-30 11:19:23 +02003184 /* We're about to destroy the connection, so make sure nobody attempts
3185 * to steal it from us.
3186 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003187 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003188 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003189
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003190 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003191 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003192
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003193do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003194 task_destroy(t);
3195
3196 if (!fconn) {
3197 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003198 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003199 return NULL;
3200 }
3201
3202 fconn->task = NULL;
3203 fconn->state = FCGI_CS_CLOSED;
3204 fcgi_wake_some_streams(fconn, 0);
3205
3206 if (br_data(fconn->mbuf)) {
3207 /* don't even try to send aborts, the buffer is stuck */
3208 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3209 goto end;
3210 }
3211
3212 /* try to send but no need to insist */
3213 if (!fcgi_conn_send_aborts(fconn))
3214 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3215
3216 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3217 conn_xprt_ready(fconn->conn)) {
3218 unsigned int released = 0;
3219 struct buffer *buf;
3220
3221 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3222 if (b_data(buf)) {
3223 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3224 buf, b_data(buf), 0);
3225 if (!ret)
3226 break;
3227 b_del(buf, ret);
3228 if (b_data(buf))
3229 break;
3230 b_free(buf);
3231 released++;
3232 }
3233 }
3234
3235 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003236 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003237 }
3238
3239 end:
3240 /* either we can release everything now or it will be done later once
3241 * the last stream closes.
3242 */
3243 if (eb_is_empty(&fconn->streams_by_id))
3244 fcgi_release(fconn);
3245
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003246 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003247 return NULL;
3248}
3249
3250
3251/*******************************************/
3252/* functions below are used by the streams */
3253/*******************************************/
3254
3255/* Append the description of what is present in error snapshot <es> into <out>.
3256 * The description must be small enough to always fit in a buffer. The output
3257 * buffer may be the trash so the trash must not be used inside this function.
3258 */
3259static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3260{
3261 chunk_appendf(out,
3262 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3263 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3264 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3265 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3266 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3267 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3268}
3269/*
3270 * Capture a bad response and archive it in the proxy's structure. By default
3271 * it tries to report the error position as h1m->err_pos. However if this one is
3272 * not set, it will then report h1m->next, which is the last known parsing
3273 * point. The function is able to deal with wrapping buffers. It always displays
3274 * buffers as a contiguous area starting at buf->p. The direction is determined
3275 * thanks to the h1m's flags.
3276 */
3277static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3278 struct h1m *h1m, struct buffer *buf)
3279{
3280 struct session *sess = fstrm->sess;
3281 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003282 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003283 union error_snapshot_ctx ctx;
3284
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003285 if (fstrm->cs && fstrm->cs->data) {
3286 if (sess == NULL)
3287 sess = si_strm(fstrm->cs->data)->sess;
3288 if (!(h1m->flags & H1_MF_RESP))
3289 other_end = si_strm(fstrm->cs->data)->be;
3290 else
3291 other_end = sess->fe;
3292 } else
3293 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003294 /* http-specific part now */
3295 ctx.h1.state = h1m->state;
3296 ctx.h1.c_flags = fconn->flags;
3297 ctx.h1.s_flags = fstrm->flags;
3298 ctx.h1.m_flags = h1m->flags;
3299 ctx.h1.m_clen = h1m->curr_len;
3300 ctx.h1.m_blen = h1m->body_len;
3301
3302 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3303 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3304 &ctx, fcgi_show_error_snapshot);
3305}
3306
3307static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3308 struct buffer *buf, size_t *ofs, size_t max)
3309{
3310 int ret;
3311
Willy Tarreau022e5e52020-09-10 09:33:15 +02003312 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 +02003313 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3314 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003315 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 +02003316 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003317 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 +02003318 fcgi_strm_error(fstrm);
3319 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3320 }
3321 goto end;
3322 }
3323
3324 *ofs += ret;
3325 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003326 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 +02003327 return ret;
3328
3329}
3330
Christopher Fauletaf542632019-10-01 21:52:49 +02003331static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003332 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3333{
3334 int ret;
3335
Willy Tarreau022e5e52020-09-10 09:33:15 +02003336 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 +02003337 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003338 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003339 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 +02003340 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003341 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 +02003342 fcgi_strm_error(fstrm);
3343 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3344 }
3345 goto end;
3346 }
3347 *ofs += ret;
3348 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003349 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 +02003350 return ret;
3351}
3352
3353static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3354 struct buffer *buf, size_t *ofs, size_t max)
3355{
3356 int ret;
3357
Willy Tarreau022e5e52020-09-10 09:33:15 +02003358 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 +02003359 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003360 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003361 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 +02003362 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003363 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 +02003364 fcgi_strm_error(fstrm);
3365 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3366 }
3367 goto end;
3368 }
3369 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003370 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003371 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 +02003372 return ret;
3373}
3374
Christopher Faulet99eff652019-08-11 23:11:30 +02003375static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3376{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003377 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003378 struct htx *htx;
3379 struct h1m *h1m = &fstrm->h1m;
3380 size_t ret, data, total = 0;
3381
3382 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003383 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3384
Christopher Faulet99eff652019-08-11 23:11:30 +02003385 data = htx->data;
3386 if (fstrm->state == FCGI_SS_ERROR)
3387 goto end;
3388
3389 do {
3390 size_t used = htx_used_space(htx);
3391
3392 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003393 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003394 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3395 if (!ret)
3396 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003397
3398 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3399
Christopher Faulet99eff652019-08-11 23:11:30 +02003400 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3401 struct htx_blk *blk = htx_get_head_blk(htx);
3402 struct htx_sl *sl;
3403
3404 if (!blk)
3405 break;
3406 sl = htx_get_blk_ptr(htx, blk);
3407 sl->flags |= HTX_SL_F_XFER_LEN;
3408 htx->extra = 0;
3409 }
3410 }
3411 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003412 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003413 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003414
3415 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3416 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3417 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003418 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003419 h1m->state = H1_MSG_DONE;
3420 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3421 }
3422
Christopher Faulet76014fd2019-12-10 11:47:22 +01003423 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003424 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003425
3426 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 +02003427 }
3428 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003429 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3430 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3431 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003432 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003433
Christopher Faulet76014fd2019-12-10 11:47:22 +01003434 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 +02003435 }
3436 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003437 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 +02003438 if (b_data(&fstrm->rxbuf) > total) {
3439 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003440 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003441 fcgi_strm_error(fstrm);
3442 }
3443 break;
3444 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003445 else {
3446 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003447 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003448 fcgi_strm_error(fstrm);
3449 break;
3450 }
3451
3452 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003453 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003454
3455 if (fstrm->state == FCGI_SS_ERROR) {
3456 b_reset(&fstrm->rxbuf);
3457 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003458 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003459 return 0;
3460 }
3461
3462 b_del(&fstrm->rxbuf, total);
3463
3464 end:
3465 htx_to_buf(htx, buf);
3466 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003467 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003468 return ret;
3469}
3470
3471/*
3472 * Attach a new stream to a connection
3473 * (Used for outgoing connections)
3474 */
3475static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3476{
3477 struct conn_stream *cs;
3478 struct fcgi_strm *fstrm;
3479 struct fcgi_conn *fconn = conn->ctx;
3480
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003481 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003482 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003483 if (!cs) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003484 TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3485 goto err;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003486 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003487 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3488 if (!fstrm) {
3489 cs_free(cs);
Christopher Faulet73518be2021-01-27 12:06:54 +01003490 goto err;
Christopher Faulet99eff652019-08-11 23:11:30 +02003491 }
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003492
3493 /* the connection is not idle anymore, let's mark this */
3494 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003495 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003497 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003498 return cs;
Christopher Faulet73518be2021-01-27 12:06:54 +01003499
3500 err:
3501 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3502 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003503}
3504
3505/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3506 * We have to scan because we may have some orphan streams. It might be
3507 * beneficial to scan backwards from the end to reduce the likeliness to find
3508 * orphans.
3509 */
3510static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3511{
3512 struct fcgi_conn *fconn = conn->ctx;
3513 struct fcgi_strm *fstrm;
3514 struct eb32_node *node;
3515
3516 node = eb32_first(&fconn->streams_by_id);
3517 while (node) {
3518 fstrm = container_of(node, struct fcgi_strm, by_id);
3519 if (fstrm->cs)
3520 return fstrm->cs;
3521 node = eb32_next(node);
3522 }
3523 return NULL;
3524}
3525
3526/*
3527 * Destroy the mux and the associated connection, if it is no longer used
3528 */
3529static void fcgi_destroy(void *ctx)
3530{
3531 struct fcgi_conn *fconn = ctx;
3532
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003533 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003534 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3535 fcgi_release(fconn);
3536}
3537
3538/*
3539 * Detach the stream from the connection and possibly release the connection.
3540 */
3541static void fcgi_detach(struct conn_stream *cs)
3542{
3543 struct fcgi_strm *fstrm = cs->ctx;
3544 struct fcgi_conn *fconn;
3545 struct session *sess;
3546
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003547 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3548
Christopher Faulet99eff652019-08-11 23:11:30 +02003549 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003550 if (!fstrm) {
3551 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003552 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003553 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003554
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003555 /* there's no txbuf so we're certain no to be able to send anything */
3556 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003557
3558 sess = fstrm->sess;
3559 fconn = fstrm->fconn;
3560 fstrm->cs = NULL;
3561 fconn->nb_cs--;
3562
3563 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3564 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3565 fconn->streams_limit = 1;
3566 }
3567 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3568 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3569 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3570 fconn->state = FCGI_CS_CLOSED;
3571 }
3572
3573 /* this stream may be blocked waiting for some data to leave, so orphan
3574 * it in this case.
3575 */
3576 if (!(cs->conn->flags & CO_FL_ERROR) &&
3577 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003578 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003579 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003580 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003581 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003582 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003583
3584 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3585 /* unblock the connection if it was blocked on this stream. */
3586 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3587 fcgi_conn_restart_reading(fconn, 1);
3588 }
3589
3590 fcgi_strm_destroy(fstrm);
3591
3592 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003593 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003594 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003595 /* Add the connection in the session serverlist, if not already done */
3596 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3597 fconn->conn->owner = NULL;
3598 if (eb_is_empty(&fconn->streams_by_id)) {
3599 /* let's kill the connection right away */
3600 fconn->conn->mux->destroy(fconn);
3601 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3602 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003603 }
3604 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003605 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003606 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3607 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003608 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003609 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003610 }
3611 }
3612 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003613 else {
3614 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003615 /* If the connection is owned by the session, first remove it
3616 * from its list
3617 */
3618 if (fconn->conn->owner) {
3619 session_unown_conn(fconn->conn->owner, fconn->conn);
3620 fconn->conn->owner = NULL;
3621 }
3622
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003623 /* mark that the tasklet may lose its context to another thread and
3624 * that the handler needs to check it under the idle conns lock.
3625 */
3626 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003627 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3628
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003629 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003630 /* The server doesn't want it, let's kill the connection right away */
3631 fconn->conn->mux->destroy(fconn);
3632 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3633 return;
3634 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003635 /* At this point, the connection has been added to the
3636 * server idle list, so another thread may already have
3637 * hijacked it, so we can't do anything with it.
3638 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003639 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3640 return;
3641 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003642 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003643 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3644 !LIST_ADDED(&fconn->conn->session_list)) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003645 ebmb_insert(&__objt_server(fconn->conn->target)->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003646 &fconn->conn->hash_node->node,
3647 sizeof(fconn->conn->hash_node->hash));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003648 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003649 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003650 }
3651
3652 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003653 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 */
3655 if (fcgi_conn_is_dead(fconn)) {
3656 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003657 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003658 fcgi_release(fconn);
3659 }
3660 else if (fconn->task) {
3661 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3662 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003663 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003664 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003665 else
3666 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003667}
3668
3669
3670/* Performs a synchronous or asynchronous shutr(). */
3671static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3672{
3673 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003674
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003675 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3676
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 if (fstrm->state == FCGI_SS_CLOSED)
3678 goto done;
3679
3680 /* a connstream may require us to immediately kill the whole connection
3681 * for example because of a "tcp-request content reject" rule that is
3682 * normally used to limit abuse.
3683 */
3684 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003685 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3686 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003687 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003688 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003689 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003690 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003691 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3692 !fcgi_strm_send_abort(fconn, fstrm))
3693 goto add_to_list;
3694 }
3695
3696 fcgi_strm_close(fstrm);
3697
3698 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3699 tasklet_wakeup(fconn->wait_event.tasklet);
3700 done:
3701 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003702 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003703 return;
3704
3705 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003706 /* Let the handler know we want to shutr, and add ourselves to the
3707 * send list if not yet done. fcgi_deferred_shut() will be
3708 * automatically called via the shut_tl tasklet when there's room
3709 * again.
3710 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003711 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003712 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003713 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3714 }
3715 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003717 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 return;
3719}
3720
3721/* Performs a synchronous or asynchronous shutw(). */
3722static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3723{
3724 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003725
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003726 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3727
Christopher Faulet99eff652019-08-11 23:11:30 +02003728 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3729 goto done;
3730
3731 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3732 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 if (fstrm->state == FCGI_SS_HREM)
3737 fcgi_strm_close(fstrm);
3738 else
3739 fstrm->state = FCGI_SS_HLOC;
3740 } else {
3741 /* a connstream may require us to immediately kill the whole connection
3742 * for example because of a "tcp-request content reject" rule that is
3743 * normally used to limit abuse.
3744 */
3745 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003746 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3747 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003748 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003749 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003750
3751 fcgi_strm_close(fstrm);
3752 }
3753
3754 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3755 tasklet_wakeup(fconn->wait_event.tasklet);
3756 done:
3757 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
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 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003762 /* Let the handler know we want to shutr, and add ourselves to the
3763 * send list if not yet done. fcgi_deferred_shut() will be
3764 * automatically called via the shut_tl tasklet when there's room
3765 * again.
3766 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003767 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003768 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003769 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3770 }
3771 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003772 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003773 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003774 return;
3775}
3776
Willy Tarreau7aad7032020-01-16 17:20:57 +01003777/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003778 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003779 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003780 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003781struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003782{
3783 struct fcgi_strm *fstrm = ctx;
3784 struct fcgi_conn *fconn = fstrm->fconn;
3785
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003786 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3787
Willy Tarreau7aad7032020-01-16 17:20:57 +01003788 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3789 /* some data processing remains to be done first */
3790 goto end;
3791 }
3792
Christopher Faulet99eff652019-08-11 23:11:30 +02003793 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3794 fcgi_do_shutw(fstrm);
3795
3796 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3797 fcgi_do_shutr(fstrm);
3798
3799 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3800 /* We're done trying to send, remove ourself from the send_list */
3801 LIST_DEL_INIT(&fstrm->send_list);
3802
3803 if (!fstrm->cs) {
3804 fcgi_strm_destroy(fstrm);
3805 if (fcgi_conn_is_dead(fconn))
3806 fcgi_release(fconn);
3807 }
3808 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003809 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003810 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003811 return NULL;
3812}
3813
3814/* shutr() called by the conn_stream (mux_ops.shutr) */
3815static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3816{
3817 struct fcgi_strm *fstrm = cs->ctx;
3818
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003819 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003820 if (cs->flags & CS_FL_KILL_CONN)
3821 fstrm->flags |= FCGI_SF_KILL_CONN;
3822
3823 if (!mode)
3824 return;
3825
3826 fcgi_do_shutr(fstrm);
3827}
3828
3829/* shutw() called by the conn_stream (mux_ops.shutw) */
3830static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3831{
3832 struct fcgi_strm *fstrm = cs->ctx;
3833
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003834 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003835 if (cs->flags & CS_FL_KILL_CONN)
3836 fstrm->flags |= FCGI_SF_KILL_CONN;
3837
3838 fcgi_do_shutw(fstrm);
3839}
3840
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003841/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3842 * event subscriber <es> is not allowed to change from a previous call as long
3843 * as at least one event is still subscribed. The <event_type> must only be a
3844 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003845 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003846static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003847{
Christopher Faulet99eff652019-08-11 23:11:30 +02003848 struct fcgi_strm *fstrm = cs->ctx;
3849 struct fcgi_conn *fconn = fstrm->fconn;
3850
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003851 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003852 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003853
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003854 es->events |= event_type;
3855 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003856
3857 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003858 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003859
Christopher Faulet99eff652019-08-11 23:11:30 +02003860 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003861 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003862 if (!LIST_ADDED(&fstrm->send_list))
3863 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003865 return 0;
3866}
3867
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003868/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3869 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3870 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003871 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003872static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003873{
Christopher Faulet99eff652019-08-11 23:11:30 +02003874 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003875 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003876
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003877 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003878 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003879
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003880 es->events &= ~event_type;
3881 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003882 fstrm->subs = NULL;
3883
3884 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003885 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003886
Christopher Faulet99eff652019-08-11 23:11:30 +02003887 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003888 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003889 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003890 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3891 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 }
3893 return 0;
3894}
3895
3896/* Called from the upper layer, to receive data */
3897static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3898{
3899 struct fcgi_strm *fstrm = cs->ctx;
3900 struct fcgi_conn *fconn = fstrm->fconn;
3901 size_t ret = 0;
3902
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003903 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3904
Christopher Faulet99eff652019-08-11 23:11:30 +02003905 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3906 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003907 else
3908 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003909
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003910 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003911 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3912 else {
3913 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003914 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003915 cs->flags |= CS_FL_EOI;
3916 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3917 cs->flags |= CS_FL_EOS;
3918 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003919 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003920 cs->flags |= CS_FL_EOS;
3921 if (cs->flags & CS_FL_ERR_PENDING)
3922 cs->flags |= CS_FL_ERROR;
3923 fcgi_release_buf(fconn, &fstrm->rxbuf);
3924 }
3925
3926 if (ret && fconn->dsi == fstrm->id) {
3927 /* demux is blocking on this stream's buffer */
3928 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3929 fcgi_conn_restart_reading(fconn, 1);
3930 }
3931
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003932 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003933 return ret;
3934}
3935
3936
Christopher Faulet99eff652019-08-11 23:11:30 +02003937/* Called from the upper layer, to send data from buffer <buf> for no more than
3938 * <count> bytes. Returns the number of bytes effectively sent. Some status
3939 * flags may be updated on the conn_stream.
3940 */
3941static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3942{
3943 struct fcgi_strm *fstrm = cs->ctx;
3944 struct fcgi_conn *fconn = fstrm->fconn;
3945 size_t total = 0;
3946 size_t ret;
3947 struct htx *htx = NULL;
3948 struct htx_sl *sl;
3949 struct htx_blk *blk;
3950 uint32_t bsize;
3951
Willy Tarreau022e5e52020-09-10 09:33:15 +02003952 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953
Christopher Faulet99eff652019-08-11 23:11:30 +02003954 /* If we were not just woken because we wanted to send but couldn't,
3955 * and there's somebody else that is waiting to send, do nothing,
3956 * we will subscribe later and be put at the end of the list
3957 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003958 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003959 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 +02003960 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003961 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003962 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003963
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003964 if (fconn->state < FCGI_CS_RECORD_H) {
3965 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003966 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003967 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003968
3969 htx = htxbuf(buf);
3970 if (fstrm->id == 0) {
3971 int32_t id = fcgi_conn_get_next_sid(fconn);
3972
3973 if (id < 0) {
3974 fcgi_strm_close(fstrm);
3975 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 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 +02003977 return 0;
3978 }
3979
3980 eb32_delete(&fstrm->by_id);
3981 fstrm->by_id.key = fstrm->id = id;
3982 fconn->max_id = id;
3983 fconn->nb_reserved--;
3984 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3985
3986
3987 /* Check if length of the body is known or if the message is
3988 * full. Otherwise, the request is invalid.
3989 */
3990 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003991 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003992 htx->flags |= HTX_FL_PARSING_ERROR;
3993 fcgi_strm_error(fstrm);
3994 goto done;
3995 }
3996 }
3997
3998 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003999 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 +02004000 if (!fcgi_strm_send_begin_request(fconn, fstrm))
4001 goto done;
4002 }
4003
4004 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
4005 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
4006
Christopher Fauletfe410d62020-05-19 15:13:00 +02004007 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02004008 count && !htx_is_empty(htx)) {
4009 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02004010 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02004011 bsize = htx_get_blksz(blk);
4012
4013 switch (htx_get_blk_type(blk)) {
4014 case HTX_BLK_REQ_SL:
4015 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004016 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 +02004017 ret = fcgi_strm_send_params(fconn, fstrm, htx);
4018 if (!ret) {
4019 goto done;
4020 }
4021 total += ret;
4022 count -= ret;
4023 break;
4024
4025 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004026 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4027 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4028 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4029 if (!ret)
4030 goto done;
4031 }
4032 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4033 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4034 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4035 if (!ret)
4036 goto done;
4037 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004038 goto remove_blk;
4039
4040 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004041 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 +02004042 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4043 if (ret > 0) {
4044 htx = htx_from_buf(buf);
4045 total += ret;
4046 count -= ret;
4047 if (ret < bsize)
4048 goto done;
4049 }
4050 break;
4051
Christopher Faulet99eff652019-08-11 23:11:30 +02004052 default:
4053 remove_blk:
4054 htx_remove_blk(htx, blk);
4055 total += bsize;
4056 count -= bsize;
4057 break;
4058 }
4059 }
4060
4061 done:
4062 if (fstrm->state >= FCGI_SS_HLOC) {
4063 /* trim any possibly pending data after we close (extra CR-LF,
4064 * unprocessed trailers, abnormal extra data, ...)
4065 */
4066 total += count;
4067 count = 0;
4068 }
4069
4070 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004071 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 +02004072 cs_set_error(cs);
4073 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4074 fcgi_strm_close(fstrm);
4075 }
4076
4077 if (htx)
4078 htx_to_buf(htx, buf);
4079
Christopher Faulet99eff652019-08-11 23:11:30 +02004080 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004081 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4082 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 +02004083 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004084 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004085
4086 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004087 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4088 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004089 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004090
4091 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004092 return total;
4093}
4094
4095/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004096static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004097{
4098 struct fcgi_conn *fconn = conn->ctx;
4099 struct fcgi_strm *fstrm = NULL;
4100 struct eb32_node *node;
4101 int send_cnt = 0;
4102 int tree_cnt = 0;
4103 int orph_cnt = 0;
4104 struct buffer *hmbuf, *tmbuf;
4105
4106 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004107 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004108
4109 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4110 send_cnt++;
4111
4112 fstrm = NULL;
4113 node = eb32_first(&fconn->streams_by_id);
4114 while (node) {
4115 fstrm = container_of(node, struct fcgi_strm, by_id);
4116 tree_cnt++;
4117 if (!fstrm->cs)
4118 orph_cnt++;
4119 node = eb32_next(node);
4120 }
4121
4122 hmbuf = br_head(fconn->mbuf);
4123 tmbuf = br_tail(fconn->mbuf);
4124 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4125 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4126 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4127 fconn->state, fconn->max_id, fconn->flags,
4128 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4129 fconn->wait_event.events, fconn->dsi,
4130 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4131 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4132 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4133 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4134 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4135 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4136 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4137
4138 if (fstrm) {
4139 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4140 fstrm, fstrm->id, fstrm->flags,
4141 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4142 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4143 fstrm->cs);
4144 if (fstrm->cs)
4145 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4146 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004147 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4148 if (fstrm->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004149 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4150 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4151 fstrm->subs->tasklet->calls,
4152 fstrm->subs->tasklet->context);
4153 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4154 chunk_appendf(&trash, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004155 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004156 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004157 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004158}
4159
4160/* Migrate the the connection to the current thread.
4161 * Return 0 if successful, non-zero otherwise.
4162 * Expected to be called with the old thread lock held.
4163 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004164static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004165{
4166 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004167 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004168
4169 if (fd_takeover(conn->handle.fd, conn) != 0)
4170 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004171
4172 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4173 /* We failed to takeover the xprt, even if the connection may
4174 * still be valid, flag it as error'd, as we have already
4175 * taken over the fd, and wake the tasklet, so that it will
4176 * destroy it.
4177 */
4178 conn->flags |= CO_FL_ERROR;
4179 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4180 return -1;
4181 }
4182
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004183 if (fcgi->wait_event.events)
4184 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4185 fcgi->wait_event.events, &fcgi->wait_event);
4186 /* To let the tasklet know it should free itself, and do nothing else,
4187 * set its context to NULL;
4188 */
4189 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004190 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004191
4192 task = fcgi->task;
4193 if (task) {
4194 task->context = NULL;
4195 fcgi->task = NULL;
4196 __ha_barrier_store();
4197 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004198
4199 fcgi->task = task_new(tid_bit);
4200 if (!fcgi->task) {
4201 fcgi_release(fcgi);
4202 return -1;
4203 }
4204 fcgi->task->process = fcgi_timeout_task;
4205 fcgi->task->context = fcgi;
4206 }
4207 fcgi->wait_event.tasklet = tasklet_new();
4208 if (!fcgi->wait_event.tasklet) {
4209 fcgi_release(fcgi);
4210 return -1;
4211 }
4212 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4213 fcgi->wait_event.tasklet->context = fcgi;
4214 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4215 SUB_RETRY_RECV, &fcgi->wait_event);
4216
4217 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004218}
4219
4220/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004221/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004222/****************************************/
4223
4224/* The mux operations */
4225static const struct mux_ops mux_fcgi_ops = {
4226 .init = fcgi_init,
4227 .wake = fcgi_wake,
4228 .attach = fcgi_attach,
4229 .get_first_cs = fcgi_get_first_cs,
4230 .detach = fcgi_detach,
4231 .destroy = fcgi_destroy,
4232 .avail_streams = fcgi_avail_streams,
4233 .used_streams = fcgi_used_streams,
4234 .rcv_buf = fcgi_rcv_buf,
4235 .snd_buf = fcgi_snd_buf,
4236 .subscribe = fcgi_subscribe,
4237 .unsubscribe = fcgi_unsubscribe,
4238 .shutr = fcgi_shutr,
4239 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004240 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004241 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004242 .takeover = fcgi_takeover,
Christopher Fauleta4600572021-03-08 15:28:28 +01004243 .flags = MX_FL_HTX|MX_FL_HOL_RISK|MX_FL_NO_UPG,
Christopher Faulet99eff652019-08-11 23:11:30 +02004244 .name = "FCGI",
4245};
4246
4247
4248/* this mux registers FCGI proto */
4249static struct mux_proto_list mux_proto_fcgi =
4250{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4251
4252INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4253
4254/*
4255 * Local variables:
4256 * c-indent-level: 8
4257 * c-basic-offset: 8
4258 * End:
4259 */