blob: defe955e21278d463bd752a55e5326ff8a332a6e [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
356static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
357static 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);
361static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
362static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200363static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
364static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200365static void fcgi_strm_alert(struct fcgi_strm *fstrm);
366static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
367
368/* a dmumy management stream */
369static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
370 .cs = NULL,
371 .fconn = NULL,
372 .state = FCGI_SS_CLOSED,
373 .flags = FCGI_SF_NONE,
374 .id = 0,
375};
376
377/* and a dummy idle stream for use with any unknown stream */
378static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
379 .cs = NULL,
380 .fconn = NULL,
381 .state = FCGI_SS_IDLE,
382 .flags = FCGI_SF_NONE,
383 .id = 0,
384};
385
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200386/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
387static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
388{
389 switch (st) {
390 case FCGI_CS_INIT : return "INI";
391 case FCGI_CS_SETTINGS : return "STG";
392 case FCGI_CS_RECORD_H : return "RDH";
393 case FCGI_CS_RECORD_D : return "RDD";
394 case FCGI_CS_RECORD_P : return "RDP";
395 case FCGI_CS_CLOSED : return "CLO";
396 default : return "???";
397 }
398}
399
400/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
401static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
402{
403 switch (st) {
404 case FCGI_SS_IDLE : return "IDL";
405 case FCGI_SS_OPEN : return "OPN";
406 case FCGI_SS_HREM : return "RCL";
407 case FCGI_SS_HLOC : return "HCL";
408 case FCGI_SS_ERROR : return "ERR";
409 case FCGI_SS_CLOSED : return "CLO";
410 default : return "???";
411 }
412}
413
414
415/* the FCGI traces always expect that arg1, if non-null, is of type connection
416 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
417 * and that arg3, if non-null, is a htx for rx/tx headers.
418 */
419static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
420 const struct ist where, const struct ist func,
421 const void *a1, const void *a2, const void *a3, const void *a4)
422{
423 const struct connection *conn = a1;
424 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
425 const struct fcgi_strm *fstrm = a2;
426 const struct htx *htx = a3;
427 const size_t *val = a4;
428
429 if (!fconn)
430 fconn = (fstrm ? fstrm->fconn : NULL);
431
432 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
433 return;
434
435 /* Display the response state if fstrm is defined */
436 if (fstrm)
437 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
438
439 if (src->verbosity == FCGI_VERB_CLEAN)
440 return;
441
442 /* Display the value to the 4th argument (level > STATE) */
443 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100444 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200445
446 /* Display status-line if possible (verbosity > MINIMAL) */
447 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
448 const struct htx_blk *blk = htx_get_head_blk(htx);
449 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
450 enum htx_blk_type type = htx_get_blk_type(blk);
451
452 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
453 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
454 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
455 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
456 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
457 }
458
459 /* Display fconn info and, if defined, fstrm info */
460 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
461 if (fstrm)
462 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
463
464 if (!fstrm || fstrm->id <= 0)
465 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
466 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
467 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
468
469 if (src->verbosity == FCGI_VERB_MINIMAL)
470 return;
471
472 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
473 if (src->level > TRACE_LEVEL_USER) {
474 if (src->verbosity == FCGI_VERB_COMPLETE ||
475 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
476 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
477 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
478 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
479 if (src->verbosity == FCGI_VERB_COMPLETE ||
480 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
481 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
482 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
483
484 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
485 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
486 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
487 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
488 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
489 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
490 }
491
492 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
493 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
494 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
495 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
496 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
497 }
498
499 /* Display htx info if defined (level > USER) */
500 if (src->level > TRACE_LEVEL_USER && htx) {
501 int full = 0;
502
503 /* Full htx info (level > STATE && verbosity > SIMPLE) */
504 if (src->level > TRACE_LEVEL_STATE) {
505 if (src->verbosity == FCGI_VERB_COMPLETE)
506 full = 1;
507 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
508 full = 1;
509 }
510
511 chunk_memcat(&trace_buf, "\n\t", 2);
512 htx_dump(&trace_buf, htx, full);
513 }
514}
Christopher Faulet99eff652019-08-11 23:11:30 +0200515
516/*****************************************************/
517/* functions below are for dynamic buffer management */
518/*****************************************************/
519
520/* Indicates whether or not the we may call the fcgi_recv() function to attempt
521 * to receive data into the buffer and/or demux pending data. The condition is
522 * a bit complex due to some API limits for now. The rules are the following :
523 * - if an error or a shutdown was detected on the connection and the buffer
524 * is empty, we must not attempt to receive
525 * - if the demux buf failed to be allocated, we must not try to receive and
526 * we know there is nothing pending
527 * - if no flag indicates a blocking condition, we may attempt to receive,
528 * regardless of whether the demux buffer is full or not, so that only
529 * de demux part decides whether or not to block. This is needed because
530 * the connection API indeed prevents us from re-enabling receipt that is
531 * already enabled in a polled state, so we must always immediately stop
532 * as soon as the demux can't proceed so as never to hit an end of read
533 * with data pending in the buffers.
534 * - otherwise must may not attempt
535 */
536static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
537{
538 if (b_data(&fconn->dbuf) == 0 &&
539 (fconn->state == FCGI_CS_CLOSED ||
540 fconn->conn->flags & CO_FL_ERROR ||
541 conn_xprt_read0_pending(fconn->conn)))
542 return 0;
543
544 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
545 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
546 return 1;
547
548 return 0;
549}
550
551/* Restarts reading on the connection if it was not enabled */
552static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
553{
554 if (!fcgi_recv_allowed(fconn))
555 return;
556 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
557 (fconn->wait_event.events & SUB_RETRY_RECV))
558 return;
559 tasklet_wakeup(fconn->wait_event.tasklet);
560}
561
562
563/* Tries to grab a buffer and to re-enable processing on mux <target>. The
564 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
565 * the allocation succeeds, in which case the connection is woken up, or 0 if
566 * it's impossible to wake up and we prefer to be woken up later.
567 */
568static int fcgi_buf_available(void *target)
569{
570 struct fcgi_conn *fconn = target;
571 struct fcgi_strm *fstrm;
572
573 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200574 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200575 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
576 fcgi_conn_restart_reading(fconn, 1);
577 return 1;
578 }
579
580 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200581 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200582 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200583 if (fconn->flags & FCGI_CF_DEM_MROOM) {
584 fconn->flags &= ~FCGI_CF_DEM_MROOM;
585 fcgi_conn_restart_reading(fconn, 1);
586 }
587 return 1;
588 }
589
590 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
591 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
592 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200593 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200594 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
595 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200596 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200597 return 1;
598 }
599
600 return 0;
601}
602
603static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
604{
605 struct buffer *buf = NULL;
606
Willy Tarreau21046592020-02-26 10:39:36 +0100607 if (likely(!MT_LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200608 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
609 fconn->buf_wait.target = fconn;
610 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200611 MT_LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200612 }
613 return buf;
614}
615
616static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
617{
618 if (bptr->size) {
619 b_free(bptr);
620 offer_buffers(NULL, tasks_run_queue);
621 }
622}
623
624static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
625{
626 struct buffer *buf;
627 unsigned int count = 0;
628
629 while (b_size(buf = br_head_pick(fconn->mbuf))) {
630 b_free(buf);
631 count++;
632 }
633 if (count)
634 offer_buffers(NULL, tasks_run_queue);
635}
636
637/* Returns the number of allocatable outgoing streams for the connection taking
638 * the number reserved streams into account.
639 */
640static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
641{
642 int ret;
643
644 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
645 if (ret < 0)
646 ret = 0;
647 return ret;
648}
649
650/* Returns the number of streams in use on a connection to figure if it's
651 * idle or not. We check nb_cs and not nb_streams as the caller will want
652 * to know if it was the last one after a detach().
653 */
654static int fcgi_used_streams(struct connection *conn)
655{
656 struct fcgi_conn *fconn = conn->ctx;
657
658 return fconn->nb_cs;
659}
660
661/* Returns the number of concurrent streams available on the connection */
662static int fcgi_avail_streams(struct connection *conn)
663{
664 struct server *srv = objt_server(conn->target);
665 struct fcgi_conn *fconn = conn->ctx;
666 int ret1, ret2;
667
668 /* Don't open new stream if the connection is closed */
669 if (fconn->state == FCGI_CS_CLOSED)
670 return 0;
671
672 /* May be negative if this setting has changed */
673 ret1 = (fconn->streams_limit - fconn->nb_streams);
674
675 /* we must also consider the limit imposed by stream IDs */
676 ret2 = fcgi_streams_left(fconn);
677 ret1 = MIN(ret1, ret2);
678 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
679 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
680 ret1 = MIN(ret1, ret2);
681 }
682 return ret1;
683}
684
685/*****************************************************************/
686/* functions below are dedicated to the mux setup and management */
687/*****************************************************************/
688
689/* Initializes the mux once it's attached. Only outgoing connections are
690 * supported. So the context is already initialized before installing the
691 * mux. <input> is always used as Input buffer and may contain data. It is the
692 * caller responsibility to not reuse it anymore. Returns < 0 on error.
693 */
694static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
695 struct buffer *input)
696{
697 struct fcgi_conn *fconn;
698 struct fcgi_strm *fstrm;
699 struct fcgi_app *app = get_px_fcgi_app(px);
700 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200701 void *conn_ctx = conn->ctx;
702
703 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200704
705 if (!app)
706 goto fail_conn;
707
708 fconn = pool_alloc(pool_head_fcgi_conn);
709 if (!fconn)
710 goto fail_conn;
711
712 fconn->shut_timeout = fconn->timeout = px->timeout.server;
713 if (tick_isset(px->timeout.serverfin))
714 fconn->shut_timeout = px->timeout.serverfin;
715
716 fconn->flags = FCGI_CF_NONE;
717
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500718 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200719 if (app->flags & FCGI_APP_FL_KEEP_CONN)
720 fconn->flags |= FCGI_CF_KEEP_CONN;
721 if (app->flags & FCGI_APP_FL_GET_VALUES)
722 fconn->flags |= FCGI_CF_GET_VALUES;
723 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
724 fconn->flags |= FCGI_CF_MPXS_CONNS;
725
726 fconn->proxy = px;
727 fconn->app = app;
728 fconn->task = NULL;
729 if (tick_isset(fconn->timeout)) {
730 t = task_new(tid_bit);
731 if (!t)
732 goto fail;
733
734 fconn->task = t;
735 t->process = fcgi_timeout_task;
736 t->context = fconn;
737 t->expire = tick_add(now_ms, fconn->timeout);
738 }
739
740 fconn->wait_event.tasklet = tasklet_new();
741 if (!fconn->wait_event.tasklet)
742 goto fail;
743 fconn->wait_event.tasklet->process = fcgi_io_cb;
744 fconn->wait_event.tasklet->context = fconn;
745 fconn->wait_event.events = 0;
746
747 /* Initialise the context. */
748 fconn->state = FCGI_CS_INIT;
749 fconn->conn = conn;
750 fconn->streams_limit = app->maxreqs;
751 fconn->max_id = -1;
752 fconn->nb_streams = 0;
753 fconn->nb_cs = 0;
754 fconn->nb_reserved = 0;
755 fconn->stream_cnt = 0;
756
757 fconn->dbuf = *input;
758 fconn->dsi = -1;
759
760 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
761 fconn->streams_by_id = EB_ROOT;
762 LIST_INIT(&fconn->send_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100763 MT_LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200764
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200765 conn->ctx = fconn;
766
Christopher Faulet99eff652019-08-11 23:11:30 +0200767 if (t)
768 task_queue(t);
769
770 /* FIXME: this is temporary, for outgoing connections we need to
771 * immediately allocate a stream until the code is modified so that the
772 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200773 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200774 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200775 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200776 if (!fstrm)
777 goto fail;
778
Christopher Faulet99eff652019-08-11 23:11:30 +0200779
780 /* Repare to read something */
781 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200782 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200783 return 0;
784
785 fail:
786 task_destroy(t);
787 if (fconn->wait_event.tasklet)
788 tasklet_free(fconn->wait_event.tasklet);
789 pool_free(pool_head_fcgi_conn, fconn);
790 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200791 conn->ctx = conn_ctx; // restore saved ctx
792 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200793 return -1;
794}
795
796/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
797 * -1 if no more is allocatable.
798 */
799static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
800{
801 int32_t id = (fconn->max_id + 1) | 1;
802
803 if ((id & 0x80000000U))
804 id = -1;
805 return id;
806}
807
808/* Returns the stream associated with id <id> or NULL if not found */
809static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
810{
811 struct eb32_node *node;
812
813 if (id == 0)
814 return (struct fcgi_strm *)fcgi_mgmt_stream;
815
816 if (id > fconn->max_id)
817 return (struct fcgi_strm *)fcgi_unknown_stream;
818
819 node = eb32_lookup(&fconn->streams_by_id, id);
820 if (!node)
821 return (struct fcgi_strm *)fcgi_unknown_stream;
822 return container_of(node, struct fcgi_strm, by_id);
823}
824
825
826/* Release function. This one should be called to free all resources allocated
827 * to the mux.
828 */
829static void fcgi_release(struct fcgi_conn *fconn)
830{
William Dauchy477757c2020-08-07 22:19:23 +0200831 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200832
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200833 TRACE_POINT(FCGI_EV_FCONN_END);
834
Christopher Faulet99eff652019-08-11 23:11:30 +0200835 if (fconn) {
836 /* The connection must be attached to this mux to be released */
837 if (fconn->conn && fconn->conn->ctx == fconn)
838 conn = fconn->conn;
839
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200840 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
841
Willy Tarreau21046592020-02-26 10:39:36 +0100842 if (MT_LIST_ADDED(&fconn->buf_wait.list))
843 MT_LIST_DEL(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200844
845 fcgi_release_buf(fconn, &fconn->dbuf);
846 fcgi_release_mbuf(fconn);
847
848 if (fconn->task) {
849 fconn->task->context = NULL;
850 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
851 fconn->task = NULL;
852 }
853 if (fconn->wait_event.tasklet)
854 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200855 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200856 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
857 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200858
859 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200860 }
861
862 if (conn) {
863 conn->mux = NULL;
864 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200865 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200866
867 conn_stop_tracking(conn);
868 conn_full_close(conn);
869 if (conn->destroy_cb)
870 conn->destroy_cb(conn);
871 conn_free(conn);
872 }
873}
874
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200875/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
876 * pending on the connection AND if there is no more data in the demux
877 * buffer. The function returns 1 to report a read0 or 0 otherwise.
878 */
879static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
880{
881 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
882 return 1;
883 return 0;
884}
885
Christopher Faulet99eff652019-08-11 23:11:30 +0200886
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500887/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200888static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
889{
890 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
891 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
892 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
893 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
894 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
895 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
896 conn_xprt_read0_pending(fconn->conn))))
897 return 1;
898 return 0;
899}
900
901
902/********************************************************/
903/* functions below are for the FCGI protocol processing */
904/********************************************************/
905
Christopher Faulet99eff652019-08-11 23:11:30 +0200906/* Marks an error on the stream. */
907static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
908{
909 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200910 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
911 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200912 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200913 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
914 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200915 if (fstrm->cs)
916 cs_set_error(fstrm->cs);
917 }
918}
919
920/* Attempts to notify the data layer of recv availability */
921static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
922{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100923 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200924 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100925 tasklet_wakeup(fstrm->subs->tasklet);
926 fstrm->subs->events &= ~SUB_RETRY_RECV;
927 if (!fstrm->subs->events)
928 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200929 }
930}
931
932/* Attempts to notify the data layer of send availability */
933static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
934{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100935 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200936 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100937 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100938 tasklet_wakeup(fstrm->subs->tasklet);
939 fstrm->subs->events &= ~SUB_RETRY_SEND;
940 if (!fstrm->subs->events)
941 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200942 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100943 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
944 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
945 tasklet_wakeup(fstrm->shut_tl);
946 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200947}
948
949/* Alerts the data layer, trying to wake it up by all means, following
950 * this sequence :
951 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
952 * for recv
953 * - if its subscribed to send, then it's woken up for send
954 * - if it was subscribed to neither, its ->wake() callback is called
955 * It is safe to call this function with a closed stream which doesn't have a
956 * conn_stream anymore.
957 */
958static void fcgi_strm_alert(struct fcgi_strm *fstrm)
959{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200960 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100961 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100962 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200963 fcgi_strm_notify_recv(fstrm);
964 fcgi_strm_notify_send(fstrm);
965 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200966 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
967 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200968 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200969 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200970}
971
972/* Writes the 16-bit record size <len> at address <record> */
973static inline void fcgi_set_record_size(void *record, uint16_t len)
974{
975 uint8_t *out = (record + 4);
976
977 *out = (len >> 8);
978 *(out + 1) = (len & 0xff);
979}
980
981/* Writes the 16-bit stream id <id> at address <record> */
982static inline void fcgi_set_record_id(void *record, uint16_t id)
983{
984 uint8_t *out = (record + 2);
985
986 *out = (id >> 8);
987 *(out + 1) = (id & 0xff);
988}
989
990/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
991 * its connection if the stream was not yet closed. Please use this exclusively
992 * before closing a stream to ensure stream count is well maintained.
993 */
994static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
995{
996 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200997 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200998 fstrm->fconn->nb_streams--;
999 if (!fstrm->id)
1000 fstrm->fconn->nb_reserved--;
1001 if (fstrm->cs) {
1002 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1003 fcgi_strm_notify_recv(fstrm);
1004 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001005 fstrm->state = FCGI_SS_CLOSED;
1006 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1007 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001008 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001009}
1010
1011/* Detaches a FCGI stream from its FCGI connection and releases it to the
1012 * fcgi_strm pool.
1013 */
1014static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1015{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001016 struct connection *conn = fstrm->fconn->conn;
1017
1018 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1019
Christopher Faulet99eff652019-08-11 23:11:30 +02001020 fcgi_strm_close(fstrm);
1021 eb32_delete(&fstrm->by_id);
1022 if (b_size(&fstrm->rxbuf)) {
1023 b_free(&fstrm->rxbuf);
1024 offer_buffers(NULL, tasks_run_queue);
1025 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001026 if (fstrm->subs)
1027 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001028 /* There's no need to explicitly call unsubscribe here, the only
1029 * reference left would be in the fconn send_list/fctl_list, and if
1030 * we're in it, we're getting out anyway
1031 */
1032 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001033 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001034 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001035
1036 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001037}
1038
1039/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1040 * stream tree. In case of error, nothing is added and NULL is returned. The
1041 * causes of errors can be any failed memory allocation. The caller is
1042 * responsible for checking if the connection may support an extra stream prior
1043 * to calling this function.
1044 */
1045static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1046{
1047 struct fcgi_strm *fstrm;
1048
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001049 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1050
Christopher Faulet99eff652019-08-11 23:11:30 +02001051 fstrm = pool_alloc(pool_head_fcgi_strm);
1052 if (!fstrm)
1053 goto out;
1054
Willy Tarreau7aad7032020-01-16 17:20:57 +01001055 fstrm->shut_tl = tasklet_new();
1056 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001057 pool_free(pool_head_fcgi_strm, fstrm);
1058 goto out;
1059 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001060 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001061 fstrm->shut_tl->process = fcgi_deferred_shut;
1062 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001063 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001064 fstrm->fconn = fconn;
1065 fstrm->cs = NULL;
1066 fstrm->flags = FCGI_SF_NONE;
1067 fstrm->proto_status = 0;
1068 fstrm->state = FCGI_SS_IDLE;
1069 fstrm->rxbuf = BUF_NULL;
1070
1071 h1m_init_res(&fstrm->h1m);
1072 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1073 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1074
1075 fstrm->by_id.key = fstrm->id = id;
1076 if (id > 0)
1077 fconn->max_id = id;
1078 else
1079 fconn->nb_reserved++;
1080
1081 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1082 fconn->nb_streams++;
1083 fconn->stream_cnt++;
1084
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001085 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001086 return fstrm;
1087
1088 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001089 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 +02001090 return NULL;
1091}
1092
1093/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1094 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1095 * highest possible stream ID was reached.
1096 */
1097static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1098 struct session *sess)
1099{
1100 struct fcgi_strm *fstrm = NULL;
1101
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001102 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1103 if (fconn->nb_streams >= fconn->streams_limit) {
1104 TRACE_DEVEL("leaving on streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001105 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001106 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001107
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001108 if (fcgi_streams_left(fconn) < 1) {
1109 TRACE_DEVEL("leaving on !streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001110 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001111 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001112
1113 /* Defer choosing the ID until we send the first message to create the stream */
1114 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 if (!fstrm) {
1116 TRACE_DEVEL("leaving on fstrm creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001117 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001118 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001119
1120 fstrm->cs = cs;
1121 fstrm->sess = sess;
1122 cs->ctx = fstrm;
1123 fconn->nb_cs++;
1124
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001125 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001126 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001127
1128 out:
1129 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001130}
1131
1132/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1133 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1134 * automatically updated accordingly. If the stream is orphaned, it is
1135 * destroyed.
1136 */
1137static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1138{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001139 struct fcgi_conn *fconn = fstrm->fconn;
1140
1141 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1142
Christopher Faulet99eff652019-08-11 23:11:30 +02001143 if (!fstrm->cs) {
1144 /* this stream was already orphaned */
1145 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001146 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001147 return;
1148 }
1149
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001150 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001151 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001152 fstrm->state = FCGI_SS_HREM;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001153 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001154 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001155 else if (fstrm->state == FCGI_SS_HLOC)
1156 fcgi_strm_close(fstrm);
1157 }
1158
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001159 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001160 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1161 if (fstrm->cs->flags & CS_FL_EOS)
1162 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001163
1164 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001166 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1167 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001168 }
1169
1170 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001171
1172 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001173}
1174
1175/* Wakes unassigned streams (ID == 0) attached to the connection. */
1176static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1177{
1178 struct eb32_node *node;
1179 struct fcgi_strm *fstrm;
1180
1181 node = eb32_lookup(&fconn->streams_by_id, 0);
1182 while (node) {
1183 fstrm = container_of(node, struct fcgi_strm, by_id);
1184 if (fstrm->id > 0)
1185 break;
1186 node = eb32_next(node);
1187 fcgi_strm_wake_one_stream(fstrm);
1188 }
1189}
1190
1191/* Wakes the streams attached to the connection, whose id is greater than <last>
1192 * or unassigned.
1193 */
1194static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1195{
1196 struct eb32_node *node;
1197 struct fcgi_strm *fstrm;
1198
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001199 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1200
Christopher Faulet99eff652019-08-11 23:11:30 +02001201 /* Wake all streams with ID > last */
1202 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1203 while (node) {
1204 fstrm = container_of(node, struct fcgi_strm, by_id);
1205 node = eb32_next(node);
1206 fcgi_strm_wake_one_stream(fstrm);
1207 }
1208 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001209
1210 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001211}
1212
1213static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1214 struct htx *htx, struct htx_sl *sl,
1215 struct fcgi_strm_params *params)
1216{
1217 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1218 struct ist p;
1219
1220 if (!sl)
1221 goto error;
1222
1223 if (!(params->mask & FCGI_SP_DOC_ROOT))
1224 params->docroot = fconn->app->docroot;
1225
1226 if (!(params->mask & FCGI_SP_REQ_METH)) {
1227 p = htx_sl_req_meth(sl);
1228 params->meth = ist2(b_tail(params->p), p.len);
1229 chunk_memcat(params->p, p.ptr, p.len);
1230 }
1231 if (!(params->mask & FCGI_SP_REQ_URI)) {
1232 p = htx_sl_req_uri(sl);
1233 params->uri = ist2(b_tail(params->p), p.len);
1234 chunk_memcat(params->p, p.ptr, p.len);
1235 }
1236 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1237 p = htx_sl_req_vsn(sl);
1238 params->vsn = ist2(b_tail(params->p), p.len);
1239 chunk_memcat(params->p, p.ptr, p.len);
1240 }
1241 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1242 char *end;
1243 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001244 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001245 port = get_host_port(cli_conn->dst);
1246 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1247 if (!end)
1248 goto error;
1249 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1250 params->p->data += params->srv_port.len;
1251 }
1252 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1253 /* If no Host header found, use the server address to fill
1254 * srv_name */
1255 if (!istlen(params->srv_name)) {
1256 char *ptr = NULL;
1257
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001258 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001259 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1260 ptr = b_tail(params->p);
1261 if (ptr) {
1262 params->srv_name = ist2(ptr, strlen(ptr));
1263 params->p->data += params->srv_name.len;
1264 }
1265 }
1266 }
1267 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1268 char *ptr = NULL;
1269
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001270 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001271 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1272 ptr = b_tail(params->p);
1273 if (ptr) {
1274 params->rem_addr = ist2(ptr, strlen(ptr));
1275 params->p->data += params->rem_addr.len;
1276 }
1277 }
1278 if (!(params->mask & FCGI_SP_REM_PORT)) {
1279 char *end;
1280 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001281 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001282 port = get_host_port(cli_conn->src);
1283 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1284 if (!end)
1285 goto error;
1286 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1287 params->p->data += params->rem_port.len;
1288 }
1289 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1290 struct htx_blk *blk;
1291 enum htx_blk_type type;
1292 char *end;
1293 size_t len = 0;
1294
1295 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1296 type = htx_get_blk_type(blk);
1297
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001298 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet99eff652019-08-11 23:11:30 +02001299 break;
1300 if (type == HTX_BLK_DATA)
1301 len += htx_get_blksz(blk);
1302 }
1303 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1304 if (!end)
1305 goto error;
1306 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1307 params->p->data += params->cont_len.len;
1308 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001309#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001310 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001311 if (cli_conn)
1312 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001313 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001314#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001315 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1316 /* one of scriptname, pathinfo or query_string is no set */
1317 struct ist path = http_get_path(params->uri);
1318 int len;
1319
Christopher Faulet99eff652019-08-11 23:11:30 +02001320 /* No scrit_name set but no valid path ==> error */
1321 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1322 goto error;
1323
Christopher Faulet99eff652019-08-11 23:11:30 +02001324 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001325 if (!(params->mask & FCGI_SP_REQ_QS)) {
1326 struct ist qs = istfind(path, '?');
1327
1328 /* Update the path length */
1329 path.len -= qs.len;
1330
1331 /* Set the query-string skipping the '?', if any */
1332 if (istlen(qs))
1333 params->qs = istnext(qs);
1334 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001335
1336 /* If the script_name is set, don't try to deduce the path_info
1337 * too. The opposite is not true.
1338 */
1339 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1340 params->mask |= FCGI_SP_PATH_INFO;
1341 goto end;
1342 }
1343
Christopher Faulet0f17a442020-07-23 15:44:37 +02001344 /* Decode the path. it must first be copied to keep the URI
1345 * untouched.
1346 */
1347 chunk_memcat(params->p, path.ptr, path.len);
1348 path.ptr = b_tail(params->p) - path.len;
1349 len = url_decode(ist0(path), 0);
1350 if (len < 0)
1351 goto error;
1352 path.len = len;
1353
Christopher Faulet99eff652019-08-11 23:11:30 +02001354 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001355 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001356
1357 /* If there is no regex to match the pathinfo, just to the last
1358 * part and see if the index must be used.
1359 */
1360 if (!fconn->app->pathinfo_re)
1361 goto check_index;
1362
Christopher Faulet28cb3662020-02-14 14:47:37 +01001363 /* If some special characters are found in the decoded path (\n
1364 * or \0), the PATH_INFO regex cannot match. This is theorically
1365 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001366 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001367 * case.
1368 */
1369 if (istchr(path, '\n') || istchr(path, '\0'))
1370 goto error;
1371
Christopher Faulet99eff652019-08-11 23:11:30 +02001372 /* The regex does not match, just to the last part and see if
1373 * the index must be used.
1374 */
1375 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1376 goto check_index;
1377
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001378 /* We must have at least 1 capture for the script name,
1379 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001380 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001381 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001382 goto check_index;
1383
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001384 /* Finally we can set the script_name and the path_info. The
1385 * path_info is set if not already defined, and if it was
1386 * captured
1387 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001388 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001389 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1390 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001391
1392 check_index:
1393 len = params->scriptname.len;
1394 /* the script_name if finished by a '/' so we can add the index
1395 * part, if any.
1396 */
1397 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1398 struct ist sn = params->scriptname;
1399
1400 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1401 chunk_memcat(params->p, sn.ptr, sn.len);
1402 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1403 }
1404 }
1405
1406 end:
1407 return 1;
1408 error:
1409 return 0;
1410}
1411
1412static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1413 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1414{
1415 struct fcgi_param p;
1416
1417 if (params->mask & flag)
1418 return 1;
1419
1420 chunk_reset(&trash);
1421
1422 switch (flag) {
1423 case FCGI_SP_CGI_GATEWAY:
1424 p.n = ist("GATEWAY_INTERFACE");
1425 p.v = ist("CGI/1.1");
1426 goto encode;
1427 case FCGI_SP_DOC_ROOT:
1428 p.n = ist("DOCUMENT_ROOT");
1429 p.v = params->docroot;
1430 goto encode;
1431 case FCGI_SP_SCRIPT_NAME:
1432 p.n = ist("SCRIPT_NAME");
1433 p.v = params->scriptname;
1434 goto encode;
1435 case FCGI_SP_PATH_INFO:
1436 p.n = ist("PATH_INFO");
1437 p.v = params->pathinfo;
1438 goto encode;
1439 case FCGI_SP_REQ_URI:
1440 p.n = ist("REQUEST_URI");
1441 p.v = params->uri;
1442 goto encode;
1443 case FCGI_SP_REQ_METH:
1444 p.n = ist("REQUEST_METHOD");
1445 p.v = params->meth;
1446 goto encode;
1447 case FCGI_SP_REQ_QS:
1448 p.n = ist("QUERY_STRING");
1449 p.v = params->qs;
1450 goto encode;
1451 case FCGI_SP_SRV_NAME:
1452 p.n = ist("SERVER_NAME");
1453 p.v = params->srv_name;
1454 goto encode;
1455 case FCGI_SP_SRV_PORT:
1456 p.n = ist("SERVER_PORT");
1457 p.v = params->srv_port;
1458 goto encode;
1459 case FCGI_SP_SRV_PROTO:
1460 p.n = ist("SERVER_PROTOCOL");
1461 p.v = params->vsn;
1462 goto encode;
1463 case FCGI_SP_REM_ADDR:
1464 p.n = ist("REMOTE_ADDR");
1465 p.v = params->rem_addr;
1466 goto encode;
1467 case FCGI_SP_REM_PORT:
1468 p.n = ist("REMOTE_PORT");
1469 p.v = params->rem_port;
1470 goto encode;
1471 case FCGI_SP_SCRIPT_FILE:
1472 p.n = ist("SCRIPT_FILENAME");
1473 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1474 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1475 p.v = ist2(b_head(&trash), b_data(&trash));
1476 goto encode;
1477 case FCGI_SP_PATH_TRANS:
1478 if (!istlen(params->pathinfo))
1479 goto skip;
1480 p.n = ist("PATH_TRANSLATED");
1481 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1482 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1483 p.v = ist2(b_head(&trash), b_data(&trash));
1484 goto encode;
1485 case FCGI_SP_CONT_LEN:
1486 p.n = ist("CONTENT_LENGTH");
1487 p.v = params->cont_len;
1488 goto encode;
1489 case FCGI_SP_HTTPS:
1490 if (!params->https)
1491 goto skip;
1492 p.n = ist("HTTPS");
1493 p.v = ist("on");
1494 goto encode;
1495 default:
1496 goto skip;
1497 }
1498
1499 encode:
1500 if (!istlen(p.v))
1501 goto skip;
1502 if (!fcgi_encode_param(outbuf, &p))
1503 return 0;
1504 skip:
1505 params->mask |= flag;
1506 return 1;
1507}
1508
1509/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1510 * anything. It is highly unexpected, but if the record is larger than a buffer
1511 * and cannot be encoded in one time, an error is triggered and the connection is
1512 * closed. GET_VALUES record cannot be split.
1513 */
1514static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1515{
1516 struct buffer outbuf;
1517 struct buffer *mbuf;
1518 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1519 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001520 int ret = 0;
1521
1522 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001523
1524 mbuf = br_tail(fconn->mbuf);
1525 retry:
1526 if (!fcgi_get_buf(fconn, mbuf)) {
1527 fconn->flags |= FCGI_CF_MUX_MALLOC;
1528 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001529 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1530 ret = 0;
1531 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001532 }
1533
1534 while (1) {
1535 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001536 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001537 break;
1538 realign_again:
1539 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1540 }
1541
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001542 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001543 goto full;
1544
1545 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1546 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001547 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
1548 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001549
1550 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1551 * handled by HAProxy.
1552 */
1553 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1554 goto full;
1555
1556 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001557 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 +01001558 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001559 b_add(mbuf, outbuf.data);
1560 ret = 1;
1561
1562 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001563 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001564 return ret;
1565 full:
1566 /* Too large to be encoded. For GET_VALUES records, it is an error */
1567 if (!b_data(mbuf))
1568 goto fail;
1569
1570 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1571 goto retry;
1572 fconn->flags |= FCGI_CF_MUX_MFULL;
1573 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001574 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001575 ret = 0;
1576 goto end;
1577 fail:
1578 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001579 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1580 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1581 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001582}
1583
1584/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1585 * couldn't do anything. It is highly unexpected, but if the record is larger
1586 * than a buffer and cannot be decoded in one time, an error is triggered and
1587 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1588 */
1589static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1590{
1591 struct buffer inbuf;
1592 struct buffer *dbuf;
1593 size_t offset;
1594
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001595 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1596
Christopher Faulet99eff652019-08-11 23:11:30 +02001597 dbuf = &fconn->dbuf;
1598
1599 /* Record too large to be fully decoded */
1600 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1601 goto fail;
1602
1603 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001604 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1605 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001606 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001607 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001608
1609 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1610 /* Realign the dmux buffer if the record wraps. It is unexpected
1611 * at this stage because it should be the first record received
1612 * from the FCGI application.
1613 */
1614 b_slow_realign(dbuf, trash.area, 0);
1615 }
1616
1617 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1618
1619 for (offset = 0; offset < b_data(&inbuf); ) {
1620 struct fcgi_param p;
1621 size_t ret;
1622
1623 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1624 if (!ret) {
1625 /* name or value too large to be decoded at once */
1626 goto fail;
1627 }
1628 offset += ret;
1629
1630 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001631 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001632 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 +02001633 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001634 }
1635 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001636 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 +02001637 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001638 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001639 }
1640 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1641 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001642 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 +02001643 }
1644 /*
1645 * Ignore all other params
1646 */
1647 }
1648
1649 /* Reset the number of concurrent streams supported if the FCGI
1650 * application does not support connection multiplexing
1651 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001652 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001653 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001654 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1655 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001656
1657 /* We must be sure to have read exactly the announced record length, no
1658 * more no less
1659 */
1660 if (offset != fconn->drl)
1661 goto fail;
1662
Willy Tarreau022e5e52020-09-10 09:33:15 +02001663 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 +02001664 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1665 fconn->drl = 0;
1666 fconn->drp = 0;
1667 fconn->state = FCGI_CS_RECORD_H;
1668 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001669 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1670 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001671 return 1;
1672 fail:
1673 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001674 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1675 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 +02001676 return 0;
1677}
1678
1679/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1680 * excluded, as the streams which already received the end-of-stream. It returns
1681 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1682 */
1683static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1684{
1685 struct eb32_node *node;
1686 struct fcgi_strm *fstrm;
1687
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001688 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1689
Christopher Faulet99eff652019-08-11 23:11:30 +02001690 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1691 while (node) {
1692 fstrm = container_of(node, struct fcgi_strm, by_id);
1693 node = eb32_next(node);
1694 if (fstrm->state != FCGI_SS_CLOSED &&
1695 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1696 !fcgi_strm_send_abort(fconn, fstrm))
1697 return 0;
1698 }
1699 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001700 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1701 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001702 return 1;
1703}
1704
1705/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1706 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1707 * space to proceed. It is small enough to be encoded in an empty buffer.
1708 */
1709static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1710{
1711 struct buffer outbuf;
1712 struct buffer *mbuf;
1713 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1714 int ret;
1715
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001716 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1717
Christopher Faulet99eff652019-08-11 23:11:30 +02001718 mbuf = br_tail(fconn->mbuf);
1719 retry:
1720 if (!fcgi_get_buf(fconn, mbuf)) {
1721 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001722 fstrm->flags |= FCGI_SF_BLK_MROOM;
1723 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1724 ret = 0;
1725 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001726 }
1727
1728 while (1) {
1729 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001730 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001731 break;
1732 realign_again:
1733 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1734 }
1735
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001736 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001737 goto full;
1738
1739 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1740 * len: 0x0008, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001741 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001742 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001743 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001744
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001745 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1746 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001747 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001748 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001749 if (!fcgi_encode_begin_request(&outbuf, &rec))
1750 goto full;
1751
1752 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001753 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 +02001754 b_add(mbuf, outbuf.data);
1755 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1756 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001757 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001758 ret = 1;
1759
1760 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001761 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001762 return ret;
1763 full:
1764 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1765 goto retry;
1766 fconn->flags |= FCGI_CF_MUX_MFULL;
1767 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001768 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 +02001769 ret = 0;
1770 goto end;
1771}
1772
1773/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1774 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1775 * space to proceed. It is small enough to be encoded in an empty buffer.
1776 */
1777static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1778 enum fcgi_record_type rtype)
1779{
1780 struct buffer outbuf;
1781 struct buffer *mbuf;
1782 int ret;
1783
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001784 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001785 mbuf = br_tail(fconn->mbuf);
1786 retry:
1787 if (!fcgi_get_buf(fconn, mbuf)) {
1788 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001789 fstrm->flags |= FCGI_SF_BLK_MROOM;
1790 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1791 ret = 0;
1792 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001793 }
1794
1795 while (1) {
1796 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001797 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001798 break;
1799 realign_again:
1800 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1801 }
1802
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001803 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001804 goto full;
1805
1806 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1807 * len: 0x0000, padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001808 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001809 outbuf.area[1] = rtype;
1810 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001811 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001812
1813 /* commit the record */
1814 b_add(mbuf, outbuf.data);
1815 ret = 1;
1816
1817 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001818 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001819 return ret;
1820 full:
1821 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1822 goto retry;
1823 fconn->flags |= FCGI_CF_MUX_MFULL;
1824 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001825 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 +02001826 ret = 0;
1827 goto end;
1828}
1829
1830
1831/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1832 * marks the end of params.
1833 */
1834static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1835{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001836 int ret;
1837
1838 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1839 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001840 if (ret) {
1841 fstrm->flags |= FCGI_SF_EP_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001842 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 +01001843 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001844 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001845}
1846
1847/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1848 * marks the end of input. On success, all the request was successfully sent.
1849 */
1850static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1851{
1852 int ret;
1853
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001854 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001855 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001856 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001858 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 +02001859 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1860 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1861 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001862 return ret;
1863}
1864
1865/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1866 * stops the request processing.
1867 */
1868static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1869{
1870 int ret;
1871
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001872 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001873 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001874 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001875 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001876 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 +02001877 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1878 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1879 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001880 return ret;
1881}
1882
1883/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1884 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1885 * several records are sent. However, a K/V param cannot be split between 2
1886 * records.
1887 */
1888static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1889 struct htx *htx)
1890{
1891 struct buffer outbuf;
1892 struct buffer *mbuf;
1893 struct htx_blk *blk;
1894 struct htx_sl *sl = NULL;
1895 struct fcgi_strm_params params;
1896 size_t total = 0;
1897
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001898 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1899
Christopher Faulet99eff652019-08-11 23:11:30 +02001900 memset(&params, 0, sizeof(params));
1901 params.p = get_trash_chunk();
1902
1903 mbuf = br_tail(fconn->mbuf);
1904 retry:
1905 if (!fcgi_get_buf(fconn, mbuf)) {
1906 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001907 fstrm->flags |= FCGI_SF_BLK_MROOM;
1908 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1909 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001910 }
1911
1912 while (1) {
1913 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001914 if (outbuf.size >= FCGI_RECORD_HEADER_SZ || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02001915 break;
1916 realign_again:
1917 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1918 }
1919
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001920 if (outbuf.size < FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02001921 goto full;
1922
1923 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1924 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001925 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02001926 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001927 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02001928
1929 blk = htx_get_head_blk(htx);
1930 while (blk) {
1931 enum htx_blk_type type;
1932 uint32_t size = htx_get_blksz(blk);
1933 struct fcgi_param p;
1934
1935 type = htx_get_blk_type(blk);
1936 switch (type) {
1937 case HTX_BLK_REQ_SL:
1938 sl = htx_get_blk_ptr(htx, blk);
1939 if (sl->info.req.meth == HTTP_METH_HEAD)
1940 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1941 if (sl->flags & HTX_SL_F_VER_11)
1942 fstrm->h1m.flags |= H1_MF_VER_11;
1943 break;
1944
1945 case HTX_BLK_HDR:
1946 p.n = htx_get_blk_name(htx, blk);
1947 p.v = htx_get_blk_value(htx, blk);
1948
1949 if (istmatch(p.n, ist(":fcgi-"))) {
1950 p.n.ptr += 6;
1951 p.n.len -= 6;
1952 if (isteq(p.n, ist("gateway_interface")))
1953 params.mask |= FCGI_SP_CGI_GATEWAY;
1954 else if (isteq(p.n, ist("document_root"))) {
1955 params.mask |= FCGI_SP_DOC_ROOT;
1956 params.docroot = p.v;
1957 }
1958 else if (isteq(p.n, ist("script_name"))) {
1959 params.mask |= FCGI_SP_SCRIPT_NAME;
1960 params.scriptname = p.v;
1961 }
1962 else if (isteq(p.n, ist("path_info"))) {
1963 params.mask |= FCGI_SP_PATH_INFO;
1964 params.pathinfo = p.v;
1965 }
1966 else if (isteq(p.n, ist("request_uri"))) {
1967 params.mask |= FCGI_SP_REQ_URI;
1968 params.uri = p.v;
1969 }
1970 else if (isteq(p.n, ist("request_meth")))
1971 params.mask |= FCGI_SP_REQ_METH;
1972 else if (isteq(p.n, ist("query_string")))
1973 params.mask |= FCGI_SP_REQ_QS;
1974 else if (isteq(p.n, ist("server_name")))
1975 params.mask |= FCGI_SP_SRV_NAME;
1976 else if (isteq(p.n, ist("server_port")))
1977 params.mask |= FCGI_SP_SRV_PORT;
1978 else if (isteq(p.n, ist("server_protocol")))
1979 params.mask |= FCGI_SP_SRV_PROTO;
1980 else if (isteq(p.n, ist("remote_addr")))
1981 params.mask |= FCGI_SP_REM_ADDR;
1982 else if (isteq(p.n, ist("remote_port")))
1983 params.mask |= FCGI_SP_REM_PORT;
1984 else if (isteq(p.n, ist("script_filename")))
1985 params.mask |= FCGI_SP_SCRIPT_FILE;
1986 else if (isteq(p.n, ist("path_translated")))
1987 params.mask |= FCGI_SP_PATH_TRANS;
1988 else if (isteq(p.n, ist("https")))
1989 params.mask |= FCGI_SP_HTTPS;
1990 }
1991 else if (isteq(p.n, ist("content-length"))) {
1992 p.n = ist("CONTENT_LENGTH");
1993 params.mask |= FCGI_SP_CONT_LEN;
1994 }
1995 else if (isteq(p.n, ist("content-type")))
1996 p.n = ist("CONTENT_TYPE");
1997 else {
1998 if (isteq(p.n, ist("host")))
1999 params.srv_name = p.v;
2000
Christopher Faulet67d58092019-10-02 10:51:38 +02002001 /* Skip header if same name is used to add the server name */
2002 if (fconn->proxy->server_id_hdr_name &&
2003 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2004 break;
2005
Christopher Faulet99eff652019-08-11 23:11:30 +02002006 memcpy(trash.area, "http_", 5);
2007 memcpy(trash.area+5, p.n.ptr, p.n.len);
2008 p.n = ist2(trash.area, p.n.len+5);
2009 }
2010
2011 if (!fcgi_encode_param(&outbuf, &p)) {
2012 if (b_space_wraps(mbuf))
2013 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002014 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002015 goto full;
2016 goto done;
2017 }
2018 break;
2019
2020 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002021 if (fconn->proxy->server_id_hdr_name) {
2022 struct server *srv = objt_server(fconn->conn->target);
2023
2024 if (!srv)
2025 goto done;
2026
2027 memcpy(trash.area, "http_", 5);
2028 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2029 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2030 p.v = ist(srv->id);
2031
2032 if (!fcgi_encode_param(&outbuf, &p)) {
2033 if (b_space_wraps(mbuf))
2034 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002035 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002036 goto full;
2037 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002038 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002039 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002040 goto done;
2041
2042 default:
2043 break;
2044 }
2045 total += size;
2046 blk = htx_remove_blk(htx, blk);
2047 }
2048
2049 done:
2050 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2051 goto error;
2052
2053 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2054 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2055 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2056 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2057 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2058 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2059 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2060 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2061 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2062 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2063 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2064 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2065 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2066 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2067 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2068 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2069 goto error;
2070
2071 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002072 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});
2073 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002074 b_add(mbuf, outbuf.data);
2075
2076 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002077 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002078 return total;
2079 full:
2080 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2081 goto retry;
2082 fconn->flags |= FCGI_CF_MUX_MFULL;
2083 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002084 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 +02002085 if (total)
2086 goto error;
2087 goto end;
2088
2089 error:
2090 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002091 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002092 fcgi_strm_error(fstrm);
2093 goto end;
2094}
2095
2096/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2097 * anything. STDIN records contain the request body.
2098 */
2099static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2100 struct htx *htx, size_t count, struct buffer *buf)
2101{
2102 struct buffer outbuf;
2103 struct buffer *mbuf;
2104 struct htx_blk *blk;
2105 enum htx_blk_type type;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002106 uint32_t size, extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002107 size_t total = 0;
2108
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002109 extra_bytes = 0;
2110
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002111 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002112 if (!count)
2113 goto end;
2114
2115 mbuf = br_tail(fconn->mbuf);
2116 retry:
2117 if (!fcgi_get_buf(fconn, mbuf)) {
2118 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002119 fstrm->flags |= FCGI_SF_BLK_MROOM;
2120 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2121 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002122 }
2123
2124 /* Perform some optimizations to reduce the number of buffer copies.
2125 * First, if the mux's buffer is empty and the htx area contains exactly
2126 * one data block of the same size as the requested count, and this
2127 * count fits within the record size, then it's possible to simply swap
2128 * the caller's buffer with the mux's output buffer and adjust offsets
2129 * and length to match the entire DATA HTX block in the middle. In this
2130 * case we perform a true zero-copy operation from end-to-end. This is
2131 * the situation that happens all the time with large files. Second, if
2132 * this is not possible, but the mux's output buffer is empty, we still
2133 * have an opportunity to avoid the copy to the intermediary buffer, by
2134 * making the intermediary buffer's area point to the output buffer's
2135 * area. In this case we want to skip the HTX header to make sure that
2136 * copies remain aligned and that this operation remains possible all
2137 * the time. This goes for headers, data blocks and any data extracted
2138 * from the HTX blocks.
2139 */
2140 blk = htx_get_head_blk(htx);
2141 if (!blk)
2142 goto end;
2143 type = htx_get_blk_type(blk);
2144 size = htx_get_blksz(blk);
2145 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2146 void *old_area = mbuf->area;
2147
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002148 /* Last block of the message: Reserve the size for the empty stdin record */
2149 if (htx->flags & HTX_FL_EOM)
2150 extra_bytes = FCGI_RECORD_HEADER_SZ;
2151
Christopher Faulet99eff652019-08-11 23:11:30 +02002152 if (b_data(mbuf)) {
2153 /* Too bad there are data left there. We're willing to memcpy/memmove
2154 * up to 1/4 of the buffer, which means that it's OK to copy a large
2155 * record into a buffer containing few data if it needs to be realigned,
2156 * and that it's also OK to copy few data without realigning. Otherwise
2157 * we'll pretend the mbuf is full and wait for it to become empty.
2158 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002159 if (size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002160 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002161 (size <= b_size(mbuf) / 4 && size + FCGI_RECORD_HEADER_SZ + extra_bytes <= b_contig_space(mbuf))))
Christopher Faulet99eff652019-08-11 23:11:30 +02002162 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002163 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002164 }
2165
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002166 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 +02002167 /* map a FCGI record to the HTX block so that we can put the
2168 * record header there.
2169 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002170 *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 +02002171 outbuf.area = b_head(mbuf);
2172
2173 /* prepend a FCGI record header just before the DATA block */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002174 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002175 fcgi_set_record_id(outbuf.area, fstrm->id);
2176 fcgi_set_record_size(outbuf.area, size);
2177
2178 /* and exchange with our old area */
2179 buf->area = old_area;
2180 buf->data = buf->head = 0;
2181 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002182
2183 htx = (struct htx *)buf->area;
2184 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002185 goto end;
2186 }
2187
2188 copy:
2189 while (1) {
2190 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002191 if (outbuf.size >= FCGI_RECORD_HEADER_SZ + extra_bytes || !b_space_wraps(mbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02002192 break;
2193 realign_again:
2194 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2195 }
2196
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002197 if (outbuf.size < FCGI_RECORD_HEADER_SZ + extra_bytes)
Christopher Faulet99eff652019-08-11 23:11:30 +02002198 goto full;
2199
2200 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2201 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002202 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002203 fcgi_set_record_id(outbuf.area, fstrm->id);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002204 outbuf.data = FCGI_RECORD_HEADER_SZ;
Christopher Faulet99eff652019-08-11 23:11:30 +02002205
2206 blk = htx_get_head_blk(htx);
2207 while (blk && count) {
2208 enum htx_blk_type type = htx_get_blk_type(blk);
2209 uint32_t size = htx_get_blksz(blk);
2210 struct ist v;
2211
2212 switch (type) {
2213 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002214 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 +02002215 v = htx_get_blk_value(htx, blk);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002216
2217 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM))
2218 extra_bytes = FCGI_RECORD_HEADER_SZ; /* Last block of the message */
2219
2220 if (v.len > count) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002221 v.len = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002222 extra_bytes = 0;
2223 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002224
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002225 if (v.len + FCGI_RECORD_HEADER_SZ + extra_bytes > b_room(&outbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002226 /* It doesn't fit at once. If it at least fits once split and
2227 * the amount of data to move is low, let's defragment the
2228 * buffer now.
2229 */
2230 if (b_space_wraps(mbuf) &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002231 b_data(&outbuf) + v.len + extra_bytes <= b_room(mbuf) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02002232 b_data(mbuf) <= MAX_DATA_REALIGN)
2233 goto realign_again;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002234 v.len = b_room(&outbuf) - FCGI_RECORD_HEADER_SZ - extra_bytes;
Christopher Faulet99eff652019-08-11 23:11:30 +02002235 }
2236 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002237 if (outbuf.data == FCGI_RECORD_HEADER_SZ)
Christopher Faulet99eff652019-08-11 23:11:30 +02002238 goto full;
2239 goto done;
2240 }
2241 if (v.len != size) {
2242 total += v.len;
2243 count -= v.len;
2244 htx_cut_data_blk(htx, blk, v.len);
2245 goto done;
2246 }
2247 break;
2248
Christopher Faulet99eff652019-08-11 23:11:30 +02002249 default:
2250 break;
2251 }
2252 total += size;
2253 count -= size;
2254 blk = htx_remove_blk(htx, blk);
2255 }
2256
2257 done:
2258 /* update the record's size */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002259 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});
2260 fcgi_set_record_size(outbuf.area, outbuf.data - FCGI_RECORD_HEADER_SZ);
Christopher Faulet99eff652019-08-11 23:11:30 +02002261 b_add(mbuf, outbuf.data);
2262
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002263 /* Send the empty stding here to finish the message */
2264 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) {
2265 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
2266 if (!fcgi_strm_send_empty_stdin(fconn, fstrm)) {
2267 /* bytes already reserved for this record. It should not fail */
2268 htx->flags |= HTX_FL_PROCESSING_ERROR;
2269 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
2270 fcgi_strm_error(fstrm);
2271 }
2272 }
2273
Christopher Faulet99eff652019-08-11 23:11:30 +02002274 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002275 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002276 return total;
2277 full:
2278 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2279 goto retry;
2280 fconn->flags |= FCGI_CF_MUX_MFULL;
2281 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002282 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 +02002283 goto end;
2284}
2285
2286/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2287 * anything. STDOUT records contain the entire response. All the content is
2288 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2289 */
2290static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2291{
2292 struct buffer *dbuf;
2293 size_t ret;
2294 size_t max;
2295
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002296 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2297
Christopher Faulet99eff652019-08-11 23:11:30 +02002298 dbuf = &fconn->dbuf;
2299
2300 /* Only padding remains */
2301 if (fconn->state == FCGI_CS_RECORD_P)
2302 goto end_transfer;
2303
2304 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2305 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2306 buf_room_for_htx_data(dbuf))
2307 goto fail; // incomplete record
2308
2309 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2310 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002311 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2312 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002313 }
2314
2315 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2316 max = buf_room_for_htx_data(&fstrm->rxbuf);
2317 if (!b_data(&fstrm->rxbuf))
2318 fstrm->rxbuf.head = sizeof(struct htx);
2319 if (max > fconn->drl)
2320 max = fconn->drl;
2321
2322 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2323 if (!ret)
2324 goto fail;
2325 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002326 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2327 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 +02002328
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002329 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002330 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002331 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2332 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002333
2334 if (fconn->drl)
2335 goto fail;
2336
2337 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002338 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002339 fconn->drl += fconn->drp;
2340 fconn->drp = 0;
2341 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2342 b_del(&fconn->dbuf, ret);
2343 fconn->drl -= ret;
2344 if (fconn->drl)
2345 goto fail;
2346
2347 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002348 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2349 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002350 return 1;
2351 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002352 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 +02002353 return 0;
2354}
2355
2356
2357/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2358 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002359 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002360 */
2361static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2362{
2363 int ret;
2364
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002365 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2366
Christopher Faulet99eff652019-08-11 23:11:30 +02002367 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002368 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002369 fconn->drl += fconn->drp;
2370 fconn->drp = 0;
2371 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2372 b_del(&fconn->dbuf, ret);
2373 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002374 if (fconn->drl) {
2375 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 +02002376 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002377 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002378 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002379 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002380 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 +02002381 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);
2382 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002383 return 1;
2384}
2385
2386/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2387 * anything.
2388 */
2389static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2390{
2391 struct buffer *dbuf;
2392 struct buffer tag;
2393 size_t ret;
2394
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002395 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002396 dbuf = &fconn->dbuf;
2397
2398 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002399 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002400 goto end_transfer;
2401
2402 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2403 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2404 buf_room_for_htx_data(dbuf))
2405 goto fail; // incomplete record
2406
2407 chunk_reset(&trash);
2408 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2409 if (!ret)
2410 goto fail;
2411 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002412 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 +02002413
2414 trash.area[ret] = '\n';
2415 trash.area[ret+1] = '\0';
2416 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002417 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002418
2419 if (fconn->drl)
2420 goto fail;
2421
2422 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002423 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002424 fconn->drl += fconn->drp;
2425 fconn->drp = 0;
2426 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2427 b_del(&fconn->dbuf, ret);
2428 fconn->drl -= ret;
2429 if (fconn->drl)
2430 goto fail;
2431 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002432 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2433 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002434 return 1;
2435 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002436 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 +02002437 return 0;
2438}
2439
2440/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2441 * anything. If the empty STDOUT record is not already received, this one marks
2442 * the end of the response. It is highly unexpected, but if the record is larger
2443 * than a buffer and cannot be decoded in one time, an error is triggered and
2444 * the connection is closed. END_REQUEST record cannot be split.
2445 */
2446static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2447{
2448 struct buffer inbuf;
2449 struct buffer *dbuf;
2450 struct fcgi_end_request endreq;
2451
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002452 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002453 dbuf = &fconn->dbuf;
2454
2455 /* Record too large to be fully decoded */
2456 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2457 goto fail;
2458
2459 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002460 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2461 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002462 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002463 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002464
2465 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2466 /* Realign the dmux buffer if the record wraps. It is unexpected
2467 * at this stage because it should be the first record received
2468 * from the FCGI application.
2469 */
2470 b_slow_realign(dbuf, trash.area, 0);
2471 }
2472
2473 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2474
2475 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2476 goto fail;
2477
2478 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002479 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 +02002480 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 +02002481 fstrm->proto_status = endreq.errcode;
2482 fcgi_strm_close(fstrm);
2483
2484 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2485 fconn->drl = 0;
2486 fconn->drp = 0;
2487 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002488 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2489 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002490 return 1;
2491
2492 fail:
2493 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002494 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 +02002495 return 0;
2496}
2497
2498/* process Rx records to be demultiplexed */
2499static void fcgi_process_demux(struct fcgi_conn *fconn)
2500{
2501 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2502 struct fcgi_header hdr;
2503 int ret;
2504
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002505 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2506
Christopher Faulet99eff652019-08-11 23:11:30 +02002507 if (fconn->state == FCGI_CS_CLOSED)
2508 return;
2509
2510 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002511 if (fconn->state == FCGI_CS_INIT) {
2512 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2513 return;
2514 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002515 if (fconn->state == FCGI_CS_SETTINGS) {
2516 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002517 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002518 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2519 if (!ret)
2520 goto fail;
2521 b_del(&fconn->dbuf, ret);
2522
2523 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2524 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002525 TRACE_PROTO("unexpected record type or flags", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
2526 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 +02002527 goto fail;
2528 }
2529 goto new_record;
2530 }
2531 }
2532
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002533 /* process as many incoming records as possible below */
2534 while (1) {
2535 if (!b_data(&fconn->dbuf)) {
2536 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2537 break;
2538 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002539
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002540 if (fconn->state == FCGI_CS_CLOSED) {
2541 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002542 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002543 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002544
2545 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002546 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002547 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2548 if (!ret)
2549 break;
2550 b_del(&fconn->dbuf, ret);
2551
2552 new_record:
2553 fconn->dsi = hdr.id;
2554 fconn->drt = hdr.type;
2555 fconn->drl = hdr.len;
2556 fconn->drp = hdr.padding;
2557 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002558 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 +02002559 }
2560
2561 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2562 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2563
2564 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2565 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002566 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002567 fstrm->state == FCGI_SS_CLOSED ||
2568 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2569 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2570 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002571 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 +02002572 fstrm->cs->flags |= CS_FL_RCV_MORE;
2573 fcgi_strm_notify_recv(fstrm);
2574 }
2575 fstrm = tmp_fstrm;
2576
2577 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2578 /* ignore all record for closed streams */
2579 goto ignore_record;
2580 }
2581 if (fstrm->state == FCGI_SS_IDLE) {
2582 /* ignore all record for unknown streams */
2583 goto ignore_record;
2584 }
2585
2586 switch (fconn->drt) {
2587 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 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 +02002589 ret = fcgi_conn_handle_values_result(fconn);
2590 break;
2591
2592 case FCGI_STDOUT:
2593 if (fstrm->flags & FCGI_SF_ES_RCVD)
2594 goto ignore_record;
2595
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002597 if (fconn->drl)
2598 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2599 else
2600 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2601 break;
2602
2603 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002604 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002605 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2606 break;
2607
2608 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002609 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 +02002610 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2611 break;
2612
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002613 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002614 default:
2615 ignore_record:
2616 /* drop records that we ignore. They may be
2617 * larger than the buffer so we drain all of
2618 * their contents until we reach the end.
2619 */
2620 fconn->state = FCGI_CS_RECORD_P;
2621 fconn->drl += fconn->drp;
2622 fconn->drp = 0;
2623 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002624 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002625 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002626 b_del(&fconn->dbuf, ret);
2627 fconn->drl -= ret;
2628 ret = (fconn->drl == 0);
2629 }
2630
2631 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002632 if (ret <= 0) {
2633 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002634 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002635 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002636
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002638 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002639 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2640 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002641 }
2642
2643 fail:
2644 /* we can go here on missing data, blocked response or error */
2645 if (fstrm && fstrm->cs &&
2646 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002647 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002648 fstrm->state == FCGI_SS_CLOSED ||
2649 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2650 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2651 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002652 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 +02002653 fstrm->cs->flags |= CS_FL_RCV_MORE;
2654 fcgi_strm_notify_recv(fstrm);
2655 }
2656
2657 fcgi_conn_restart_reading(fconn, 0);
2658}
2659
2660/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2661 * the end.
2662 */
2663static int fcgi_process_mux(struct fcgi_conn *fconn)
2664{
2665 struct fcgi_strm *fstrm, *fstrm_back;
2666
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002667 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2668
Christopher Faulet99eff652019-08-11 23:11:30 +02002669 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2670 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2671 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2672 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002673 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 +02002674 fcgi_wake_unassigned_streams(fconn);
2675 goto mux;
2676 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2679 goto fail;
2680 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002681 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 +02002682 }
2683 /* need to wait for the other side */
2684 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002685 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002686 }
2687
2688 mux:
2689 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2690 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2691 break;
2692
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002693 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 continue;
2695
Willy Tarreau7aad7032020-01-16 17:20:57 +01002696 /* If the sender changed his mind and unsubscribed, let's just
2697 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002698 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002699 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2700 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002701 LIST_DEL_INIT(&fstrm->send_list);
2702 continue;
2703 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002704
2705 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002706 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2707 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002708 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002709 tasklet_wakeup(fstrm->subs->tasklet);
2710 fstrm->subs->events &= ~SUB_RETRY_SEND;
2711 if (!fstrm->subs->events)
2712 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002713 } else {
2714 /* it's the shut request that was queued */
2715 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2716 tasklet_wakeup(fstrm->shut_tl);
2717 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002718 }
2719
2720 fail:
2721 if (fconn->state == FCGI_CS_CLOSED) {
2722 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2723 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002724 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2725 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002728 }
2729 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002730
2731 done:
2732 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002733 return 1;
2734}
2735
2736
2737/* Attempt to read data, and subscribe if none available.
2738 * The function returns 1 if data has been received, otherwise zero.
2739 */
2740static int fcgi_recv(struct fcgi_conn *fconn)
2741{
2742 struct connection *conn = fconn->conn;
2743 struct buffer *buf;
2744 int max;
2745 size_t ret;
2746
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002747 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2748
2749 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2750 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002752 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002753
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002754 if (!fcgi_recv_allowed(fconn)) {
2755 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002756 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002757 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002758
2759 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2760 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002761 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002762 fconn->flags |= FCGI_CF_DEM_DALLOC;
2763 return 0;
2764 }
2765
2766 b_realign_if_empty(buf);
2767 if (!b_data(buf)) {
2768 /* try to pre-align the buffer like the
2769 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002770 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002771 * HTX block to alias it upon recv. We cannot use the
2772 * head because rcv_buf() will realign the buffer if
2773 * it's empty. Thus we cheat and pretend we already
2774 * have a few bytes there.
2775 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002776 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
2777 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? FCGI_RECORD_HEADER_SZ : 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02002778 }
2779 else
2780 max = buf_room_for_htx_data(buf);
2781
2782 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2783
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002784 if (max && !ret && fcgi_recv_allowed(fconn)) {
2785 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002786 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002787 }
2788 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002789 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002790
2791 if (!b_data(buf)) {
2792 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002793 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002794 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2795 }
2796
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002797 if (ret == max) {
2798 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002799 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002800 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002801
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002802 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002803 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2804}
2805
2806
2807/* Try to send data if possible.
2808 * The function returns 1 if data have been sent, otherwise zero.
2809 */
2810static int fcgi_send(struct fcgi_conn *fconn)
2811{
2812 struct connection *conn = fconn->conn;
2813 int done;
2814 int sent = 0;
2815
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002816 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2817
2818 if (conn->flags & CO_FL_ERROR) {
2819 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002820 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002821 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002822
2823
Willy Tarreau911db9b2020-01-23 16:27:54 +01002824 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002825 /* a handshake was requested */
2826 goto schedule;
2827 }
2828
2829 /* This loop is quite simple : it tries to fill as much as it can from
2830 * pending streams into the existing buffer until it's reportedly full
2831 * or the end of send requests is reached. Then it tries to send this
2832 * buffer's contents out, marks it not full if at least one byte could
2833 * be sent, and tries again.
2834 *
2835 * The snd_buf() function normally takes a "flags" argument which may
2836 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2837 * data immediately comes and CO_SFL_STREAMER to indicate that the
2838 * connection is streaming lots of data (used to increase TLS record
2839 * size at the expense of latency). The former can be sent any time
2840 * there's a buffer full flag, as it indicates at least one stream
2841 * attempted to send and failed so there are pending data. An
2842 * alternative would be to set it as long as there's an active stream
2843 * but that would be problematic for ACKs until we have an absolute
2844 * guarantee that all waiters have at least one byte to send. The
2845 * latter should possibly not be set for now.
2846 */
2847
2848 done = 0;
2849 while (!done) {
2850 unsigned int flags = 0;
2851 unsigned int released = 0;
2852 struct buffer *buf;
2853
2854 /* fill as much as we can into the current buffer */
2855 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2856 done = fcgi_process_mux(fconn);
2857
2858 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2859 done = 1; // we won't go further without extra buffers
2860
2861 if (conn->flags & CO_FL_ERROR)
2862 break;
2863
2864 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2865 flags |= CO_SFL_MSG_MORE;
2866
2867 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2868 if (b_data(buf)) {
2869 int ret;
2870
2871 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2872 if (!ret) {
2873 done = 1;
2874 break;
2875 }
2876 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002877 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002878 b_del(buf, ret);
2879 if (b_data(buf)) {
2880 done = 1;
2881 break;
2882 }
2883 }
2884 b_free(buf);
2885 released++;
2886 }
2887
2888 if (released)
2889 offer_buffers(NULL, tasks_run_queue);
2890
2891 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002892 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2893 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002894 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2895 }
2896
2897 if (conn->flags & CO_FL_SOCK_WR_SH) {
2898 /* output closed, nothing to send, clear the buffer to release it */
2899 b_reset(br_tail(fconn->mbuf));
2900 }
2901 /* We're not full anymore, so we can wake any task that are waiting
2902 * for us.
2903 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002904 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002905 struct fcgi_strm *fstrm;
2906
2907 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2908 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2909 break;
2910
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002911 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002912 continue;
2913
Willy Tarreau7aad7032020-01-16 17:20:57 +01002914 /* If the sender changed his mind and unsubscribed, let's just
2915 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002916 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002917 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2918 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002919 LIST_DEL_INIT(&fstrm->send_list);
2920 continue;
2921 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002922
2923 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002924 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002925 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002926 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002927 tasklet_wakeup(fstrm->subs->tasklet);
2928 fstrm->subs->events &= ~SUB_RETRY_SEND;
2929 if (!fstrm->subs->events)
2930 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002931 } else {
2932 /* it's the shut request that was queued */
2933 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2934 tasklet_wakeup(fstrm->shut_tl);
2935 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002936 }
2937 }
2938 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002939 if (!br_data(fconn->mbuf)) {
2940 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002941 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002942 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002943schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002944 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2945 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002946 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002947 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002948
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002949 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002950 return sent;
2951}
2952
2953/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreau691d5032021-01-20 14:55:01 +01002954struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet99eff652019-08-11 23:11:30 +02002955{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002956 struct connection *conn;
2957 struct fcgi_conn *fconn;
2958 struct tasklet *tl = (struct tasklet *)t;
2959 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002960 int ret = 0;
2961
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002962
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002963 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002964 if (tl->context == NULL) {
2965 /* The connection has been taken over by another thread,
2966 * we're no longer responsible for it, so just free the
2967 * tasklet, and do nothing.
2968 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002969 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002970 tasklet_free(tl);
2971 return NULL;
2972
2973 }
2974 fconn = ctx;
2975 conn = fconn->conn;
2976
2977 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2978
2979 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2980 if (conn_in_list)
2981 MT_LIST_DEL(&conn->list);
2982
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002983 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002984
Christopher Faulet99eff652019-08-11 23:11:30 +02002985 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2986 ret = fcgi_send(fconn);
2987 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2988 ret |= fcgi_recv(fconn);
2989 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002990 ret = fcgi_process(fconn);
2991
2992 /* If we were in an idle list, we want to add it back into it,
2993 * unless fcgi_process() returned -1, which mean it has destroyed
2994 * the connection (testing !ret is enough, if fcgi_process() wasn't
2995 * called then ret will be 0 anyway.
2996 */
2997 if (!ret && conn_in_list) {
2998 struct server *srv = objt_server(conn->target);
2999
3000 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003001 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003002 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02003003 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003004 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003005 return NULL;
3006}
3007
3008/* callback called on any event by the connection handler.
3009 * It applies changes and returns zero, or < 0 if it wants immediate
3010 * destruction of the connection (which normally doesn not happen in FCGI).
3011 */
3012static int fcgi_process(struct fcgi_conn *fconn)
3013{
3014 struct connection *conn = fconn->conn;
3015
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003016 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3017
Christopher Faulet99eff652019-08-11 23:11:30 +02003018 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3019 fcgi_process_demux(fconn);
3020
3021 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3022 b_reset(&fconn->dbuf);
3023
3024 if (buf_room_for_htx_data(&fconn->dbuf))
3025 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3026 }
3027 fcgi_send(fconn);
3028
Willy Tarreauc3914d42020-09-24 08:39:22 +02003029 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003030 /* frontend is stopping, reload likely in progress, let's try
3031 * to announce a graceful shutdown if not yet done. We don't
3032 * care if it fails, it will be tried again later.
3033 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003034 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 +02003035 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3036 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3037 fcgi_conn_send_aborts(fconn);
3038 }
3039 }
3040
3041 /*
3042 * If we received early data, and the handshake is done, wake
3043 * any stream that was waiting for it.
3044 */
3045 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003046 (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 +02003047 struct eb32_node *node;
3048 struct fcgi_strm *fstrm;
3049
3050 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3051 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3052
3053 while (node) {
3054 fstrm = container_of(node, struct fcgi_strm, by_id);
3055 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3056 fcgi_strm_notify_recv(fstrm);
3057 node = eb32_next(node);
3058 }
3059 }
3060
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003061 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003062 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3063 eb_is_empty(&fconn->streams_by_id)) {
3064 fcgi_wake_some_streams(fconn, 0);
3065
3066 if (eb_is_empty(&fconn->streams_by_id)) {
3067 /* no more stream, kill the connection now */
3068 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003069 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003070 return -1;
3071 }
3072 }
3073
3074 if (!b_data(&fconn->dbuf))
3075 fcgi_release_buf(fconn, &fconn->dbuf);
3076
3077 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3078 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3079 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3080 fcgi_release_mbuf(fconn);
3081
3082 if (fconn->task) {
3083 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3084 task_queue(fconn->task);
3085 }
3086
3087 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003088 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003089 return 0;
3090}
3091
3092
3093/* wake-up function called by the connection layer (mux_ops.wake) */
3094static int fcgi_wake(struct connection *conn)
3095{
3096 struct fcgi_conn *fconn = conn->ctx;
3097
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003098 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003099 return (fcgi_process(fconn));
3100}
3101
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003102
3103static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3104{
3105 int ret = 0;
3106 switch (mux_ctl) {
3107 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003108 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003109 ret |= MUX_STATUS_READY;
3110 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003111 case MUX_EXIT_STATUS:
3112 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003113 default:
3114 return -1;
3115 }
3116}
3117
Christopher Faulet99eff652019-08-11 23:11:30 +02003118/* Connection timeout management. The principle is that if there's no receipt
3119 * nor sending for a certain amount of time, the connection is closed. If the
3120 * MUX buffer still has lying data or is not allocatable, the connection is
3121 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003122 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003123 */
3124static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3125{
3126 struct fcgi_conn *fconn = context;
3127 int expired = tick_is_expired(t->expire, now_ms);
3128
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003129 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3130
Willy Tarreau60814ff2020-06-30 11:19:23 +02003131 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003132 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3133
3134 /* Somebody already stole the connection from us, so we should not
3135 * free it, we just have to free the task.
3136 */
3137 if (!t->context) {
3138 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3139 fconn = NULL;
3140 goto do_leave;
3141 }
3142
Willy Tarreau60814ff2020-06-30 11:19:23 +02003143 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003144 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003145 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3146 return t;
3147 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003148
Willy Tarreau60814ff2020-06-30 11:19:23 +02003149 /* We're about to destroy the connection, so make sure nobody attempts
3150 * to steal it from us.
3151 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003152 if (fconn->conn->flags & CO_FL_LIST_MASK)
3153 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003154
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003155 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003156 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003157
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003158do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003159 task_destroy(t);
3160
3161 if (!fconn) {
3162 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003163 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003164 return NULL;
3165 }
3166
3167 fconn->task = NULL;
3168 fconn->state = FCGI_CS_CLOSED;
3169 fcgi_wake_some_streams(fconn, 0);
3170
3171 if (br_data(fconn->mbuf)) {
3172 /* don't even try to send aborts, the buffer is stuck */
3173 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3174 goto end;
3175 }
3176
3177 /* try to send but no need to insist */
3178 if (!fcgi_conn_send_aborts(fconn))
3179 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3180
3181 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3182 conn_xprt_ready(fconn->conn)) {
3183 unsigned int released = 0;
3184 struct buffer *buf;
3185
3186 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3187 if (b_data(buf)) {
3188 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3189 buf, b_data(buf), 0);
3190 if (!ret)
3191 break;
3192 b_del(buf, ret);
3193 if (b_data(buf))
3194 break;
3195 b_free(buf);
3196 released++;
3197 }
3198 }
3199
3200 if (released)
3201 offer_buffers(NULL, tasks_run_queue);
3202 }
3203
3204 end:
3205 /* either we can release everything now or it will be done later once
3206 * the last stream closes.
3207 */
3208 if (eb_is_empty(&fconn->streams_by_id))
3209 fcgi_release(fconn);
3210
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003211 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003212 return NULL;
3213}
3214
3215
3216/*******************************************/
3217/* functions below are used by the streams */
3218/*******************************************/
3219
3220/* Append the description of what is present in error snapshot <es> into <out>.
3221 * The description must be small enough to always fit in a buffer. The output
3222 * buffer may be the trash so the trash must not be used inside this function.
3223 */
3224static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3225{
3226 chunk_appendf(out,
3227 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3228 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3229 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3230 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3231 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3232 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3233}
3234/*
3235 * Capture a bad response and archive it in the proxy's structure. By default
3236 * it tries to report the error position as h1m->err_pos. However if this one is
3237 * not set, it will then report h1m->next, which is the last known parsing
3238 * point. The function is able to deal with wrapping buffers. It always displays
3239 * buffers as a contiguous area starting at buf->p. The direction is determined
3240 * thanks to the h1m's flags.
3241 */
3242static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3243 struct h1m *h1m, struct buffer *buf)
3244{
3245 struct session *sess = fstrm->sess;
3246 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003247 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003248 union error_snapshot_ctx ctx;
3249
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003250 if (fstrm->cs && fstrm->cs->data) {
3251 if (sess == NULL)
3252 sess = si_strm(fstrm->cs->data)->sess;
3253 if (!(h1m->flags & H1_MF_RESP))
3254 other_end = si_strm(fstrm->cs->data)->be;
3255 else
3256 other_end = sess->fe;
3257 } else
3258 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003259 /* http-specific part now */
3260 ctx.h1.state = h1m->state;
3261 ctx.h1.c_flags = fconn->flags;
3262 ctx.h1.s_flags = fstrm->flags;
3263 ctx.h1.m_flags = h1m->flags;
3264 ctx.h1.m_clen = h1m->curr_len;
3265 ctx.h1.m_blen = h1m->body_len;
3266
3267 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3268 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3269 &ctx, fcgi_show_error_snapshot);
3270}
3271
3272static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3273 struct buffer *buf, size_t *ofs, size_t max)
3274{
3275 int ret;
3276
Willy Tarreau022e5e52020-09-10 09:33:15 +02003277 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 +02003278 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3279 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003280 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 +02003281 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003282 TRACE_USER("rejected 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 +02003283 fcgi_strm_error(fstrm);
3284 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3285 }
3286 goto end;
3287 }
3288
3289 *ofs += ret;
3290 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003291 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 +02003292 return ret;
3293
3294}
3295
Christopher Fauletaf542632019-10-01 21:52:49 +02003296static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003297 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3298{
3299 int ret;
3300
Willy Tarreau022e5e52020-09-10 09:33:15 +02003301 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 +02003302 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003303 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003304 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 +02003305 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003306 TRACE_USER("rejected 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 +02003307 fcgi_strm_error(fstrm);
3308 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3309 }
3310 goto end;
3311 }
3312 *ofs += ret;
3313 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003314 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 +02003315 return ret;
3316}
3317
3318static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3319 struct buffer *buf, size_t *ofs, size_t max)
3320{
3321 int ret;
3322
Willy Tarreau022e5e52020-09-10 09:33:15 +02003323 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 +02003324 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003325 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003326 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 +02003327 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003328 TRACE_USER("rejected 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 +02003329 fcgi_strm_error(fstrm);
3330 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3331 }
3332 goto end;
3333 }
3334 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003335 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003336 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 +02003337 return ret;
3338}
3339
Christopher Faulet99eff652019-08-11 23:11:30 +02003340static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3341{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003342 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003343 struct htx *htx;
3344 struct h1m *h1m = &fstrm->h1m;
3345 size_t ret, data, total = 0;
3346
3347 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003348 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3349
Christopher Faulet99eff652019-08-11 23:11:30 +02003350 data = htx->data;
3351 if (fstrm->state == FCGI_SS_ERROR)
3352 goto end;
3353
3354 do {
3355 size_t used = htx_used_space(htx);
3356
3357 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003358 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003359 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3360 if (!ret)
3361 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003362
3363 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3364
Christopher Faulet99eff652019-08-11 23:11:30 +02003365 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3366 struct htx_blk *blk = htx_get_head_blk(htx);
3367 struct htx_sl *sl;
3368
3369 if (!blk)
3370 break;
3371 sl = htx_get_blk_ptr(htx, blk);
3372 sl->flags |= HTX_SL_F_XFER_LEN;
3373 htx->extra = 0;
3374 }
3375 }
3376 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003377 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003378 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet1e857782020-12-08 10:38:22 +01003379
3380 if (!(h1m->flags & H1_MF_XFER_LEN) && fstrm->state != FCGI_SS_ERROR &&
3381 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
3382 TRACE_DEVEL("end of data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003383 htx->flags |= HTX_FL_EOM;
Christopher Faulet1e857782020-12-08 10:38:22 +01003384 h1m->state = H1_MSG_DONE;
3385 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3386 }
3387
Christopher Faulet76014fd2019-12-10 11:47:22 +01003388 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003389 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003390
3391 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 +02003392 }
3393 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003394 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3395 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3396 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003397 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003398
Christopher Faulet76014fd2019-12-10 11:47:22 +01003399 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 +02003400 }
3401 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003402 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 +02003403 if (b_data(&fstrm->rxbuf) > total) {
3404 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003405 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003406 fcgi_strm_error(fstrm);
3407 }
3408 break;
3409 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003410 else {
3411 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003412 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003413 fcgi_strm_error(fstrm);
3414 break;
3415 }
3416
3417 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003418 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003419
3420 if (fstrm->state == FCGI_SS_ERROR) {
3421 b_reset(&fstrm->rxbuf);
3422 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003423 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003424 return 0;
3425 }
3426
3427 b_del(&fstrm->rxbuf, total);
3428
3429 end:
3430 htx_to_buf(htx, buf);
3431 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003432 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003433 return ret;
3434}
3435
3436/*
3437 * Attach a new stream to a connection
3438 * (Used for outgoing connections)
3439 */
3440static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3441{
3442 struct conn_stream *cs;
3443 struct fcgi_strm *fstrm;
3444 struct fcgi_conn *fconn = conn->ctx;
3445
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003446 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003447 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003448 if (!cs) {
3449 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003450 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003451 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003452 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3453 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003454 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003455 cs_free(cs);
3456 return NULL;
3457 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003458 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003459 return cs;
3460}
3461
3462/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3463 * We have to scan because we may have some orphan streams. It might be
3464 * beneficial to scan backwards from the end to reduce the likeliness to find
3465 * orphans.
3466 */
3467static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3468{
3469 struct fcgi_conn *fconn = conn->ctx;
3470 struct fcgi_strm *fstrm;
3471 struct eb32_node *node;
3472
3473 node = eb32_first(&fconn->streams_by_id);
3474 while (node) {
3475 fstrm = container_of(node, struct fcgi_strm, by_id);
3476 if (fstrm->cs)
3477 return fstrm->cs;
3478 node = eb32_next(node);
3479 }
3480 return NULL;
3481}
3482
3483/*
3484 * Destroy the mux and the associated connection, if it is no longer used
3485 */
3486static void fcgi_destroy(void *ctx)
3487{
3488 struct fcgi_conn *fconn = ctx;
3489
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003490 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003491 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3492 fcgi_release(fconn);
3493}
3494
3495/*
3496 * Detach the stream from the connection and possibly release the connection.
3497 */
3498static void fcgi_detach(struct conn_stream *cs)
3499{
3500 struct fcgi_strm *fstrm = cs->ctx;
3501 struct fcgi_conn *fconn;
3502 struct session *sess;
3503
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003504 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3505
Christopher Faulet99eff652019-08-11 23:11:30 +02003506 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003507 if (!fstrm) {
3508 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003509 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003510 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003511
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003512 /* there's no txbuf so we're certain no to be able to send anything */
3513 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003514
3515 sess = fstrm->sess;
3516 fconn = fstrm->fconn;
3517 fstrm->cs = NULL;
3518 fconn->nb_cs--;
3519
3520 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3521 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3522 fconn->streams_limit = 1;
3523 }
3524 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3525 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3526 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3527 fconn->state = FCGI_CS_CLOSED;
3528 }
3529
3530 /* this stream may be blocked waiting for some data to leave, so orphan
3531 * it in this case.
3532 */
3533 if (!(cs->conn->flags & CO_FL_ERROR) &&
3534 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003535 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003536 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003537 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
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
3541 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3542 /* unblock the connection if it was blocked on this stream. */
3543 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3544 fcgi_conn_restart_reading(fconn, 1);
3545 }
3546
3547 fcgi_strm_destroy(fstrm);
3548
3549 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003550 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003551 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003552 /* Add the connection in the session serverlist, if not already done */
3553 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3554 fconn->conn->owner = NULL;
3555 if (eb_is_empty(&fconn->streams_by_id)) {
3556 /* let's kill the connection right away */
3557 fconn->conn->mux->destroy(fconn);
3558 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3559 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003560 }
3561 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003562 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003563 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3564 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003565 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003566 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003567 }
3568 }
3569 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003570 else {
3571 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003572 /* If the connection is owned by the session, first remove it
3573 * from its list
3574 */
3575 if (fconn->conn->owner) {
3576 session_unown_conn(fconn->conn->owner, fconn->conn);
3577 fconn->conn->owner = NULL;
3578 }
3579
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003580 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003581 /* The server doesn't want it, let's kill the connection right away */
3582 fconn->conn->mux->destroy(fconn);
3583 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3584 return;
3585 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003586 /* At this point, the connection has been added to the
3587 * server idle list, so another thread may already have
3588 * hijacked it, so we can't do anything with it.
3589 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003590 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3591 return;
3592 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003593 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003594 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3595 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003596 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003597 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003598 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003599 }
3600
3601 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003602 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003603 */
3604 if (fcgi_conn_is_dead(fconn)) {
3605 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003606 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003607 fcgi_release(fconn);
3608 }
3609 else if (fconn->task) {
3610 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3611 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003612 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003613 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003614 else
3615 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003616}
3617
3618
3619/* Performs a synchronous or asynchronous shutr(). */
3620static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3621{
3622 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003623
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003624 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3625
Christopher Faulet99eff652019-08-11 23:11:30 +02003626 if (fstrm->state == FCGI_SS_CLOSED)
3627 goto done;
3628
3629 /* a connstream may require us to immediately kill the whole connection
3630 * for example because of a "tcp-request content reject" rule that is
3631 * normally used to limit abuse.
3632 */
3633 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003634 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3635 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003636 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003637 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003638 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003639 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003640 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3641 !fcgi_strm_send_abort(fconn, fstrm))
3642 goto add_to_list;
3643 }
3644
3645 fcgi_strm_close(fstrm);
3646
3647 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3648 tasklet_wakeup(fconn->wait_event.tasklet);
3649 done:
3650 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003651 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 return;
3653
3654 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003655 /* Let the handler know we want to shutr, and add ourselves to the
3656 * send list if not yet done. fcgi_deferred_shut() will be
3657 * automatically called via the shut_tl tasklet when there's room
3658 * again.
3659 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003660 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003661 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003662 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3663 }
3664 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003665 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003666 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003667 return;
3668}
3669
3670/* Performs a synchronous or asynchronous shutw(). */
3671static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3672{
3673 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003674
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003675 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3676
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3678 goto done;
3679
3680 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3681 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3682 !fcgi_strm_send_abort(fconn, fstrm))
3683 goto add_to_list;
3684
3685 if (fstrm->state == FCGI_SS_HREM)
3686 fcgi_strm_close(fstrm);
3687 else
3688 fstrm->state = FCGI_SS_HLOC;
3689 } else {
3690 /* a connstream may require us to immediately kill the whole connection
3691 * for example because of a "tcp-request content reject" rule that is
3692 * normally used to limit abuse.
3693 */
3694 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003695 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3696 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003697 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003698 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003699
3700 fcgi_strm_close(fstrm);
3701 }
3702
3703 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3704 tasklet_wakeup(fconn->wait_event.tasklet);
3705 done:
3706 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003707 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003708 return;
3709
3710 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003711 /* Let the handler know we want to shutr, and add ourselves to the
3712 * send list if not yet done. fcgi_deferred_shut() will be
3713 * automatically called via the shut_tl tasklet when there's room
3714 * again.
3715 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003717 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3719 }
3720 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003721 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003722 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003723 return;
3724}
3725
Willy Tarreau7aad7032020-01-16 17:20:57 +01003726/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003727 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003728 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003729 */
3730static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3731{
3732 struct fcgi_strm *fstrm = ctx;
3733 struct fcgi_conn *fconn = fstrm->fconn;
3734
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003735 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3736
Willy Tarreau7aad7032020-01-16 17:20:57 +01003737 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3738 /* some data processing remains to be done first */
3739 goto end;
3740 }
3741
Christopher Faulet99eff652019-08-11 23:11:30 +02003742 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3743 fcgi_do_shutw(fstrm);
3744
3745 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3746 fcgi_do_shutr(fstrm);
3747
3748 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3749 /* We're done trying to send, remove ourself from the send_list */
3750 LIST_DEL_INIT(&fstrm->send_list);
3751
3752 if (!fstrm->cs) {
3753 fcgi_strm_destroy(fstrm);
3754 if (fcgi_conn_is_dead(fconn))
3755 fcgi_release(fconn);
3756 }
3757 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003758 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003759 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003760 return NULL;
3761}
3762
3763/* shutr() called by the conn_stream (mux_ops.shutr) */
3764static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3765{
3766 struct fcgi_strm *fstrm = cs->ctx;
3767
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003768 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003769 if (cs->flags & CS_FL_KILL_CONN)
3770 fstrm->flags |= FCGI_SF_KILL_CONN;
3771
3772 if (!mode)
3773 return;
3774
3775 fcgi_do_shutr(fstrm);
3776}
3777
3778/* shutw() called by the conn_stream (mux_ops.shutw) */
3779static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3780{
3781 struct fcgi_strm *fstrm = cs->ctx;
3782
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003783 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003784 if (cs->flags & CS_FL_KILL_CONN)
3785 fstrm->flags |= FCGI_SF_KILL_CONN;
3786
3787 fcgi_do_shutw(fstrm);
3788}
3789
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003790/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3791 * event subscriber <es> is not allowed to change from a previous call as long
3792 * as at least one event is still subscribed. The <event_type> must only be a
3793 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003794 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003795static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003796{
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 struct fcgi_strm *fstrm = cs->ctx;
3798 struct fcgi_conn *fconn = fstrm->fconn;
3799
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003800 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003801 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003802
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003803 es->events |= event_type;
3804 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003805
3806 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003807 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003808
Christopher Faulet99eff652019-08-11 23:11:30 +02003809 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003810 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003811 if (!LIST_ADDED(&fstrm->send_list))
3812 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003813 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003814 return 0;
3815}
3816
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003817/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3818 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3819 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003820 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003821static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003822{
Christopher Faulet99eff652019-08-11 23:11:30 +02003823 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003824 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003825
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003826 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003827 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003828
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003829 es->events &= ~event_type;
3830 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003831 fstrm->subs = NULL;
3832
3833 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003834 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003835
Christopher Faulet99eff652019-08-11 23:11:30 +02003836 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003837 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003838 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003839 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3840 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003841 }
3842 return 0;
3843}
3844
3845/* Called from the upper layer, to receive data */
3846static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3847{
3848 struct fcgi_strm *fstrm = cs->ctx;
3849 struct fcgi_conn *fconn = fstrm->fconn;
3850 size_t ret = 0;
3851
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003852 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3853
Christopher Faulet99eff652019-08-11 23:11:30 +02003854 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3855 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003856 else
3857 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003858
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003859 if (b_data(&fstrm->rxbuf))
Christopher Faulet99eff652019-08-11 23:11:30 +02003860 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3861 else {
3862 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003863 if (fstrm->state == FCGI_SS_ERROR || (fstrm->h1m.state == H1_MSG_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 cs->flags |= CS_FL_EOI;
3865 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3866 cs->flags |= CS_FL_EOS;
3867 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003868 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003869 cs->flags |= CS_FL_EOS;
3870 if (cs->flags & CS_FL_ERR_PENDING)
3871 cs->flags |= CS_FL_ERROR;
3872 fcgi_release_buf(fconn, &fstrm->rxbuf);
3873 }
3874
3875 if (ret && fconn->dsi == fstrm->id) {
3876 /* demux is blocking on this stream's buffer */
3877 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3878 fcgi_conn_restart_reading(fconn, 1);
3879 }
3880
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003881 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003882 return ret;
3883}
3884
3885
Christopher Faulet99eff652019-08-11 23:11:30 +02003886/* Called from the upper layer, to send data from buffer <buf> for no more than
3887 * <count> bytes. Returns the number of bytes effectively sent. Some status
3888 * flags may be updated on the conn_stream.
3889 */
3890static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3891{
3892 struct fcgi_strm *fstrm = cs->ctx;
3893 struct fcgi_conn *fconn = fstrm->fconn;
3894 size_t total = 0;
3895 size_t ret;
3896 struct htx *htx = NULL;
3897 struct htx_sl *sl;
3898 struct htx_blk *blk;
3899 uint32_t bsize;
3900
Willy Tarreau022e5e52020-09-10 09:33:15 +02003901 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003902
Christopher Faulet99eff652019-08-11 23:11:30 +02003903 /* If we were not just woken because we wanted to send but couldn't,
3904 * and there's somebody else that is waiting to send, do nothing,
3905 * we will subscribe later and be put at the end of the list
3906 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003907 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003908 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 +02003909 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003910 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003911 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003912
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003913 if (fconn->state < FCGI_CS_RECORD_H) {
3914 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003915 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003916 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003917
3918 htx = htxbuf(buf);
3919 if (fstrm->id == 0) {
3920 int32_t id = fcgi_conn_get_next_sid(fconn);
3921
3922 if (id < 0) {
3923 fcgi_strm_close(fstrm);
3924 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003925 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 +02003926 return 0;
3927 }
3928
3929 eb32_delete(&fstrm->by_id);
3930 fstrm->by_id.key = fstrm->id = id;
3931 fconn->max_id = id;
3932 fconn->nb_reserved--;
3933 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3934
3935
3936 /* Check if length of the body is known or if the message is
3937 * full. Otherwise, the request is invalid.
3938 */
3939 sl = http_get_stline(htx);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003940 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && !(htx->flags & HTX_FL_EOM))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003941 htx->flags |= HTX_FL_PARSING_ERROR;
3942 fcgi_strm_error(fstrm);
3943 goto done;
3944 }
3945 }
3946
3947 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003948 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 +02003949 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3950 goto done;
3951 }
3952
3953 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3954 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3955
Christopher Fauletfe410d62020-05-19 15:13:00 +02003956 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003957 count && !htx_is_empty(htx)) {
3958 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003959 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003960 bsize = htx_get_blksz(blk);
3961
3962 switch (htx_get_blk_type(blk)) {
3963 case HTX_BLK_REQ_SL:
3964 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003965 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 +02003966 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3967 if (!ret) {
3968 goto done;
3969 }
3970 total += ret;
3971 count -= ret;
3972 break;
3973
3974 case HTX_BLK_EOH:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01003975 if (!(fstrm->flags & FCGI_SF_EP_SENT)) {
3976 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
3977 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3978 if (!ret)
3979 goto done;
3980 }
3981 if (htx_is_unique_blk(htx, blk) && (htx->flags & HTX_FL_EOM)) {
3982 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
3983 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3984 if (!ret)
3985 goto done;
3986 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003987 goto remove_blk;
3988
3989 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003990 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 +02003991 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3992 if (ret > 0) {
3993 htx = htx_from_buf(buf);
3994 total += ret;
3995 count -= ret;
3996 if (ret < bsize)
3997 goto done;
3998 }
3999 break;
4000
Christopher Faulet99eff652019-08-11 23:11:30 +02004001 default:
4002 remove_blk:
4003 htx_remove_blk(htx, blk);
4004 total += bsize;
4005 count -= bsize;
4006 break;
4007 }
4008 }
4009
4010 done:
4011 if (fstrm->state >= FCGI_SS_HLOC) {
4012 /* trim any possibly pending data after we close (extra CR-LF,
4013 * unprocessed trailers, abnormal extra data, ...)
4014 */
4015 total += count;
4016 count = 0;
4017 }
4018
4019 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004020 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 +02004021 cs_set_error(cs);
4022 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4023 fcgi_strm_close(fstrm);
4024 }
4025
4026 if (htx)
4027 htx_to_buf(htx, buf);
4028
Christopher Faulet99eff652019-08-11 23:11:30 +02004029 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004030 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4031 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 +02004032 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004033 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004034
4035 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004036 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4037 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004038 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004039
4040 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004041 return total;
4042}
4043
4044/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004045static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004046{
4047 struct fcgi_conn *fconn = conn->ctx;
4048 struct fcgi_strm *fstrm = NULL;
4049 struct eb32_node *node;
4050 int send_cnt = 0;
4051 int tree_cnt = 0;
4052 int orph_cnt = 0;
4053 struct buffer *hmbuf, *tmbuf;
4054
4055 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004056 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004057
4058 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4059 send_cnt++;
4060
4061 fstrm = NULL;
4062 node = eb32_first(&fconn->streams_by_id);
4063 while (node) {
4064 fstrm = container_of(node, struct fcgi_strm, by_id);
4065 tree_cnt++;
4066 if (!fstrm->cs)
4067 orph_cnt++;
4068 node = eb32_next(node);
4069 }
4070
4071 hmbuf = br_head(fconn->mbuf);
4072 tmbuf = br_tail(fconn->mbuf);
4073 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4074 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4075 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4076 fconn->state, fconn->max_id, fconn->flags,
4077 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4078 fconn->wait_event.events, fconn->dsi,
4079 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4080 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4081 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4082 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4083 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4084 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4085 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4086
4087 if (fstrm) {
4088 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4089 fstrm, fstrm->id, fstrm->flags,
4090 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4091 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4092 fstrm->cs);
4093 if (fstrm->cs)
4094 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4095 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004096 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4097 if (fstrm->subs) {
4098 if (fstrm->subs) {
4099 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4100 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4101 fstrm->subs->tasklet->calls,
4102 fstrm->subs->tasklet->context);
4103 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4104 chunk_appendf(&trash, ")");
4105 }
4106 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004107 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004108 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004109}
4110
4111/* Migrate the the connection to the current thread.
4112 * Return 0 if successful, non-zero otherwise.
4113 * Expected to be called with the old thread lock held.
4114 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004115static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004116{
4117 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004118 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004119
4120 if (fd_takeover(conn->handle.fd, conn) != 0)
4121 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004122
4123 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4124 /* We failed to takeover the xprt, even if the connection may
4125 * still be valid, flag it as error'd, as we have already
4126 * taken over the fd, and wake the tasklet, so that it will
4127 * destroy it.
4128 */
4129 conn->flags |= CO_FL_ERROR;
4130 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4131 return -1;
4132 }
4133
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004134 if (fcgi->wait_event.events)
4135 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4136 fcgi->wait_event.events, &fcgi->wait_event);
4137 /* To let the tasklet know it should free itself, and do nothing else,
4138 * set its context to NULL;
4139 */
4140 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004141 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004142
4143 task = fcgi->task;
4144 if (task) {
4145 task->context = NULL;
4146 fcgi->task = NULL;
4147 __ha_barrier_store();
4148 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004149
4150 fcgi->task = task_new(tid_bit);
4151 if (!fcgi->task) {
4152 fcgi_release(fcgi);
4153 return -1;
4154 }
4155 fcgi->task->process = fcgi_timeout_task;
4156 fcgi->task->context = fcgi;
4157 }
4158 fcgi->wait_event.tasklet = tasklet_new();
4159 if (!fcgi->wait_event.tasklet) {
4160 fcgi_release(fcgi);
4161 return -1;
4162 }
4163 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4164 fcgi->wait_event.tasklet->context = fcgi;
4165 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4166 SUB_RETRY_RECV, &fcgi->wait_event);
4167
4168 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004169}
4170
4171/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004172/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004173/****************************************/
4174
4175/* The mux operations */
4176static const struct mux_ops mux_fcgi_ops = {
4177 .init = fcgi_init,
4178 .wake = fcgi_wake,
4179 .attach = fcgi_attach,
4180 .get_first_cs = fcgi_get_first_cs,
4181 .detach = fcgi_detach,
4182 .destroy = fcgi_destroy,
4183 .avail_streams = fcgi_avail_streams,
4184 .used_streams = fcgi_used_streams,
4185 .rcv_buf = fcgi_rcv_buf,
4186 .snd_buf = fcgi_snd_buf,
4187 .subscribe = fcgi_subscribe,
4188 .unsubscribe = fcgi_unsubscribe,
4189 .shutr = fcgi_shutr,
4190 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004191 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004192 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004193 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004194 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004195 .name = "FCGI",
4196};
4197
4198
4199/* this mux registers FCGI proto */
4200static struct mux_proto_list mux_proto_fcgi =
4201{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4202
4203INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4204
4205/*
4206 * Local variables:
4207 * c-indent-level: 8
4208 * c-basic-offset: 8
4209 * End:
4210 */