blob: 7cc69e427834ed4a577a6a1c076d83460c88e5d2 [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 Faulet7e0d0d82021-06-11 13:34:42 +020035#include <haproxy/version.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020036
Willy Tarreaub2551052020-06-09 09:07:15 +020037
Christopher Faulet99eff652019-08-11 23:11:30 +020038/* FCGI Connection flags (32 bits) */
39#define FCGI_CF_NONE 0x00000000
40
41/* Flags indicating why writing to the mux is blockes */
42#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
43#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
44#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
45
46/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
51#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
52#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
53#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
54#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
55#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
56#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
57#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
58
59/* Other flags */
60#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
61#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
62#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
63#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
64#define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */
65#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
66
67/* FCGI connection state (fcgi_conn->state) */
68enum fcgi_conn_st {
69 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
70 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
71 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
72 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
73 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
74 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
75 FCGI_CS_ENTRIES
76} __attribute__((packed));
77
78/* 32 buffers: one for the ring's root, rest for the mbuf itself */
79#define FCGI_C_MBUF_CNT 32
80
81/* FCGI connection descriptor */
82struct fcgi_conn {
83 struct connection *conn;
84
85 enum fcgi_conn_st state; /* FCGI connection state */
86 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
87 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
88 uint32_t flags; /* Connection flags: FCGI_CF_* */
89
90 int16_t dsi; /* dmux stream ID (<0 = idle ) */
91 uint16_t drl; /* demux record length (if dsi >= 0) */
92 uint8_t drt; /* demux record type (if dsi >= 0) */
93 uint8_t drp; /* demux record padding (if dsi >= 0) */
94
95 struct buffer dbuf; /* demux buffer */
96 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
97
98 int timeout; /* idle timeout duration in ticks */
99 int shut_timeout; /* idle timeout duration in ticks after shutdown */
100 unsigned int nb_streams; /* number of streams in the tree */
101 unsigned int nb_cs; /* number of attached conn_streams */
102 unsigned int nb_reserved; /* number of reserved streams */
103 unsigned int stream_cnt; /* total number of streams seen */
104
105 struct proxy *proxy; /* the proxy this connection was created for */
106 struct fcgi_app *app; /* FCGI application used by this mux */
107 struct task *task; /* timeout management task */
108 struct eb_root streams_by_id; /* all active streams by their ID */
109
110 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200111
112 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
113 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
114};
115
116
117/* FCGI stream state, in fcgi_strm->state */
118enum fcgi_strm_st {
119 FCGI_SS_IDLE = 0,
120 FCGI_SS_OPEN,
121 FCGI_SS_HREM, // half-closed(remote)
122 FCGI_SS_HLOC, // half-closed(local)
123 FCGI_SS_ERROR,
124 FCGI_SS_CLOSED,
125 FCGI_SS_ENTRIES
126} __attribute__((packed));
127
128
129/* FCGI stream flags (32 bits) */
130#define FCGI_SF_NONE 0x00000000
131#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
132#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
133#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
134
135/* Stream flags indicating the reason the stream is blocked */
136#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
137#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
138#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
139
140#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
141#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100142#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200143
144#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
145#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
146#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
147
148/* Other flags */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100149#define FCGI_SF_H1_PARSING_DONE 0x00010000
Christopher Faulet99eff652019-08-11 23:11:30 +0200150
151/* FCGI stream descriptor */
152struct fcgi_strm {
153 struct conn_stream *cs;
154 struct session *sess;
155 struct fcgi_conn *fconn;
156
157 int32_t id; /* stream ID */
158
159 uint32_t flags; /* Connection flags: FCGI_SF_* */
160 enum fcgi_strm_st state; /* FCGI stream state */
161 int proto_status; /* FCGI_PS_* */
162
163 struct h1m h1m; /* response parser state for H1 */
164
165 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
166
167 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100168 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200169 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100170 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 +0200171};
172
173/* Flags representing all default FCGI parameters */
174#define FCGI_SP_CGI_GATEWAY 0x00000001
175#define FCGI_SP_DOC_ROOT 0x00000002
176#define FCGI_SP_SCRIPT_NAME 0x00000004
177#define FCGI_SP_PATH_INFO 0x00000008
178#define FCGI_SP_REQ_URI 0x00000010
179#define FCGI_SP_REQ_METH 0x00000020
180#define FCGI_SP_REQ_QS 0x00000040
181#define FCGI_SP_SRV_PORT 0x00000080
182#define FCGI_SP_SRV_PROTO 0x00000100
183#define FCGI_SP_SRV_NAME 0x00000200
184#define FCGI_SP_REM_ADDR 0x00000400
185#define FCGI_SP_REM_PORT 0x00000800
186#define FCGI_SP_SCRIPT_FILE 0x00001000
187#define FCGI_SP_PATH_TRANS 0x00002000
188#define FCGI_SP_CONT_LEN 0x00004000
189#define FCGI_SP_HTTPS 0x00008000
Christopher Faulet7e0d0d82021-06-11 13:34:42 +0200190#define FCGI_SP_SRV_SOFT 0x00010000
191#define FCGI_SP_MASK 0x0001FFFF
Christopher Faulet99eff652019-08-11 23:11:30 +0200192#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;
Christopher Faulet7e0d0d82021-06-11 13:34:42 +0200209 struct ist srv_soft;
Christopher Faulet99eff652019-08-11 23:11:30 +0200210 int https;
211 struct buffer *p;
212};
213
214/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
215#define MAX_DATA_REALIGN 1024
216
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200217/* trace source and events */
218static void fcgi_trace(enum trace_level level, uint64_t mask,
219 const struct trace_source *src,
220 const struct ist where, const struct ist func,
221 const void *a1, const void *a2, const void *a3, const void *a4);
222
223/* The event representation is split like this :
224 * fconn - internal FCGI connection
225 * fstrm - internal FCGI stream
226 * strm - application layer
227 * rx - data receipt
228 * tx - data transmission
229 * rsp - response parsing
230 */
231static const struct trace_event fcgi_trace_events[] = {
232#define FCGI_EV_FCONN_NEW (1ULL << 0)
233 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
234#define FCGI_EV_FCONN_RECV (1ULL << 1)
235 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
236#define FCGI_EV_FCONN_SEND (1ULL << 2)
237 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
238#define FCGI_EV_FCONN_BLK (1ULL << 3)
239 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
240#define FCGI_EV_FCONN_WAKE (1ULL << 4)
241 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
242#define FCGI_EV_FCONN_END (1ULL << 5)
243 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
244#define FCGI_EV_FCONN_ERR (1ULL << 6)
245 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
246
247#define FCGI_EV_RX_FHDR (1ULL << 7)
248 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
249#define FCGI_EV_RX_RECORD (1ULL << 8)
250 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
251#define FCGI_EV_RX_EOI (1ULL << 9)
252 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
253#define FCGI_EV_RX_GETVAL (1ULL << 10)
254 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
255#define FCGI_EV_RX_STDOUT (1ULL << 11)
256 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
257#define FCGI_EV_RX_STDERR (1ULL << 12)
258 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
259#define FCGI_EV_RX_ENDREQ (1ULL << 13)
260 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
261
262#define FCGI_EV_TX_RECORD (1ULL << 14)
263 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
264#define FCGI_EV_TX_EOI (1ULL << 15)
265 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
266#define FCGI_EV_TX_BEGREQ (1ULL << 16)
267 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
268#define FCGI_EV_TX_GETVAL (1ULL << 17)
269 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
270#define FCGI_EV_TX_PARAMS (1ULL << 18)
271 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
272#define FCGI_EV_TX_STDIN (1ULL << 19)
273 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
274#define FCGI_EV_TX_ABORT (1ULL << 20)
275 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
276
277#define FCGI_EV_RSP_DATA (1ULL << 21)
278 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
279#define FCGI_EV_RSP_EOM (1ULL << 22)
280 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
281#define FCGI_EV_RSP_HDRS (1ULL << 23)
282 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
283#define FCGI_EV_RSP_BODY (1ULL << 24)
284 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
285#define FCGI_EV_RSP_TLRS (1ULL << 25)
286 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
287
288#define FCGI_EV_FSTRM_NEW (1ULL << 26)
289 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
290#define FCGI_EV_FSTRM_BLK (1ULL << 27)
291 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
292#define FCGI_EV_FSTRM_END (1ULL << 28)
293 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
294#define FCGI_EV_FSTRM_ERR (1ULL << 29)
295 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
296
297#define FCGI_EV_STRM_NEW (1ULL << 30)
298 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
299#define FCGI_EV_STRM_RECV (1ULL << 31)
300 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
301#define FCGI_EV_STRM_SEND (1ULL << 32)
302 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
303#define FCGI_EV_STRM_FULL (1ULL << 33)
304 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
305#define FCGI_EV_STRM_WAKE (1ULL << 34)
306 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
307#define FCGI_EV_STRM_SHUT (1ULL << 35)
308 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
309#define FCGI_EV_STRM_END (1ULL << 36)
310 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
311#define FCGI_EV_STRM_ERR (1ULL << 37)
312 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
313
314 { }
315};
316
317static const struct name_desc fcgi_trace_lockon_args[4] = {
318 /* arg1 */ { /* already used by the connection */ },
319 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
320 /* arg3 */ { },
321 /* arg4 */ { }
322};
323
324
325static const struct name_desc fcgi_trace_decoding[] = {
326#define FCGI_VERB_CLEAN 1
327 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
328#define FCGI_VERB_MINIMAL 2
329 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
330#define FCGI_VERB_SIMPLE 3
331 { .name="simple", .desc="add request/response status line or htx info when available" },
332#define FCGI_VERB_ADVANCED 4
333 { .name="advanced", .desc="add header fields or record decoding when available" },
334#define FCGI_VERB_COMPLETE 5
335 { .name="complete", .desc="add full data dump when available" },
336 { /* end */ }
337};
338
339static struct trace_source trace_fcgi = {
340 .name = IST("fcgi"),
341 .desc = "FastCGI multiplexer",
342 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
343 .default_cb = fcgi_trace,
344 .known_events = fcgi_trace_events,
345 .lockon_args = fcgi_trace_lockon_args,
346 .decoding = fcgi_trace_decoding,
347 .report_events = ~0, // report everything by default
348};
349
350#define TRACE_SOURCE &trace_fcgi
351INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
352
Christopher Faulet99eff652019-08-11 23:11:30 +0200353/* FCGI connection and stream pools */
354DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
355DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
356
357static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
358static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau9a5a0e02021-01-20 14:55:01 +0100359/* fcgi_io_cb is exported to see it resolved in "show fd" */
360struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200361static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
362static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
363static 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 +0200364static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
365static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200366static void fcgi_strm_alert(struct fcgi_strm *fstrm);
367static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
368
369/* a dmumy management stream */
370static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
371 .cs = NULL,
372 .fconn = NULL,
373 .state = FCGI_SS_CLOSED,
374 .flags = FCGI_SF_NONE,
375 .id = 0,
376};
377
378/* and a dummy idle stream for use with any unknown stream */
379static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
380 .cs = NULL,
381 .fconn = NULL,
382 .state = FCGI_SS_IDLE,
383 .flags = FCGI_SF_NONE,
384 .id = 0,
385};
386
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200387/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
388static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
389{
390 switch (st) {
391 case FCGI_CS_INIT : return "INI";
392 case FCGI_CS_SETTINGS : return "STG";
393 case FCGI_CS_RECORD_H : return "RDH";
394 case FCGI_CS_RECORD_D : return "RDD";
395 case FCGI_CS_RECORD_P : return "RDP";
396 case FCGI_CS_CLOSED : return "CLO";
397 default : return "???";
398 }
399}
400
401/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
402static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
403{
404 switch (st) {
405 case FCGI_SS_IDLE : return "IDL";
406 case FCGI_SS_OPEN : return "OPN";
407 case FCGI_SS_HREM : return "RCL";
408 case FCGI_SS_HLOC : return "HCL";
409 case FCGI_SS_ERROR : return "ERR";
410 case FCGI_SS_CLOSED : return "CLO";
411 default : return "???";
412 }
413}
414
415
416/* the FCGI traces always expect that arg1, if non-null, is of type connection
417 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
418 * and that arg3, if non-null, is a htx for rx/tx headers.
419 */
420static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
421 const struct ist where, const struct ist func,
422 const void *a1, const void *a2, const void *a3, const void *a4)
423{
424 const struct connection *conn = a1;
425 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
426 const struct fcgi_strm *fstrm = a2;
427 const struct htx *htx = a3;
428 const size_t *val = a4;
429
430 if (!fconn)
431 fconn = (fstrm ? fstrm->fconn : NULL);
432
433 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
434 return;
435
436 /* Display the response state if fstrm is defined */
437 if (fstrm)
438 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
439
440 if (src->verbosity == FCGI_VERB_CLEAN)
441 return;
442
443 /* Display the value to the 4th argument (level > STATE) */
444 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100445 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200446
447 /* Display status-line if possible (verbosity > MINIMAL) */
448 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
449 const struct htx_blk *blk = htx_get_head_blk(htx);
450 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
451 enum htx_blk_type type = htx_get_blk_type(blk);
452
453 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
454 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
455 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
456 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
457 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
458 }
459
460 /* Display fconn info and, if defined, fstrm info */
461 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
462 if (fstrm)
463 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
464
465 if (!fstrm || fstrm->id <= 0)
466 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
467 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
468 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
469
470 if (src->verbosity == FCGI_VERB_MINIMAL)
471 return;
472
473 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
474 if (src->level > TRACE_LEVEL_USER) {
475 if (src->verbosity == FCGI_VERB_COMPLETE ||
476 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
477 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
478 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
479 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
480 if (src->verbosity == FCGI_VERB_COMPLETE ||
481 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
482 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
483 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
484
485 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
486 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
487 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
488 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
489 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
490 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
491 }
492
493 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
494 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
495 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
496 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
497 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
498 }
499
500 /* Display htx info if defined (level > USER) */
501 if (src->level > TRACE_LEVEL_USER && htx) {
502 int full = 0;
503
504 /* Full htx info (level > STATE && verbosity > SIMPLE) */
505 if (src->level > TRACE_LEVEL_STATE) {
506 if (src->verbosity == FCGI_VERB_COMPLETE)
507 full = 1;
508 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
509 full = 1;
510 }
511
512 chunk_memcat(&trace_buf, "\n\t", 2);
513 htx_dump(&trace_buf, htx, full);
514 }
515}
Christopher Faulet99eff652019-08-11 23:11:30 +0200516
517/*****************************************************/
518/* functions below are for dynamic buffer management */
519/*****************************************************/
520
521/* Indicates whether or not the we may call the fcgi_recv() function to attempt
522 * to receive data into the buffer and/or demux pending data. The condition is
523 * a bit complex due to some API limits for now. The rules are the following :
524 * - if an error or a shutdown was detected on the connection and the buffer
525 * is empty, we must not attempt to receive
526 * - if the demux buf failed to be allocated, we must not try to receive and
527 * we know there is nothing pending
528 * - if no flag indicates a blocking condition, we may attempt to receive,
529 * regardless of whether the demux buffer is full or not, so that only
530 * de demux part decides whether or not to block. This is needed because
531 * the connection API indeed prevents us from re-enabling receipt that is
532 * already enabled in a polled state, so we must always immediately stop
533 * as soon as the demux can't proceed so as never to hit an end of read
534 * with data pending in the buffers.
535 * - otherwise must may not attempt
536 */
537static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
538{
539 if (b_data(&fconn->dbuf) == 0 &&
540 (fconn->state == FCGI_CS_CLOSED ||
541 fconn->conn->flags & CO_FL_ERROR ||
542 conn_xprt_read0_pending(fconn->conn)))
543 return 0;
544
545 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
546 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
547 return 1;
548
549 return 0;
550}
551
552/* Restarts reading on the connection if it was not enabled */
553static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
554{
555 if (!fcgi_recv_allowed(fconn))
556 return;
557 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
558 (fconn->wait_event.events & SUB_RETRY_RECV))
559 return;
560 tasklet_wakeup(fconn->wait_event.tasklet);
561}
562
563
564/* Tries to grab a buffer and to re-enable processing on mux <target>. The
565 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
566 * the allocation succeeds, in which case the connection is woken up, or 0 if
567 * it's impossible to wake up and we prefer to be woken up later.
568 */
569static int fcgi_buf_available(void *target)
570{
571 struct fcgi_conn *fconn = target;
572 struct fcgi_strm *fstrm;
573
574 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200575 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 +0200576 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
577 fcgi_conn_restart_reading(fconn, 1);
578 return 1;
579 }
580
581 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200582 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 +0200583 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200584 if (fconn->flags & FCGI_CF_DEM_MROOM) {
585 fconn->flags &= ~FCGI_CF_DEM_MROOM;
586 fcgi_conn_restart_reading(fconn, 1);
587 }
588 return 1;
589 }
590
591 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
592 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
593 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200594 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 +0200595 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
596 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200597 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200598 return 1;
599 }
600
601 return 0;
602}
603
604static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
605{
606 struct buffer *buf = NULL;
607
Willy Tarreau954827a2021-02-20 11:49:49 +0100608 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200609 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
610 fconn->buf_wait.target = fconn;
611 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau954827a2021-02-20 11:49:49 +0100612 LIST_ADDQ(&ti->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200613 }
614 return buf;
615}
616
617static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
618{
619 if (bptr->size) {
620 b_free(bptr);
Willy Tarreau132b3a42021-02-20 12:02:46 +0100621 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200622 }
623}
624
625static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
626{
627 struct buffer *buf;
628 unsigned int count = 0;
629
630 while (b_size(buf = br_head_pick(fconn->mbuf))) {
631 b_free(buf);
632 count++;
633 }
634 if (count)
Willy Tarreau132b3a42021-02-20 12:02:46 +0100635 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200636}
637
638/* Returns the number of allocatable outgoing streams for the connection taking
639 * the number reserved streams into account.
640 */
641static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
642{
643 int ret;
644
645 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
646 if (ret < 0)
647 ret = 0;
648 return ret;
649}
650
651/* Returns the number of streams in use on a connection to figure if it's
652 * idle or not. We check nb_cs and not nb_streams as the caller will want
653 * to know if it was the last one after a detach().
654 */
655static int fcgi_used_streams(struct connection *conn)
656{
657 struct fcgi_conn *fconn = conn->ctx;
658
659 return fconn->nb_cs;
660}
661
662/* Returns the number of concurrent streams available on the connection */
663static int fcgi_avail_streams(struct connection *conn)
664{
665 struct server *srv = objt_server(conn->target);
666 struct fcgi_conn *fconn = conn->ctx;
667 int ret1, ret2;
668
669 /* Don't open new stream if the connection is closed */
670 if (fconn->state == FCGI_CS_CLOSED)
671 return 0;
672
673 /* May be negative if this setting has changed */
674 ret1 = (fconn->streams_limit - fconn->nb_streams);
675
676 /* we must also consider the limit imposed by stream IDs */
677 ret2 = fcgi_streams_left(fconn);
678 ret1 = MIN(ret1, ret2);
679 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
680 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
681 ret1 = MIN(ret1, ret2);
682 }
683 return ret1;
684}
685
686/*****************************************************************/
687/* functions below are dedicated to the mux setup and management */
688/*****************************************************************/
689
690/* Initializes the mux once it's attached. Only outgoing connections are
691 * supported. So the context is already initialized before installing the
692 * mux. <input> is always used as Input buffer and may contain data. It is the
693 * caller responsibility to not reuse it anymore. Returns < 0 on error.
694 */
695static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
696 struct buffer *input)
697{
698 struct fcgi_conn *fconn;
699 struct fcgi_strm *fstrm;
700 struct fcgi_app *app = get_px_fcgi_app(px);
701 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200702 void *conn_ctx = conn->ctx;
703
704 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200705
706 if (!app)
707 goto fail_conn;
708
709 fconn = pool_alloc(pool_head_fcgi_conn);
710 if (!fconn)
711 goto fail_conn;
712
713 fconn->shut_timeout = fconn->timeout = px->timeout.server;
714 if (tick_isset(px->timeout.serverfin))
715 fconn->shut_timeout = px->timeout.serverfin;
716
717 fconn->flags = FCGI_CF_NONE;
718
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500719 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200720 if (app->flags & FCGI_APP_FL_KEEP_CONN)
721 fconn->flags |= FCGI_CF_KEEP_CONN;
722 if (app->flags & FCGI_APP_FL_GET_VALUES)
723 fconn->flags |= FCGI_CF_GET_VALUES;
724 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
725 fconn->flags |= FCGI_CF_MPXS_CONNS;
726
727 fconn->proxy = px;
728 fconn->app = app;
729 fconn->task = NULL;
730 if (tick_isset(fconn->timeout)) {
731 t = task_new(tid_bit);
732 if (!t)
733 goto fail;
734
735 fconn->task = t;
736 t->process = fcgi_timeout_task;
737 t->context = fconn;
738 t->expire = tick_add(now_ms, fconn->timeout);
739 }
740
741 fconn->wait_event.tasklet = tasklet_new();
742 if (!fconn->wait_event.tasklet)
743 goto fail;
744 fconn->wait_event.tasklet->process = fcgi_io_cb;
745 fconn->wait_event.tasklet->context = fconn;
746 fconn->wait_event.events = 0;
747
748 /* Initialise the context. */
749 fconn->state = FCGI_CS_INIT;
750 fconn->conn = conn;
751 fconn->streams_limit = app->maxreqs;
752 fconn->max_id = -1;
753 fconn->nb_streams = 0;
754 fconn->nb_cs = 0;
755 fconn->nb_reserved = 0;
756 fconn->stream_cnt = 0;
757
758 fconn->dbuf = *input;
759 fconn->dsi = -1;
760
761 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
762 fconn->streams_by_id = EB_ROOT;
763 LIST_INIT(&fconn->send_list);
Willy Tarreau954827a2021-02-20 11:49:49 +0100764 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200765
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200766 conn->ctx = fconn;
767
Christopher Faulet99eff652019-08-11 23:11:30 +0200768 if (t)
769 task_queue(t);
770
771 /* FIXME: this is temporary, for outgoing connections we need to
772 * immediately allocate a stream until the code is modified so that the
773 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200774 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200775 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200776 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200777 if (!fstrm)
778 goto fail;
779
Christopher Faulet99eff652019-08-11 23:11:30 +0200780
781 /* Repare to read something */
782 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200783 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200784 return 0;
785
786 fail:
787 task_destroy(t);
788 if (fconn->wait_event.tasklet)
789 tasklet_free(fconn->wait_event.tasklet);
790 pool_free(pool_head_fcgi_conn, fconn);
791 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200792 conn->ctx = conn_ctx; // restore saved ctx
793 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200794 return -1;
795}
796
797/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
798 * -1 if no more is allocatable.
799 */
800static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
801{
802 int32_t id = (fconn->max_id + 1) | 1;
803
804 if ((id & 0x80000000U))
805 id = -1;
806 return id;
807}
808
809/* Returns the stream associated with id <id> or NULL if not found */
810static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
811{
812 struct eb32_node *node;
813
814 if (id == 0)
815 return (struct fcgi_strm *)fcgi_mgmt_stream;
816
817 if (id > fconn->max_id)
818 return (struct fcgi_strm *)fcgi_unknown_stream;
819
820 node = eb32_lookup(&fconn->streams_by_id, id);
821 if (!node)
822 return (struct fcgi_strm *)fcgi_unknown_stream;
823 return container_of(node, struct fcgi_strm, by_id);
824}
825
826
827/* Release function. This one should be called to free all resources allocated
828 * to the mux.
829 */
830static void fcgi_release(struct fcgi_conn *fconn)
831{
William Dauchy477757c2020-08-07 22:19:23 +0200832 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200833
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200834 TRACE_POINT(FCGI_EV_FCONN_END);
835
Christopher Faulet99eff652019-08-11 23:11:30 +0200836 if (fconn) {
837 /* The connection must be attached to this mux to be released */
838 if (fconn->conn && fconn->conn->ctx == fconn)
839 conn = fconn->conn;
840
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200841 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
842
Willy Tarreau954827a2021-02-20 11:49:49 +0100843 if (LIST_ADDED(&fconn->buf_wait.list))
844 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200845
846 fcgi_release_buf(fconn, &fconn->dbuf);
847 fcgi_release_mbuf(fconn);
848
849 if (fconn->task) {
850 fconn->task->context = NULL;
851 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
852 fconn->task = NULL;
853 }
854 if (fconn->wait_event.tasklet)
855 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200856 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200857 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
858 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200859
860 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200861 }
862
863 if (conn) {
864 conn->mux = NULL;
865 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200866 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200867
868 conn_stop_tracking(conn);
869 conn_full_close(conn);
870 if (conn->destroy_cb)
871 conn->destroy_cb(conn);
872 conn_free(conn);
873 }
874}
875
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200876/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
877 * pending on the connection AND if there is no more data in the demux
878 * buffer. The function returns 1 to report a read0 or 0 otherwise.
879 */
880static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
881{
882 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
883 return 1;
884 return 0;
885}
886
Christopher Faulet99eff652019-08-11 23:11:30 +0200887
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500888/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200889static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
890{
891 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
892 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
893 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
894 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
895 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
896 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
897 conn_xprt_read0_pending(fconn->conn))))
898 return 1;
899 return 0;
900}
901
902
903/********************************************************/
904/* functions below are for the FCGI protocol processing */
905/********************************************************/
906
Christopher Faulet99eff652019-08-11 23:11:30 +0200907/* Marks an error on the stream. */
908static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
909{
910 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200911 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
912 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200913 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200914 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
915 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200916 if (fstrm->cs)
917 cs_set_error(fstrm->cs);
918 }
919}
920
921/* Attempts to notify the data layer of recv availability */
922static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
923{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100924 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200925 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100926 tasklet_wakeup(fstrm->subs->tasklet);
927 fstrm->subs->events &= ~SUB_RETRY_RECV;
928 if (!fstrm->subs->events)
929 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200930 }
931}
932
933/* Attempts to notify the data layer of send availability */
934static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
935{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100936 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200937 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100938 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100939 tasklet_wakeup(fstrm->subs->tasklet);
940 fstrm->subs->events &= ~SUB_RETRY_SEND;
941 if (!fstrm->subs->events)
942 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200943 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100944 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
945 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
946 tasklet_wakeup(fstrm->shut_tl);
947 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200948}
949
950/* Alerts the data layer, trying to wake it up by all means, following
951 * this sequence :
952 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
953 * for recv
954 * - if its subscribed to send, then it's woken up for send
955 * - if it was subscribed to neither, its ->wake() callback is called
956 * It is safe to call this function with a closed stream which doesn't have a
957 * conn_stream anymore.
958 */
959static void fcgi_strm_alert(struct fcgi_strm *fstrm)
960{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200961 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100962 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100963 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200964 fcgi_strm_notify_recv(fstrm);
965 fcgi_strm_notify_send(fstrm);
966 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200967 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
968 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200969 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200970 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200971}
972
973/* Writes the 16-bit record size <len> at address <record> */
974static inline void fcgi_set_record_size(void *record, uint16_t len)
975{
976 uint8_t *out = (record + 4);
977
978 *out = (len >> 8);
979 *(out + 1) = (len & 0xff);
980}
981
982/* Writes the 16-bit stream id <id> at address <record> */
983static inline void fcgi_set_record_id(void *record, uint16_t id)
984{
985 uint8_t *out = (record + 2);
986
987 *out = (id >> 8);
988 *(out + 1) = (id & 0xff);
989}
990
991/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
992 * its connection if the stream was not yet closed. Please use this exclusively
993 * before closing a stream to ensure stream count is well maintained.
994 */
995static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
996{
997 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200998 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200999 fstrm->fconn->nb_streams--;
1000 if (!fstrm->id)
1001 fstrm->fconn->nb_reserved--;
1002 if (fstrm->cs) {
1003 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1004 fcgi_strm_notify_recv(fstrm);
1005 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001006 fstrm->state = FCGI_SS_CLOSED;
1007 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1008 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001009 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001010}
1011
1012/* Detaches a FCGI stream from its FCGI connection and releases it to the
1013 * fcgi_strm pool.
1014 */
1015static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1016{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001017 struct connection *conn = fstrm->fconn->conn;
1018
1019 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1020
Christopher Faulet99eff652019-08-11 23:11:30 +02001021 fcgi_strm_close(fstrm);
1022 eb32_delete(&fstrm->by_id);
1023 if (b_size(&fstrm->rxbuf)) {
1024 b_free(&fstrm->rxbuf);
Willy Tarreau132b3a42021-02-20 12:02:46 +01001025 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001026 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001027 if (fstrm->subs)
1028 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001029 /* There's no need to explicitly call unsubscribe here, the only
1030 * reference left would be in the fconn send_list/fctl_list, and if
1031 * we're in it, we're getting out anyway
1032 */
1033 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001034 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001035 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001036
1037 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001038}
1039
1040/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1041 * stream tree. In case of error, nothing is added and NULL is returned. The
1042 * causes of errors can be any failed memory allocation. The caller is
1043 * responsible for checking if the connection may support an extra stream prior
1044 * to calling this function.
1045 */
1046static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1047{
1048 struct fcgi_strm *fstrm;
1049
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001050 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1051
Christopher Faulet99eff652019-08-11 23:11:30 +02001052 fstrm = pool_alloc(pool_head_fcgi_strm);
1053 if (!fstrm)
1054 goto out;
1055
Willy Tarreau7aad7032020-01-16 17:20:57 +01001056 fstrm->shut_tl = tasklet_new();
1057 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001058 pool_free(pool_head_fcgi_strm, fstrm);
1059 goto out;
1060 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001061 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001062 fstrm->shut_tl->process = fcgi_deferred_shut;
1063 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001064 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001065 fstrm->fconn = fconn;
1066 fstrm->cs = NULL;
1067 fstrm->flags = FCGI_SF_NONE;
1068 fstrm->proto_status = 0;
1069 fstrm->state = FCGI_SS_IDLE;
1070 fstrm->rxbuf = BUF_NULL;
1071
1072 h1m_init_res(&fstrm->h1m);
1073 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1074 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1075
1076 fstrm->by_id.key = fstrm->id = id;
1077 if (id > 0)
1078 fconn->max_id = id;
1079 else
1080 fconn->nb_reserved++;
1081
1082 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1083 fconn->nb_streams++;
1084 fconn->stream_cnt++;
1085
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001086 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001087 return fstrm;
1088
1089 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001090 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 +02001091 return NULL;
1092}
1093
1094/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1095 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1096 * highest possible stream ID was reached.
1097 */
1098static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1099 struct session *sess)
1100{
1101 struct fcgi_strm *fstrm = NULL;
1102
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001103 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1104 if (fconn->nb_streams >= fconn->streams_limit) {
1105 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 +02001106 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001107 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001108
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001109 if (fcgi_streams_left(fconn) < 1) {
1110 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 +02001111 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001112 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001113
1114 /* Defer choosing the ID until we send the first message to create the stream */
1115 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001116 if (!fstrm) {
1117 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 +02001118 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001119 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001120
1121 fstrm->cs = cs;
1122 fstrm->sess = sess;
1123 cs->ctx = fstrm;
1124 fconn->nb_cs++;
1125
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001126 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001127 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001128
1129 out:
1130 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001131}
1132
1133/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1134 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1135 * automatically updated accordingly. If the stream is orphaned, it is
1136 * destroyed.
1137 */
1138static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1139{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001140 struct fcgi_conn *fconn = fstrm->fconn;
1141
1142 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1143
Christopher Faulet99eff652019-08-11 23:11:30 +02001144 if (!fstrm->cs) {
1145 /* this stream was already orphaned */
1146 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001147 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001148 return;
1149 }
1150
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001151 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001152 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001154 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1155 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001156 else if (fstrm->state == FCGI_SS_HLOC)
1157 fcgi_strm_close(fstrm);
1158 }
1159
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001161 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1162 if (fstrm->cs->flags & CS_FL_EOS)
1163 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164
1165 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001166 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001167 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1168 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001169 }
1170
1171 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001172
1173 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001174}
1175
1176/* Wakes unassigned streams (ID == 0) attached to the connection. */
1177static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1178{
1179 struct eb32_node *node;
1180 struct fcgi_strm *fstrm;
1181
1182 node = eb32_lookup(&fconn->streams_by_id, 0);
1183 while (node) {
1184 fstrm = container_of(node, struct fcgi_strm, by_id);
1185 if (fstrm->id > 0)
1186 break;
1187 node = eb32_next(node);
1188 fcgi_strm_wake_one_stream(fstrm);
1189 }
1190}
1191
1192/* Wakes the streams attached to the connection, whose id is greater than <last>
1193 * or unassigned.
1194 */
1195static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1196{
1197 struct eb32_node *node;
1198 struct fcgi_strm *fstrm;
1199
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001200 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1201
Christopher Faulet99eff652019-08-11 23:11:30 +02001202 /* Wake all streams with ID > last */
1203 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1204 while (node) {
1205 fstrm = container_of(node, struct fcgi_strm, by_id);
1206 node = eb32_next(node);
1207 fcgi_strm_wake_one_stream(fstrm);
1208 }
1209 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001210
1211 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001212}
1213
1214static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1215 struct htx *htx, struct htx_sl *sl,
1216 struct fcgi_strm_params *params)
1217{
1218 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1219 struct ist p;
1220
1221 if (!sl)
1222 goto error;
1223
1224 if (!(params->mask & FCGI_SP_DOC_ROOT))
1225 params->docroot = fconn->app->docroot;
1226
1227 if (!(params->mask & FCGI_SP_REQ_METH)) {
1228 p = htx_sl_req_meth(sl);
1229 params->meth = ist2(b_tail(params->p), p.len);
1230 chunk_memcat(params->p, p.ptr, p.len);
1231 }
1232 if (!(params->mask & FCGI_SP_REQ_URI)) {
Christopher Fauletbdda8452021-04-26 09:38:55 +02001233 p = h1_get_uri(sl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001234 params->uri = ist2(b_tail(params->p), p.len);
1235 chunk_memcat(params->p, p.ptr, p.len);
1236 }
1237 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1238 p = htx_sl_req_vsn(sl);
1239 params->vsn = ist2(b_tail(params->p), p.len);
1240 chunk_memcat(params->p, p.ptr, p.len);
1241 }
1242 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1243 char *end;
1244 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001245 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001246 port = get_host_port(cli_conn->dst);
1247 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1248 if (!end)
1249 goto error;
1250 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1251 params->p->data += params->srv_port.len;
1252 }
1253 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1254 /* If no Host header found, use the server address to fill
1255 * srv_name */
1256 if (!istlen(params->srv_name)) {
1257 char *ptr = NULL;
1258
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001259 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001260 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1261 ptr = b_tail(params->p);
1262 if (ptr) {
1263 params->srv_name = ist2(ptr, strlen(ptr));
1264 params->p->data += params->srv_name.len;
1265 }
1266 }
1267 }
1268 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1269 char *ptr = NULL;
1270
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001271 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001272 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1273 ptr = b_tail(params->p);
1274 if (ptr) {
1275 params->rem_addr = ist2(ptr, strlen(ptr));
1276 params->p->data += params->rem_addr.len;
1277 }
1278 }
1279 if (!(params->mask & FCGI_SP_REM_PORT)) {
1280 char *end;
1281 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001282 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001283 port = get_host_port(cli_conn->src);
1284 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1285 if (!end)
1286 goto error;
1287 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1288 params->p->data += params->rem_port.len;
1289 }
1290 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1291 struct htx_blk *blk;
1292 enum htx_blk_type type;
1293 char *end;
1294 size_t len = 0;
1295
1296 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1297 type = htx_get_blk_type(blk);
1298
1299 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1300 break;
1301 if (type == HTX_BLK_DATA)
1302 len += htx_get_blksz(blk);
1303 }
1304 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1305 if (!end)
1306 goto error;
1307 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1308 params->p->data += params->cont_len.len;
1309 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001310#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001311 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001312 if (cli_conn)
1313 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001314 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001315#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001316 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1317 /* one of scriptname, pathinfo or query_string is no set */
1318 struct ist path = http_get_path(params->uri);
1319 int len;
1320
Christopher Faulet99eff652019-08-11 23:11:30 +02001321 /* No scrit_name set but no valid path ==> error */
1322 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1323 goto error;
1324
Christopher Faulet99eff652019-08-11 23:11:30 +02001325 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001326 if (!(params->mask & FCGI_SP_REQ_QS)) {
1327 struct ist qs = istfind(path, '?');
1328
1329 /* Update the path length */
1330 path.len -= qs.len;
1331
1332 /* Set the query-string skipping the '?', if any */
1333 if (istlen(qs))
1334 params->qs = istnext(qs);
1335 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001336
1337 /* If the script_name is set, don't try to deduce the path_info
1338 * too. The opposite is not true.
1339 */
1340 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1341 params->mask |= FCGI_SP_PATH_INFO;
1342 goto end;
1343 }
1344
Christopher Faulet0f17a442020-07-23 15:44:37 +02001345 /* Decode the path. it must first be copied to keep the URI
1346 * untouched.
1347 */
1348 chunk_memcat(params->p, path.ptr, path.len);
1349 path.ptr = b_tail(params->p) - path.len;
1350 len = url_decode(ist0(path), 0);
1351 if (len < 0)
1352 goto error;
1353 path.len = len;
1354
Christopher Faulet99eff652019-08-11 23:11:30 +02001355 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001356 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001357
1358 /* If there is no regex to match the pathinfo, just to the last
1359 * part and see if the index must be used.
1360 */
1361 if (!fconn->app->pathinfo_re)
1362 goto check_index;
1363
Christopher Faulet28cb3662020-02-14 14:47:37 +01001364 /* If some special characters are found in the decoded path (\n
1365 * or \0), the PATH_INFO regex cannot match. This is theorically
1366 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001367 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001368 * case.
1369 */
1370 if (istchr(path, '\n') || istchr(path, '\0'))
1371 goto error;
1372
Christopher Faulet99eff652019-08-11 23:11:30 +02001373 /* The regex does not match, just to the last part and see if
1374 * the index must be used.
1375 */
1376 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1377 goto check_index;
1378
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001379 /* We must have at least 1 capture for the script name,
1380 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001381 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001382 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001383 goto check_index;
1384
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001385 /* Finally we can set the script_name and the path_info. The
1386 * path_info is set if not already defined, and if it was
1387 * captured
1388 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001389 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001390 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1391 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001392
1393 check_index:
1394 len = params->scriptname.len;
1395 /* the script_name if finished by a '/' so we can add the index
1396 * part, if any.
1397 */
1398 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1399 struct ist sn = params->scriptname;
1400
1401 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1402 chunk_memcat(params->p, sn.ptr, sn.len);
1403 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1404 }
1405 }
1406
Christopher Faulet7e0d0d82021-06-11 13:34:42 +02001407 if (!(params->mask & FCGI_SP_SRV_SOFT)) {
1408 params->srv_soft = ist2(b_tail(params->p), 0);
1409 chunk_appendf(params->p, "HAProxy %s", haproxy_version);
1410 params->srv_soft.len = b_tail(params->p) - params->srv_soft.ptr;
1411 }
1412
Christopher Faulet99eff652019-08-11 23:11:30 +02001413 end:
1414 return 1;
1415 error:
1416 return 0;
1417}
1418
1419static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1420 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1421{
1422 struct fcgi_param p;
1423
1424 if (params->mask & flag)
1425 return 1;
1426
1427 chunk_reset(&trash);
1428
1429 switch (flag) {
1430 case FCGI_SP_CGI_GATEWAY:
1431 p.n = ist("GATEWAY_INTERFACE");
1432 p.v = ist("CGI/1.1");
1433 goto encode;
1434 case FCGI_SP_DOC_ROOT:
1435 p.n = ist("DOCUMENT_ROOT");
1436 p.v = params->docroot;
1437 goto encode;
1438 case FCGI_SP_SCRIPT_NAME:
1439 p.n = ist("SCRIPT_NAME");
1440 p.v = params->scriptname;
1441 goto encode;
1442 case FCGI_SP_PATH_INFO:
1443 p.n = ist("PATH_INFO");
1444 p.v = params->pathinfo;
1445 goto encode;
1446 case FCGI_SP_REQ_URI:
1447 p.n = ist("REQUEST_URI");
1448 p.v = params->uri;
1449 goto encode;
1450 case FCGI_SP_REQ_METH:
1451 p.n = ist("REQUEST_METHOD");
1452 p.v = params->meth;
1453 goto encode;
1454 case FCGI_SP_REQ_QS:
1455 p.n = ist("QUERY_STRING");
1456 p.v = params->qs;
1457 goto encode;
1458 case FCGI_SP_SRV_NAME:
1459 p.n = ist("SERVER_NAME");
1460 p.v = params->srv_name;
1461 goto encode;
1462 case FCGI_SP_SRV_PORT:
1463 p.n = ist("SERVER_PORT");
1464 p.v = params->srv_port;
1465 goto encode;
1466 case FCGI_SP_SRV_PROTO:
1467 p.n = ist("SERVER_PROTOCOL");
1468 p.v = params->vsn;
1469 goto encode;
1470 case FCGI_SP_REM_ADDR:
1471 p.n = ist("REMOTE_ADDR");
1472 p.v = params->rem_addr;
1473 goto encode;
1474 case FCGI_SP_REM_PORT:
1475 p.n = ist("REMOTE_PORT");
1476 p.v = params->rem_port;
1477 goto encode;
1478 case FCGI_SP_SCRIPT_FILE:
1479 p.n = ist("SCRIPT_FILENAME");
1480 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1481 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1482 p.v = ist2(b_head(&trash), b_data(&trash));
1483 goto encode;
1484 case FCGI_SP_PATH_TRANS:
1485 if (!istlen(params->pathinfo))
1486 goto skip;
1487 p.n = ist("PATH_TRANSLATED");
1488 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1489 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1490 p.v = ist2(b_head(&trash), b_data(&trash));
1491 goto encode;
1492 case FCGI_SP_CONT_LEN:
1493 p.n = ist("CONTENT_LENGTH");
1494 p.v = params->cont_len;
1495 goto encode;
1496 case FCGI_SP_HTTPS:
1497 if (!params->https)
1498 goto skip;
1499 p.n = ist("HTTPS");
1500 p.v = ist("on");
1501 goto encode;
Christopher Faulet7e0d0d82021-06-11 13:34:42 +02001502 case FCGI_SP_SRV_SOFT:
1503 p.n = ist("SERVER_SOFTWARE");
1504 p.v = params->srv_soft;
1505 goto encode;
Christopher Faulet99eff652019-08-11 23:11:30 +02001506 default:
1507 goto skip;
1508 }
1509
1510 encode:
1511 if (!istlen(p.v))
1512 goto skip;
1513 if (!fcgi_encode_param(outbuf, &p))
1514 return 0;
1515 skip:
1516 params->mask |= flag;
1517 return 1;
1518}
1519
1520/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1521 * anything. It is highly unexpected, but if the record is larger than a buffer
1522 * and cannot be encoded in one time, an error is triggered and the connection is
1523 * closed. GET_VALUES record cannot be split.
1524 */
1525static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1526{
1527 struct buffer outbuf;
1528 struct buffer *mbuf;
1529 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1530 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001531 int ret = 0;
1532
1533 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001534
1535 mbuf = br_tail(fconn->mbuf);
1536 retry:
1537 if (!fcgi_get_buf(fconn, mbuf)) {
1538 fconn->flags |= FCGI_CF_MUX_MALLOC;
1539 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001540 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1541 ret = 0;
1542 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001543 }
1544
1545 while (1) {
1546 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1547 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1548 break;
1549 realign_again:
1550 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1551 }
1552
1553 if (outbuf.size < 8)
1554 goto full;
1555
1556 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1557 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1558 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1559 outbuf.data = 8;
1560
1561 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1562 * handled by HAProxy.
1563 */
1564 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1565 goto full;
1566
1567 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001568 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 Faulet99eff652019-08-11 23:11:30 +02001569 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1570 b_add(mbuf, outbuf.data);
1571 ret = 1;
1572
1573 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001574 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001575 return ret;
1576 full:
1577 /* Too large to be encoded. For GET_VALUES records, it is an error */
1578 if (!b_data(mbuf))
1579 goto fail;
1580
1581 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1582 goto retry;
1583 fconn->flags |= FCGI_CF_MUX_MFULL;
1584 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001585 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001586 ret = 0;
1587 goto end;
1588 fail:
1589 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001590 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1591 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1592 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001593}
1594
1595/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1596 * couldn't do anything. It is highly unexpected, but if the record is larger
1597 * than a buffer and cannot be decoded in one time, an error is triggered and
1598 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1599 */
1600static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1601{
1602 struct buffer inbuf;
1603 struct buffer *dbuf;
1604 size_t offset;
1605
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001606 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1607
Christopher Faulet99eff652019-08-11 23:11:30 +02001608 dbuf = &fconn->dbuf;
1609
1610 /* Record too large to be fully decoded */
1611 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1612 goto fail;
1613
1614 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001615 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1616 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001617 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001618 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001619
1620 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1621 /* Realign the dmux buffer if the record wraps. It is unexpected
1622 * at this stage because it should be the first record received
1623 * from the FCGI application.
1624 */
1625 b_slow_realign(dbuf, trash.area, 0);
1626 }
1627
1628 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1629
1630 for (offset = 0; offset < b_data(&inbuf); ) {
1631 struct fcgi_param p;
1632 size_t ret;
1633
1634 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1635 if (!ret) {
1636 /* name or value too large to be decoded at once */
1637 goto fail;
1638 }
1639 offset += ret;
1640
1641 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001642 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001643 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 +02001644 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001645 }
1646 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001647 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 +02001648 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001649 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001650 }
1651 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1652 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001653 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 +02001654 }
1655 /*
1656 * Ignore all other params
1657 */
1658 }
1659
1660 /* Reset the number of concurrent streams supported if the FCGI
1661 * application does not support connection multiplexing
1662 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001663 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001664 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001665 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1666 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001667
1668 /* We must be sure to have read exactly the announced record length, no
1669 * more no less
1670 */
1671 if (offset != fconn->drl)
1672 goto fail;
1673
Willy Tarreau022e5e52020-09-10 09:33:15 +02001674 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 +02001675 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1676 fconn->drl = 0;
1677 fconn->drp = 0;
1678 fconn->state = FCGI_CS_RECORD_H;
1679 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001680 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1681 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001682 return 1;
1683 fail:
1684 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001685 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1686 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 +02001687 return 0;
1688}
1689
1690/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1691 * excluded, as the streams which already received the end-of-stream. It returns
1692 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1693 */
1694static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1695{
1696 struct eb32_node *node;
1697 struct fcgi_strm *fstrm;
1698
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001699 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1700
Christopher Faulet99eff652019-08-11 23:11:30 +02001701 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1702 while (node) {
1703 fstrm = container_of(node, struct fcgi_strm, by_id);
1704 node = eb32_next(node);
1705 if (fstrm->state != FCGI_SS_CLOSED &&
1706 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1707 !fcgi_strm_send_abort(fconn, fstrm))
1708 return 0;
1709 }
1710 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001711 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1712 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001713 return 1;
1714}
1715
1716/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1717 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1718 * space to proceed. It is small enough to be encoded in an empty buffer.
1719 */
1720static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1721{
1722 struct buffer outbuf;
1723 struct buffer *mbuf;
1724 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1725 int ret;
1726
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001727 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1728
Christopher Faulet99eff652019-08-11 23:11:30 +02001729 mbuf = br_tail(fconn->mbuf);
1730 retry:
1731 if (!fcgi_get_buf(fconn, mbuf)) {
1732 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001733 fstrm->flags |= FCGI_SF_BLK_MROOM;
1734 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1735 ret = 0;
1736 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001737 }
1738
1739 while (1) {
1740 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1741 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1742 break;
1743 realign_again:
1744 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1745 }
1746
1747 if (outbuf.size < 8)
1748 goto full;
1749
1750 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1751 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1752 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1753 fcgi_set_record_id(outbuf.area, fstrm->id);
1754 outbuf.data = 8;
1755
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001756 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1757 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001758 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001759 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001760 if (!fcgi_encode_begin_request(&outbuf, &rec))
1761 goto full;
1762
1763 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001764 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 +02001765 b_add(mbuf, outbuf.data);
1766 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1767 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001768 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001769 ret = 1;
1770
1771 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001772 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001773 return ret;
1774 full:
1775 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1776 goto retry;
1777 fconn->flags |= FCGI_CF_MUX_MFULL;
1778 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001779 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 +02001780 ret = 0;
1781 goto end;
1782}
1783
1784/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1785 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1786 * space to proceed. It is small enough to be encoded in an empty buffer.
1787 */
1788static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1789 enum fcgi_record_type rtype)
1790{
1791 struct buffer outbuf;
1792 struct buffer *mbuf;
1793 int ret;
1794
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001795 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001796 mbuf = br_tail(fconn->mbuf);
1797 retry:
1798 if (!fcgi_get_buf(fconn, mbuf)) {
1799 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001800 fstrm->flags |= FCGI_SF_BLK_MROOM;
1801 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1802 ret = 0;
1803 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001804 }
1805
1806 while (1) {
1807 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1808 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1809 break;
1810 realign_again:
1811 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1812 }
1813
1814 if (outbuf.size < 8)
1815 goto full;
1816
1817 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1818 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1819 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1820 outbuf.area[1] = rtype;
1821 fcgi_set_record_id(outbuf.area, fstrm->id);
1822 outbuf.data = 8;
1823
1824 /* commit the record */
1825 b_add(mbuf, outbuf.data);
1826 ret = 1;
1827
1828 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001829 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001830 return ret;
1831 full:
1832 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1833 goto retry;
1834 fconn->flags |= FCGI_CF_MUX_MFULL;
1835 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001836 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 +02001837 ret = 0;
1838 goto end;
1839}
1840
1841
1842/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1843 * marks the end of params.
1844 */
1845static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1846{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001847 int ret;
1848
1849 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1850 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1851 if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02001852 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001853 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001854}
1855
1856/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1857 * marks the end of input. On success, all the request was successfully sent.
1858 */
1859static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1860{
1861 int ret;
1862
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001863 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001864 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001865 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001866 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001867 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 +02001868 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1869 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1870 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001871 return ret;
1872}
1873
1874/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1875 * stops the request processing.
1876 */
1877static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1878{
1879 int ret;
1880
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001881 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001882 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001883 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001884 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001885 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 +02001886 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1887 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1888 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001889 return ret;
1890}
1891
1892/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1893 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1894 * several records are sent. However, a K/V param cannot be split between 2
1895 * records.
1896 */
1897static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1898 struct htx *htx)
1899{
1900 struct buffer outbuf;
1901 struct buffer *mbuf;
1902 struct htx_blk *blk;
1903 struct htx_sl *sl = NULL;
1904 struct fcgi_strm_params params;
1905 size_t total = 0;
1906
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001907 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1908
Christopher Faulet99eff652019-08-11 23:11:30 +02001909 memset(&params, 0, sizeof(params));
1910 params.p = get_trash_chunk();
1911
1912 mbuf = br_tail(fconn->mbuf);
1913 retry:
1914 if (!fcgi_get_buf(fconn, mbuf)) {
1915 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001916 fstrm->flags |= FCGI_SF_BLK_MROOM;
1917 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1918 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001919 }
1920
1921 while (1) {
1922 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1923 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1924 break;
1925 realign_again:
1926 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1927 }
1928
1929 if (outbuf.size < 8)
1930 goto full;
1931
1932 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1933 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1934 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1935 fcgi_set_record_id(outbuf.area, fstrm->id);
1936 outbuf.data = 8;
1937
1938 blk = htx_get_head_blk(htx);
1939 while (blk) {
1940 enum htx_blk_type type;
1941 uint32_t size = htx_get_blksz(blk);
1942 struct fcgi_param p;
1943
1944 type = htx_get_blk_type(blk);
1945 switch (type) {
1946 case HTX_BLK_REQ_SL:
1947 sl = htx_get_blk_ptr(htx, blk);
1948 if (sl->info.req.meth == HTTP_METH_HEAD)
1949 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1950 if (sl->flags & HTX_SL_F_VER_11)
1951 fstrm->h1m.flags |= H1_MF_VER_11;
1952 break;
1953
1954 case HTX_BLK_HDR:
1955 p.n = htx_get_blk_name(htx, blk);
1956 p.v = htx_get_blk_value(htx, blk);
1957
1958 if (istmatch(p.n, ist(":fcgi-"))) {
1959 p.n.ptr += 6;
1960 p.n.len -= 6;
1961 if (isteq(p.n, ist("gateway_interface")))
1962 params.mask |= FCGI_SP_CGI_GATEWAY;
1963 else if (isteq(p.n, ist("document_root"))) {
1964 params.mask |= FCGI_SP_DOC_ROOT;
1965 params.docroot = p.v;
1966 }
1967 else if (isteq(p.n, ist("script_name"))) {
1968 params.mask |= FCGI_SP_SCRIPT_NAME;
1969 params.scriptname = p.v;
1970 }
1971 else if (isteq(p.n, ist("path_info"))) {
1972 params.mask |= FCGI_SP_PATH_INFO;
1973 params.pathinfo = p.v;
1974 }
1975 else if (isteq(p.n, ist("request_uri"))) {
1976 params.mask |= FCGI_SP_REQ_URI;
1977 params.uri = p.v;
1978 }
1979 else if (isteq(p.n, ist("request_meth")))
1980 params.mask |= FCGI_SP_REQ_METH;
1981 else if (isteq(p.n, ist("query_string")))
1982 params.mask |= FCGI_SP_REQ_QS;
1983 else if (isteq(p.n, ist("server_name")))
1984 params.mask |= FCGI_SP_SRV_NAME;
1985 else if (isteq(p.n, ist("server_port")))
1986 params.mask |= FCGI_SP_SRV_PORT;
1987 else if (isteq(p.n, ist("server_protocol")))
1988 params.mask |= FCGI_SP_SRV_PROTO;
1989 else if (isteq(p.n, ist("remote_addr")))
1990 params.mask |= FCGI_SP_REM_ADDR;
1991 else if (isteq(p.n, ist("remote_port")))
1992 params.mask |= FCGI_SP_REM_PORT;
1993 else if (isteq(p.n, ist("script_filename")))
1994 params.mask |= FCGI_SP_SCRIPT_FILE;
1995 else if (isteq(p.n, ist("path_translated")))
1996 params.mask |= FCGI_SP_PATH_TRANS;
1997 else if (isteq(p.n, ist("https")))
1998 params.mask |= FCGI_SP_HTTPS;
Christopher Faulet7e0d0d82021-06-11 13:34:42 +02001999 else if (isteq(p.n, ist("server_software")))
2000 params.mask |= FCGI_SP_SRV_SOFT;
Christopher Faulet99eff652019-08-11 23:11:30 +02002001 }
2002 else if (isteq(p.n, ist("content-length"))) {
2003 p.n = ist("CONTENT_LENGTH");
2004 params.mask |= FCGI_SP_CONT_LEN;
2005 }
2006 else if (isteq(p.n, ist("content-type")))
2007 p.n = ist("CONTENT_TYPE");
2008 else {
2009 if (isteq(p.n, ist("host")))
2010 params.srv_name = p.v;
2011
Christopher Faulet67d58092019-10-02 10:51:38 +02002012 /* Skip header if same name is used to add the server name */
2013 if (fconn->proxy->server_id_hdr_name &&
2014 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2015 break;
2016
Christopher Faulet99eff652019-08-11 23:11:30 +02002017 memcpy(trash.area, "http_", 5);
2018 memcpy(trash.area+5, p.n.ptr, p.n.len);
2019 p.n = ist2(trash.area, p.n.len+5);
2020 }
2021
2022 if (!fcgi_encode_param(&outbuf, &p)) {
2023 if (b_space_wraps(mbuf))
2024 goto realign_again;
2025 if (outbuf.data == 8)
2026 goto full;
2027 goto done;
2028 }
2029 break;
2030
2031 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002032 if (fconn->proxy->server_id_hdr_name) {
2033 struct server *srv = objt_server(fconn->conn->target);
2034
2035 if (!srv)
2036 goto done;
2037
2038 memcpy(trash.area, "http_", 5);
2039 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2040 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2041 p.v = ist(srv->id);
2042
2043 if (!fcgi_encode_param(&outbuf, &p)) {
2044 if (b_space_wraps(mbuf))
2045 goto realign_again;
2046 if (outbuf.data == 8)
2047 goto full;
2048 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002049 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002050 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002051 goto done;
2052
2053 default:
2054 break;
2055 }
2056 total += size;
2057 blk = htx_remove_blk(htx, blk);
2058 }
2059
2060 done:
2061 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2062 goto error;
2063
2064 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2065 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2066 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2067 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2068 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2069 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2070 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2071 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2072 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2073 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2074 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2075 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2076 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2077 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2078 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
Christopher Faulet7e0d0d82021-06-11 13:34:42 +02002079 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_SOFT) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002080 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2081 goto error;
2082
2083 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002084 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002085 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2086 b_add(mbuf, outbuf.data);
2087
2088 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002089 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002090 return total;
2091 full:
2092 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2093 goto retry;
2094 fconn->flags |= FCGI_CF_MUX_MFULL;
2095 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002096 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 +02002097 if (total)
2098 goto error;
2099 goto end;
2100
2101 error:
2102 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002103 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002104 fcgi_strm_error(fstrm);
2105 goto end;
2106}
2107
2108/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2109 * anything. STDIN records contain the request body.
2110 */
2111static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2112 struct htx *htx, size_t count, struct buffer *buf)
2113{
2114 struct buffer outbuf;
2115 struct buffer *mbuf;
2116 struct htx_blk *blk;
2117 enum htx_blk_type type;
2118 uint32_t size;
2119 size_t total = 0;
2120
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002121 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002122 if (!count)
2123 goto end;
2124
2125 mbuf = br_tail(fconn->mbuf);
2126 retry:
2127 if (!fcgi_get_buf(fconn, mbuf)) {
2128 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002129 fstrm->flags |= FCGI_SF_BLK_MROOM;
2130 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2131 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002132 }
2133
2134 /* Perform some optimizations to reduce the number of buffer copies.
2135 * First, if the mux's buffer is empty and the htx area contains exactly
2136 * one data block of the same size as the requested count, and this
2137 * count fits within the record size, then it's possible to simply swap
2138 * the caller's buffer with the mux's output buffer and adjust offsets
2139 * and length to match the entire DATA HTX block in the middle. In this
2140 * case we perform a true zero-copy operation from end-to-end. This is
2141 * the situation that happens all the time with large files. Second, if
2142 * this is not possible, but the mux's output buffer is empty, we still
2143 * have an opportunity to avoid the copy to the intermediary buffer, by
2144 * making the intermediary buffer's area point to the output buffer's
2145 * area. In this case we want to skip the HTX header to make sure that
2146 * copies remain aligned and that this operation remains possible all
2147 * the time. This goes for headers, data blocks and any data extracted
2148 * from the HTX blocks.
2149 */
2150 blk = htx_get_head_blk(htx);
2151 if (!blk)
2152 goto end;
2153 type = htx_get_blk_type(blk);
2154 size = htx_get_blksz(blk);
2155 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2156 void *old_area = mbuf->area;
2157
2158 if (b_data(mbuf)) {
2159 /* Too bad there are data left there. We're willing to memcpy/memmove
2160 * up to 1/4 of the buffer, which means that it's OK to copy a large
2161 * record into a buffer containing few data if it needs to be realigned,
2162 * and that it's also OK to copy few data without realigning. Otherwise
2163 * we'll pretend the mbuf is full and wait for it to become empty.
2164 */
2165 if (size + 8 <= b_room(mbuf) &&
2166 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2167 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2168 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002169 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002170 }
2171
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002172 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 +02002173 /* map a FCGI record to the HTX block so that we can put the
2174 * record header there.
2175 */
2176 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2177 outbuf.area = b_head(mbuf);
2178
2179 /* prepend a FCGI record header just before the DATA block */
2180 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2181 fcgi_set_record_id(outbuf.area, fstrm->id);
2182 fcgi_set_record_size(outbuf.area, size);
2183
2184 /* and exchange with our old area */
2185 buf->area = old_area;
2186 buf->data = buf->head = 0;
2187 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002188
2189 htx = (struct htx *)buf->area;
2190 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002191 goto end;
2192 }
2193
2194 copy:
2195 while (1) {
2196 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2197 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2198 break;
2199 realign_again:
2200 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2201 }
2202
2203 if (outbuf.size < 8)
2204 goto full;
2205
2206 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2207 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2208 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2209 fcgi_set_record_id(outbuf.area, fstrm->id);
2210 outbuf.data = 8;
2211
2212 blk = htx_get_head_blk(htx);
2213 while (blk && count) {
2214 enum htx_blk_type type = htx_get_blk_type(blk);
2215 uint32_t size = htx_get_blksz(blk);
2216 struct ist v;
2217
2218 switch (type) {
2219 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002220 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 +02002221 v = htx_get_blk_value(htx, blk);
2222 if (v.len > count)
2223 v.len = count;
2224
2225 if (v.len > b_room(&outbuf)) {
2226 /* 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) &&
2231 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2232 b_data(mbuf) <= MAX_DATA_REALIGN)
2233 goto realign_again;
2234 v.len = b_room(&outbuf);
2235 }
2236 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2237 if (outbuf.data == 8)
2238 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
2249 case HTX_BLK_EOM:
2250 goto done;
2251
2252 default:
2253 break;
2254 }
2255 total += size;
2256 count -= size;
2257 blk = htx_remove_blk(htx, blk);
2258 }
2259
2260 done:
2261 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002262 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002263 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2264 b_add(mbuf, outbuf.data);
2265
2266 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002267 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002268 return total;
2269 full:
2270 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2271 goto retry;
2272 fconn->flags |= FCGI_CF_MUX_MFULL;
2273 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002274 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 +02002275 goto end;
2276}
2277
2278/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2279 * anything. STDOUT records contain the entire response. All the content is
2280 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2281 */
2282static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2283{
2284 struct buffer *dbuf;
2285 size_t ret;
2286 size_t max;
2287
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002288 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2289
Christopher Faulet99eff652019-08-11 23:11:30 +02002290 dbuf = &fconn->dbuf;
2291
2292 /* Only padding remains */
2293 if (fconn->state == FCGI_CS_RECORD_P)
2294 goto end_transfer;
2295
2296 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2297 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2298 buf_room_for_htx_data(dbuf))
2299 goto fail; // incomplete record
2300
2301 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2302 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002303 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2304 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002305 }
2306
2307 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2308 max = buf_room_for_htx_data(&fstrm->rxbuf);
2309 if (!b_data(&fstrm->rxbuf))
2310 fstrm->rxbuf.head = sizeof(struct htx);
2311 if (max > fconn->drl)
2312 max = fconn->drl;
2313
2314 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2315 if (!ret)
2316 goto fail;
2317 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002318 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2319 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 +02002320
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002321 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002322 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002323 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2324 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002325
2326 if (fconn->drl)
2327 goto fail;
2328
2329 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002330 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002331 fconn->drl += fconn->drp;
2332 fconn->drp = 0;
2333 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2334 b_del(&fconn->dbuf, ret);
2335 fconn->drl -= ret;
2336 if (fconn->drl)
2337 goto fail;
2338
2339 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002340 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2341 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002342 return 1;
2343 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002344 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 +02002345 return 0;
2346}
2347
2348
2349/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2350 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002351 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002352 */
2353static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2354{
2355 int ret;
2356
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002357 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2358
Christopher Faulet99eff652019-08-11 23:11:30 +02002359 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002360 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002361 fconn->drl += fconn->drp;
2362 fconn->drp = 0;
2363 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2364 b_del(&fconn->dbuf, ret);
2365 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002366 if (fconn->drl) {
2367 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 +02002368 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002369 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002370 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002371 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002372 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 +02002373 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);
2374 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002375 return 1;
2376}
2377
2378/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2379 * anything.
2380 */
2381static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2382{
2383 struct buffer *dbuf;
2384 struct buffer tag;
2385 size_t ret;
2386
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002387 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002388 dbuf = &fconn->dbuf;
2389
2390 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002391 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002392 goto end_transfer;
2393
2394 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2395 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2396 buf_room_for_htx_data(dbuf))
2397 goto fail; // incomplete record
2398
2399 chunk_reset(&trash);
2400 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2401 if (!ret)
2402 goto fail;
2403 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002404 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 +02002405
2406 trash.area[ret] = '\n';
2407 trash.area[ret+1] = '\0';
2408 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002409 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002410
2411 if (fconn->drl)
2412 goto fail;
2413
2414 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002415 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002416 fconn->drl += fconn->drp;
2417 fconn->drp = 0;
2418 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2419 b_del(&fconn->dbuf, ret);
2420 fconn->drl -= ret;
2421 if (fconn->drl)
2422 goto fail;
2423 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002424 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2425 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002426 return 1;
2427 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002428 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 +02002429 return 0;
2430}
2431
2432/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2433 * anything. If the empty STDOUT record is not already received, this one marks
2434 * the end of the response. It is highly unexpected, but if the record is larger
2435 * than a buffer and cannot be decoded in one time, an error is triggered and
2436 * the connection is closed. END_REQUEST record cannot be split.
2437 */
2438static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2439{
2440 struct buffer inbuf;
2441 struct buffer *dbuf;
2442 struct fcgi_end_request endreq;
2443
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002444 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002445 dbuf = &fconn->dbuf;
2446
2447 /* Record too large to be fully decoded */
2448 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2449 goto fail;
2450
2451 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002452 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2453 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002454 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002455 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002456
2457 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2458 /* Realign the dmux buffer if the record wraps. It is unexpected
2459 * at this stage because it should be the first record received
2460 * from the FCGI application.
2461 */
2462 b_slow_realign(dbuf, trash.area, 0);
2463 }
2464
2465 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2466
2467 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2468 goto fail;
2469
2470 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 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 +02002472 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 +02002473 fstrm->proto_status = endreq.errcode;
2474 fcgi_strm_close(fstrm);
2475
2476 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2477 fconn->drl = 0;
2478 fconn->drp = 0;
2479 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002480 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2481 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002482 return 1;
2483
2484 fail:
2485 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002486 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 +02002487 return 0;
2488}
2489
2490/* process Rx records to be demultiplexed */
2491static void fcgi_process_demux(struct fcgi_conn *fconn)
2492{
2493 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2494 struct fcgi_header hdr;
2495 int ret;
2496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002497 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2498
Christopher Faulet99eff652019-08-11 23:11:30 +02002499 if (fconn->state == FCGI_CS_CLOSED)
2500 return;
2501
2502 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002503 if (fconn->state == FCGI_CS_INIT) {
2504 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2505 return;
2506 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002507 if (fconn->state == FCGI_CS_SETTINGS) {
2508 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002509 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002510 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2511 if (!ret)
2512 goto fail;
2513 b_del(&fconn->dbuf, ret);
2514
2515 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2516 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002517 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);
2518 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 +02002519 goto fail;
2520 }
2521 goto new_record;
2522 }
2523 }
2524
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002525 /* process as many incoming records as possible below */
2526 while (1) {
2527 if (!b_data(&fconn->dbuf)) {
2528 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2529 break;
2530 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002531
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002532 if (fconn->state == FCGI_CS_CLOSED) {
2533 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002534 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002535 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002536
2537 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002538 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002539 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2540 if (!ret)
2541 break;
2542 b_del(&fconn->dbuf, ret);
2543
2544 new_record:
2545 fconn->dsi = hdr.id;
2546 fconn->drt = hdr.type;
2547 fconn->drl = hdr.len;
2548 fconn->drp = hdr.padding;
2549 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002550 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 +02002551 }
2552
2553 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2554 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2555
2556 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2557 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002558 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002559 fstrm->state == FCGI_SS_CLOSED ||
2560 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2561 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2562 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002563 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 +02002564 fstrm->cs->flags |= CS_FL_RCV_MORE;
2565 fcgi_strm_notify_recv(fstrm);
2566 }
2567 fstrm = tmp_fstrm;
2568
2569 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2570 /* ignore all record for closed streams */
2571 goto ignore_record;
2572 }
2573 if (fstrm->state == FCGI_SS_IDLE) {
2574 /* ignore all record for unknown streams */
2575 goto ignore_record;
2576 }
2577
2578 switch (fconn->drt) {
2579 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002580 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 +02002581 ret = fcgi_conn_handle_values_result(fconn);
2582 break;
2583
2584 case FCGI_STDOUT:
2585 if (fstrm->flags & FCGI_SF_ES_RCVD)
2586 goto ignore_record;
2587
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002589 if (fconn->drl)
2590 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2591 else
2592 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2593 break;
2594
2595 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002597 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2598 break;
2599
2600 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 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 +02002602 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2603 break;
2604
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002605 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002606 default:
2607 ignore_record:
2608 /* drop records that we ignore. They may be
2609 * larger than the buffer so we drain all of
2610 * their contents until we reach the end.
2611 */
2612 fconn->state = FCGI_CS_RECORD_P;
2613 fconn->drl += fconn->drp;
2614 fconn->drp = 0;
2615 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002616 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002617 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002618 b_del(&fconn->dbuf, ret);
2619 fconn->drl -= ret;
2620 ret = (fconn->drl == 0);
2621 }
2622
2623 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002624 if (ret <= 0) {
2625 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002626 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002627 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002628
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002629 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002630 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002631 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2632 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002633 }
2634
2635 fail:
2636 /* we can go here on missing data, blocked response or error */
2637 if (fstrm && fstrm->cs &&
2638 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002639 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002640 fstrm->state == FCGI_SS_CLOSED ||
2641 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2642 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2643 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002644 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 +02002645 fstrm->cs->flags |= CS_FL_RCV_MORE;
2646 fcgi_strm_notify_recv(fstrm);
2647 }
2648
2649 fcgi_conn_restart_reading(fconn, 0);
2650}
2651
2652/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2653 * the end.
2654 */
2655static int fcgi_process_mux(struct fcgi_conn *fconn)
2656{
2657 struct fcgi_strm *fstrm, *fstrm_back;
2658
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002659 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2660
Christopher Faulet99eff652019-08-11 23:11:30 +02002661 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2662 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2663 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2664 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002665 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 +02002666 fcgi_wake_unassigned_streams(fconn);
2667 goto mux;
2668 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002669 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002670 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2671 goto fail;
2672 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002673 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 +02002674 }
2675 /* need to wait for the other side */
2676 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 }
2679
2680 mux:
2681 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2682 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2683 break;
2684
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002685 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002686 continue;
2687
Willy Tarreau7aad7032020-01-16 17:20:57 +01002688 /* If the sender changed his mind and unsubscribed, let's just
2689 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002691 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2692 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002693 LIST_DEL_INIT(&fstrm->send_list);
2694 continue;
2695 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002696
2697 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002698 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2699 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002700 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002701 tasklet_wakeup(fstrm->subs->tasklet);
2702 fstrm->subs->events &= ~SUB_RETRY_SEND;
2703 if (!fstrm->subs->events)
2704 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002705 } else {
2706 /* it's the shut request that was queued */
2707 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2708 tasklet_wakeup(fstrm->shut_tl);
2709 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002710 }
2711
2712 fail:
2713 if (fconn->state == FCGI_CS_CLOSED) {
2714 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2715 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002716 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2717 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002718 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002719 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002720 }
2721 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002722
2723 done:
2724 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002725 return 1;
2726}
2727
2728
2729/* Attempt to read data, and subscribe if none available.
2730 * The function returns 1 if data has been received, otherwise zero.
2731 */
2732static int fcgi_recv(struct fcgi_conn *fconn)
2733{
2734 struct connection *conn = fconn->conn;
2735 struct buffer *buf;
2736 int max;
2737 size_t ret;
2738
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002739 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2740
2741 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2742 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002744 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002745
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002746 if (!fcgi_recv_allowed(fconn)) {
2747 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002748 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002750
2751 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2752 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002753 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002754 fconn->flags |= FCGI_CF_DEM_DALLOC;
2755 return 0;
2756 }
2757
2758 b_realign_if_empty(buf);
2759 if (!b_data(buf)) {
2760 /* try to pre-align the buffer like the
2761 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002762 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002763 * HTX block to alias it upon recv. We cannot use the
2764 * head because rcv_buf() will realign the buffer if
2765 * it's empty. Thus we cheat and pretend we already
2766 * have a few bytes there.
2767 */
2768 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2769 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2770 }
2771 else
2772 max = buf_room_for_htx_data(buf);
2773
2774 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2775
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002776 if (max && !ret && fcgi_recv_allowed(fconn)) {
2777 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002778 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002779 }
2780 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002781 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002782
2783 if (!b_data(buf)) {
2784 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002785 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002786 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2787 }
2788
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002789 if (ret == max) {
2790 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002791 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002792 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002793
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002794 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002795 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2796}
2797
2798
2799/* Try to send data if possible.
2800 * The function returns 1 if data have been sent, otherwise zero.
2801 */
2802static int fcgi_send(struct fcgi_conn *fconn)
2803{
2804 struct connection *conn = fconn->conn;
2805 int done;
2806 int sent = 0;
2807
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002808 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2809
2810 if (conn->flags & CO_FL_ERROR) {
2811 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002812 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002813 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002814
2815
Willy Tarreau911db9b2020-01-23 16:27:54 +01002816 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002817 /* a handshake was requested */
2818 goto schedule;
2819 }
2820
2821 /* This loop is quite simple : it tries to fill as much as it can from
2822 * pending streams into the existing buffer until it's reportedly full
2823 * or the end of send requests is reached. Then it tries to send this
2824 * buffer's contents out, marks it not full if at least one byte could
2825 * be sent, and tries again.
2826 *
2827 * The snd_buf() function normally takes a "flags" argument which may
2828 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2829 * data immediately comes and CO_SFL_STREAMER to indicate that the
2830 * connection is streaming lots of data (used to increase TLS record
2831 * size at the expense of latency). The former can be sent any time
2832 * there's a buffer full flag, as it indicates at least one stream
2833 * attempted to send and failed so there are pending data. An
2834 * alternative would be to set it as long as there's an active stream
2835 * but that would be problematic for ACKs until we have an absolute
2836 * guarantee that all waiters have at least one byte to send. The
2837 * latter should possibly not be set for now.
2838 */
2839
2840 done = 0;
2841 while (!done) {
2842 unsigned int flags = 0;
2843 unsigned int released = 0;
2844 struct buffer *buf;
2845
2846 /* fill as much as we can into the current buffer */
2847 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2848 done = fcgi_process_mux(fconn);
2849
2850 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2851 done = 1; // we won't go further without extra buffers
2852
2853 if (conn->flags & CO_FL_ERROR)
2854 break;
2855
2856 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2857 flags |= CO_SFL_MSG_MORE;
2858
2859 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2860 if (b_data(buf)) {
2861 int ret;
2862
2863 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2864 if (!ret) {
2865 done = 1;
2866 break;
2867 }
2868 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002869 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002870 b_del(buf, ret);
2871 if (b_data(buf)) {
2872 done = 1;
2873 break;
2874 }
2875 }
2876 b_free(buf);
2877 released++;
2878 }
2879
2880 if (released)
Willy Tarreau132b3a42021-02-20 12:02:46 +01002881 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002882
2883 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002884 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2885 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002886 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2887 }
2888
2889 if (conn->flags & CO_FL_SOCK_WR_SH) {
2890 /* output closed, nothing to send, clear the buffer to release it */
2891 b_reset(br_tail(fconn->mbuf));
2892 }
2893 /* We're not full anymore, so we can wake any task that are waiting
2894 * for us.
2895 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002896 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002897 struct fcgi_strm *fstrm;
2898
2899 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2900 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2901 break;
2902
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002903 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002904 continue;
2905
Willy Tarreau7aad7032020-01-16 17:20:57 +01002906 /* If the sender changed his mind and unsubscribed, let's just
2907 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002908 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002909 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2910 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002911 LIST_DEL_INIT(&fstrm->send_list);
2912 continue;
2913 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002914
2915 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002916 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002917 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002918 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002919 tasklet_wakeup(fstrm->subs->tasklet);
2920 fstrm->subs->events &= ~SUB_RETRY_SEND;
2921 if (!fstrm->subs->events)
2922 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002923 } else {
2924 /* it's the shut request that was queued */
2925 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2926 tasklet_wakeup(fstrm->shut_tl);
2927 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002928 }
2929 }
2930 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002931 if (!br_data(fconn->mbuf)) {
2932 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002933 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002934 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002935schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002936 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2937 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002938 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002939 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002940
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002941 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002942 return sent;
2943}
2944
2945/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreau9a5a0e02021-01-20 14:55:01 +01002946struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet99eff652019-08-11 23:11:30 +02002947{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002948 struct connection *conn;
Willy Tarreaub360bb82021-03-02 16:51:09 +01002949 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002950 struct tasklet *tl = (struct tasklet *)t;
2951 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002952 int ret = 0;
2953
Willy Tarreaub360bb82021-03-02 16:51:09 +01002954 if (status & TASK_F_USR1) {
2955 /* the tasklet was idling on an idle connection, it might have
2956 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002957 */
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002958
Willy Tarreaub360bb82021-03-02 16:51:09 +01002959 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2960 if (tl->context == NULL) {
2961 /* The connection has been taken over by another thread,
2962 * we're no longer responsible for it, so just free the
2963 * tasklet, and do nothing.
2964 */
2965 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2966 tasklet_free(tl);
2967 return NULL;
2968 }
2969 conn = fconn->conn;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002970
Willy Tarreaub360bb82021-03-02 16:51:09 +01002971 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002972
Willy Tarreaub360bb82021-03-02 16:51:09 +01002973 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2974 if (conn_in_list)
2975 MT_LIST_DEL(&conn->list);
Christopher Faulet2b7a3c42021-03-22 13:29:52 +01002976
2977 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaub360bb82021-03-02 16:51:09 +01002978 } else {
2979 /* we're certain the connection was not in an idle list */
2980 conn = fconn->conn;
2981 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
2982 conn_in_list = 0;
2983 }
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;
3111 default:
3112 return -1;
3113 }
3114}
3115
Christopher Faulet99eff652019-08-11 23:11:30 +02003116/* Connection timeout management. The principle is that if there's no receipt
3117 * nor sending for a certain amount of time, the connection is closed. If the
3118 * MUX buffer still has lying data or is not allocatable, the connection is
3119 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003120 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003121 */
3122static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3123{
3124 struct fcgi_conn *fconn = context;
3125 int expired = tick_is_expired(t->expire, now_ms);
3126
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003127 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3128
Willy Tarreau60814ff2020-06-30 11:19:23 +02003129 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003130 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3131
3132 /* Somebody already stole the connection from us, so we should not
3133 * free it, we just have to free the task.
3134 */
3135 if (!t->context) {
3136 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3137 fconn = NULL;
3138 goto do_leave;
3139 }
3140
Willy Tarreau60814ff2020-06-30 11:19:23 +02003141 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003142 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003143 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3144 return t;
3145 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003146
Willy Tarreau60814ff2020-06-30 11:19:23 +02003147 /* We're about to destroy the connection, so make sure nobody attempts
3148 * to steal it from us.
3149 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003150 if (fconn->conn->flags & CO_FL_LIST_MASK)
3151 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003152
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003153 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003154 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003155
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003156do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003157 task_destroy(t);
3158
3159 if (!fconn) {
3160 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003161 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003162 return NULL;
3163 }
3164
3165 fconn->task = NULL;
3166 fconn->state = FCGI_CS_CLOSED;
3167 fcgi_wake_some_streams(fconn, 0);
3168
3169 if (br_data(fconn->mbuf)) {
3170 /* don't even try to send aborts, the buffer is stuck */
3171 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3172 goto end;
3173 }
3174
3175 /* try to send but no need to insist */
3176 if (!fcgi_conn_send_aborts(fconn))
3177 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3178
3179 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3180 conn_xprt_ready(fconn->conn)) {
3181 unsigned int released = 0;
3182 struct buffer *buf;
3183
3184 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3185 if (b_data(buf)) {
3186 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3187 buf, b_data(buf), 0);
3188 if (!ret)
3189 break;
3190 b_del(buf, ret);
3191 if (b_data(buf))
3192 break;
3193 b_free(buf);
3194 released++;
3195 }
3196 }
3197
3198 if (released)
Willy Tarreau132b3a42021-02-20 12:02:46 +01003199 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003200 }
3201
3202 end:
3203 /* either we can release everything now or it will be done later once
3204 * the last stream closes.
3205 */
3206 if (eb_is_empty(&fconn->streams_by_id))
3207 fcgi_release(fconn);
3208
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003209 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003210 return NULL;
3211}
3212
3213
3214/*******************************************/
3215/* functions below are used by the streams */
3216/*******************************************/
3217
3218/* Append the description of what is present in error snapshot <es> into <out>.
3219 * The description must be small enough to always fit in a buffer. The output
3220 * buffer may be the trash so the trash must not be used inside this function.
3221 */
3222static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3223{
3224 chunk_appendf(out,
3225 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3226 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3227 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3228 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3229 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3230 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3231}
3232/*
3233 * Capture a bad response and archive it in the proxy's structure. By default
3234 * it tries to report the error position as h1m->err_pos. However if this one is
3235 * not set, it will then report h1m->next, which is the last known parsing
3236 * point. The function is able to deal with wrapping buffers. It always displays
3237 * buffers as a contiguous area starting at buf->p. The direction is determined
3238 * thanks to the h1m's flags.
3239 */
3240static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3241 struct h1m *h1m, struct buffer *buf)
3242{
3243 struct session *sess = fstrm->sess;
3244 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003245 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003246 union error_snapshot_ctx ctx;
3247
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003248 if (fstrm->cs && fstrm->cs->data) {
3249 if (sess == NULL)
3250 sess = si_strm(fstrm->cs->data)->sess;
3251 if (!(h1m->flags & H1_MF_RESP))
3252 other_end = si_strm(fstrm->cs->data)->be;
3253 else
3254 other_end = sess->fe;
3255 } else
3256 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003257 /* http-specific part now */
3258 ctx.h1.state = h1m->state;
3259 ctx.h1.c_flags = fconn->flags;
3260 ctx.h1.s_flags = fstrm->flags;
3261 ctx.h1.m_flags = h1m->flags;
3262 ctx.h1.m_clen = h1m->curr_len;
3263 ctx.h1.m_blen = h1m->body_len;
3264
3265 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3266 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3267 &ctx, fcgi_show_error_snapshot);
3268}
3269
3270static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3271 struct buffer *buf, size_t *ofs, size_t max)
3272{
3273 int ret;
3274
Willy Tarreau022e5e52020-09-10 09:33:15 +02003275 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 +02003276 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3277 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003278 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 +02003279 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003280 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 +02003281 fcgi_strm_error(fstrm);
3282 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3283 }
3284 goto end;
3285 }
3286
3287 *ofs += ret;
3288 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003289 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 +02003290 return ret;
3291
3292}
3293
Christopher Fauletaf542632019-10-01 21:52:49 +02003294static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003295 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3296{
3297 int ret;
3298
Willy Tarreau022e5e52020-09-10 09:33:15 +02003299 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 +02003300 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003301 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003302 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 +02003303 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003304 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 +02003305 fcgi_strm_error(fstrm);
3306 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3307 }
3308 goto end;
3309 }
3310 *ofs += ret;
3311 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003312 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 +02003313 return ret;
3314}
3315
3316static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3317 struct buffer *buf, size_t *ofs, size_t max)
3318{
3319 int ret;
3320
Willy Tarreau022e5e52020-09-10 09:33:15 +02003321 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 +02003322 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003323 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003324 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 +02003325 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003326 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 +02003327 fcgi_strm_error(fstrm);
3328 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3329 }
3330 goto end;
3331 }
3332 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003333 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003334 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 +02003335 return ret;
3336}
3337
3338static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003339 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003340{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003341 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003342
Willy Tarreau022e5e52020-09-10 09:33:15 +02003343 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003344 ret = h1_parse_msg_eom(h1m, htx, max);
3345 if (!ret) {
3346 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3347 if (htx->flags & HTX_FL_PARSING_ERROR) {
3348 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3349 fcgi_strm_error(fstrm);
3350 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3351 }
3352 goto end;
3353 }
3354 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3355 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003356 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003357 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003358}
3359
3360static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3361{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003362 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003363 struct htx *htx;
3364 struct h1m *h1m = &fstrm->h1m;
3365 size_t ret, data, total = 0;
3366
3367 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003368 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3369
Christopher Faulet99eff652019-08-11 23:11:30 +02003370 data = htx->data;
3371 if (fstrm->state == FCGI_SS_ERROR)
3372 goto end;
3373
3374 do {
3375 size_t used = htx_used_space(htx);
3376
3377 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003378 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003379 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3380 if (!ret)
3381 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003382
3383 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3384
Christopher Faulet99eff652019-08-11 23:11:30 +02003385 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3386 struct htx_blk *blk = htx_get_head_blk(htx);
3387 struct htx_sl *sl;
3388
3389 if (!blk)
3390 break;
3391 sl = htx_get_blk_ptr(htx, blk);
3392 sl->flags |= HTX_SL_F_XFER_LEN;
3393 htx->extra = 0;
3394 }
3395 }
3396 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003397 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003398 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003399 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003400 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003401
3402 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 +02003403 }
3404 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003405 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3406 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3407 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003408 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003409
Christopher Faulet76014fd2019-12-10 11:47:22 +01003410 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 +02003411 }
3412 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003413 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3414 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3415 break;
3416
3417 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3418 }
3419
Christopher Faulet99eff652019-08-11 23:11:30 +02003420 if (b_data(&fstrm->rxbuf) > total) {
3421 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003422 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003423 fcgi_strm_error(fstrm);
3424 }
3425 break;
3426 }
3427 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003428 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003429 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003430
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 if (fstrm->state != FCGI_SS_ERROR &&
3432 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003433 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003434 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3435 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3436 h1m->state = H1_MSG_DONE;
3437 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003438 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003439 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003440 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003441
3442 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003443 }
3444 else {
3445 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003446 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003447 fcgi_strm_error(fstrm);
3448 break;
3449 }
3450
3451 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003452 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003453
3454 if (fstrm->state == FCGI_SS_ERROR) {
3455 b_reset(&fstrm->rxbuf);
3456 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003457 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003458 return 0;
3459 }
3460
3461 b_del(&fstrm->rxbuf, total);
3462
3463 end:
3464 htx_to_buf(htx, buf);
3465 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003466 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003467 return ret;
3468}
3469
3470/*
3471 * Attach a new stream to a connection
3472 * (Used for outgoing connections)
3473 */
3474static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3475{
3476 struct conn_stream *cs;
3477 struct fcgi_strm *fstrm;
3478 struct fcgi_conn *fconn = conn->ctx;
3479
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003480 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003481 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003482 if (!cs) {
3483 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003484 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003485 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003486 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3487 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003488 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 cs_free(cs);
3490 return NULL;
3491 }
Willy Tarreaub360bb82021-03-02 16:51:09 +01003492
3493 /* the connection is not idle anymore, let's mark this */
3494 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01003495 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaub360bb82021-03-02 16:51:09 +01003496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003497 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003498 return cs;
3499}
3500
3501/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3502 * We have to scan because we may have some orphan streams. It might be
3503 * beneficial to scan backwards from the end to reduce the likeliness to find
3504 * orphans.
3505 */
3506static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3507{
3508 struct fcgi_conn *fconn = conn->ctx;
3509 struct fcgi_strm *fstrm;
3510 struct eb32_node *node;
3511
3512 node = eb32_first(&fconn->streams_by_id);
3513 while (node) {
3514 fstrm = container_of(node, struct fcgi_strm, by_id);
3515 if (fstrm->cs)
3516 return fstrm->cs;
3517 node = eb32_next(node);
3518 }
3519 return NULL;
3520}
3521
3522/*
3523 * Destroy the mux and the associated connection, if it is no longer used
3524 */
3525static void fcgi_destroy(void *ctx)
3526{
3527 struct fcgi_conn *fconn = ctx;
3528
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003529 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003530 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3531 fcgi_release(fconn);
3532}
3533
3534/*
3535 * Detach the stream from the connection and possibly release the connection.
3536 */
3537static void fcgi_detach(struct conn_stream *cs)
3538{
3539 struct fcgi_strm *fstrm = cs->ctx;
3540 struct fcgi_conn *fconn;
3541 struct session *sess;
3542
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003543 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3544
Christopher Faulet99eff652019-08-11 23:11:30 +02003545 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003546 if (!fstrm) {
3547 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003548 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003549 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003550
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003551 /* there's no txbuf so we're certain no to be able to send anything */
3552 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003553
3554 sess = fstrm->sess;
3555 fconn = fstrm->fconn;
3556 fstrm->cs = NULL;
3557 fconn->nb_cs--;
3558
3559 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3560 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3561 fconn->streams_limit = 1;
3562 }
3563 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3564 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3565 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3566 fconn->state = FCGI_CS_CLOSED;
3567 }
3568
3569 /* this stream may be blocked waiting for some data to leave, so orphan
3570 * it in this case.
3571 */
3572 if (!(cs->conn->flags & CO_FL_ERROR) &&
3573 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003574 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003575 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003576 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003577 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003578 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003579
3580 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3581 /* unblock the connection if it was blocked on this stream. */
3582 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3583 fcgi_conn_restart_reading(fconn, 1);
3584 }
3585
3586 fcgi_strm_destroy(fstrm);
3587
3588 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003589 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003590 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003591 /* Add the connection in the session serverlist, if not already done */
3592 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3593 fconn->conn->owner = NULL;
3594 if (eb_is_empty(&fconn->streams_by_id)) {
3595 /* let's kill the connection right away */
3596 fconn->conn->mux->destroy(fconn);
3597 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3598 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003599 }
3600 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003601 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003602 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3603 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003604 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003605 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003606 }
3607 }
3608 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003609 else {
3610 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003611 /* If the connection is owned by the session, first remove it
3612 * from its list
3613 */
3614 if (fconn->conn->owner) {
3615 session_unown_conn(fconn->conn->owner, fconn->conn);
3616 fconn->conn->owner = NULL;
3617 }
3618
Willy Tarreaub360bb82021-03-02 16:51:09 +01003619 /* mark that the tasklet may lose its context to another thread and
3620 * that the handler needs to check it under the idle conns lock.
3621 */
3622 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01003623 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3624
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003625 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003626 /* The server doesn't want it, let's kill the connection right away */
3627 fconn->conn->mux->destroy(fconn);
3628 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3629 return;
3630 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003631 /* At this point, the connection has been added to the
3632 * server idle list, so another thread may already have
3633 * hijacked it, so we can't do anything with it.
3634 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003635 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3636 return;
3637 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003638 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003639 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3640 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003641 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003642 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003643 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003644 }
3645
3646 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003647 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003648 */
3649 if (fcgi_conn_is_dead(fconn)) {
3650 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003651 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 fcgi_release(fconn);
3653 }
3654 else if (fconn->task) {
3655 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3656 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003657 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003658 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003659 else
3660 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003661}
3662
3663
3664/* Performs a synchronous or asynchronous shutr(). */
3665static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3666{
3667 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003668
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003669 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3670
Christopher Faulet99eff652019-08-11 23:11:30 +02003671 if (fstrm->state == FCGI_SS_CLOSED)
3672 goto done;
3673
3674 /* a connstream may require us to immediately kill the whole connection
3675 * for example because of a "tcp-request content reject" rule that is
3676 * normally used to limit abuse.
3677 */
3678 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003679 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3680 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003681 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003683 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003684 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003685 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3686 !fcgi_strm_send_abort(fconn, fstrm))
3687 goto add_to_list;
3688 }
3689
3690 fcgi_strm_close(fstrm);
3691
3692 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3693 tasklet_wakeup(fconn->wait_event.tasklet);
3694 done:
3695 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003696 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003697 return;
3698
3699 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003700 /* Let the handler know we want to shutr, and add ourselves to the
3701 * send list if not yet done. fcgi_deferred_shut() will be
3702 * automatically called via the shut_tl tasklet when there's room
3703 * again.
3704 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003706 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003707 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3708 }
3709 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003710 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003711 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003712 return;
3713}
3714
3715/* Performs a synchronous or asynchronous shutw(). */
3716static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3717{
3718 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003719
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003720 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3721
Christopher Faulet99eff652019-08-11 23:11:30 +02003722 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3723 goto done;
3724
3725 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3726 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3727 !fcgi_strm_send_abort(fconn, fstrm))
3728 goto add_to_list;
3729
3730 if (fstrm->state == FCGI_SS_HREM)
3731 fcgi_strm_close(fstrm);
3732 else
3733 fstrm->state = FCGI_SS_HLOC;
3734 } else {
3735 /* a connstream may require us to immediately kill the whole connection
3736 * for example because of a "tcp-request content reject" rule that is
3737 * normally used to limit abuse.
3738 */
3739 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003740 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3741 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003742 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003743 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003744
3745 fcgi_strm_close(fstrm);
3746 }
3747
3748 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3749 tasklet_wakeup(fconn->wait_event.tasklet);
3750 done:
3751 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003752 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003753 return;
3754
3755 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003756 /* Let the handler know we want to shutr, and add ourselves to the
3757 * send list if not yet done. fcgi_deferred_shut() will be
3758 * automatically called via the shut_tl tasklet when there's room
3759 * again.
3760 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003761 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003762 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003763 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3764 }
3765 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003766 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003767 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003768 return;
3769}
3770
Willy Tarreau7aad7032020-01-16 17:20:57 +01003771/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003772 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003773 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003774 */
3775static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3776{
3777 struct fcgi_strm *fstrm = ctx;
3778 struct fcgi_conn *fconn = fstrm->fconn;
3779
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003780 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3781
Willy Tarreau7aad7032020-01-16 17:20:57 +01003782 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3783 /* some data processing remains to be done first */
3784 goto end;
3785 }
3786
Christopher Faulet99eff652019-08-11 23:11:30 +02003787 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3788 fcgi_do_shutw(fstrm);
3789
3790 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3791 fcgi_do_shutr(fstrm);
3792
3793 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3794 /* We're done trying to send, remove ourself from the send_list */
3795 LIST_DEL_INIT(&fstrm->send_list);
3796
3797 if (!fstrm->cs) {
3798 fcgi_strm_destroy(fstrm);
3799 if (fcgi_conn_is_dead(fconn))
3800 fcgi_release(fconn);
3801 }
3802 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003803 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003804 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003805 return NULL;
3806}
3807
3808/* shutr() called by the conn_stream (mux_ops.shutr) */
3809static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3810{
3811 struct fcgi_strm *fstrm = cs->ctx;
3812
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003813 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003814 if (cs->flags & CS_FL_KILL_CONN)
3815 fstrm->flags |= FCGI_SF_KILL_CONN;
3816
3817 if (!mode)
3818 return;
3819
3820 fcgi_do_shutr(fstrm);
3821}
3822
3823/* shutw() called by the conn_stream (mux_ops.shutw) */
3824static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3825{
3826 struct fcgi_strm *fstrm = cs->ctx;
3827
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003828 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003829 if (cs->flags & CS_FL_KILL_CONN)
3830 fstrm->flags |= FCGI_SF_KILL_CONN;
3831
3832 fcgi_do_shutw(fstrm);
3833}
3834
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003835/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3836 * event subscriber <es> is not allowed to change from a previous call as long
3837 * as at least one event is still subscribed. The <event_type> must only be a
3838 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003839 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003840static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003841{
Christopher Faulet99eff652019-08-11 23:11:30 +02003842 struct fcgi_strm *fstrm = cs->ctx;
3843 struct fcgi_conn *fconn = fstrm->fconn;
3844
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003845 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003846 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003847
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003848 es->events |= event_type;
3849 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003850
3851 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003852 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003853
Christopher Faulet99eff652019-08-11 23:11:30 +02003854 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003855 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003856 if (!LIST_ADDED(&fstrm->send_list))
3857 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003858 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003859 return 0;
3860}
3861
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003862/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3863 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3864 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003865 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003866static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003867{
Christopher Faulet99eff652019-08-11 23:11:30 +02003868 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003869 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003870
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003871 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003872 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003873
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003874 es->events &= ~event_type;
3875 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003876 fstrm->subs = NULL;
3877
3878 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003879 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003880
Christopher Faulet99eff652019-08-11 23:11:30 +02003881 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003882 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003883 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003884 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3885 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003886 }
3887 return 0;
3888}
3889
3890/* Called from the upper layer, to receive data */
3891static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3892{
3893 struct fcgi_strm *fstrm = cs->ctx;
3894 struct fcgi_conn *fconn = fstrm->fconn;
3895 size_t ret = 0;
3896
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003897 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3898
Christopher Faulet99eff652019-08-11 23:11:30 +02003899 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3900 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003901 else
3902 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003903
Christopher Faulet76014fd2019-12-10 11:47:22 +01003904 if (b_data(&fstrm->rxbuf) || (fstrm->h1m.state == H1_MSG_DONE && !(fstrm->flags & FCGI_SF_H1_PARSING_DONE)))
Christopher Faulet99eff652019-08-11 23:11:30 +02003905 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3906 else {
3907 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003908 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003909 cs->flags |= CS_FL_EOI;
3910 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3911 cs->flags |= CS_FL_EOS;
3912 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003913 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003914 cs->flags |= CS_FL_EOS;
3915 if (cs->flags & CS_FL_ERR_PENDING)
3916 cs->flags |= CS_FL_ERROR;
3917 fcgi_release_buf(fconn, &fstrm->rxbuf);
3918 }
3919
3920 if (ret && fconn->dsi == fstrm->id) {
3921 /* demux is blocking on this stream's buffer */
3922 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3923 fcgi_conn_restart_reading(fconn, 1);
3924 }
3925
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003926 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003927 return ret;
3928}
3929
3930
Christopher Faulet99eff652019-08-11 23:11:30 +02003931/* Called from the upper layer, to send data from buffer <buf> for no more than
3932 * <count> bytes. Returns the number of bytes effectively sent. Some status
3933 * flags may be updated on the conn_stream.
3934 */
3935static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3936{
3937 struct fcgi_strm *fstrm = cs->ctx;
3938 struct fcgi_conn *fconn = fstrm->fconn;
3939 size_t total = 0;
3940 size_t ret;
3941 struct htx *htx = NULL;
3942 struct htx_sl *sl;
3943 struct htx_blk *blk;
3944 uint32_t bsize;
3945
Willy Tarreau022e5e52020-09-10 09:33:15 +02003946 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003947
Christopher Faulet99eff652019-08-11 23:11:30 +02003948 /* If we were not just woken because we wanted to send but couldn't,
3949 * and there's somebody else that is waiting to send, do nothing,
3950 * we will subscribe later and be put at the end of the list
3951 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003952 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953 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 +02003954 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003955 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003956 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003957
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003958 if (fconn->state < FCGI_CS_RECORD_H) {
3959 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003960 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003961 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003962
3963 htx = htxbuf(buf);
3964 if (fstrm->id == 0) {
3965 int32_t id = fcgi_conn_get_next_sid(fconn);
3966
3967 if (id < 0) {
3968 fcgi_strm_close(fstrm);
3969 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003970 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 +02003971 return 0;
3972 }
3973
3974 eb32_delete(&fstrm->by_id);
3975 fstrm->by_id.key = fstrm->id = id;
3976 fconn->max_id = id;
3977 fconn->nb_reserved--;
3978 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3979
3980
3981 /* Check if length of the body is known or if the message is
3982 * full. Otherwise, the request is invalid.
3983 */
3984 sl = http_get_stline(htx);
3985 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3986 htx->flags |= HTX_FL_PARSING_ERROR;
3987 fcgi_strm_error(fstrm);
3988 goto done;
3989 }
3990 }
3991
3992 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993 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 +02003994 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3995 goto done;
3996 }
3997
3998 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3999 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
4000
Christopher Fauletfe410d62020-05-19 15:13:00 +02004001 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02004002 count && !htx_is_empty(htx)) {
4003 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02004004 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02004005 bsize = htx_get_blksz(blk);
4006
4007 switch (htx_get_blk_type(blk)) {
4008 case HTX_BLK_REQ_SL:
4009 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004010 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 +02004011 ret = fcgi_strm_send_params(fconn, fstrm, htx);
4012 if (!ret) {
4013 goto done;
4014 }
4015 total += ret;
4016 count -= ret;
4017 break;
4018
4019 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004020 TRACE_PROTO("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02004021 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4022 if (!ret)
4023 goto done;
4024 goto remove_blk;
4025
4026 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004027 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 +02004028 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4029 if (ret > 0) {
4030 htx = htx_from_buf(buf);
4031 total += ret;
4032 count -= ret;
4033 if (ret < bsize)
4034 goto done;
4035 }
4036 break;
4037
4038 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004039 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 +02004040 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4041 if (!ret)
4042 goto done;
4043 goto remove_blk;
4044
4045 default:
4046 remove_blk:
4047 htx_remove_blk(htx, blk);
4048 total += bsize;
4049 count -= bsize;
4050 break;
4051 }
4052 }
4053
4054 done:
4055 if (fstrm->state >= FCGI_SS_HLOC) {
4056 /* trim any possibly pending data after we close (extra CR-LF,
4057 * unprocessed trailers, abnormal extra data, ...)
4058 */
4059 total += count;
4060 count = 0;
4061 }
4062
4063 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004064 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 +02004065 cs_set_error(cs);
4066 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4067 fcgi_strm_close(fstrm);
4068 }
4069
4070 if (htx)
4071 htx_to_buf(htx, buf);
4072
Christopher Faulet99eff652019-08-11 23:11:30 +02004073 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004074 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4075 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 +02004076 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004077 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004078
4079 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004080 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4081 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004082 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004083
4084 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004085 return total;
4086}
4087
4088/* for debugging with CLI's "show fd" command */
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004089static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004090{
4091 struct fcgi_conn *fconn = conn->ctx;
4092 struct fcgi_strm *fstrm = NULL;
4093 struct eb32_node *node;
4094 int send_cnt = 0;
4095 int tree_cnt = 0;
4096 int orph_cnt = 0;
4097 struct buffer *hmbuf, *tmbuf;
4098
4099 if (!fconn)
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004100 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004101
4102 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4103 send_cnt++;
4104
4105 fstrm = NULL;
4106 node = eb32_first(&fconn->streams_by_id);
4107 while (node) {
4108 fstrm = container_of(node, struct fcgi_strm, by_id);
4109 tree_cnt++;
4110 if (!fstrm->cs)
4111 orph_cnt++;
4112 node = eb32_next(node);
4113 }
4114
4115 hmbuf = br_head(fconn->mbuf);
4116 tmbuf = br_tail(fconn->mbuf);
4117 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4118 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4119 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4120 fconn->state, fconn->max_id, fconn->flags,
4121 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4122 fconn->wait_event.events, fconn->dsi,
4123 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4124 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4125 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4126 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4127 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4128 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4129 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4130
4131 if (fstrm) {
4132 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4133 fstrm, fstrm->id, fstrm->flags,
4134 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4135 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4136 fstrm->cs);
4137 if (fstrm->cs)
4138 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4139 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau5a090d62021-01-20 17:10:46 +01004140 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4141 if (fstrm->subs) {
Christopher Faulet70da1072021-02-25 10:06:29 +01004142 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4143 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4144 fstrm->subs->tasklet->calls,
4145 fstrm->subs->tasklet->context);
4146 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4147 chunk_appendf(&trash, ")");
Willy Tarreau5a090d62021-01-20 17:10:46 +01004148 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004149 }
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004150 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004151}
4152
4153/* Migrate the the connection to the current thread.
4154 * Return 0 if successful, non-zero otherwise.
4155 * Expected to be called with the old thread lock held.
4156 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004157static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004158{
4159 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004160 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004161
4162 if (fd_takeover(conn->handle.fd, conn) != 0)
4163 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004164
4165 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4166 /* We failed to takeover the xprt, even if the connection may
4167 * still be valid, flag it as error'd, as we have already
4168 * taken over the fd, and wake the tasklet, so that it will
4169 * destroy it.
4170 */
4171 conn->flags |= CO_FL_ERROR;
4172 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4173 return -1;
4174 }
4175
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004176 if (fcgi->wait_event.events)
4177 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4178 fcgi->wait_event.events, &fcgi->wait_event);
4179 /* To let the tasklet know it should free itself, and do nothing else,
4180 * set its context to NULL;
4181 */
4182 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004183 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004184
4185 task = fcgi->task;
4186 if (task) {
4187 task->context = NULL;
4188 fcgi->task = NULL;
4189 __ha_barrier_store();
4190 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004191
4192 fcgi->task = task_new(tid_bit);
4193 if (!fcgi->task) {
4194 fcgi_release(fcgi);
4195 return -1;
4196 }
4197 fcgi->task->process = fcgi_timeout_task;
4198 fcgi->task->context = fcgi;
4199 }
4200 fcgi->wait_event.tasklet = tasklet_new();
4201 if (!fcgi->wait_event.tasklet) {
4202 fcgi_release(fcgi);
4203 return -1;
4204 }
4205 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4206 fcgi->wait_event.tasklet->context = fcgi;
4207 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4208 SUB_RETRY_RECV, &fcgi->wait_event);
4209
4210 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004211}
4212
4213/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004214/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004215/****************************************/
4216
4217/* The mux operations */
4218static const struct mux_ops mux_fcgi_ops = {
4219 .init = fcgi_init,
4220 .wake = fcgi_wake,
4221 .attach = fcgi_attach,
4222 .get_first_cs = fcgi_get_first_cs,
4223 .detach = fcgi_detach,
4224 .destroy = fcgi_destroy,
4225 .avail_streams = fcgi_avail_streams,
4226 .used_streams = fcgi_used_streams,
4227 .rcv_buf = fcgi_rcv_buf,
4228 .snd_buf = fcgi_snd_buf,
4229 .subscribe = fcgi_subscribe,
4230 .unsubscribe = fcgi_unsubscribe,
4231 .shutr = fcgi_shutr,
4232 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004233 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004234 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004235 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004236 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004237 .name = "FCGI",
4238};
4239
4240
4241/* this mux registers FCGI proto */
4242static struct mux_proto_list mux_proto_fcgi =
4243{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4244
4245INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4246
4247/*
4248 * Local variables:
4249 * c-indent-level: 8
4250 * c-basic-offset: 8
4251 * End:
4252 */