blob: 3e1cff85208a5bf22e990663cbb829607dbaa7f0 [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 Tarreauac6322d2021-01-29 12:33:46 +0100356struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short 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" */
359struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short 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 Tarreauac6322d2021-01-29 12:33:46 +0100361struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short 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
573 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
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
580 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
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 &&
592 b_alloc_margin(&fstrm->rxbuf, 0)) {
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 Tarreau21046592020-02-26 10:39:36 +0100607 if (likely(!MT_LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200608 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
609 fconn->buf_wait.target = fconn;
610 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200611 MT_LIST_ADDQ(&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);
620 offer_buffers(NULL, tasks_run_queue);
621 }
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)
634 offer_buffers(NULL, tasks_run_queue);
635}
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 Tarreau21046592020-02-26 10:39:36 +0100769 MT_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 Tarreau21046592020-02-26 10:39:36 +0100848 if (MT_LIST_ADDED(&fconn->buf_wait.list))
849 MT_LIST_DEL(&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);
1030 offer_buffers(NULL, tasks_run_queue);
1031 }
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) {
1272 params->srv_name = ist2(ptr, strlen(ptr));
1273 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) {
1284 params->rem_addr = ist2(ptr, strlen(ptr));
1285 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
2791 b_realign_if_empty(buf);
2792 if (!b_data(buf)) {
2793 /* try to pre-align the buffer like the
2794 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002795 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002796 * HTX block to alias it upon recv. We cannot use the
2797 * head because rcv_buf() will realign the buffer if
2798 * it's empty. Thus we cheat and pretend we already
2799 * have a few bytes there.
2800 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002801 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2802 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002803 }
2804 else
2805 max = buf_room_for_htx_data(buf);
2806
2807 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2808
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002809 if (max && !ret && fcgi_recv_allowed(fconn)) {
2810 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002811 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002812 }
2813 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002814 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002815
2816 if (!b_data(buf)) {
2817 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002818 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002819 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2820 }
2821
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002822 if (ret == max) {
2823 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002824 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002825 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002826
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002827 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002828 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2829}
2830
2831
2832/* Try to send data if possible.
2833 * The function returns 1 if data have been sent, otherwise zero.
2834 */
2835static int fcgi_send(struct fcgi_conn *fconn)
2836{
2837 struct connection *conn = fconn->conn;
2838 int done;
2839 int sent = 0;
2840
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002841 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2842
2843 if (conn->flags & CO_FL_ERROR) {
2844 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002845 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002846 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002847
2848
Willy Tarreau911db9b2020-01-23 16:27:54 +01002849 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002850 /* a handshake was requested */
2851 goto schedule;
2852 }
2853
2854 /* This loop is quite simple : it tries to fill as much as it can from
2855 * pending streams into the existing buffer until it's reportedly full
2856 * or the end of send requests is reached. Then it tries to send this
2857 * buffer's contents out, marks it not full if at least one byte could
2858 * be sent, and tries again.
2859 *
2860 * The snd_buf() function normally takes a "flags" argument which may
2861 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2862 * data immediately comes and CO_SFL_STREAMER to indicate that the
2863 * connection is streaming lots of data (used to increase TLS record
2864 * size at the expense of latency). The former can be sent any time
2865 * there's a buffer full flag, as it indicates at least one stream
2866 * attempted to send and failed so there are pending data. An
2867 * alternative would be to set it as long as there's an active stream
2868 * but that would be problematic for ACKs until we have an absolute
2869 * guarantee that all waiters have at least one byte to send. The
2870 * latter should possibly not be set for now.
2871 */
2872
2873 done = 0;
2874 while (!done) {
2875 unsigned int flags = 0;
2876 unsigned int released = 0;
2877 struct buffer *buf;
2878
2879 /* fill as much as we can into the current buffer */
2880 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2881 done = fcgi_process_mux(fconn);
2882
2883 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2884 done = 1; // we won't go further without extra buffers
2885
2886 if (conn->flags & CO_FL_ERROR)
2887 break;
2888
2889 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2890 flags |= CO_SFL_MSG_MORE;
2891
2892 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2893 if (b_data(buf)) {
2894 int ret;
2895
2896 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2897 if (!ret) {
2898 done = 1;
2899 break;
2900 }
2901 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002902 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002903 b_del(buf, ret);
2904 if (b_data(buf)) {
2905 done = 1;
2906 break;
2907 }
2908 }
2909 b_free(buf);
2910 released++;
2911 }
2912
2913 if (released)
2914 offer_buffers(NULL, tasks_run_queue);
2915
2916 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002917 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2918 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002919 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2920 }
2921
2922 if (conn->flags & CO_FL_SOCK_WR_SH) {
2923 /* output closed, nothing to send, clear the buffer to release it */
2924 b_reset(br_tail(fconn->mbuf));
2925 }
2926 /* We're not full anymore, so we can wake any task that are waiting
2927 * for us.
2928 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002929 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002930 struct fcgi_strm *fstrm;
2931
2932 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2933 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2934 break;
2935
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002936 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002937 continue;
2938
Willy Tarreau7aad7032020-01-16 17:20:57 +01002939 /* If the sender changed his mind and unsubscribed, let's just
2940 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002941 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002942 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2943 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002944 LIST_DEL_INIT(&fstrm->send_list);
2945 continue;
2946 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002947
2948 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002949 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002950 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002951 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002952 tasklet_wakeup(fstrm->subs->tasklet);
2953 fstrm->subs->events &= ~SUB_RETRY_SEND;
2954 if (!fstrm->subs->events)
2955 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002956 } else {
2957 /* it's the shut request that was queued */
2958 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2959 tasklet_wakeup(fstrm->shut_tl);
2960 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002961 }
2962 }
2963 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002964 if (!br_data(fconn->mbuf)) {
2965 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002966 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002967 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002968schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002969 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2970 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002971 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002972 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002973
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002974 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002975 return sent;
2976}
2977
2978/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreau691d5032021-01-20 14:55:01 +01002979struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet99eff652019-08-11 23:11:30 +02002980{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002981 struct connection *conn;
2982 struct fcgi_conn *fconn;
2983 struct tasklet *tl = (struct tasklet *)t;
2984 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002985 int ret = 0;
2986
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002987
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002988 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002989 if (tl->context == NULL) {
2990 /* The connection has been taken over by another thread,
2991 * we're no longer responsible for it, so just free the
2992 * tasklet, and do nothing.
2993 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002994 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002995 tasklet_free(tl);
2996 return NULL;
2997
2998 }
2999 fconn = ctx;
3000 conn = fconn->conn;
3001
3002 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3003
3004 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3005 if (conn_in_list)
3006 MT_LIST_DEL(&conn->list);
3007
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003008 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003009
Christopher Faulet99eff652019-08-11 23:11:30 +02003010 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3011 ret = fcgi_send(fconn);
3012 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3013 ret |= fcgi_recv(fconn);
3014 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003015 ret = fcgi_process(fconn);
3016
3017 /* If we were in an idle list, we want to add it back into it,
3018 * unless fcgi_process() returned -1, which mean it has destroyed
3019 * the connection (testing !ret is enough, if fcgi_process() wasn't
3020 * called then ret will be 0 anyway.
3021 */
3022 if (!ret && conn_in_list) {
3023 struct server *srv = objt_server(conn->target);
3024
3025 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003026 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003027 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003028 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003029 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003030 return NULL;
3031}
3032
3033/* callback called on any event by the connection handler.
3034 * It applies changes and returns zero, or < 0 if it wants immediate
3035 * destruction of the connection (which normally doesn not happen in FCGI).
3036 */
3037static int fcgi_process(struct fcgi_conn *fconn)
3038{
3039 struct connection *conn = fconn->conn;
3040
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003041 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3042
Christopher Faulet99eff652019-08-11 23:11:30 +02003043 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3044 fcgi_process_demux(fconn);
3045
3046 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3047 b_reset(&fconn->dbuf);
3048
3049 if (buf_room_for_htx_data(&fconn->dbuf))
3050 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3051 }
3052 fcgi_send(fconn);
3053
Willy Tarreauc3914d42020-09-24 08:39:22 +02003054 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003055 /* frontend is stopping, reload likely in progress, let's try
3056 * to announce a graceful shutdown if not yet done. We don't
3057 * care if it fails, it will be tried again later.
3058 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003059 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 +02003060 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3061 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3062 fcgi_conn_send_aborts(fconn);
3063 }
3064 }
3065
3066 /*
3067 * If we received early data, and the handshake is done, wake
3068 * any stream that was waiting for it.
3069 */
3070 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003071 (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 +02003072 struct eb32_node *node;
3073 struct fcgi_strm *fstrm;
3074
3075 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3076 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3077
3078 while (node) {
3079 fstrm = container_of(node, struct fcgi_strm, by_id);
3080 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3081 fcgi_strm_notify_recv(fstrm);
3082 node = eb32_next(node);
3083 }
3084 }
3085
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003086 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003087 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3088 eb_is_empty(&fconn->streams_by_id)) {
3089 fcgi_wake_some_streams(fconn, 0);
3090
3091 if (eb_is_empty(&fconn->streams_by_id)) {
3092 /* no more stream, kill the connection now */
3093 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003094 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003095 return -1;
3096 }
3097 }
3098
3099 if (!b_data(&fconn->dbuf))
3100 fcgi_release_buf(fconn, &fconn->dbuf);
3101
3102 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3103 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3104 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3105 fcgi_release_mbuf(fconn);
3106
3107 if (fconn->task) {
3108 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3109 task_queue(fconn->task);
3110 }
3111
3112 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003113 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003114 return 0;
3115}
3116
3117
3118/* wake-up function called by the connection layer (mux_ops.wake) */
3119static int fcgi_wake(struct connection *conn)
3120{
3121 struct fcgi_conn *fconn = conn->ctx;
3122
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003123 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003124 return (fcgi_process(fconn));
3125}
3126
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003127
3128static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3129{
3130 int ret = 0;
3131 switch (mux_ctl) {
3132 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003133 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003134 ret |= MUX_STATUS_READY;
3135 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003136 case MUX_EXIT_STATUS:
3137 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003138 default:
3139 return -1;
3140 }
3141}
3142
Christopher Faulet99eff652019-08-11 23:11:30 +02003143/* Connection timeout management. The principle is that if there's no receipt
3144 * nor sending for a certain amount of time, the connection is closed. If the
3145 * MUX buffer still has lying data or is not allocatable, the connection is
3146 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003147 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003148 */
Willy Tarreauac6322d2021-01-29 12:33:46 +01003149struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003150{
3151 struct fcgi_conn *fconn = context;
3152 int expired = tick_is_expired(t->expire, now_ms);
3153
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003154 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3155
Willy Tarreau60814ff2020-06-30 11:19:23 +02003156 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003157 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3158
3159 /* Somebody already stole the connection from us, so we should not
3160 * free it, we just have to free the task.
3161 */
3162 if (!t->context) {
3163 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3164 fconn = NULL;
3165 goto do_leave;
3166 }
3167
Willy Tarreau60814ff2020-06-30 11:19:23 +02003168 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003169 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003170 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3171 return t;
3172 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003173
Willy Tarreau60814ff2020-06-30 11:19:23 +02003174 /* We're about to destroy the connection, so make sure nobody attempts
3175 * to steal it from us.
3176 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003177 if (fconn->conn->flags & CO_FL_LIST_MASK)
3178 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003179
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003180 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003181 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003182
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003183do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003184 task_destroy(t);
3185
3186 if (!fconn) {
3187 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003188 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003189 return NULL;
3190 }
3191
3192 fconn->task = NULL;
3193 fconn->state = FCGI_CS_CLOSED;
3194 fcgi_wake_some_streams(fconn, 0);
3195
3196 if (br_data(fconn->mbuf)) {
3197 /* don't even try to send aborts, the buffer is stuck */
3198 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3199 goto end;
3200 }
3201
3202 /* try to send but no need to insist */
3203 if (!fcgi_conn_send_aborts(fconn))
3204 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3205
3206 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3207 conn_xprt_ready(fconn->conn)) {
3208 unsigned int released = 0;
3209 struct buffer *buf;
3210
3211 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3212 if (b_data(buf)) {
3213 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3214 buf, b_data(buf), 0);
3215 if (!ret)
3216 break;
3217 b_del(buf, ret);
3218 if (b_data(buf))
3219 break;
3220 b_free(buf);
3221 released++;
3222 }
3223 }
3224
3225 if (released)
3226 offer_buffers(NULL, tasks_run_queue);
3227 }
3228
3229 end:
3230 /* either we can release everything now or it will be done later once
3231 * the last stream closes.
3232 */
3233 if (eb_is_empty(&fconn->streams_by_id))
3234 fcgi_release(fconn);
3235
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003236 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003237 return NULL;
3238}
3239
3240
3241/*******************************************/
3242/* functions below are used by the streams */
3243/*******************************************/
3244
3245/* Append the description of what is present in error snapshot <es> into <out>.
3246 * The description must be small enough to always fit in a buffer. The output
3247 * buffer may be the trash so the trash must not be used inside this function.
3248 */
3249static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3250{
3251 chunk_appendf(out,
3252 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3253 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3254 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3255 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3256 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3257 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3258}
3259/*
3260 * Capture a bad response and archive it in the proxy's structure. By default
3261 * it tries to report the error position as h1m->err_pos. However if this one is
3262 * not set, it will then report h1m->next, which is the last known parsing
3263 * point. The function is able to deal with wrapping buffers. It always displays
3264 * buffers as a contiguous area starting at buf->p. The direction is determined
3265 * thanks to the h1m's flags.
3266 */
3267static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3268 struct h1m *h1m, struct buffer *buf)
3269{
3270 struct session *sess = fstrm->sess;
3271 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003272 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003273 union error_snapshot_ctx ctx;
3274
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003275 if (fstrm->cs && fstrm->cs->data) {
3276 if (sess == NULL)
3277 sess = si_strm(fstrm->cs->data)->sess;
3278 if (!(h1m->flags & H1_MF_RESP))
3279 other_end = si_strm(fstrm->cs->data)->be;
3280 else
3281 other_end = sess->fe;
3282 } else
3283 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003284 /* http-specific part now */
3285 ctx.h1.state = h1m->state;
3286 ctx.h1.c_flags = fconn->flags;
3287 ctx.h1.s_flags = fstrm->flags;
3288 ctx.h1.m_flags = h1m->flags;
3289 ctx.h1.m_clen = h1m->curr_len;
3290 ctx.h1.m_blen = h1m->body_len;
3291
3292 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3293 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3294 &ctx, fcgi_show_error_snapshot);
3295}
3296
3297static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3298 struct buffer *buf, size_t *ofs, size_t max)
3299{
3300 int ret;
3301
Willy Tarreau022e5e52020-09-10 09:33:15 +02003302 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 +02003303 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3304 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003305 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 +02003306 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003307 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 +02003308 fcgi_strm_error(fstrm);
3309 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3310 }
3311 goto end;
3312 }
3313
3314 *ofs += ret;
3315 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003316 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 +02003317 return ret;
3318
3319}
3320
Christopher Fauletaf542632019-10-01 21:52:49 +02003321static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003322 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3323{
3324 int ret;
3325
Willy Tarreau022e5e52020-09-10 09:33:15 +02003326 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 +02003327 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003328 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003329 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 +02003330 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003331 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 +02003332 fcgi_strm_error(fstrm);
3333 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3334 }
3335 goto end;
3336 }
3337 *ofs += ret;
3338 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003339 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 +02003340 return ret;
3341}
3342
3343static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3344 struct buffer *buf, size_t *ofs, size_t max)
3345{
3346 int ret;
3347
Willy Tarreau022e5e52020-09-10 09:33:15 +02003348 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 +02003349 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003350 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003351 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 +02003352 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003353 TRACE_ERROR("parsing error, reject H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003354 fcgi_strm_error(fstrm);
3355 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3356 }
3357 goto end;
3358 }
3359 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003360 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003361 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 +02003362 return ret;
3363}
3364
Christopher Faulet99eff652019-08-11 23:11:30 +02003365static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3366{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003367 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003368 struct htx *htx;
3369 struct h1m *h1m = &fstrm->h1m;
3370 size_t ret, data, total = 0;
3371
3372 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003373 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3374
Christopher Faulet99eff652019-08-11 23:11:30 +02003375 data = htx->data;
3376 if (fstrm->state == FCGI_SS_ERROR)
3377 goto end;
3378
3379 do {
3380 size_t used = htx_used_space(htx);
3381
3382 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003383 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003384 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3385 if (!ret)
3386 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003387
3388 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3389
Christopher Faulet99eff652019-08-11 23:11:30 +02003390 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3391 struct htx_blk *blk = htx_get_head_blk(htx);
3392 struct htx_sl *sl;
3393
3394 if (!blk)
3395 break;
3396 sl = htx_get_blk_ptr(htx, blk);
3397 sl->flags |= HTX_SL_F_XFER_LEN;
3398 htx->extra = 0;
3399 }
3400 }
3401 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003402 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003403 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003404
3405 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3406 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3407 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003408 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003409 h1m->state = H1_MSG_DONE;
3410 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3411 }
3412
Christopher Faulet76014fd2019-12-10 11:47:22 +01003413 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003414 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003415
3416 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 +02003417 }
3418 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003419 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3420 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3421 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003423
Christopher Faulet76014fd2019-12-10 11:47:22 +01003424 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 +02003425 }
3426 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003427 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 +02003428 if (b_data(&fstrm->rxbuf) > total) {
3429 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003430 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 fcgi_strm_error(fstrm);
3432 }
3433 break;
3434 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003435 else {
3436 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003437 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003438 fcgi_strm_error(fstrm);
3439 break;
3440 }
3441
3442 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003443 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003444
3445 if (fstrm->state == FCGI_SS_ERROR) {
3446 b_reset(&fstrm->rxbuf);
3447 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003448 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003449 return 0;
3450 }
3451
3452 b_del(&fstrm->rxbuf, total);
3453
3454 end:
3455 htx_to_buf(htx, buf);
3456 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003457 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003458 return ret;
3459}
3460
3461/*
3462 * Attach a new stream to a connection
3463 * (Used for outgoing connections)
3464 */
3465static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3466{
3467 struct conn_stream *cs;
3468 struct fcgi_strm *fstrm;
3469 struct fcgi_conn *fconn = conn->ctx;
3470
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003471 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003472 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003473 if (!cs) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003474 TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3475 goto err;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003476 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003477 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3478 if (!fstrm) {
3479 cs_free(cs);
Christopher Faulet73518be2021-01-27 12:06:54 +01003480 goto err;
Christopher Faulet99eff652019-08-11 23:11:30 +02003481 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003482 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003483 return cs;
Christopher Faulet73518be2021-01-27 12:06:54 +01003484
3485 err:
3486 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3487 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003488}
3489
3490/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3491 * We have to scan because we may have some orphan streams. It might be
3492 * beneficial to scan backwards from the end to reduce the likeliness to find
3493 * orphans.
3494 */
3495static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3496{
3497 struct fcgi_conn *fconn = conn->ctx;
3498 struct fcgi_strm *fstrm;
3499 struct eb32_node *node;
3500
3501 node = eb32_first(&fconn->streams_by_id);
3502 while (node) {
3503 fstrm = container_of(node, struct fcgi_strm, by_id);
3504 if (fstrm->cs)
3505 return fstrm->cs;
3506 node = eb32_next(node);
3507 }
3508 return NULL;
3509}
3510
3511/*
3512 * Destroy the mux and the associated connection, if it is no longer used
3513 */
3514static void fcgi_destroy(void *ctx)
3515{
3516 struct fcgi_conn *fconn = ctx;
3517
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003518 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003519 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3520 fcgi_release(fconn);
3521}
3522
3523/*
3524 * Detach the stream from the connection and possibly release the connection.
3525 */
3526static void fcgi_detach(struct conn_stream *cs)
3527{
3528 struct fcgi_strm *fstrm = cs->ctx;
3529 struct fcgi_conn *fconn;
3530 struct session *sess;
3531
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003532 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3533
Christopher Faulet99eff652019-08-11 23:11:30 +02003534 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003535 if (!fstrm) {
3536 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003537 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003538 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003539
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003540 /* there's no txbuf so we're certain no to be able to send anything */
3541 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003542
3543 sess = fstrm->sess;
3544 fconn = fstrm->fconn;
3545 fstrm->cs = NULL;
3546 fconn->nb_cs--;
3547
3548 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3549 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3550 fconn->streams_limit = 1;
3551 }
3552 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3553 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3554 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3555 fconn->state = FCGI_CS_CLOSED;
3556 }
3557
3558 /* this stream may be blocked waiting for some data to leave, so orphan
3559 * it in this case.
3560 */
3561 if (!(cs->conn->flags & CO_FL_ERROR) &&
3562 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003563 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003564 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003565 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003566 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003567 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003568
3569 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3570 /* unblock the connection if it was blocked on this stream. */
3571 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3572 fcgi_conn_restart_reading(fconn, 1);
3573 }
3574
3575 fcgi_strm_destroy(fstrm);
3576
3577 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003578 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003579 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003580 /* Add the connection in the session serverlist, if not already done */
3581 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3582 fconn->conn->owner = NULL;
3583 if (eb_is_empty(&fconn->streams_by_id)) {
3584 /* let's kill the connection right away */
3585 fconn->conn->mux->destroy(fconn);
3586 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3587 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003588 }
3589 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003590 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003591 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3592 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003593 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003594 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003595 }
3596 }
3597 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003598 else {
3599 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003600 /* If the connection is owned by the session, first remove it
3601 * from its list
3602 */
3603 if (fconn->conn->owner) {
3604 session_unown_conn(fconn->conn->owner, fconn->conn);
3605 fconn->conn->owner = NULL;
3606 }
3607
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003608 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003609 /* The server doesn't want it, let's kill the connection right away */
3610 fconn->conn->mux->destroy(fconn);
3611 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3612 return;
3613 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003614 /* At this point, the connection has been added to the
3615 * server idle list, so another thread may already have
3616 * hijacked it, so we can't do anything with it.
3617 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003618 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3619 return;
3620 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003621 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003622 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3623 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003624 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003625 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003626 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 }
3628
3629 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003630 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003631 */
3632 if (fcgi_conn_is_dead(fconn)) {
3633 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003634 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 fcgi_release(fconn);
3636 }
3637 else if (fconn->task) {
3638 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3639 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003640 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003641 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003642 else
3643 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003644}
3645
3646
3647/* Performs a synchronous or asynchronous shutr(). */
3648static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3649{
3650 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003651
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003652 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3653
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 if (fstrm->state == FCGI_SS_CLOSED)
3655 goto done;
3656
3657 /* a connstream may require us to immediately kill the whole connection
3658 * for example because of a "tcp-request content reject" rule that is
3659 * normally used to limit abuse.
3660 */
3661 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003662 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3663 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003664 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003665 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003666 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003667 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003668 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3669 !fcgi_strm_send_abort(fconn, fstrm))
3670 goto add_to_list;
3671 }
3672
3673 fcgi_strm_close(fstrm);
3674
3675 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3676 tasklet_wakeup(fconn->wait_event.tasklet);
3677 done:
3678 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003679 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003680 return;
3681
3682 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003683 /* Let the handler know we want to shutr, and add ourselves to the
3684 * send list if not yet done. fcgi_deferred_shut() will be
3685 * automatically called via the shut_tl tasklet when there's room
3686 * again.
3687 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003688 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003689 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003690 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3691 }
3692 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003694 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003695 return;
3696}
3697
3698/* Performs a synchronous or asynchronous shutw(). */
3699static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3700{
3701 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003703 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3704
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3706 goto done;
3707
3708 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3709 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3710 !fcgi_strm_send_abort(fconn, fstrm))
3711 goto add_to_list;
3712
3713 if (fstrm->state == FCGI_SS_HREM)
3714 fcgi_strm_close(fstrm);
3715 else
3716 fstrm->state = FCGI_SS_HLOC;
3717 } else {
3718 /* a connstream may require us to immediately kill the whole connection
3719 * for example because of a "tcp-request content reject" rule that is
3720 * normally used to limit abuse.
3721 */
3722 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003723 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3724 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003725 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003726 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003727
3728 fcgi_strm_close(fstrm);
3729 }
3730
3731 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3732 tasklet_wakeup(fconn->wait_event.tasklet);
3733 done:
3734 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003735 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003736 return;
3737
3738 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003739 /* Let the handler know we want to shutr, and add ourselves to the
3740 * send list if not yet done. fcgi_deferred_shut() will be
3741 * automatically called via the shut_tl tasklet when there's room
3742 * again.
3743 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003744 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003745 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003746 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3747 }
3748 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003749 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003750 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003751 return;
3752}
3753
Willy Tarreau7aad7032020-01-16 17:20:57 +01003754/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003755 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003756 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 */
Willy Tarreauac6322d2021-01-29 12:33:46 +01003758struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003759{
3760 struct fcgi_strm *fstrm = ctx;
3761 struct fcgi_conn *fconn = fstrm->fconn;
3762
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003763 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3764
Willy Tarreau7aad7032020-01-16 17:20:57 +01003765 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3766 /* some data processing remains to be done first */
3767 goto end;
3768 }
3769
Christopher Faulet99eff652019-08-11 23:11:30 +02003770 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3771 fcgi_do_shutw(fstrm);
3772
3773 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3774 fcgi_do_shutr(fstrm);
3775
3776 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3777 /* We're done trying to send, remove ourself from the send_list */
3778 LIST_DEL_INIT(&fstrm->send_list);
3779
3780 if (!fstrm->cs) {
3781 fcgi_strm_destroy(fstrm);
3782 if (fcgi_conn_is_dead(fconn))
3783 fcgi_release(fconn);
3784 }
3785 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003786 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003787 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 return NULL;
3789}
3790
3791/* shutr() called by the conn_stream (mux_ops.shutr) */
3792static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3793{
3794 struct fcgi_strm *fstrm = cs->ctx;
3795
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003796 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 if (cs->flags & CS_FL_KILL_CONN)
3798 fstrm->flags |= FCGI_SF_KILL_CONN;
3799
3800 if (!mode)
3801 return;
3802
3803 fcgi_do_shutr(fstrm);
3804}
3805
3806/* shutw() called by the conn_stream (mux_ops.shutw) */
3807static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3808{
3809 struct fcgi_strm *fstrm = cs->ctx;
3810
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003811 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003812 if (cs->flags & CS_FL_KILL_CONN)
3813 fstrm->flags |= FCGI_SF_KILL_CONN;
3814
3815 fcgi_do_shutw(fstrm);
3816}
3817
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003818/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3819 * event subscriber <es> is not allowed to change from a previous call as long
3820 * as at least one event is still subscribed. The <event_type> must only be a
3821 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003822 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003823static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003824{
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 struct fcgi_strm *fstrm = cs->ctx;
3826 struct fcgi_conn *fconn = fstrm->fconn;
3827
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003828 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003829 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003830
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003831 es->events |= event_type;
3832 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003833
3834 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003835 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003836
Christopher Faulet99eff652019-08-11 23:11:30 +02003837 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003838 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003839 if (!LIST_ADDED(&fstrm->send_list))
3840 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003841 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003842 return 0;
3843}
3844
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003845/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3846 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3847 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003848 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003849static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003850{
Christopher Faulet99eff652019-08-11 23:11:30 +02003851 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003852 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003853
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003854 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003855 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003856
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003857 es->events &= ~event_type;
3858 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003859 fstrm->subs = NULL;
3860
3861 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003862 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003863
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003865 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003866 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003867 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3868 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003869 }
3870 return 0;
3871}
3872
3873/* Called from the upper layer, to receive data */
3874static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3875{
3876 struct fcgi_strm *fstrm = cs->ctx;
3877 struct fcgi_conn *fconn = fstrm->fconn;
3878 size_t ret = 0;
3879
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003880 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3881
Christopher Faulet99eff652019-08-11 23:11:30 +02003882 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3883 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003884 else
3885 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003886
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003887 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003888 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3889 else {
3890 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003891 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 cs->flags |= CS_FL_EOI;
3893 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3894 cs->flags |= CS_FL_EOS;
3895 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003896 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003897 cs->flags |= CS_FL_EOS;
3898 if (cs->flags & CS_FL_ERR_PENDING)
3899 cs->flags |= CS_FL_ERROR;
3900 fcgi_release_buf(fconn, &fstrm->rxbuf);
3901 }
3902
3903 if (ret && fconn->dsi == fstrm->id) {
3904 /* demux is blocking on this stream's buffer */
3905 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3906 fcgi_conn_restart_reading(fconn, 1);
3907 }
3908
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003909 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003910 return ret;
3911}
3912
3913
Christopher Faulet99eff652019-08-11 23:11:30 +02003914/* Called from the upper layer, to send data from buffer <buf> for no more than
3915 * <count> bytes. Returns the number of bytes effectively sent. Some status
3916 * flags may be updated on the conn_stream.
3917 */
3918static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3919{
3920 struct fcgi_strm *fstrm = cs->ctx;
3921 struct fcgi_conn *fconn = fstrm->fconn;
3922 size_t total = 0;
3923 size_t ret;
3924 struct htx *htx = NULL;
3925 struct htx_sl *sl;
3926 struct htx_blk *blk;
3927 uint32_t bsize;
3928
Willy Tarreau022e5e52020-09-10 09:33:15 +02003929 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003930
Christopher Faulet99eff652019-08-11 23:11:30 +02003931 /* If we were not just woken because we wanted to send but couldn't,
3932 * and there's somebody else that is waiting to send, do nothing,
3933 * we will subscribe later and be put at the end of the list
3934 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003935 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003936 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 +02003937 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003938 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003939 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003940
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003941 if (fconn->state < FCGI_CS_RECORD_H) {
3942 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003943 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003944 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003945
3946 htx = htxbuf(buf);
3947 if (fstrm->id == 0) {
3948 int32_t id = fcgi_conn_get_next_sid(fconn);
3949
3950 if (id < 0) {
3951 fcgi_strm_close(fstrm);
3952 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953 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 +02003954 return 0;
3955 }
3956
3957 eb32_delete(&fstrm->by_id);
3958 fstrm->by_id.key = fstrm->id = id;
3959 fconn->max_id = id;
3960 fconn->nb_reserved--;
3961 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3962
3963
3964 /* Check if length of the body is known or if the message is
3965 * full. Otherwise, the request is invalid.
3966 */
3967 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003968 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003969 htx->flags |= HTX_FL_PARSING_ERROR;
3970 fcgi_strm_error(fstrm);
3971 goto done;
3972 }
3973 }
3974
3975 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 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 +02003977 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3978 goto done;
3979 }
3980
3981 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3982 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3983
Christopher Fauletfe410d62020-05-19 15:13:00 +02003984 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003985 count && !htx_is_empty(htx)) {
3986 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003987 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 bsize = htx_get_blksz(blk);
3989
3990 switch (htx_get_blk_type(blk)) {
3991 case HTX_BLK_REQ_SL:
3992 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993 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 +02003994 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3995 if (!ret) {
3996 goto done;
3997 }
3998 total += ret;
3999 count -= ret;
4000 break;
4001
4002 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004003 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4004 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4005 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4006 if (!ret)
4007 goto done;
4008 }
4009 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4010 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4011 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4012 if (!ret)
4013 goto done;
4014 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004015 goto remove_blk;
4016
4017 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004018 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 +02004019 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4020 if (ret > 0) {
4021 htx = htx_from_buf(buf);
4022 total += ret;
4023 count -= ret;
4024 if (ret < bsize)
4025 goto done;
4026 }
4027 break;
4028
Christopher Faulet99eff652019-08-11 23:11:30 +02004029 default:
4030 remove_blk:
4031 htx_remove_blk(htx, blk);
4032 total += bsize;
4033 count -= bsize;
4034 break;
4035 }
4036 }
4037
4038 done:
4039 if (fstrm->state >= FCGI_SS_HLOC) {
4040 /* trim any possibly pending data after we close (extra CR-LF,
4041 * unprocessed trailers, abnormal extra data, ...)
4042 */
4043 total += count;
4044 count = 0;
4045 }
4046
4047 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004048 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 +02004049 cs_set_error(cs);
4050 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4051 fcgi_strm_close(fstrm);
4052 }
4053
4054 if (htx)
4055 htx_to_buf(htx, buf);
4056
Christopher Faulet99eff652019-08-11 23:11:30 +02004057 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004058 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4059 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 +02004060 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004061 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004062
4063 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004064 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4065 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004066 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004067
4068 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004069 return total;
4070}
4071
4072/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004073static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004074{
4075 struct fcgi_conn *fconn = conn->ctx;
4076 struct fcgi_strm *fstrm = NULL;
4077 struct eb32_node *node;
4078 int send_cnt = 0;
4079 int tree_cnt = 0;
4080 int orph_cnt = 0;
4081 struct buffer *hmbuf, *tmbuf;
4082
4083 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004084 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004085
4086 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4087 send_cnt++;
4088
4089 fstrm = NULL;
4090 node = eb32_first(&fconn->streams_by_id);
4091 while (node) {
4092 fstrm = container_of(node, struct fcgi_strm, by_id);
4093 tree_cnt++;
4094 if (!fstrm->cs)
4095 orph_cnt++;
4096 node = eb32_next(node);
4097 }
4098
4099 hmbuf = br_head(fconn->mbuf);
4100 tmbuf = br_tail(fconn->mbuf);
4101 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4102 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4103 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4104 fconn->state, fconn->max_id, fconn->flags,
4105 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4106 fconn->wait_event.events, fconn->dsi,
4107 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4108 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4109 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4110 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4111 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4112 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4113 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4114
4115 if (fstrm) {
4116 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4117 fstrm, fstrm->id, fstrm->flags,
4118 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4119 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4120 fstrm->cs);
4121 if (fstrm->cs)
4122 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4123 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004124 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4125 if (fstrm->subs) {
4126 if (fstrm->subs) {
4127 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4128 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4129 fstrm->subs->tasklet->calls,
4130 fstrm->subs->tasklet->context);
4131 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4132 chunk_appendf(&trash, ")");
4133 }
4134 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004135 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004136 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004137}
4138
4139/* Migrate the the connection to the current thread.
4140 * Return 0 if successful, non-zero otherwise.
4141 * Expected to be called with the old thread lock held.
4142 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004143static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004144{
4145 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004146 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004147
4148 if (fd_takeover(conn->handle.fd, conn) != 0)
4149 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004150
4151 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4152 /* We failed to takeover the xprt, even if the connection may
4153 * still be valid, flag it as error'd, as we have already
4154 * taken over the fd, and wake the tasklet, so that it will
4155 * destroy it.
4156 */
4157 conn->flags |= CO_FL_ERROR;
4158 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4159 return -1;
4160 }
4161
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004162 if (fcgi->wait_event.events)
4163 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4164 fcgi->wait_event.events, &fcgi->wait_event);
4165 /* To let the tasklet know it should free itself, and do nothing else,
4166 * set its context to NULL;
4167 */
4168 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004169 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004170
4171 task = fcgi->task;
4172 if (task) {
4173 task->context = NULL;
4174 fcgi->task = NULL;
4175 __ha_barrier_store();
4176 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004177
4178 fcgi->task = task_new(tid_bit);
4179 if (!fcgi->task) {
4180 fcgi_release(fcgi);
4181 return -1;
4182 }
4183 fcgi->task->process = fcgi_timeout_task;
4184 fcgi->task->context = fcgi;
4185 }
4186 fcgi->wait_event.tasklet = tasklet_new();
4187 if (!fcgi->wait_event.tasklet) {
4188 fcgi_release(fcgi);
4189 return -1;
4190 }
4191 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4192 fcgi->wait_event.tasklet->context = fcgi;
4193 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4194 SUB_RETRY_RECV, &fcgi->wait_event);
4195
4196 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004197}
4198
4199/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004200/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004201/****************************************/
4202
4203/* The mux operations */
4204static const struct mux_ops mux_fcgi_ops = {
4205 .init = fcgi_init,
4206 .wake = fcgi_wake,
4207 .attach = fcgi_attach,
4208 .get_first_cs = fcgi_get_first_cs,
4209 .detach = fcgi_detach,
4210 .destroy = fcgi_destroy,
4211 .avail_streams = fcgi_avail_streams,
4212 .used_streams = fcgi_used_streams,
4213 .rcv_buf = fcgi_rcv_buf,
4214 .snd_buf = fcgi_snd_buf,
4215 .subscribe = fcgi_subscribe,
4216 .unsubscribe = fcgi_unsubscribe,
4217 .shutr = fcgi_shutr,
4218 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004219 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004220 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004221 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004222 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004223 .name = "FCGI",
4224};
4225
4226
4227/* this mux registers FCGI proto */
4228static struct mux_proto_list mux_proto_fcgi =
4229{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4230
4231INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4232
4233/*
4234 * Local variables:
4235 * c-indent-level: 8
4236 * c-basic-offset: 8
4237 * End:
4238 */