blob: fabc83e90691e496edd0f6dc1d60aac436969640 [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 Tarreau90f366b2021-02-20 11:49:49 +0100607 if (likely(!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 Tarreau90f366b2021-02-20 11:49:49 +0100611 LIST_ADDQ(&ti->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200612 }
613 return buf;
614}
615
616static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
617{
618 if (bptr->size) {
619 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100620 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200621 }
622}
623
624static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
625{
626 struct buffer *buf;
627 unsigned int count = 0;
628
629 while (b_size(buf = br_head_pick(fconn->mbuf))) {
630 b_free(buf);
631 count++;
632 }
633 if (count)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100634 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200635}
636
637/* Returns the number of allocatable outgoing streams for the connection taking
638 * the number reserved streams into account.
639 */
640static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
641{
642 int ret;
643
644 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
645 if (ret < 0)
646 ret = 0;
647 return ret;
648}
649
650/* Returns the number of streams in use on a connection to figure if it's
651 * idle or not. We check nb_cs and not nb_streams as the caller will want
652 * to know if it was the last one after a detach().
653 */
654static int fcgi_used_streams(struct connection *conn)
655{
656 struct fcgi_conn *fconn = conn->ctx;
657
658 return fconn->nb_cs;
659}
660
661/* Returns the number of concurrent streams available on the connection */
662static int fcgi_avail_streams(struct connection *conn)
663{
664 struct server *srv = objt_server(conn->target);
665 struct fcgi_conn *fconn = conn->ctx;
666 int ret1, ret2;
667
668 /* Don't open new stream if the connection is closed */
669 if (fconn->state == FCGI_CS_CLOSED)
670 return 0;
671
672 /* May be negative if this setting has changed */
673 ret1 = (fconn->streams_limit - fconn->nb_streams);
674
675 /* we must also consider the limit imposed by stream IDs */
676 ret2 = fcgi_streams_left(fconn);
677 ret1 = MIN(ret1, ret2);
678 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
679 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
680 ret1 = MIN(ret1, ret2);
681 }
682 return ret1;
683}
684
685/*****************************************************************/
686/* functions below are dedicated to the mux setup and management */
687/*****************************************************************/
688
689/* Initializes the mux once it's attached. Only outgoing connections are
690 * supported. So the context is already initialized before installing the
691 * mux. <input> is always used as Input buffer and may contain data. It is the
692 * caller responsibility to not reuse it anymore. Returns < 0 on error.
693 */
694static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
695 struct buffer *input)
696{
697 struct fcgi_conn *fconn;
698 struct fcgi_strm *fstrm;
699 struct fcgi_app *app = get_px_fcgi_app(px);
700 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200701 void *conn_ctx = conn->ctx;
702
703 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200704
Christopher Faulet73518be2021-01-27 12:06:54 +0100705 if (!app) {
706 TRACE_ERROR("No FCGI app found, don't create fconn", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200707 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100708 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200709
710 fconn = pool_alloc(pool_head_fcgi_conn);
Christopher Faulet73518be2021-01-27 12:06:54 +0100711 if (!fconn) {
712 TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200713 goto fail_conn;
Christopher Faulet73518be2021-01-27 12:06:54 +0100714 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200715
716 fconn->shut_timeout = fconn->timeout = px->timeout.server;
717 if (tick_isset(px->timeout.serverfin))
718 fconn->shut_timeout = px->timeout.serverfin;
719
720 fconn->flags = FCGI_CF_NONE;
721
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500722 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200723 if (app->flags & FCGI_APP_FL_KEEP_CONN)
724 fconn->flags |= FCGI_CF_KEEP_CONN;
725 if (app->flags & FCGI_APP_FL_GET_VALUES)
726 fconn->flags |= FCGI_CF_GET_VALUES;
727 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
728 fconn->flags |= FCGI_CF_MPXS_CONNS;
729
730 fconn->proxy = px;
731 fconn->app = app;
732 fconn->task = NULL;
733 if (tick_isset(fconn->timeout)) {
734 t = task_new(tid_bit);
Christopher Faulet73518be2021-01-27 12:06:54 +0100735 if (!t) {
736 TRACE_ERROR("fconn task allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200737 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +0100738 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200739
740 fconn->task = t;
741 t->process = fcgi_timeout_task;
742 t->context = fconn;
743 t->expire = tick_add(now_ms, fconn->timeout);
744 }
745
746 fconn->wait_event.tasklet = tasklet_new();
747 if (!fconn->wait_event.tasklet)
748 goto fail;
749 fconn->wait_event.tasklet->process = fcgi_io_cb;
750 fconn->wait_event.tasklet->context = fconn;
751 fconn->wait_event.events = 0;
752
753 /* Initialise the context. */
754 fconn->state = FCGI_CS_INIT;
755 fconn->conn = conn;
756 fconn->streams_limit = app->maxreqs;
757 fconn->max_id = -1;
758 fconn->nb_streams = 0;
759 fconn->nb_cs = 0;
760 fconn->nb_reserved = 0;
761 fconn->stream_cnt = 0;
762
763 fconn->dbuf = *input;
764 fconn->dsi = -1;
765
766 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
767 fconn->streams_by_id = EB_ROOT;
768 LIST_INIT(&fconn->send_list);
Willy Tarreau90f366b2021-02-20 11:49:49 +0100769 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200770
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200771 conn->ctx = fconn;
772
Christopher Faulet99eff652019-08-11 23:11:30 +0200773 if (t)
774 task_queue(t);
775
776 /* FIXME: this is temporary, for outgoing connections we need to
777 * immediately allocate a stream until the code is modified so that the
778 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200779 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200781 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200782 if (!fstrm)
783 goto fail;
784
Christopher Faulet99eff652019-08-11 23:11:30 +0200785
786 /* Repare to read something */
787 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200788 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200789 return 0;
790
791 fail:
792 task_destroy(t);
793 if (fconn->wait_event.tasklet)
794 tasklet_free(fconn->wait_event.tasklet);
795 pool_free(pool_head_fcgi_conn, fconn);
796 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200797 conn->ctx = conn_ctx; // restore saved ctx
798 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200799 return -1;
800}
801
802/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
803 * -1 if no more is allocatable.
804 */
805static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
806{
807 int32_t id = (fconn->max_id + 1) | 1;
808
809 if ((id & 0x80000000U))
810 id = -1;
811 return id;
812}
813
814/* Returns the stream associated with id <id> or NULL if not found */
815static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
816{
817 struct eb32_node *node;
818
819 if (id == 0)
820 return (struct fcgi_strm *)fcgi_mgmt_stream;
821
822 if (id > fconn->max_id)
823 return (struct fcgi_strm *)fcgi_unknown_stream;
824
825 node = eb32_lookup(&fconn->streams_by_id, id);
826 if (!node)
827 return (struct fcgi_strm *)fcgi_unknown_stream;
828 return container_of(node, struct fcgi_strm, by_id);
829}
830
831
832/* Release function. This one should be called to free all resources allocated
833 * to the mux.
834 */
835static void fcgi_release(struct fcgi_conn *fconn)
836{
William Dauchy477757c2020-08-07 22:19:23 +0200837 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200838
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200839 TRACE_POINT(FCGI_EV_FCONN_END);
840
Christopher Faulet99eff652019-08-11 23:11:30 +0200841 if (fconn) {
842 /* The connection must be attached to this mux to be released */
843 if (fconn->conn && fconn->conn->ctx == fconn)
844 conn = fconn->conn;
845
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200846 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
847
Willy Tarreau90f366b2021-02-20 11:49:49 +0100848 if (LIST_ADDED(&fconn->buf_wait.list))
849 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200850
851 fcgi_release_buf(fconn, &fconn->dbuf);
852 fcgi_release_mbuf(fconn);
853
854 if (fconn->task) {
855 fconn->task->context = NULL;
856 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
857 fconn->task = NULL;
858 }
859 if (fconn->wait_event.tasklet)
860 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200861 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200862 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
863 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200864
865 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200866 }
867
868 if (conn) {
869 conn->mux = NULL;
870 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200871 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200872
873 conn_stop_tracking(conn);
874 conn_full_close(conn);
875 if (conn->destroy_cb)
876 conn->destroy_cb(conn);
877 conn_free(conn);
878 }
879}
880
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200881/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
882 * pending on the connection AND if there is no more data in the demux
883 * buffer. The function returns 1 to report a read0 or 0 otherwise.
884 */
885static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
886{
887 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
888 return 1;
889 return 0;
890}
891
Christopher Faulet99eff652019-08-11 23:11:30 +0200892
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500893/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200894static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
895{
896 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
897 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
898 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
899 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
900 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
901 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
902 conn_xprt_read0_pending(fconn->conn))))
903 return 1;
904 return 0;
905}
906
907
908/********************************************************/
909/* functions below are for the FCGI protocol processing */
910/********************************************************/
911
Christopher Faulet99eff652019-08-11 23:11:30 +0200912/* Marks an error on the stream. */
913static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
914{
915 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200916 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
917 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200918 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200919 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
920 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200921 if (fstrm->cs)
922 cs_set_error(fstrm->cs);
923 }
924}
925
926/* Attempts to notify the data layer of recv availability */
927static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
928{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100929 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200930 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100931 tasklet_wakeup(fstrm->subs->tasklet);
932 fstrm->subs->events &= ~SUB_RETRY_RECV;
933 if (!fstrm->subs->events)
934 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200935 }
936}
937
938/* Attempts to notify the data layer of send availability */
939static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
940{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100941 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200942 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100943 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100944 tasklet_wakeup(fstrm->subs->tasklet);
945 fstrm->subs->events &= ~SUB_RETRY_SEND;
946 if (!fstrm->subs->events)
947 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200948 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100949 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
950 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
951 tasklet_wakeup(fstrm->shut_tl);
952 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200953}
954
955/* Alerts the data layer, trying to wake it up by all means, following
956 * this sequence :
957 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
958 * for recv
959 * - if its subscribed to send, then it's woken up for send
960 * - if it was subscribed to neither, its ->wake() callback is called
961 * It is safe to call this function with a closed stream which doesn't have a
962 * conn_stream anymore.
963 */
964static void fcgi_strm_alert(struct fcgi_strm *fstrm)
965{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200966 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100967 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100968 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200969 fcgi_strm_notify_recv(fstrm);
970 fcgi_strm_notify_send(fstrm);
971 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200972 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
973 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200974 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200975 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200976}
977
978/* Writes the 16-bit record size <len> at address <record> */
979static inline void fcgi_set_record_size(void *record, uint16_t len)
980{
981 uint8_t *out = (record + 4);
982
983 *out = (len >> 8);
984 *(out + 1) = (len & 0xff);
985}
986
987/* Writes the 16-bit stream id <id> at address <record> */
988static inline void fcgi_set_record_id(void *record, uint16_t id)
989{
990 uint8_t *out = (record + 2);
991
992 *out = (id >> 8);
993 *(out + 1) = (id & 0xff);
994}
995
996/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
997 * its connection if the stream was not yet closed. Please use this exclusively
998 * before closing a stream to ensure stream count is well maintained.
999 */
1000static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
1001{
1002 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001003 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001004 fstrm->fconn->nb_streams--;
1005 if (!fstrm->id)
1006 fstrm->fconn->nb_reserved--;
1007 if (fstrm->cs) {
1008 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1009 fcgi_strm_notify_recv(fstrm);
1010 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001011 fstrm->state = FCGI_SS_CLOSED;
1012 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1013 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001014 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001015}
1016
1017/* Detaches a FCGI stream from its FCGI connection and releases it to the
1018 * fcgi_strm pool.
1019 */
1020static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1021{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001022 struct connection *conn = fstrm->fconn->conn;
1023
1024 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1025
Christopher Faulet99eff652019-08-11 23:11:30 +02001026 fcgi_strm_close(fstrm);
1027 eb32_delete(&fstrm->by_id);
1028 if (b_size(&fstrm->rxbuf)) {
1029 b_free(&fstrm->rxbuf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01001030 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001031 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001032 if (fstrm->subs)
1033 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001034 /* There's no need to explicitly call unsubscribe here, the only
1035 * reference left would be in the fconn send_list/fctl_list, and if
1036 * we're in it, we're getting out anyway
1037 */
1038 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001039 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001040 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001041
1042 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001043}
1044
1045/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1046 * stream tree. In case of error, nothing is added and NULL is returned. The
1047 * causes of errors can be any failed memory allocation. The caller is
1048 * responsible for checking if the connection may support an extra stream prior
1049 * to calling this function.
1050 */
1051static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1052{
1053 struct fcgi_strm *fstrm;
1054
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001055 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1056
Christopher Faulet99eff652019-08-11 23:11:30 +02001057 fstrm = pool_alloc(pool_head_fcgi_strm);
Christopher Faulet73518be2021-01-27 12:06:54 +01001058 if (!fstrm) {
1059 TRACE_ERROR("fstrm allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001060 goto out;
Christopher Faulet73518be2021-01-27 12:06:54 +01001061 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001062
Willy Tarreau7aad7032020-01-16 17:20:57 +01001063 fstrm->shut_tl = tasklet_new();
1064 if (!fstrm->shut_tl) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001065 TRACE_ERROR("fstrm shut tasklet allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001066 pool_free(pool_head_fcgi_strm, fstrm);
1067 goto out;
1068 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001069 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001070 fstrm->shut_tl->process = fcgi_deferred_shut;
1071 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001072 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001073 fstrm->fconn = fconn;
1074 fstrm->cs = NULL;
1075 fstrm->flags = FCGI_SF_NONE;
1076 fstrm->proto_status = 0;
1077 fstrm->state = FCGI_SS_IDLE;
1078 fstrm->rxbuf = BUF_NULL;
1079
1080 h1m_init_res(&fstrm->h1m);
1081 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1082 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1083
1084 fstrm->by_id.key = fstrm->id = id;
1085 if (id > 0)
1086 fconn->max_id = id;
1087 else
1088 fconn->nb_reserved++;
1089
1090 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1091 fconn->nb_streams++;
1092 fconn->stream_cnt++;
1093
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001094 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001095 return fstrm;
1096
1097 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001098 TRACE_DEVEL("leaving in error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001099 return NULL;
1100}
1101
1102/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1103 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1104 * highest possible stream ID was reached.
1105 */
1106static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1107 struct session *sess)
1108{
1109 struct fcgi_strm *fstrm = NULL;
1110
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001111 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1112 if (fconn->nb_streams >= fconn->streams_limit) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001113 TRACE_ERROR("streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001114 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001116
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001117 if (fcgi_streams_left(fconn) < 1) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001118 TRACE_ERROR("!streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001119 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001120 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001121
1122 /* Defer choosing the ID until we send the first message to create the stream */
1123 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001124 if (!fstrm) {
Christopher Faulet73518be2021-01-27 12:06:54 +01001125 TRACE_ERROR("fstream allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001126 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001127 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001128
1129 fstrm->cs = cs;
1130 fstrm->sess = sess;
1131 cs->ctx = fstrm;
1132 fconn->nb_cs++;
1133
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001134 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001135 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001136
1137 out:
Christopher Faulet73518be2021-01-27 12:06:54 +01001138 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001139 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001140}
1141
1142/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1143 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1144 * automatically updated accordingly. If the stream is orphaned, it is
1145 * destroyed.
1146 */
1147static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1148{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149 struct fcgi_conn *fconn = fstrm->fconn;
1150
1151 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1152
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 if (!fstrm->cs) {
1154 /* this stream was already orphaned */
1155 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001156 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001157 return;
1158 }
1159
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001160 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001161 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001162 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001163 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 else if (fstrm->state == FCGI_SS_HLOC)
1166 fcgi_strm_close(fstrm);
1167 }
1168
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001169 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001170 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1171 if (fstrm->cs->flags & CS_FL_EOS)
1172 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001173
1174 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001175 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001176 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1177 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001178 }
1179
1180 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001181
1182 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001183}
1184
1185/* Wakes unassigned streams (ID == 0) attached to the connection. */
1186static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1187{
1188 struct eb32_node *node;
1189 struct fcgi_strm *fstrm;
1190
1191 node = eb32_lookup(&fconn->streams_by_id, 0);
1192 while (node) {
1193 fstrm = container_of(node, struct fcgi_strm, by_id);
1194 if (fstrm->id > 0)
1195 break;
1196 node = eb32_next(node);
1197 fcgi_strm_wake_one_stream(fstrm);
1198 }
1199}
1200
1201/* Wakes the streams attached to the connection, whose id is greater than <last>
1202 * or unassigned.
1203 */
1204static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1205{
1206 struct eb32_node *node;
1207 struct fcgi_strm *fstrm;
1208
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001209 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1210
Christopher Faulet99eff652019-08-11 23:11:30 +02001211 /* Wake all streams with ID > last */
1212 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1213 while (node) {
1214 fstrm = container_of(node, struct fcgi_strm, by_id);
1215 node = eb32_next(node);
1216 fcgi_strm_wake_one_stream(fstrm);
1217 }
1218 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001219
1220 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001221}
1222
1223static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1224 struct htx *htx, struct htx_sl *sl,
1225 struct fcgi_strm_params *params)
1226{
1227 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1228 struct ist p;
1229
1230 if (!sl)
1231 goto error;
1232
1233 if (!(params->mask & FCGI_SP_DOC_ROOT))
1234 params->docroot = fconn->app->docroot;
1235
1236 if (!(params->mask & FCGI_SP_REQ_METH)) {
1237 p = htx_sl_req_meth(sl);
1238 params->meth = ist2(b_tail(params->p), p.len);
1239 chunk_memcat(params->p, p.ptr, p.len);
1240 }
1241 if (!(params->mask & FCGI_SP_REQ_URI)) {
1242 p = htx_sl_req_uri(sl);
1243 params->uri = ist2(b_tail(params->p), p.len);
1244 chunk_memcat(params->p, p.ptr, p.len);
1245 }
1246 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1247 p = htx_sl_req_vsn(sl);
1248 params->vsn = ist2(b_tail(params->p), p.len);
1249 chunk_memcat(params->p, p.ptr, p.len);
1250 }
1251 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1252 char *end;
1253 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001254 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001255 port = get_host_port(cli_conn->dst);
1256 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1257 if (!end)
1258 goto error;
1259 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1260 params->p->data += params->srv_port.len;
1261 }
1262 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1263 /* If no Host header found, use the server address to fill
1264 * srv_name */
1265 if (!istlen(params->srv_name)) {
1266 char *ptr = NULL;
1267
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001268 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001269 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1270 ptr = b_tail(params->p);
1271 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001272 params->srv_name = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001273 params->p->data += params->srv_name.len;
1274 }
1275 }
1276 }
1277 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1278 char *ptr = NULL;
1279
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001280 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001281 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1282 ptr = b_tail(params->p);
1283 if (ptr) {
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001284 params->rem_addr = ist(ptr);
Christopher Faulet99eff652019-08-11 23:11:30 +02001285 params->p->data += params->rem_addr.len;
1286 }
1287 }
1288 if (!(params->mask & FCGI_SP_REM_PORT)) {
1289 char *end;
1290 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001291 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001292 port = get_host_port(cli_conn->src);
1293 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1294 if (!end)
1295 goto error;
1296 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1297 params->p->data += params->rem_port.len;
1298 }
1299 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1300 struct htx_blk *blk;
1301 enum htx_blk_type type;
1302 char *end;
1303 size_t len = 0;
1304
1305 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1306 type = htx_get_blk_type(blk);
1307
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001308 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001309 break;
1310 if (type == HTX_BLK_DATA)
1311 len += htx_get_blksz(blk);
1312 }
1313 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1314 if (!end)
1315 goto error;
1316 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1317 params->p->data += params->cont_len.len;
1318 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001319#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001320 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001321 if (cli_conn)
1322 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001323 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001324#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001325 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1326 /* one of scriptname, pathinfo or query_string is no set */
1327 struct ist path = http_get_path(params->uri);
1328 int len;
1329
Christopher Faulet99eff652019-08-11 23:11:30 +02001330 /* No scrit_name set but no valid path ==> error */
1331 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1332 goto error;
1333
Christopher Faulet99eff652019-08-11 23:11:30 +02001334 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001335 if (!(params->mask & FCGI_SP_REQ_QS)) {
1336 struct ist qs = istfind(path, '?');
1337
1338 /* Update the path length */
1339 path.len -= qs.len;
1340
1341 /* Set the query-string skipping the '?', if any */
1342 if (istlen(qs))
1343 params->qs = istnext(qs);
1344 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001345
1346 /* If the script_name is set, don't try to deduce the path_info
1347 * too. The opposite is not true.
1348 */
1349 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1350 params->mask |= FCGI_SP_PATH_INFO;
1351 goto end;
1352 }
1353
Christopher Faulet0f17a442020-07-23 15:44:37 +02001354 /* Decode the path. it must first be copied to keep the URI
1355 * untouched.
1356 */
1357 chunk_memcat(params->p, path.ptr, path.len);
1358 path.ptr = b_tail(params->p) - path.len;
1359 len = url_decode(ist0(path), 0);
1360 if (len < 0)
1361 goto error;
1362 path.len = len;
1363
Christopher Faulet99eff652019-08-11 23:11:30 +02001364 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001365 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001366
1367 /* If there is no regex to match the pathinfo, just to the last
1368 * part and see if the index must be used.
1369 */
1370 if (!fconn->app->pathinfo_re)
1371 goto check_index;
1372
Christopher Faulet28cb3662020-02-14 14:47:37 +01001373 /* If some special characters are found in the decoded path (\n
1374 * or \0), the PATH_INFO regex cannot match. This is theorically
1375 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001376 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001377 * case.
1378 */
1379 if (istchr(path, '\n') || istchr(path, '\0'))
1380 goto error;
1381
Christopher Faulet99eff652019-08-11 23:11:30 +02001382 /* The regex does not match, just to the last part and see if
1383 * the index must be used.
1384 */
1385 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1386 goto check_index;
1387
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001388 /* We must have at least 1 capture for the script name,
1389 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001390 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001391 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001392 goto check_index;
1393
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001394 /* Finally we can set the script_name and the path_info. The
1395 * path_info is set if not already defined, and if it was
1396 * captured
1397 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001398 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001399 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1400 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001401
1402 check_index:
1403 len = params->scriptname.len;
1404 /* the script_name if finished by a '/' so we can add the index
1405 * part, if any.
1406 */
1407 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1408 struct ist sn = params->scriptname;
1409
1410 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1411 chunk_memcat(params->p, sn.ptr, sn.len);
1412 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1413 }
1414 }
1415
1416 end:
1417 return 1;
1418 error:
1419 return 0;
1420}
1421
1422static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1423 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1424{
1425 struct fcgi_param p;
1426
1427 if (params->mask & flag)
1428 return 1;
1429
1430 chunk_reset(&trash);
1431
1432 switch (flag) {
1433 case FCGI_SP_CGI_GATEWAY:
1434 p.n = ist("GATEWAY_INTERFACE");
1435 p.v = ist("CGI/1.1");
1436 goto encode;
1437 case FCGI_SP_DOC_ROOT:
1438 p.n = ist("DOCUMENT_ROOT");
1439 p.v = params->docroot;
1440 goto encode;
1441 case FCGI_SP_SCRIPT_NAME:
1442 p.n = ist("SCRIPT_NAME");
1443 p.v = params->scriptname;
1444 goto encode;
1445 case FCGI_SP_PATH_INFO:
1446 p.n = ist("PATH_INFO");
1447 p.v = params->pathinfo;
1448 goto encode;
1449 case FCGI_SP_REQ_URI:
1450 p.n = ist("REQUEST_URI");
1451 p.v = params->uri;
1452 goto encode;
1453 case FCGI_SP_REQ_METH:
1454 p.n = ist("REQUEST_METHOD");
1455 p.v = params->meth;
1456 goto encode;
1457 case FCGI_SP_REQ_QS:
1458 p.n = ist("QUERY_STRING");
1459 p.v = params->qs;
1460 goto encode;
1461 case FCGI_SP_SRV_NAME:
1462 p.n = ist("SERVER_NAME");
1463 p.v = params->srv_name;
1464 goto encode;
1465 case FCGI_SP_SRV_PORT:
1466 p.n = ist("SERVER_PORT");
1467 p.v = params->srv_port;
1468 goto encode;
1469 case FCGI_SP_SRV_PROTO:
1470 p.n = ist("SERVER_PROTOCOL");
1471 p.v = params->vsn;
1472 goto encode;
1473 case FCGI_SP_REM_ADDR:
1474 p.n = ist("REMOTE_ADDR");
1475 p.v = params->rem_addr;
1476 goto encode;
1477 case FCGI_SP_REM_PORT:
1478 p.n = ist("REMOTE_PORT");
1479 p.v = params->rem_port;
1480 goto encode;
1481 case FCGI_SP_SCRIPT_FILE:
1482 p.n = ist("SCRIPT_FILENAME");
1483 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1484 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1485 p.v = ist2(b_head(&trash), b_data(&trash));
1486 goto encode;
1487 case FCGI_SP_PATH_TRANS:
1488 if (!istlen(params->pathinfo))
1489 goto skip;
1490 p.n = ist("PATH_TRANSLATED");
1491 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1492 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1493 p.v = ist2(b_head(&trash), b_data(&trash));
1494 goto encode;
1495 case FCGI_SP_CONT_LEN:
1496 p.n = ist("CONTENT_LENGTH");
1497 p.v = params->cont_len;
1498 goto encode;
1499 case FCGI_SP_HTTPS:
1500 if (!params->https)
1501 goto skip;
1502 p.n = ist("HTTPS");
1503 p.v = ist("on");
1504 goto encode;
1505 default:
1506 goto skip;
1507 }
1508
1509 encode:
1510 if (!istlen(p.v))
1511 goto skip;
1512 if (!fcgi_encode_param(outbuf, &p))
1513 return 0;
1514 skip:
1515 params->mask |= flag;
1516 return 1;
1517}
1518
1519/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1520 * anything. It is highly unexpected, but if the record is larger than a buffer
1521 * and cannot be encoded in one time, an error is triggered and the connection is
1522 * closed. GET_VALUES record cannot be split.
1523 */
1524static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1525{
1526 struct buffer outbuf;
1527 struct buffer *mbuf;
1528 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1529 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001530 int ret = 0;
1531
1532 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001533
1534 mbuf = br_tail(fconn->mbuf);
1535 retry:
1536 if (!fcgi_get_buf(fconn, mbuf)) {
1537 fconn->flags |= FCGI_CF_MUX_MALLOC;
1538 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001539 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1540 ret = 0;
1541 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001542 }
1543
1544 while (1) {
1545 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001546 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001547 break;
1548 realign_again:
1549 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1550 }
1551
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001552 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001553 goto full;
1554
1555 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1556 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001557 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1558 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001559
1560 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1561 * handled by HAProxy.
1562 */
1563 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1564 goto full;
1565
1566 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001567 TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn, 0, 0, (size_t[]){outbuf.data-8});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001568 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001569 b_add(mbuf, outbuf.data);
1570 ret = 1;
1571
1572 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001573 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001574 return ret;
1575 full:
1576 /* Too large to be encoded. For GET_VALUES records, it is an error */
Christopher Faulet73518be2021-01-27 12:06:54 +01001577 if (!b_data(mbuf)) {
1578 TRACE_ERROR("GET_VALUES record too large", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001579 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001580 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001581
1582 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1583 goto retry;
1584 fconn->flags |= FCGI_CF_MUX_MFULL;
1585 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001586 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001587 ret = 0;
1588 goto end;
1589 fail:
1590 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001591 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1592 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1593 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001594}
1595
1596/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1597 * couldn't do anything. It is highly unexpected, but if the record is larger
1598 * than a buffer and cannot be decoded in one time, an error is triggered and
1599 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1600 */
1601static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1602{
1603 struct buffer inbuf;
1604 struct buffer *dbuf;
1605 size_t offset;
1606
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001607 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1608
Christopher Faulet99eff652019-08-11 23:11:30 +02001609 dbuf = &fconn->dbuf;
1610
1611 /* Record too large to be fully decoded */
1612 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1613 goto fail;
1614
1615 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001616 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1617 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001618 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001619 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001620
1621 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1622 /* Realign the dmux buffer if the record wraps. It is unexpected
1623 * at this stage because it should be the first record received
1624 * from the FCGI application.
1625 */
1626 b_slow_realign(dbuf, trash.area, 0);
1627 }
1628
1629 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1630
1631 for (offset = 0; offset < b_data(&inbuf); ) {
1632 struct fcgi_param p;
1633 size_t ret;
1634
1635 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1636 if (!ret) {
1637 /* name or value too large to be decoded at once */
Christopher Faulet73518be2021-01-27 12:06:54 +01001638 TRACE_ERROR("error decoding GET_VALUES_RESULT param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001639 goto fail;
1640 }
1641 offset += ret;
1642
1643 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001644 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001645 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001646 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001647 }
1648 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001649 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001650 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001651 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001652 }
1653 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1654 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001655 TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->streams_limit});
Christopher Faulet99eff652019-08-11 23:11:30 +02001656 }
1657 /*
1658 * Ignore all other params
1659 */
1660 }
1661
1662 /* Reset the number of concurrent streams supported if the FCGI
1663 * application does not support connection multiplexing
1664 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001665 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001666 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001667 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1668 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001669
1670 /* We must be sure to have read exactly the announced record length, no
1671 * more no less
1672 */
Christopher Faulet73518be2021-01-27 12:06:54 +01001673 if (offset != fconn->drl) {
1674 TRACE_ERROR("invalid GET_VALUES_RESULT record length", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001675 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01001676 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001677
Willy Tarreau022e5e52020-09-10 09:33:15 +02001678 TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02001679 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1680 fconn->drl = 0;
1681 fconn->drp = 0;
1682 fconn->state = FCGI_CS_RECORD_H;
1683 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001684 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1685 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 return 1;
1687 fail:
1688 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001689 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1690 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001691 return 0;
1692}
1693
1694/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1695 * excluded, as the streams which already received the end-of-stream. It returns
1696 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1697 */
1698static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1699{
1700 struct eb32_node *node;
1701 struct fcgi_strm *fstrm;
1702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001703 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1704
Christopher Faulet99eff652019-08-11 23:11:30 +02001705 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1706 while (node) {
1707 fstrm = container_of(node, struct fcgi_strm, by_id);
1708 node = eb32_next(node);
1709 if (fstrm->state != FCGI_SS_CLOSED &&
1710 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1711 !fcgi_strm_send_abort(fconn, fstrm))
1712 return 0;
1713 }
1714 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001715 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1716 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001717 return 1;
1718}
1719
1720/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1721 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1722 * space to proceed. It is small enough to be encoded in an empty buffer.
1723 */
1724static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1725{
1726 struct buffer outbuf;
1727 struct buffer *mbuf;
1728 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1729 int ret;
1730
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001731 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1732
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 mbuf = br_tail(fconn->mbuf);
1734 retry:
1735 if (!fcgi_get_buf(fconn, mbuf)) {
1736 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001737 fstrm->flags |= FCGI_SF_BLK_MROOM;
1738 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1739 ret = 0;
1740 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001741 }
1742
1743 while (1) {
1744 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001745 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 break;
1747 realign_again:
1748 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1749 }
1750
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001751 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001752 goto full;
1753
1754 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1755 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001756 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001757 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001758 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001759
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001760 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1761 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001762 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001763 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001764 if (!fcgi_encode_begin_request(&outbuf, &rec))
1765 goto full;
1766
1767 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001768 TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001769 b_add(mbuf, outbuf.data);
1770 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1771 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001772 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001773 ret = 1;
1774
1775 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001776 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 return ret;
1778 full:
1779 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1780 goto retry;
1781 fconn->flags |= FCGI_CF_MUX_MFULL;
1782 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001783 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001784 ret = 0;
1785 goto end;
1786}
1787
1788/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1789 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1790 * space to proceed. It is small enough to be encoded in an empty buffer.
1791 */
1792static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1793 enum fcgi_record_type rtype)
1794{
1795 struct buffer outbuf;
1796 struct buffer *mbuf;
1797 int ret;
1798
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001799 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001800 mbuf = br_tail(fconn->mbuf);
1801 retry:
1802 if (!fcgi_get_buf(fconn, mbuf)) {
1803 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001804 fstrm->flags |= FCGI_SF_BLK_MROOM;
1805 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1806 ret = 0;
1807 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001808 }
1809
1810 while (1) {
1811 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001812 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001813 break;
1814 realign_again:
1815 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1816 }
1817
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001818 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001819 goto full;
1820
1821 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1822 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001823 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001824 outbuf.area[1] = rtype;
1825 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001826 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001827
1828 /* commit the record */
1829 b_add(mbuf, outbuf.data);
1830 ret = 1;
1831
1832 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001834 return ret;
1835 full:
1836 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1837 goto retry;
1838 fconn->flags |= FCGI_CF_MUX_MFULL;
1839 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001840 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001841 ret = 0;
1842 goto end;
1843}
1844
1845
1846/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1847 * marks the end of params.
1848 */
1849static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1850{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001851 int ret;
1852
1853 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1854 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001855 if (ret) {
1856 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001857 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001858 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001859 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001860}
1861
1862/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1863 * marks the end of input. On success, all the request was successfully sent.
1864 */
1865static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1866{
1867 int ret;
1868
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001869 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001870 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001871 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001872 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001873 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001874 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1875 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1876 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001877 return ret;
1878}
1879
1880/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1881 * stops the request processing.
1882 */
1883static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1884{
1885 int ret;
1886
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001887 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001888 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001889 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001890 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001891 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001892 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1893 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1894 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001895 return ret;
1896}
1897
1898/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1899 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1900 * several records are sent. However, a K/V param cannot be split between 2
1901 * records.
1902 */
1903static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1904 struct htx *htx)
1905{
1906 struct buffer outbuf;
1907 struct buffer *mbuf;
1908 struct htx_blk *blk;
1909 struct htx_sl *sl = NULL;
1910 struct fcgi_strm_params params;
1911 size_t total = 0;
1912
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001913 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1914
Christopher Faulet99eff652019-08-11 23:11:30 +02001915 memset(&params, 0, sizeof(params));
1916 params.p = get_trash_chunk();
1917
1918 mbuf = br_tail(fconn->mbuf);
1919 retry:
1920 if (!fcgi_get_buf(fconn, mbuf)) {
1921 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001922 fstrm->flags |= FCGI_SF_BLK_MROOM;
1923 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1924 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001925 }
1926
1927 while (1) {
1928 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001929 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001930 break;
1931 realign_again:
1932 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1933 }
1934
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001935 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001936 goto full;
1937
1938 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1939 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001940 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001941 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001942 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001943
1944 blk = htx_get_head_blk(htx);
1945 while (blk) {
1946 enum htx_blk_type type;
1947 uint32_t size = htx_get_blksz(blk);
1948 struct fcgi_param p;
1949
1950 type = htx_get_blk_type(blk);
1951 switch (type) {
1952 case HTX_BLK_REQ_SL:
1953 sl = htx_get_blk_ptr(htx, blk);
1954 if (sl->info.req.meth == HTTP_METH_HEAD)
1955 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1956 if (sl->flags & HTX_SL_F_VER_11)
1957 fstrm->h1m.flags |= H1_MF_VER_11;
1958 break;
1959
1960 case HTX_BLK_HDR:
1961 p.n = htx_get_blk_name(htx, blk);
1962 p.v = htx_get_blk_value(htx, blk);
1963
1964 if (istmatch(p.n, ist(":fcgi-"))) {
1965 p.n.ptr += 6;
1966 p.n.len -= 6;
1967 if (isteq(p.n, ist("gateway_interface")))
1968 params.mask |= FCGI_SP_CGI_GATEWAY;
1969 else if (isteq(p.n, ist("document_root"))) {
1970 params.mask |= FCGI_SP_DOC_ROOT;
1971 params.docroot = p.v;
1972 }
1973 else if (isteq(p.n, ist("script_name"))) {
1974 params.mask |= FCGI_SP_SCRIPT_NAME;
1975 params.scriptname = p.v;
1976 }
1977 else if (isteq(p.n, ist("path_info"))) {
1978 params.mask |= FCGI_SP_PATH_INFO;
1979 params.pathinfo = p.v;
1980 }
1981 else if (isteq(p.n, ist("request_uri"))) {
1982 params.mask |= FCGI_SP_REQ_URI;
1983 params.uri = p.v;
1984 }
1985 else if (isteq(p.n, ist("request_meth")))
1986 params.mask |= FCGI_SP_REQ_METH;
1987 else if (isteq(p.n, ist("query_string")))
1988 params.mask |= FCGI_SP_REQ_QS;
1989 else if (isteq(p.n, ist("server_name")))
1990 params.mask |= FCGI_SP_SRV_NAME;
1991 else if (isteq(p.n, ist("server_port")))
1992 params.mask |= FCGI_SP_SRV_PORT;
1993 else if (isteq(p.n, ist("server_protocol")))
1994 params.mask |= FCGI_SP_SRV_PROTO;
1995 else if (isteq(p.n, ist("remote_addr")))
1996 params.mask |= FCGI_SP_REM_ADDR;
1997 else if (isteq(p.n, ist("remote_port")))
1998 params.mask |= FCGI_SP_REM_PORT;
1999 else if (isteq(p.n, ist("script_filename")))
2000 params.mask |= FCGI_SP_SCRIPT_FILE;
2001 else if (isteq(p.n, ist("path_translated")))
2002 params.mask |= FCGI_SP_PATH_TRANS;
2003 else if (isteq(p.n, ist("https")))
2004 params.mask |= FCGI_SP_HTTPS;
2005 }
2006 else if (isteq(p.n, ist("content-length"))) {
2007 p.n = ist("CONTENT_LENGTH");
2008 params.mask |= FCGI_SP_CONT_LEN;
2009 }
2010 else if (isteq(p.n, ist("content-type")))
2011 p.n = ist("CONTENT_TYPE");
2012 else {
2013 if (isteq(p.n, ist("host")))
2014 params.srv_name = p.v;
2015
Christopher Faulet67d58092019-10-02 10:51:38 +02002016 /* Skip header if same name is used to add the server name */
2017 if (fconn->proxy->server_id_hdr_name &&
2018 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2019 break;
2020
Christopher Faulet99eff652019-08-11 23:11:30 +02002021 memcpy(trash.area, "http_", 5);
2022 memcpy(trash.area+5, p.n.ptr, p.n.len);
2023 p.n = ist2(trash.area, p.n.len+5);
2024 }
2025
2026 if (!fcgi_encode_param(&outbuf, &p)) {
2027 if (b_space_wraps(mbuf))
2028 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002029 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002030 goto full;
2031 goto done;
2032 }
2033 break;
2034
2035 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002036 if (fconn->proxy->server_id_hdr_name) {
2037 struct server *srv = objt_server(fconn->conn->target);
2038
2039 if (!srv)
2040 goto done;
2041
2042 memcpy(trash.area, "http_", 5);
2043 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2044 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2045 p.v = ist(srv->id);
2046
2047 if (!fcgi_encode_param(&outbuf, &p)) {
2048 if (b_space_wraps(mbuf))
2049 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002050 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002051 goto full;
2052 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002053 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002054 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002055 goto done;
2056
2057 default:
2058 break;
2059 }
2060 total += size;
2061 blk = htx_remove_blk(htx, blk);
2062 }
2063
2064 done:
Christopher Faulet73518be2021-01-27 12:06:54 +01002065 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params)) {
2066 TRACE_ERROR("error setting default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002067 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002068 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002069
2070 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2071 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2072 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2073 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2074 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2075 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2076 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2077 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2078 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2079 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2080 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2081 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2082 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2083 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2084 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet73518be2021-01-27 12:06:54 +01002085 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS)) {
2086 TRACE_ERROR("error encoding default params", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002087 goto error;
Christopher Faulet73518be2021-01-27 12:06:54 +01002088 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002089
2090 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002091 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - FCGI_RECORD_HEADER_SZ});
2092 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002093 b_add(mbuf, outbuf.data);
2094
2095 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002096 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002097 return total;
2098 full:
2099 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2100 goto retry;
2101 fconn->flags |= FCGI_CF_MUX_MFULL;
2102 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002103 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002104 if (total)
2105 goto error;
2106 goto end;
2107
2108 error:
2109 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002110 TRACE_ERROR("processing error sending PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002111 fcgi_strm_error(fstrm);
2112 goto end;
2113}
2114
2115/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2116 * anything. STDIN records contain the request body.
2117 */
2118static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2119 struct htx *htx, size_t count, struct buffer *buf)
2120{
2121 struct buffer outbuf;
2122 struct buffer *mbuf;
2123 struct htx_blk *blk;
2124 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002125 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002126 size_t total = 0;
2127
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002128 extra_bytes = 0;
2129
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002130 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002131 if (!count)
2132 goto end;
2133
2134 mbuf = br_tail(fconn->mbuf);
2135 retry:
2136 if (!fcgi_get_buf(fconn, mbuf)) {
2137 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002138 fstrm->flags |= FCGI_SF_BLK_MROOM;
2139 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2140 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002141 }
2142
2143 /* Perform some optimizations to reduce the number of buffer copies.
2144 * First, if the mux's buffer is empty and the htx area contains exactly
2145 * one data block of the same size as the requested count, and this
2146 * count fits within the record size, then it's possible to simply swap
2147 * the caller's buffer with the mux's output buffer and adjust offsets
2148 * and length to match the entire DATA HTX block in the middle. In this
2149 * case we perform a true zero-copy operation from end-to-end. This is
2150 * the situation that happens all the time with large files. Second, if
2151 * this is not possible, but the mux's output buffer is empty, we still
2152 * have an opportunity to avoid the copy to the intermediary buffer, by
2153 * making the intermediary buffer's area point to the output buffer's
2154 * area. In this case we want to skip the HTX header to make sure that
2155 * copies remain aligned and that this operation remains possible all
2156 * the time. This goes for headers, data blocks and any data extracted
2157 * from the HTX blocks.
2158 */
2159 blk = htx_get_head_blk(htx);
2160 if (!blk)
2161 goto end;
2162 type = htx_get_blk_type(blk);
2163 size = htx_get_blksz(blk);
2164 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2165 void *old_area = mbuf->area;
2166
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002167 /* Last block of the message: Reserve the size for the empty stdin record */
2168 if (htx->flags & HTX_FL_EOM)
2169 extra_bytes = FCGI_RECORD_HEADER_SZ;
2170
Christopher Faulet99eff652019-08-11 23:11:30 +02002171 if (b_data(mbuf)) {
2172 /* Too bad there are data left there. We're willing to memcpy/memmove
2173 * up to 1/4 of the buffer, which means that it's OK to copy a large
2174 * record into a buffer containing few data if it needs to be realigned,
2175 * and that it's also OK to copy few data without realigning. Otherwise
2176 * we'll pretend the mbuf is full and wait for it to become empty.
2177 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002178 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002179 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002180 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002181 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002182 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002183 }
2184
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002185 TRACE_PROTO("sending stding data (zero-copy)", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002186 /* map a FCGI record to the HTX block so that we can put the
2187 * record header there.
2188 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002189 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - FCGI_RECORD_HEADER_SZ, size + FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002190 outbuf.area = b_head(mbuf);
2191
2192 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002193 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002194 fcgi_set_record_id(outbuf.area, fstrm->id);
2195 fcgi_set_record_size(outbuf.area, size);
2196
2197 /* and exchange with our old area */
2198 buf->area = old_area;
2199 buf->data = buf->head = 0;
2200 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002201
2202 htx = (struct htx *)buf->area;
2203 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002204 goto end;
2205 }
2206
2207 copy:
2208 while (1) {
2209 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002210 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002211 break;
2212 realign_again:
2213 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2214 }
2215
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002217 goto full;
2218
2219 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2220 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002221 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002222 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002223 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002224
2225 blk = htx_get_head_blk(htx);
2226 while (blk && count) {
2227 enum htx_blk_type type = htx_get_blk_type(blk);
2228 uint32_t size = htx_get_blksz(blk);
2229 struct ist v;
2230
2231 switch (type) {
2232 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 TRACE_PROTO("sending stding data", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002234 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002235
2236 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2237 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2238
2239 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002240 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002241 extra_bytes = 0;
2242 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002243
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002244 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002245 /* It doesn't fit at once. If it at least fits once split and
2246 * the amount of data to move is low, let's defragment the
2247 * buffer now.
2248 */
2249 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002250 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002251 b_data(mbuf) <= MAX_DATA_REALIGN)
2252 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002253 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002254 }
2255 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002256 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002257 goto full;
2258 goto done;
2259 }
2260 if (v.len != size) {
2261 total += v.len;
2262 count -= v.len;
2263 htx_cut_data_blk(htx, blk, v.len);
2264 goto done;
2265 }
2266 break;
2267
Christopher Faulet99eff652019-08-11 23:11:30 +02002268 default:
2269 break;
2270 }
2271 total += size;
2272 count -= size;
2273 blk = htx_remove_blk(htx, blk);
2274 }
2275
2276 done:
2277 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002278 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - FCGI_RECORD_HEADER_SZ});
2279 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002280 b_add(mbuf, outbuf.data);
2281
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002282 /* Send the empty stding here to finish the message */
2283 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2284 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2285 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2286 /* bytes already reserved for this record. It should not fail */
2287 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01002288 TRACE_ERROR("processing error sending empty STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002289 fcgi_strm_error(fstrm);
2290 }
2291 }
2292
Christopher Faulet99eff652019-08-11 23:11:30 +02002293 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002294 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002295 return total;
2296 full:
2297 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2298 goto retry;
2299 fconn->flags |= FCGI_CF_MUX_MFULL;
2300 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002301 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002302 goto end;
2303}
2304
2305/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2306 * anything. STDOUT records contain the entire response. All the content is
2307 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2308 */
2309static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2310{
2311 struct buffer *dbuf;
2312 size_t ret;
2313 size_t max;
2314
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002315 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2316
Christopher Faulet99eff652019-08-11 23:11:30 +02002317 dbuf = &fconn->dbuf;
2318
2319 /* Only padding remains */
2320 if (fconn->state == FCGI_CS_RECORD_P)
2321 goto end_transfer;
2322
2323 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2324 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2325 buf_room_for_htx_data(dbuf))
2326 goto fail; // incomplete record
2327
2328 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2329 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2331 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002332 }
2333
2334 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2335 max = buf_room_for_htx_data(&fstrm->rxbuf);
2336 if (!b_data(&fstrm->rxbuf))
2337 fstrm->rxbuf.head = sizeof(struct htx);
2338 if (max > fconn->drl)
2339 max = fconn->drl;
2340
2341 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2342 if (!ret)
2343 goto fail;
2344 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002345 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2346 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002347
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002348 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002349 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002350 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2351 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002352
2353 if (fconn->drl)
2354 goto fail;
2355
2356 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002357 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002358 fconn->drl += fconn->drp;
2359 fconn->drp = 0;
2360 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2361 b_del(&fconn->dbuf, ret);
2362 fconn->drl -= ret;
2363 if (fconn->drl)
2364 goto fail;
2365
2366 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002367 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2368 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002369 return 1;
2370 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002371 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002372 return 0;
2373}
2374
2375
2376/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2377 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002378 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002379 */
2380static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2381{
2382 int ret;
2383
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002384 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2385
Christopher Faulet99eff652019-08-11 23:11:30 +02002386 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002387 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002388 fconn->drl += fconn->drp;
2389 fconn->drp = 0;
2390 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2391 b_del(&fconn->dbuf, ret);
2392 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002393 if (fconn->drl) {
2394 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002395 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002397 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002398 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002399 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002400 TRACE_STATE("stdout data fully send, switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2401 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002402 return 1;
2403}
2404
2405/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2406 * anything.
2407 */
2408static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2409{
2410 struct buffer *dbuf;
2411 struct buffer tag;
2412 size_t ret;
2413
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002414 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002415 dbuf = &fconn->dbuf;
2416
2417 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002418 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002419 goto end_transfer;
2420
2421 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2422 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2423 buf_room_for_htx_data(dbuf))
2424 goto fail; // incomplete record
2425
2426 chunk_reset(&trash);
2427 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2428 if (!ret)
2429 goto fail;
2430 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002431 TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002432
2433 trash.area[ret] = '\n';
2434 trash.area[ret+1] = '\0';
2435 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002436 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002437
2438 if (fconn->drl)
2439 goto fail;
2440
2441 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002442 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002443 fconn->drl += fconn->drp;
2444 fconn->drp = 0;
2445 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2446 b_del(&fconn->dbuf, ret);
2447 fconn->drl -= ret;
2448 if (fconn->drl)
2449 goto fail;
2450 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002451 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2452 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002453 return 1;
2454 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002455 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002456 return 0;
2457}
2458
2459/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2460 * anything. If the empty STDOUT record is not already received, this one marks
2461 * the end of the response. It is highly unexpected, but if the record is larger
2462 * than a buffer and cannot be decoded in one time, an error is triggered and
2463 * the connection is closed. END_REQUEST record cannot be split.
2464 */
2465static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2466{
2467 struct buffer inbuf;
2468 struct buffer *dbuf;
2469 struct fcgi_end_request endreq;
2470
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002472 dbuf = &fconn->dbuf;
2473
2474 /* Record too large to be fully decoded */
Christopher Faulet73518be2021-01-27 12:06:54 +01002475 if (b_size(dbuf) < (fconn->drl + fconn->drp)) {
2476 TRACE_ERROR("END_REQUEST record too large", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002477 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002478 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002479
2480 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2482 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002483 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002484 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002485
2486 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2487 /* Realign the dmux buffer if the record wraps. It is unexpected
2488 * at this stage because it should be the first record received
2489 * from the FCGI application.
2490 */
2491 b_slow_realign(dbuf, trash.area, 0);
2492 }
2493
2494 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2495
Christopher Faulet73518be2021-01-27 12:06:54 +01002496 if (!fcgi_decode_end_request(&inbuf, 0, &endreq)) {
2497 TRACE_ERROR("END_REQUEST record decoding failure", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002498 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002499 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002500
2501 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002502 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002503 TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02002504 fstrm->proto_status = endreq.errcode;
2505 fcgi_strm_close(fstrm);
2506
2507 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2508 fconn->drl = 0;
2509 fconn->drp = 0;
2510 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002511 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2512 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002513 return 1;
2514
2515 fail:
2516 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002517 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002518 return 0;
2519}
2520
2521/* process Rx records to be demultiplexed */
2522static void fcgi_process_demux(struct fcgi_conn *fconn)
2523{
2524 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2525 struct fcgi_header hdr;
2526 int ret;
2527
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002528 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2529
Christopher Faulet99eff652019-08-11 23:11:30 +02002530 if (fconn->state == FCGI_CS_CLOSED)
2531 return;
2532
2533 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002534 if (fconn->state == FCGI_CS_INIT) {
2535 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2536 return;
2537 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002538 if (fconn->state == FCGI_CS_SETTINGS) {
2539 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002540 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002541 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
Christopher Faulet73518be2021-01-27 12:06:54 +01002542 if (!ret) {
2543 TRACE_ERROR("header record decoding failure", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002544 goto fail;
Christopher Faulet73518be2021-01-27 12:06:54 +01002545 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002546 b_del(&fconn->dbuf, ret);
2547
2548 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2549 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet73518be2021-01-27 12:06:54 +01002550 TRACE_ERROR("unexpected record type or flags", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002551 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002552 goto fail;
2553 }
2554 goto new_record;
2555 }
2556 }
2557
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002558 /* process as many incoming records as possible below */
2559 while (1) {
2560 if (!b_data(&fconn->dbuf)) {
2561 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2562 break;
2563 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002564
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002565 if (fconn->state == FCGI_CS_CLOSED) {
2566 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002567 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002568 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002569
2570 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002571 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002572 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2573 if (!ret)
2574 break;
2575 b_del(&fconn->dbuf, ret);
2576
2577 new_record:
2578 fconn->dsi = hdr.id;
2579 fconn->drt = hdr.type;
2580 fconn->drl = hdr.len;
2581 fconn->drp = hdr.padding;
2582 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002583 TRACE_STATE("FCGI record header rcvd, switching to RECORD_D", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002584 }
2585
2586 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2587 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2588
2589 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2590 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002591 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002592 fstrm->state == FCGI_SS_CLOSED ||
2593 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2594 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2595 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002597 fstrm->cs->flags |= CS_FL_RCV_MORE;
2598 fcgi_strm_notify_recv(fstrm);
2599 }
2600 fstrm = tmp_fstrm;
2601
2602 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2603 /* ignore all record for closed streams */
2604 goto ignore_record;
2605 }
2606 if (fstrm->state == FCGI_SS_IDLE) {
2607 /* ignore all record for unknown streams */
2608 goto ignore_record;
2609 }
2610
2611 switch (fconn->drt) {
2612 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002613 TRACE_PROTO("receiving FCGI GET_VALUES_RESULT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002614 ret = fcgi_conn_handle_values_result(fconn);
2615 break;
2616
2617 case FCGI_STDOUT:
2618 if (fstrm->flags & FCGI_SF_ES_RCVD)
2619 goto ignore_record;
2620
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002621 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002622 if (fconn->drl)
2623 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2624 else
2625 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2626 break;
2627
2628 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002629 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002630 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2631 break;
2632
2633 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 TRACE_PROTO("receiving FCGI END_REQUEST record", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002635 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2636 break;
2637
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002639 default:
2640 ignore_record:
2641 /* drop records that we ignore. They may be
2642 * larger than the buffer so we drain all of
2643 * their contents until we reach the end.
2644 */
2645 fconn->state = FCGI_CS_RECORD_P;
2646 fconn->drl += fconn->drp;
2647 fconn->drp = 0;
2648 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002649 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002650 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002651 b_del(&fconn->dbuf, ret);
2652 fconn->drl -= ret;
2653 ret = (fconn->drl == 0);
2654 }
2655
2656 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002657 if (ret <= 0) {
2658 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002659 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002660 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002661
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002662 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002663 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002664 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2665 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002666 }
2667
2668 fail:
2669 /* we can go here on missing data, blocked response or error */
2670 if (fstrm && fstrm->cs &&
2671 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002672 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002673 fstrm->state == FCGI_SS_CLOSED ||
2674 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2675 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2676 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 fstrm->cs->flags |= CS_FL_RCV_MORE;
2679 fcgi_strm_notify_recv(fstrm);
2680 }
2681
2682 fcgi_conn_restart_reading(fconn, 0);
2683}
2684
2685/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2686 * the end.
2687 */
2688static int fcgi_process_mux(struct fcgi_conn *fconn)
2689{
2690 struct fcgi_strm *fstrm, *fstrm_back;
2691
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002692 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2693
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2695 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2696 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2697 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002698 TRACE_STATE("switching to RECORD_H", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002699 fcgi_wake_unassigned_streams(fconn);
2700 goto mux;
2701 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002702 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002703 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2704 goto fail;
2705 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002706 TRACE_STATE("switching to SETTINGS", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002707 }
2708 /* need to wait for the other side */
2709 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002710 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002711 }
2712
2713 mux:
2714 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2715 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2716 break;
2717
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002718 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002719 continue;
2720
Willy Tarreau7aad7032020-01-16 17:20:57 +01002721 /* If the sender changed his mind and unsubscribed, let's just
2722 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002723 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002724 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2725 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 LIST_DEL_INIT(&fstrm->send_list);
2727 continue;
2728 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002729
2730 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002731 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2732 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002733 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002734 tasklet_wakeup(fstrm->subs->tasklet);
2735 fstrm->subs->events &= ~SUB_RETRY_SEND;
2736 if (!fstrm->subs->events)
2737 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002738 } else {
2739 /* it's the shut request that was queued */
2740 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2741 tasklet_wakeup(fstrm->shut_tl);
2742 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 }
2744
2745 fail:
2746 if (fconn->state == FCGI_CS_CLOSED) {
2747 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2748 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2750 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002752 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002753 }
2754 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002755
2756 done:
2757 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002758 return 1;
2759}
2760
2761
2762/* Attempt to read data, and subscribe if none available.
2763 * The function returns 1 if data has been received, otherwise zero.
2764 */
2765static int fcgi_recv(struct fcgi_conn *fconn)
2766{
2767 struct connection *conn = fconn->conn;
2768 struct buffer *buf;
2769 int max;
2770 size_t ret;
2771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002772 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2773
2774 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2775 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002776 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002778
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002779 if (!fcgi_recv_allowed(fconn)) {
2780 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002781 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002782 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002783
2784 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2785 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002786 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002787 fconn->flags |= FCGI_CF_DEM_DALLOC;
2788 return 0;
2789 }
2790
Christopher Faulet99eff652019-08-11 23:11:30 +02002791 if (!b_data(buf)) {
2792 /* try to pre-align the buffer like the
2793 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002794 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002795 * HTX block to alias it upon recv. We cannot use the
2796 * head because rcv_buf() will realign the buffer if
2797 * it's empty. Thus we cheat and pretend we already
2798 * have a few bytes there.
2799 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002800 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2801 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002802 }
2803 else
2804 max = buf_room_for_htx_data(buf);
2805
2806 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2807
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002808 if (max && !ret && fcgi_recv_allowed(fconn)) {
2809 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002810 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002811 }
2812 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002813 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002814
2815 if (!b_data(buf)) {
2816 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002817 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002818 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2819 }
2820
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002821 if (ret == max) {
2822 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002823 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002824 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002825
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002826 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002827 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2828}
2829
2830
2831/* Try to send data if possible.
2832 * The function returns 1 if data have been sent, otherwise zero.
2833 */
2834static int fcgi_send(struct fcgi_conn *fconn)
2835{
2836 struct connection *conn = fconn->conn;
2837 int done;
2838 int sent = 0;
2839
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002840 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2841
2842 if (conn->flags & CO_FL_ERROR) {
2843 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002844 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002845 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002846
2847
Willy Tarreau911db9b2020-01-23 16:27:54 +01002848 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002849 /* a handshake was requested */
2850 goto schedule;
2851 }
2852
2853 /* This loop is quite simple : it tries to fill as much as it can from
2854 * pending streams into the existing buffer until it's reportedly full
2855 * or the end of send requests is reached. Then it tries to send this
2856 * buffer's contents out, marks it not full if at least one byte could
2857 * be sent, and tries again.
2858 *
2859 * The snd_buf() function normally takes a "flags" argument which may
2860 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2861 * data immediately comes and CO_SFL_STREAMER to indicate that the
2862 * connection is streaming lots of data (used to increase TLS record
2863 * size at the expense of latency). The former can be sent any time
2864 * there's a buffer full flag, as it indicates at least one stream
2865 * attempted to send and failed so there are pending data. An
2866 * alternative would be to set it as long as there's an active stream
2867 * but that would be problematic for ACKs until we have an absolute
2868 * guarantee that all waiters have at least one byte to send. The
2869 * latter should possibly not be set for now.
2870 */
2871
2872 done = 0;
2873 while (!done) {
2874 unsigned int flags = 0;
2875 unsigned int released = 0;
2876 struct buffer *buf;
2877
2878 /* fill as much as we can into the current buffer */
2879 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2880 done = fcgi_process_mux(fconn);
2881
2882 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2883 done = 1; // we won't go further without extra buffers
2884
2885 if (conn->flags & CO_FL_ERROR)
2886 break;
2887
2888 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2889 flags |= CO_SFL_MSG_MORE;
2890
2891 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2892 if (b_data(buf)) {
2893 int ret;
2894
2895 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2896 if (!ret) {
2897 done = 1;
2898 break;
2899 }
2900 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002901 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002902 b_del(buf, ret);
2903 if (b_data(buf)) {
2904 done = 1;
2905 break;
2906 }
2907 }
2908 b_free(buf);
2909 released++;
2910 }
2911
2912 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002913 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002914
2915 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002916 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2917 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002918 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2919 }
2920
2921 if (conn->flags & CO_FL_SOCK_WR_SH) {
2922 /* output closed, nothing to send, clear the buffer to release it */
2923 b_reset(br_tail(fconn->mbuf));
2924 }
2925 /* We're not full anymore, so we can wake any task that are waiting
2926 * for us.
2927 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002928 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002929 struct fcgi_strm *fstrm;
2930
2931 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2932 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2933 break;
2934
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002935 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002936 continue;
2937
Willy Tarreau7aad7032020-01-16 17:20:57 +01002938 /* If the sender changed his mind and unsubscribed, let's just
2939 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002940 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002941 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2942 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002943 LIST_DEL_INIT(&fstrm->send_list);
2944 continue;
2945 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002946
2947 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002948 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002949 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002950 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002951 tasklet_wakeup(fstrm->subs->tasklet);
2952 fstrm->subs->events &= ~SUB_RETRY_SEND;
2953 if (!fstrm->subs->events)
2954 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002955 } else {
2956 /* it's the shut request that was queued */
2957 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2958 tasklet_wakeup(fstrm->shut_tl);
2959 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002960 }
2961 }
2962 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002963 if (!br_data(fconn->mbuf)) {
2964 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002965 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002966 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002967schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002968 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2969 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002970 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002971 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002972
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002973 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002974 return sent;
2975}
2976
2977/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreau691d5032021-01-20 14:55:01 +01002978struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet99eff652019-08-11 23:11:30 +02002979{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002980 struct connection *conn;
2981 struct fcgi_conn *fconn;
2982 struct tasklet *tl = (struct tasklet *)t;
2983 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002984 int ret = 0;
2985
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002986
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002987 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002988 if (tl->context == NULL) {
2989 /* The connection has been taken over by another thread,
2990 * we're no longer responsible for it, so just free the
2991 * tasklet, and do nothing.
2992 */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002993 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002994 tasklet_free(tl);
2995 return NULL;
2996
2997 }
2998 fconn = ctx;
2999 conn = fconn->conn;
3000
3001 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3002
3003 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3004 if (conn_in_list)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003005 conn_delete_from_tree(&conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003006
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003007 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003008
Christopher Faulet99eff652019-08-11 23:11:30 +02003009 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3010 ret = fcgi_send(fconn);
3011 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
3012 ret |= fcgi_recv(fconn);
3013 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003014 ret = fcgi_process(fconn);
3015
3016 /* If we were in an idle list, we want to add it back into it,
3017 * unless fcgi_process() returned -1, which mean it has destroyed
3018 * the connection (testing !ret is enough, if fcgi_process() wasn't
3019 * called then ret will be 0 anyway.
3020 */
3021 if (!ret && conn_in_list) {
3022 struct server *srv = objt_server(conn->target);
3023
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003024 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003025 if (conn_in_list == CO_FL_SAFE_LIST)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003026 ebmb_insert(&srv->safe_conns_tree[tid], &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003027 else
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003028 ebmb_insert(&srv->idle_conns_tree[tid], &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003029 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003030 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003031 return NULL;
3032}
3033
3034/* callback called on any event by the connection handler.
3035 * It applies changes and returns zero, or < 0 if it wants immediate
3036 * destruction of the connection (which normally doesn not happen in FCGI).
3037 */
3038static int fcgi_process(struct fcgi_conn *fconn)
3039{
3040 struct connection *conn = fconn->conn;
3041
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003042 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3043
Christopher Faulet99eff652019-08-11 23:11:30 +02003044 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3045 fcgi_process_demux(fconn);
3046
3047 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3048 b_reset(&fconn->dbuf);
3049
3050 if (buf_room_for_htx_data(&fconn->dbuf))
3051 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3052 }
3053 fcgi_send(fconn);
3054
Willy Tarreauc3914d42020-09-24 08:39:22 +02003055 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003056 /* frontend is stopping, reload likely in progress, let's try
3057 * to announce a graceful shutdown if not yet done. We don't
3058 * care if it fails, it will be tried again later.
3059 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003060 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 +02003061 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3062 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3063 fcgi_conn_send_aborts(fconn);
3064 }
3065 }
3066
3067 /*
3068 * If we received early data, and the handshake is done, wake
3069 * any stream that was waiting for it.
3070 */
3071 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003072 (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 +02003073 struct eb32_node *node;
3074 struct fcgi_strm *fstrm;
3075
3076 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3077 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3078
3079 while (node) {
3080 fstrm = container_of(node, struct fcgi_strm, by_id);
3081 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3082 fcgi_strm_notify_recv(fstrm);
3083 node = eb32_next(node);
3084 }
3085 }
3086
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003087 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003088 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3089 eb_is_empty(&fconn->streams_by_id)) {
3090 fcgi_wake_some_streams(fconn, 0);
3091
3092 if (eb_is_empty(&fconn->streams_by_id)) {
3093 /* no more stream, kill the connection now */
3094 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003095 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003096 return -1;
3097 }
3098 }
3099
3100 if (!b_data(&fconn->dbuf))
3101 fcgi_release_buf(fconn, &fconn->dbuf);
3102
3103 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3104 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3105 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3106 fcgi_release_mbuf(fconn);
3107
3108 if (fconn->task) {
3109 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3110 task_queue(fconn->task);
3111 }
3112
3113 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003114 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003115 return 0;
3116}
3117
3118
3119/* wake-up function called by the connection layer (mux_ops.wake) */
3120static int fcgi_wake(struct connection *conn)
3121{
3122 struct fcgi_conn *fconn = conn->ctx;
3123
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003124 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003125 return (fcgi_process(fconn));
3126}
3127
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003128
3129static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3130{
3131 int ret = 0;
3132 switch (mux_ctl) {
3133 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003134 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003135 ret |= MUX_STATUS_READY;
3136 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003137 case MUX_EXIT_STATUS:
3138 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003139 default:
3140 return -1;
3141 }
3142}
3143
Christopher Faulet99eff652019-08-11 23:11:30 +02003144/* Connection timeout management. The principle is that if there's no receipt
3145 * nor sending for a certain amount of time, the connection is closed. If the
3146 * MUX buffer still has lying data or is not allocatable, the connection is
3147 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003148 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003149 */
Willy Tarreauac6322d2021-01-29 12:33:46 +01003150struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003151{
3152 struct fcgi_conn *fconn = context;
3153 int expired = tick_is_expired(t->expire, now_ms);
3154
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003155 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3156
Willy Tarreau60814ff2020-06-30 11:19:23 +02003157 if (fconn) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003158 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003159
3160 /* Somebody already stole the connection from us, so we should not
3161 * free it, we just have to free the task.
3162 */
3163 if (!t->context) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003164 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003165 fconn = NULL;
3166 goto do_leave;
3167 }
3168
Willy Tarreau60814ff2020-06-30 11:19:23 +02003169 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003170 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003171 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3172 return t;
3173 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003174
Willy Tarreau60814ff2020-06-30 11:19:23 +02003175 /* We're about to destroy the connection, so make sure nobody attempts
3176 * to steal it from us.
3177 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003178 if (fconn->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003179 conn_delete_from_tree(&fconn->conn->hash_node->node);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003180
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003181 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003182 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003183
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003184do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003185 task_destroy(t);
3186
3187 if (!fconn) {
3188 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003189 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003190 return NULL;
3191 }
3192
3193 fconn->task = NULL;
3194 fconn->state = FCGI_CS_CLOSED;
3195 fcgi_wake_some_streams(fconn, 0);
3196
3197 if (br_data(fconn->mbuf)) {
3198 /* don't even try to send aborts, the buffer is stuck */
3199 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3200 goto end;
3201 }
3202
3203 /* try to send but no need to insist */
3204 if (!fcgi_conn_send_aborts(fconn))
3205 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3206
3207 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3208 conn_xprt_ready(fconn->conn)) {
3209 unsigned int released = 0;
3210 struct buffer *buf;
3211
3212 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3213 if (b_data(buf)) {
3214 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3215 buf, b_data(buf), 0);
3216 if (!ret)
3217 break;
3218 b_del(buf, ret);
3219 if (b_data(buf))
3220 break;
3221 b_free(buf);
3222 released++;
3223 }
3224 }
3225
3226 if (released)
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01003227 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003228 }
3229
3230 end:
3231 /* either we can release everything now or it will be done later once
3232 * the last stream closes.
3233 */
3234 if (eb_is_empty(&fconn->streams_by_id))
3235 fcgi_release(fconn);
3236
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003237 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003238 return NULL;
3239}
3240
3241
3242/*******************************************/
3243/* functions below are used by the streams */
3244/*******************************************/
3245
3246/* Append the description of what is present in error snapshot <es> into <out>.
3247 * The description must be small enough to always fit in a buffer. The output
3248 * buffer may be the trash so the trash must not be used inside this function.
3249 */
3250static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3251{
3252 chunk_appendf(out,
3253 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3254 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3255 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3256 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3257 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3258 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3259}
3260/*
3261 * Capture a bad response and archive it in the proxy's structure. By default
3262 * it tries to report the error position as h1m->err_pos. However if this one is
3263 * not set, it will then report h1m->next, which is the last known parsing
3264 * point. The function is able to deal with wrapping buffers. It always displays
3265 * buffers as a contiguous area starting at buf->p. The direction is determined
3266 * thanks to the h1m's flags.
3267 */
3268static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3269 struct h1m *h1m, struct buffer *buf)
3270{
3271 struct session *sess = fstrm->sess;
3272 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003273 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003274 union error_snapshot_ctx ctx;
3275
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003276 if (fstrm->cs && fstrm->cs->data) {
3277 if (sess == NULL)
3278 sess = si_strm(fstrm->cs->data)->sess;
3279 if (!(h1m->flags & H1_MF_RESP))
3280 other_end = si_strm(fstrm->cs->data)->be;
3281 else
3282 other_end = sess->fe;
3283 } else
3284 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003285 /* http-specific part now */
3286 ctx.h1.state = h1m->state;
3287 ctx.h1.c_flags = fconn->flags;
3288 ctx.h1.s_flags = fstrm->flags;
3289 ctx.h1.m_flags = h1m->flags;
3290 ctx.h1.m_clen = h1m->curr_len;
3291 ctx.h1.m_blen = h1m->body_len;
3292
3293 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3294 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3295 &ctx, fcgi_show_error_snapshot);
3296}
3297
3298static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3299 struct buffer *buf, size_t *ofs, size_t max)
3300{
3301 int ret;
3302
Willy Tarreau022e5e52020-09-10 09:33:15 +02003303 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 +02003304 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3305 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003306 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 +02003307 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003308 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 +02003309 fcgi_strm_error(fstrm);
3310 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3311 }
3312 goto end;
3313 }
3314
3315 *ofs += ret;
3316 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003317 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 +02003318 return ret;
3319
3320}
3321
Christopher Fauletaf542632019-10-01 21:52:49 +02003322static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003323 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3324{
3325 int ret;
3326
Willy Tarreau022e5e52020-09-10 09:33:15 +02003327 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 +02003328 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003329 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003330 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 +02003331 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003332 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 +02003333 fcgi_strm_error(fstrm);
3334 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3335 }
3336 goto end;
3337 }
3338 *ofs += ret;
3339 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003340 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 +02003341 return ret;
3342}
3343
3344static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3345 struct buffer *buf, size_t *ofs, size_t max)
3346{
3347 int ret;
3348
Willy Tarreau022e5e52020-09-10 09:33:15 +02003349 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 +02003350 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003351 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003352 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 +02003353 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003354 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 +02003355 fcgi_strm_error(fstrm);
3356 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3357 }
3358 goto end;
3359 }
3360 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003361 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003362 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 +02003363 return ret;
3364}
3365
Christopher Faulet99eff652019-08-11 23:11:30 +02003366static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3367{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003368 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003369 struct htx *htx;
3370 struct h1m *h1m = &fstrm->h1m;
3371 size_t ret, data, total = 0;
3372
3373 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003374 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3375
Christopher Faulet99eff652019-08-11 23:11:30 +02003376 data = htx->data;
3377 if (fstrm->state == FCGI_SS_ERROR)
3378 goto end;
3379
3380 do {
3381 size_t used = htx_used_space(htx);
3382
3383 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003384 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003385 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3386 if (!ret)
3387 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003388
3389 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3390
Christopher Faulet99eff652019-08-11 23:11:30 +02003391 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3392 struct htx_blk *blk = htx_get_head_blk(htx);
3393 struct htx_sl *sl;
3394
3395 if (!blk)
3396 break;
3397 sl = htx_get_blk_ptr(htx, blk);
3398 sl->flags |= HTX_SL_F_XFER_LEN;
3399 htx->extra = 0;
3400 }
3401 }
3402 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003403 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003404 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003405
3406 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3407 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3408 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003409 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003410 h1m->state = H1_MSG_DONE;
3411 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3412 }
3413
Christopher Faulet76014fd2019-12-10 11:47:22 +01003414 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003415 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003416
3417 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 +02003418 }
3419 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003420 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3421 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3422 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003423 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003424
Christopher Faulet76014fd2019-12-10 11:47:22 +01003425 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 +02003426 }
3427 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003428 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 +02003429 if (b_data(&fstrm->rxbuf) > total) {
3430 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003431 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003432 fcgi_strm_error(fstrm);
3433 }
3434 break;
3435 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003436 else {
3437 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet73518be2021-01-27 12:06:54 +01003438 TRACE_ERROR("unexpected processing error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003439 fcgi_strm_error(fstrm);
3440 break;
3441 }
3442
3443 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003444 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003445
3446 if (fstrm->state == FCGI_SS_ERROR) {
3447 b_reset(&fstrm->rxbuf);
3448 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003449 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003450 return 0;
3451 }
3452
3453 b_del(&fstrm->rxbuf, total);
3454
3455 end:
3456 htx_to_buf(htx, buf);
3457 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003458 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003459 return ret;
3460}
3461
3462/*
3463 * Attach a new stream to a connection
3464 * (Used for outgoing connections)
3465 */
3466static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3467{
3468 struct conn_stream *cs;
3469 struct fcgi_strm *fstrm;
3470 struct fcgi_conn *fconn = conn->ctx;
3471
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003472 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003473 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003474 if (!cs) {
Christopher Faulet73518be2021-01-27 12:06:54 +01003475 TRACE_ERROR("CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3476 goto err;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003477 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003478 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3479 if (!fstrm) {
3480 cs_free(cs);
Christopher Faulet73518be2021-01-27 12:06:54 +01003481 goto err;
Christopher Faulet99eff652019-08-11 23:11:30 +02003482 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003483 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003484 return cs;
Christopher Faulet73518be2021-01-27 12:06:54 +01003485
3486 err:
3487 TRACE_DEVEL("leaving on error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
3488 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003489}
3490
3491/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3492 * We have to scan because we may have some orphan streams. It might be
3493 * beneficial to scan backwards from the end to reduce the likeliness to find
3494 * orphans.
3495 */
3496static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3497{
3498 struct fcgi_conn *fconn = conn->ctx;
3499 struct fcgi_strm *fstrm;
3500 struct eb32_node *node;
3501
3502 node = eb32_first(&fconn->streams_by_id);
3503 while (node) {
3504 fstrm = container_of(node, struct fcgi_strm, by_id);
3505 if (fstrm->cs)
3506 return fstrm->cs;
3507 node = eb32_next(node);
3508 }
3509 return NULL;
3510}
3511
3512/*
3513 * Destroy the mux and the associated connection, if it is no longer used
3514 */
3515static void fcgi_destroy(void *ctx)
3516{
3517 struct fcgi_conn *fconn = ctx;
3518
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003519 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003520 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3521 fcgi_release(fconn);
3522}
3523
3524/*
3525 * Detach the stream from the connection and possibly release the connection.
3526 */
3527static void fcgi_detach(struct conn_stream *cs)
3528{
3529 struct fcgi_strm *fstrm = cs->ctx;
3530 struct fcgi_conn *fconn;
3531 struct session *sess;
3532
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003533 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3534
Christopher Faulet99eff652019-08-11 23:11:30 +02003535 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003536 if (!fstrm) {
3537 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003538 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003539 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003540
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003541 /* there's no txbuf so we're certain no to be able to send anything */
3542 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003543
3544 sess = fstrm->sess;
3545 fconn = fstrm->fconn;
3546 fstrm->cs = NULL;
3547 fconn->nb_cs--;
3548
3549 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3550 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3551 fconn->streams_limit = 1;
3552 }
3553 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3554 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3555 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3556 fconn->state = FCGI_CS_CLOSED;
3557 }
3558
3559 /* this stream may be blocked waiting for some data to leave, so orphan
3560 * it in this case.
3561 */
3562 if (!(cs->conn->flags & CO_FL_ERROR) &&
3563 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003564 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003565 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003566 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003567 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003568 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003569
3570 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3571 /* unblock the connection if it was blocked on this stream. */
3572 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3573 fcgi_conn_restart_reading(fconn, 1);
3574 }
3575
3576 fcgi_strm_destroy(fstrm);
3577
3578 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003579 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003580 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003581 /* Add the connection in the session serverlist, if not already done */
3582 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3583 fconn->conn->owner = NULL;
3584 if (eb_is_empty(&fconn->streams_by_id)) {
3585 /* let's kill the connection right away */
3586 fconn->conn->mux->destroy(fconn);
3587 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3588 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003589 }
3590 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003591 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003592 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3593 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003594 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003595 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003596 }
3597 }
3598 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003599 else {
3600 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003601 /* If the connection is owned by the session, first remove it
3602 * from its list
3603 */
3604 if (fconn->conn->owner) {
3605 session_unown_conn(fconn->conn->owner, fconn->conn);
3606 fconn->conn->owner = NULL;
3607 }
3608
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003609 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003610 /* The server doesn't want it, let's kill the connection right away */
3611 fconn->conn->mux->destroy(fconn);
3612 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3613 return;
3614 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003615 /* At this point, the connection has been added to the
3616 * server idle list, so another thread may already have
3617 * hijacked it, so we can't do anything with it.
3618 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003619 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3620 return;
3621 }
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003622 else if (!fconn->conn->hash_node->node.node.leaf_p &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003623 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3624 !LIST_ADDED(&fconn->conn->session_list)) {
Amaury Denoyellef232cb32021-01-06 16:14:12 +01003625 ebmb_insert(&__objt_server(fconn->conn->target)->available_conns_tree[tid],
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003626 &fconn->conn->hash_node->node,
3627 sizeof(fconn->conn->hash_node->hash));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003628 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003629 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003630 }
3631
3632 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003633 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003634 */
3635 if (fcgi_conn_is_dead(fconn)) {
3636 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003637 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003638 fcgi_release(fconn);
3639 }
3640 else if (fconn->task) {
3641 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3642 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003643 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003644 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003645 else
3646 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003647}
3648
3649
3650/* Performs a synchronous or asynchronous shutr(). */
3651static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3652{
3653 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003654
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003655 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3656
Christopher Faulet99eff652019-08-11 23:11:30 +02003657 if (fstrm->state == FCGI_SS_CLOSED)
3658 goto done;
3659
3660 /* a connstream may require us to immediately kill the whole connection
3661 * for example because of a "tcp-request content reject" rule that is
3662 * normally used to limit abuse.
3663 */
3664 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003665 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3666 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003667 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003668 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003669 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003670 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003671 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3672 !fcgi_strm_send_abort(fconn, fstrm))
3673 goto add_to_list;
3674 }
3675
3676 fcgi_strm_close(fstrm);
3677
3678 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3679 tasklet_wakeup(fconn->wait_event.tasklet);
3680 done:
3681 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003683 return;
3684
3685 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003686 /* Let the handler know we want to shutr, and add ourselves to the
3687 * send list if not yet done. fcgi_deferred_shut() will be
3688 * automatically called via the shut_tl tasklet when there's room
3689 * again.
3690 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003691 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003692 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3694 }
3695 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003696 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003697 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003698 return;
3699}
3700
3701/* Performs a synchronous or asynchronous shutw(). */
3702static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3703{
3704 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003705
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003706 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3707
Christopher Faulet99eff652019-08-11 23:11:30 +02003708 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3709 goto done;
3710
3711 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3712 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3713 !fcgi_strm_send_abort(fconn, fstrm))
3714 goto add_to_list;
3715
3716 if (fstrm->state == FCGI_SS_HREM)
3717 fcgi_strm_close(fstrm);
3718 else
3719 fstrm->state = FCGI_SS_HLOC;
3720 } else {
3721 /* a connstream may require us to immediately kill the whole connection
3722 * for example because of a "tcp-request content reject" rule that is
3723 * normally used to limit abuse.
3724 */
3725 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003726 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3727 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003728 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003729 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003730
3731 fcgi_strm_close(fstrm);
3732 }
3733
3734 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3735 tasklet_wakeup(fconn->wait_event.tasklet);
3736 done:
3737 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003738 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003739 return;
3740
3741 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003742 /* Let the handler know we want to shutr, and add ourselves to the
3743 * send list if not yet done. fcgi_deferred_shut() will be
3744 * automatically called via the shut_tl tasklet when there's room
3745 * again.
3746 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003747 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003748 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003749 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3750 }
3751 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003752 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003753 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003754 return;
3755}
3756
Willy Tarreau7aad7032020-01-16 17:20:57 +01003757/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003758 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003759 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003760 */
Willy Tarreauac6322d2021-01-29 12:33:46 +01003761struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
Christopher Faulet99eff652019-08-11 23:11:30 +02003762{
3763 struct fcgi_strm *fstrm = ctx;
3764 struct fcgi_conn *fconn = fstrm->fconn;
3765
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003766 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3767
Willy Tarreau7aad7032020-01-16 17:20:57 +01003768 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3769 /* some data processing remains to be done first */
3770 goto end;
3771 }
3772
Christopher Faulet99eff652019-08-11 23:11:30 +02003773 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3774 fcgi_do_shutw(fstrm);
3775
3776 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3777 fcgi_do_shutr(fstrm);
3778
3779 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3780 /* We're done trying to send, remove ourself from the send_list */
3781 LIST_DEL_INIT(&fstrm->send_list);
3782
3783 if (!fstrm->cs) {
3784 fcgi_strm_destroy(fstrm);
3785 if (fcgi_conn_is_dead(fconn))
3786 fcgi_release(fconn);
3787 }
3788 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003789 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003790 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003791 return NULL;
3792}
3793
3794/* shutr() called by the conn_stream (mux_ops.shutr) */
3795static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3796{
3797 struct fcgi_strm *fstrm = cs->ctx;
3798
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003799 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003800 if (cs->flags & CS_FL_KILL_CONN)
3801 fstrm->flags |= FCGI_SF_KILL_CONN;
3802
3803 if (!mode)
3804 return;
3805
3806 fcgi_do_shutr(fstrm);
3807}
3808
3809/* shutw() called by the conn_stream (mux_ops.shutw) */
3810static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3811{
3812 struct fcgi_strm *fstrm = cs->ctx;
3813
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003814 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003815 if (cs->flags & CS_FL_KILL_CONN)
3816 fstrm->flags |= FCGI_SF_KILL_CONN;
3817
3818 fcgi_do_shutw(fstrm);
3819}
3820
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003821/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3822 * event subscriber <es> is not allowed to change from a previous call as long
3823 * as at least one event is still subscribed. The <event_type> must only be a
3824 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003826static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003827{
Christopher Faulet99eff652019-08-11 23:11:30 +02003828 struct fcgi_strm *fstrm = cs->ctx;
3829 struct fcgi_conn *fconn = fstrm->fconn;
3830
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003831 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003832 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003833
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003834 es->events |= event_type;
3835 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003836
3837 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003838 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003839
Christopher Faulet99eff652019-08-11 23:11:30 +02003840 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003841 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003842 if (!LIST_ADDED(&fstrm->send_list))
3843 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003844 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003845 return 0;
3846}
3847
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003848/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3849 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3850 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003851 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003852static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003853{
Christopher Faulet99eff652019-08-11 23:11:30 +02003854 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003855 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003856
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003857 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003858 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003859
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003860 es->events &= ~event_type;
3861 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003862 fstrm->subs = NULL;
3863
3864 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003865 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003866
Christopher Faulet99eff652019-08-11 23:11:30 +02003867 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003868 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003869 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003870 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3871 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003872 }
3873 return 0;
3874}
3875
3876/* Called from the upper layer, to receive data */
3877static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3878{
3879 struct fcgi_strm *fstrm = cs->ctx;
3880 struct fcgi_conn *fconn = fstrm->fconn;
3881 size_t ret = 0;
3882
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003883 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3884
Christopher Faulet99eff652019-08-11 23:11:30 +02003885 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3886 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003887 else
3888 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003889
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003890 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003891 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3892 else {
3893 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003894 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003895 cs->flags |= CS_FL_EOI;
3896 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3897 cs->flags |= CS_FL_EOS;
3898 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003899 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003900 cs->flags |= CS_FL_EOS;
3901 if (cs->flags & CS_FL_ERR_PENDING)
3902 cs->flags |= CS_FL_ERROR;
3903 fcgi_release_buf(fconn, &fstrm->rxbuf);
3904 }
3905
3906 if (ret && fconn->dsi == fstrm->id) {
3907 /* demux is blocking on this stream's buffer */
3908 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3909 fcgi_conn_restart_reading(fconn, 1);
3910 }
3911
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003912 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003913 return ret;
3914}
3915
3916
Christopher Faulet99eff652019-08-11 23:11:30 +02003917/* Called from the upper layer, to send data from buffer <buf> for no more than
3918 * <count> bytes. Returns the number of bytes effectively sent. Some status
3919 * flags may be updated on the conn_stream.
3920 */
3921static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3922{
3923 struct fcgi_strm *fstrm = cs->ctx;
3924 struct fcgi_conn *fconn = fstrm->fconn;
3925 size_t total = 0;
3926 size_t ret;
3927 struct htx *htx = NULL;
3928 struct htx_sl *sl;
3929 struct htx_blk *blk;
3930 uint32_t bsize;
3931
Willy Tarreau022e5e52020-09-10 09:33:15 +02003932 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003933
Christopher Faulet99eff652019-08-11 23:11:30 +02003934 /* If we were not just woken because we wanted to send but couldn't,
3935 * and there's somebody else that is waiting to send, do nothing,
3936 * we will subscribe later and be put at the end of the list
3937 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003938 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003939 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 +02003940 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003941 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003942 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003943
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003944 if (fconn->state < FCGI_CS_RECORD_H) {
3945 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003946 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003947 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003948
3949 htx = htxbuf(buf);
3950 if (fstrm->id == 0) {
3951 int32_t id = fcgi_conn_get_next_sid(fconn);
3952
3953 if (id < 0) {
3954 fcgi_strm_close(fstrm);
3955 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003956 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 +02003957 return 0;
3958 }
3959
3960 eb32_delete(&fstrm->by_id);
3961 fstrm->by_id.key = fstrm->id = id;
3962 fconn->max_id = id;
3963 fconn->nb_reserved--;
3964 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3965
3966
3967 /* Check if length of the body is known or if the message is
3968 * full. Otherwise, the request is invalid.
3969 */
3970 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003971 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003972 htx->flags |= HTX_FL_PARSING_ERROR;
3973 fcgi_strm_error(fstrm);
3974 goto done;
3975 }
3976 }
3977
3978 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003979 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 +02003980 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3981 goto done;
3982 }
3983
3984 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3985 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3986
Christopher Fauletfe410d62020-05-19 15:13:00 +02003987 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 count && !htx_is_empty(htx)) {
3989 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003990 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003991 bsize = htx_get_blksz(blk);
3992
3993 switch (htx_get_blk_type(blk)) {
3994 case HTX_BLK_REQ_SL:
3995 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003996 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 +02003997 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3998 if (!ret) {
3999 goto done;
4000 }
4001 total += ret;
4002 count -= ret;
4003 break;
4004
4005 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004006 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
4007 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
4008 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4009 if (!ret)
4010 goto done;
4011 }
4012 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
4013 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
4014 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4015 if (!ret)
4016 goto done;
4017 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004018 goto remove_blk;
4019
4020 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004021 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 +02004022 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4023 if (ret > 0) {
4024 htx = htx_from_buf(buf);
4025 total += ret;
4026 count -= ret;
4027 if (ret < bsize)
4028 goto done;
4029 }
4030 break;
4031
Christopher Faulet99eff652019-08-11 23:11:30 +02004032 default:
4033 remove_blk:
4034 htx_remove_blk(htx, blk);
4035 total += bsize;
4036 count -= bsize;
4037 break;
4038 }
4039 }
4040
4041 done:
4042 if (fstrm->state >= FCGI_SS_HLOC) {
4043 /* trim any possibly pending data after we close (extra CR-LF,
4044 * unprocessed trailers, abnormal extra data, ...)
4045 */
4046 total += count;
4047 count = 0;
4048 }
4049
4050 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004051 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 +02004052 cs_set_error(cs);
4053 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4054 fcgi_strm_close(fstrm);
4055 }
4056
4057 if (htx)
4058 htx_to_buf(htx, buf);
4059
Christopher Faulet99eff652019-08-11 23:11:30 +02004060 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004061 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4062 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 +02004063 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004064 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004065
4066 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004067 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4068 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004069 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004070
4071 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004072 return total;
4073}
4074
4075/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004076static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004077{
4078 struct fcgi_conn *fconn = conn->ctx;
4079 struct fcgi_strm *fstrm = NULL;
4080 struct eb32_node *node;
4081 int send_cnt = 0;
4082 int tree_cnt = 0;
4083 int orph_cnt = 0;
4084 struct buffer *hmbuf, *tmbuf;
4085
4086 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004087 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004088
4089 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4090 send_cnt++;
4091
4092 fstrm = NULL;
4093 node = eb32_first(&fconn->streams_by_id);
4094 while (node) {
4095 fstrm = container_of(node, struct fcgi_strm, by_id);
4096 tree_cnt++;
4097 if (!fstrm->cs)
4098 orph_cnt++;
4099 node = eb32_next(node);
4100 }
4101
4102 hmbuf = br_head(fconn->mbuf);
4103 tmbuf = br_tail(fconn->mbuf);
4104 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4105 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4106 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4107 fconn->state, fconn->max_id, fconn->flags,
4108 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4109 fconn->wait_event.events, fconn->dsi,
4110 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4111 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4112 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4113 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4114 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4115 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4116 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4117
4118 if (fstrm) {
4119 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4120 fstrm, fstrm->id, fstrm->flags,
4121 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4122 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4123 fstrm->cs);
4124 if (fstrm->cs)
4125 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4126 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004127 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4128 if (fstrm->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01004129 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4130 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4131 fstrm->subs->tasklet->calls,
4132 fstrm->subs->tasklet->context);
4133 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4134 chunk_appendf(&trash, ")");
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004135 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004136 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004137 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004138}
4139
4140/* Migrate the the connection to the current thread.
4141 * Return 0 if successful, non-zero otherwise.
4142 * Expected to be called with the old thread lock held.
4143 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004144static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004145{
4146 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004147 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004148
4149 if (fd_takeover(conn->handle.fd, conn) != 0)
4150 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004151
4152 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4153 /* We failed to takeover the xprt, even if the connection may
4154 * still be valid, flag it as error'd, as we have already
4155 * taken over the fd, and wake the tasklet, so that it will
4156 * destroy it.
4157 */
4158 conn->flags |= CO_FL_ERROR;
4159 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4160 return -1;
4161 }
4162
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004163 if (fcgi->wait_event.events)
4164 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4165 fcgi->wait_event.events, &fcgi->wait_event);
4166 /* To let the tasklet know it should free itself, and do nothing else,
4167 * set its context to NULL;
4168 */
4169 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004170 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004171
4172 task = fcgi->task;
4173 if (task) {
4174 task->context = NULL;
4175 fcgi->task = NULL;
4176 __ha_barrier_store();
4177 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004178
4179 fcgi->task = task_new(tid_bit);
4180 if (!fcgi->task) {
4181 fcgi_release(fcgi);
4182 return -1;
4183 }
4184 fcgi->task->process = fcgi_timeout_task;
4185 fcgi->task->context = fcgi;
4186 }
4187 fcgi->wait_event.tasklet = tasklet_new();
4188 if (!fcgi->wait_event.tasklet) {
4189 fcgi_release(fcgi);
4190 return -1;
4191 }
4192 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4193 fcgi->wait_event.tasklet->context = fcgi;
4194 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4195 SUB_RETRY_RECV, &fcgi->wait_event);
4196
4197 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004198}
4199
4200/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004201/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004202/****************************************/
4203
4204/* The mux operations */
4205static const struct mux_ops mux_fcgi_ops = {
4206 .init = fcgi_init,
4207 .wake = fcgi_wake,
4208 .attach = fcgi_attach,
4209 .get_first_cs = fcgi_get_first_cs,
4210 .detach = fcgi_detach,
4211 .destroy = fcgi_destroy,
4212 .avail_streams = fcgi_avail_streams,
4213 .used_streams = fcgi_used_streams,
4214 .rcv_buf = fcgi_rcv_buf,
4215 .snd_buf = fcgi_snd_buf,
4216 .subscribe = fcgi_subscribe,
4217 .unsubscribe = fcgi_unsubscribe,
4218 .shutr = fcgi_shutr,
4219 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004220 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004221 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004222 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004223 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004224 .name = "FCGI",
4225};
4226
4227
4228/* this mux registers FCGI proto */
4229static struct mux_proto_list mux_proto_fcgi =
4230{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4231
4232INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4233
4234/*
4235 * Local variables:
4236 * c-indent-level: 8
4237 * c-basic-offset: 8
4238 * End:
4239 */