blob: 08dde23517f90efb7bad5d87e67ffd7aa26df7bd [file] [log] [blame]
Christopher Faulet99eff652019-08-11 23:11:30 +02001/*
2 * FastCGI mux-demux for connections
3 *
4 * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/ist.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020018#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020019#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020022#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020023#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020024#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020025#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020026#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020027#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020028#include <haproxy/proxy-t.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020029#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session-t.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020031#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020034#include <haproxy/trace.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020035
Willy Tarreaub2551052020-06-09 09:07:15 +020036
Christopher Faulet99eff652019-08-11 23:11:30 +020037/* FCGI Connection flags (32 bits) */
38#define FCGI_CF_NONE 0x00000000
39
40/* Flags indicating why writing to the mux is blockes */
41#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
42#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
43#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
44
45/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
50#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
51#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
52#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
53#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
54#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
55#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
56#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
57
58/* Other flags */
59#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
60#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
61#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
62#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
63#define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */
64#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
65
66/* FCGI connection state (fcgi_conn->state) */
67enum fcgi_conn_st {
68 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
69 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
70 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
71 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
72 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
73 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
74 FCGI_CS_ENTRIES
75} __attribute__((packed));
76
77/* 32 buffers: one for the ring's root, rest for the mbuf itself */
78#define FCGI_C_MBUF_CNT 32
79
80/* FCGI connection descriptor */
81struct fcgi_conn {
82 struct connection *conn;
83
84 enum fcgi_conn_st state; /* FCGI connection state */
85 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
86 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
87 uint32_t flags; /* Connection flags: FCGI_CF_* */
88
89 int16_t dsi; /* dmux stream ID (<0 = idle ) */
90 uint16_t drl; /* demux record length (if dsi >= 0) */
91 uint8_t drt; /* demux record type (if dsi >= 0) */
92 uint8_t drp; /* demux record padding (if dsi >= 0) */
93
94 struct buffer dbuf; /* demux buffer */
95 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
96
97 int timeout; /* idle timeout duration in ticks */
98 int shut_timeout; /* idle timeout duration in ticks after shutdown */
99 unsigned int nb_streams; /* number of streams in the tree */
100 unsigned int nb_cs; /* number of attached conn_streams */
101 unsigned int nb_reserved; /* number of reserved streams */
102 unsigned int stream_cnt; /* total number of streams seen */
103
104 struct proxy *proxy; /* the proxy this connection was created for */
105 struct fcgi_app *app; /* FCGI application used by this mux */
106 struct task *task; /* timeout management task */
107 struct eb_root streams_by_id; /* all active streams by their ID */
108
109 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200110
111 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
112 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
113};
114
115
116/* FCGI stream state, in fcgi_strm->state */
117enum fcgi_strm_st {
118 FCGI_SS_IDLE = 0,
119 FCGI_SS_OPEN,
120 FCGI_SS_HREM, // half-closed(remote)
121 FCGI_SS_HLOC, // half-closed(local)
122 FCGI_SS_ERROR,
123 FCGI_SS_CLOSED,
124 FCGI_SS_ENTRIES
125} __attribute__((packed));
126
127
128/* FCGI stream flags (32 bits) */
129#define FCGI_SF_NONE 0x00000000
130#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
131#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
132#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
133
134/* Stream flags indicating the reason the stream is blocked */
135#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
136#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
137#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
138
139#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
140#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100141#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200142
143#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
144#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
145#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
146
147/* Other flags */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100148#define FCGI_SF_H1_PARSING_DONE 0x00010000
Christopher Faulet99eff652019-08-11 23:11:30 +0200149
150/* FCGI stream descriptor */
151struct fcgi_strm {
152 struct conn_stream *cs;
153 struct session *sess;
154 struct fcgi_conn *fconn;
155
156 int32_t id; /* stream ID */
157
158 uint32_t flags; /* Connection flags: FCGI_SF_* */
159 enum fcgi_strm_st state; /* FCGI stream state */
160 int proto_status; /* FCGI_PS_* */
161
162 struct h1m h1m; /* response parser state for H1 */
163
164 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
165
166 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100167 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200168 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100169 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 +0200170};
171
172/* Flags representing all default FCGI parameters */
173#define FCGI_SP_CGI_GATEWAY 0x00000001
174#define FCGI_SP_DOC_ROOT 0x00000002
175#define FCGI_SP_SCRIPT_NAME 0x00000004
176#define FCGI_SP_PATH_INFO 0x00000008
177#define FCGI_SP_REQ_URI 0x00000010
178#define FCGI_SP_REQ_METH 0x00000020
179#define FCGI_SP_REQ_QS 0x00000040
180#define FCGI_SP_SRV_PORT 0x00000080
181#define FCGI_SP_SRV_PROTO 0x00000100
182#define FCGI_SP_SRV_NAME 0x00000200
183#define FCGI_SP_REM_ADDR 0x00000400
184#define FCGI_SP_REM_PORT 0x00000800
185#define FCGI_SP_SCRIPT_FILE 0x00001000
186#define FCGI_SP_PATH_TRANS 0x00002000
187#define FCGI_SP_CONT_LEN 0x00004000
188#define FCGI_SP_HTTPS 0x00008000
189#define FCGI_SP_MASK 0x0000FFFF
190#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
191
192/* FCGI parameters used when PARAMS record is sent */
193struct fcgi_strm_params {
194 uint32_t mask;
195 struct ist docroot;
196 struct ist scriptname;
197 struct ist pathinfo;
198 struct ist meth;
199 struct ist uri;
200 struct ist vsn;
201 struct ist qs;
202 struct ist srv_name;
203 struct ist srv_port;
204 struct ist rem_addr;
205 struct ist rem_port;
206 struct ist cont_len;
207 int https;
208 struct buffer *p;
209};
210
211/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
212#define MAX_DATA_REALIGN 1024
213
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200214/* trace source and events */
215static void fcgi_trace(enum trace_level level, uint64_t mask,
216 const struct trace_source *src,
217 const struct ist where, const struct ist func,
218 const void *a1, const void *a2, const void *a3, const void *a4);
219
220/* The event representation is split like this :
221 * fconn - internal FCGI connection
222 * fstrm - internal FCGI stream
223 * strm - application layer
224 * rx - data receipt
225 * tx - data transmission
226 * rsp - response parsing
227 */
228static const struct trace_event fcgi_trace_events[] = {
229#define FCGI_EV_FCONN_NEW (1ULL << 0)
230 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
231#define FCGI_EV_FCONN_RECV (1ULL << 1)
232 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
233#define FCGI_EV_FCONN_SEND (1ULL << 2)
234 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
235#define FCGI_EV_FCONN_BLK (1ULL << 3)
236 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
237#define FCGI_EV_FCONN_WAKE (1ULL << 4)
238 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
239#define FCGI_EV_FCONN_END (1ULL << 5)
240 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
241#define FCGI_EV_FCONN_ERR (1ULL << 6)
242 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
243
244#define FCGI_EV_RX_FHDR (1ULL << 7)
245 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
246#define FCGI_EV_RX_RECORD (1ULL << 8)
247 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
248#define FCGI_EV_RX_EOI (1ULL << 9)
249 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
250#define FCGI_EV_RX_GETVAL (1ULL << 10)
251 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
252#define FCGI_EV_RX_STDOUT (1ULL << 11)
253 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
254#define FCGI_EV_RX_STDERR (1ULL << 12)
255 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
256#define FCGI_EV_RX_ENDREQ (1ULL << 13)
257 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
258
259#define FCGI_EV_TX_RECORD (1ULL << 14)
260 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
261#define FCGI_EV_TX_EOI (1ULL << 15)
262 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
263#define FCGI_EV_TX_BEGREQ (1ULL << 16)
264 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
265#define FCGI_EV_TX_GETVAL (1ULL << 17)
266 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
267#define FCGI_EV_TX_PARAMS (1ULL << 18)
268 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
269#define FCGI_EV_TX_STDIN (1ULL << 19)
270 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
271#define FCGI_EV_TX_ABORT (1ULL << 20)
272 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
273
274#define FCGI_EV_RSP_DATA (1ULL << 21)
275 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
276#define FCGI_EV_RSP_EOM (1ULL << 22)
277 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
278#define FCGI_EV_RSP_HDRS (1ULL << 23)
279 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
280#define FCGI_EV_RSP_BODY (1ULL << 24)
281 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
282#define FCGI_EV_RSP_TLRS (1ULL << 25)
283 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
284
285#define FCGI_EV_FSTRM_NEW (1ULL << 26)
286 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
287#define FCGI_EV_FSTRM_BLK (1ULL << 27)
288 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
289#define FCGI_EV_FSTRM_END (1ULL << 28)
290 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
291#define FCGI_EV_FSTRM_ERR (1ULL << 29)
292 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
293
294#define FCGI_EV_STRM_NEW (1ULL << 30)
295 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
296#define FCGI_EV_STRM_RECV (1ULL << 31)
297 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
298#define FCGI_EV_STRM_SEND (1ULL << 32)
299 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
300#define FCGI_EV_STRM_FULL (1ULL << 33)
301 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
302#define FCGI_EV_STRM_WAKE (1ULL << 34)
303 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
304#define FCGI_EV_STRM_SHUT (1ULL << 35)
305 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
306#define FCGI_EV_STRM_END (1ULL << 36)
307 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
308#define FCGI_EV_STRM_ERR (1ULL << 37)
309 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
310
311 { }
312};
313
314static const struct name_desc fcgi_trace_lockon_args[4] = {
315 /* arg1 */ { /* already used by the connection */ },
316 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
317 /* arg3 */ { },
318 /* arg4 */ { }
319};
320
321
322static const struct name_desc fcgi_trace_decoding[] = {
323#define FCGI_VERB_CLEAN 1
324 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
325#define FCGI_VERB_MINIMAL 2
326 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
327#define FCGI_VERB_SIMPLE 3
328 { .name="simple", .desc="add request/response status line or htx info when available" },
329#define FCGI_VERB_ADVANCED 4
330 { .name="advanced", .desc="add header fields or record decoding when available" },
331#define FCGI_VERB_COMPLETE 5
332 { .name="complete", .desc="add full data dump when available" },
333 { /* end */ }
334};
335
336static struct trace_source trace_fcgi = {
337 .name = IST("fcgi"),
338 .desc = "FastCGI multiplexer",
339 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
340 .default_cb = fcgi_trace,
341 .known_events = fcgi_trace_events,
342 .lockon_args = fcgi_trace_lockon_args,
343 .decoding = fcgi_trace_decoding,
344 .report_events = ~0, // report everything by default
345};
346
347#define TRACE_SOURCE &trace_fcgi
348INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
349
Christopher Faulet99eff652019-08-11 23:11:30 +0200350/* FCGI connection and stream pools */
351DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
352DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
353
354static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
355static int fcgi_process(struct fcgi_conn *fconn);
Willy Tarreau9a5a0e02021-01-20 14:55:01 +0100356/* fcgi_io_cb is exported to see it resolved in "show fd" */
357struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet99eff652019-08-11 23:11:30 +0200358static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
359static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
360static 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 +0200361static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
362static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200363static void fcgi_strm_alert(struct fcgi_strm *fstrm);
364static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
365
366/* a dmumy management stream */
367static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
368 .cs = NULL,
369 .fconn = NULL,
370 .state = FCGI_SS_CLOSED,
371 .flags = FCGI_SF_NONE,
372 .id = 0,
373};
374
375/* and a dummy idle stream for use with any unknown stream */
376static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
377 .cs = NULL,
378 .fconn = NULL,
379 .state = FCGI_SS_IDLE,
380 .flags = FCGI_SF_NONE,
381 .id = 0,
382};
383
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200384/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
385static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
386{
387 switch (st) {
388 case FCGI_CS_INIT : return "INI";
389 case FCGI_CS_SETTINGS : return "STG";
390 case FCGI_CS_RECORD_H : return "RDH";
391 case FCGI_CS_RECORD_D : return "RDD";
392 case FCGI_CS_RECORD_P : return "RDP";
393 case FCGI_CS_CLOSED : return "CLO";
394 default : return "???";
395 }
396}
397
398/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
399static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
400{
401 switch (st) {
402 case FCGI_SS_IDLE : return "IDL";
403 case FCGI_SS_OPEN : return "OPN";
404 case FCGI_SS_HREM : return "RCL";
405 case FCGI_SS_HLOC : return "HCL";
406 case FCGI_SS_ERROR : return "ERR";
407 case FCGI_SS_CLOSED : return "CLO";
408 default : return "???";
409 }
410}
411
412
413/* the FCGI traces always expect that arg1, if non-null, is of type connection
414 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
415 * and that arg3, if non-null, is a htx for rx/tx headers.
416 */
417static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
418 const struct ist where, const struct ist func,
419 const void *a1, const void *a2, const void *a3, const void *a4)
420{
421 const struct connection *conn = a1;
422 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
423 const struct fcgi_strm *fstrm = a2;
424 const struct htx *htx = a3;
425 const size_t *val = a4;
426
427 if (!fconn)
428 fconn = (fstrm ? fstrm->fconn : NULL);
429
430 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
431 return;
432
433 /* Display the response state if fstrm is defined */
434 if (fstrm)
435 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
436
437 if (src->verbosity == FCGI_VERB_CLEAN)
438 return;
439
440 /* Display the value to the 4th argument (level > STATE) */
441 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100442 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200443
444 /* Display status-line if possible (verbosity > MINIMAL) */
445 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
446 const struct htx_blk *blk = htx_get_head_blk(htx);
447 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
448 enum htx_blk_type type = htx_get_blk_type(blk);
449
450 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
451 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
452 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
453 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
454 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
455 }
456
457 /* Display fconn info and, if defined, fstrm info */
458 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
459 if (fstrm)
460 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
461
462 if (!fstrm || fstrm->id <= 0)
463 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
464 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
465 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
466
467 if (src->verbosity == FCGI_VERB_MINIMAL)
468 return;
469
470 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
471 if (src->level > TRACE_LEVEL_USER) {
472 if (src->verbosity == FCGI_VERB_COMPLETE ||
473 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
474 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
475 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
476 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
477 if (src->verbosity == FCGI_VERB_COMPLETE ||
478 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
479 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
480 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
481
482 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
483 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
484 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
485 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
486 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
487 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
488 }
489
490 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
491 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
492 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
493 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
494 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
495 }
496
497 /* Display htx info if defined (level > USER) */
498 if (src->level > TRACE_LEVEL_USER && htx) {
499 int full = 0;
500
501 /* Full htx info (level > STATE && verbosity > SIMPLE) */
502 if (src->level > TRACE_LEVEL_STATE) {
503 if (src->verbosity == FCGI_VERB_COMPLETE)
504 full = 1;
505 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
506 full = 1;
507 }
508
509 chunk_memcat(&trace_buf, "\n\t", 2);
510 htx_dump(&trace_buf, htx, full);
511 }
512}
Christopher Faulet99eff652019-08-11 23:11:30 +0200513
514/*****************************************************/
515/* functions below are for dynamic buffer management */
516/*****************************************************/
517
518/* Indicates whether or not the we may call the fcgi_recv() function to attempt
519 * to receive data into the buffer and/or demux pending data. The condition is
520 * a bit complex due to some API limits for now. The rules are the following :
521 * - if an error or a shutdown was detected on the connection and the buffer
522 * is empty, we must not attempt to receive
523 * - if the demux buf failed to be allocated, we must not try to receive and
524 * we know there is nothing pending
525 * - if no flag indicates a blocking condition, we may attempt to receive,
526 * regardless of whether the demux buffer is full or not, so that only
527 * de demux part decides whether or not to block. This is needed because
528 * the connection API indeed prevents us from re-enabling receipt that is
529 * already enabled in a polled state, so we must always immediately stop
530 * as soon as the demux can't proceed so as never to hit an end of read
531 * with data pending in the buffers.
532 * - otherwise must may not attempt
533 */
534static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
535{
536 if (b_data(&fconn->dbuf) == 0 &&
537 (fconn->state == FCGI_CS_CLOSED ||
538 fconn->conn->flags & CO_FL_ERROR ||
539 conn_xprt_read0_pending(fconn->conn)))
540 return 0;
541
542 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
543 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
544 return 1;
545
546 return 0;
547}
548
549/* Restarts reading on the connection if it was not enabled */
550static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
551{
552 if (!fcgi_recv_allowed(fconn))
553 return;
554 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
555 (fconn->wait_event.events & SUB_RETRY_RECV))
556 return;
557 tasklet_wakeup(fconn->wait_event.tasklet);
558}
559
560
561/* Tries to grab a buffer and to re-enable processing on mux <target>. The
562 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
563 * the allocation succeeds, in which case the connection is woken up, or 0 if
564 * it's impossible to wake up and we prefer to be woken up later.
565 */
566static int fcgi_buf_available(void *target)
567{
568 struct fcgi_conn *fconn = target;
569 struct fcgi_strm *fstrm;
570
571 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200572 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 +0200573 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
574 fcgi_conn_restart_reading(fconn, 1);
575 return 1;
576 }
577
578 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200579 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 +0200580 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200581 if (fconn->flags & FCGI_CF_DEM_MROOM) {
582 fconn->flags &= ~FCGI_CF_DEM_MROOM;
583 fcgi_conn_restart_reading(fconn, 1);
584 }
585 return 1;
586 }
587
588 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
589 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
590 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200591 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 +0200592 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
593 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200594 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200595 return 1;
596 }
597
598 return 0;
599}
600
601static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
602{
603 struct buffer *buf = NULL;
604
Willy Tarreau954827a2021-02-20 11:49:49 +0100605 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200606 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
607 fconn->buf_wait.target = fconn;
608 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau954827a2021-02-20 11:49:49 +0100609 LIST_ADDQ(&ti->buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200610 }
611 return buf;
612}
613
614static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
615{
616 if (bptr->size) {
617 b_free(bptr);
Willy Tarreau132b3a42021-02-20 12:02:46 +0100618 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +0200619 }
620}
621
622static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
623{
624 struct buffer *buf;
625 unsigned int count = 0;
626
627 while (b_size(buf = br_head_pick(fconn->mbuf))) {
628 b_free(buf);
629 count++;
630 }
631 if (count)
Willy Tarreau132b3a42021-02-20 12:02:46 +0100632 offer_buffers(NULL, count);
Christopher Faulet99eff652019-08-11 23:11:30 +0200633}
634
635/* Returns the number of allocatable outgoing streams for the connection taking
636 * the number reserved streams into account.
637 */
638static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
639{
640 int ret;
641
642 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
643 if (ret < 0)
644 ret = 0;
645 return ret;
646}
647
648/* Returns the number of streams in use on a connection to figure if it's
649 * idle or not. We check nb_cs and not nb_streams as the caller will want
650 * to know if it was the last one after a detach().
651 */
652static int fcgi_used_streams(struct connection *conn)
653{
654 struct fcgi_conn *fconn = conn->ctx;
655
656 return fconn->nb_cs;
657}
658
659/* Returns the number of concurrent streams available on the connection */
660static int fcgi_avail_streams(struct connection *conn)
661{
662 struct server *srv = objt_server(conn->target);
663 struct fcgi_conn *fconn = conn->ctx;
664 int ret1, ret2;
665
666 /* Don't open new stream if the connection is closed */
667 if (fconn->state == FCGI_CS_CLOSED)
668 return 0;
669
670 /* May be negative if this setting has changed */
671 ret1 = (fconn->streams_limit - fconn->nb_streams);
672
673 /* we must also consider the limit imposed by stream IDs */
674 ret2 = fcgi_streams_left(fconn);
675 ret1 = MIN(ret1, ret2);
676 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
677 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
678 ret1 = MIN(ret1, ret2);
679 }
680 return ret1;
681}
682
683/*****************************************************************/
684/* functions below are dedicated to the mux setup and management */
685/*****************************************************************/
686
687/* Initializes the mux once it's attached. Only outgoing connections are
688 * supported. So the context is already initialized before installing the
689 * mux. <input> is always used as Input buffer and may contain data. It is the
690 * caller responsibility to not reuse it anymore. Returns < 0 on error.
691 */
692static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
693 struct buffer *input)
694{
695 struct fcgi_conn *fconn;
696 struct fcgi_strm *fstrm;
697 struct fcgi_app *app = get_px_fcgi_app(px);
698 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200699 void *conn_ctx = conn->ctx;
700
701 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200702
703 if (!app)
704 goto fail_conn;
705
706 fconn = pool_alloc(pool_head_fcgi_conn);
707 if (!fconn)
708 goto fail_conn;
709
710 fconn->shut_timeout = fconn->timeout = px->timeout.server;
711 if (tick_isset(px->timeout.serverfin))
712 fconn->shut_timeout = px->timeout.serverfin;
713
714 fconn->flags = FCGI_CF_NONE;
715
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500716 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200717 if (app->flags & FCGI_APP_FL_KEEP_CONN)
718 fconn->flags |= FCGI_CF_KEEP_CONN;
719 if (app->flags & FCGI_APP_FL_GET_VALUES)
720 fconn->flags |= FCGI_CF_GET_VALUES;
721 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
722 fconn->flags |= FCGI_CF_MPXS_CONNS;
723
724 fconn->proxy = px;
725 fconn->app = app;
726 fconn->task = NULL;
727 if (tick_isset(fconn->timeout)) {
728 t = task_new(tid_bit);
729 if (!t)
730 goto fail;
731
732 fconn->task = t;
733 t->process = fcgi_timeout_task;
734 t->context = fconn;
735 t->expire = tick_add(now_ms, fconn->timeout);
736 }
737
738 fconn->wait_event.tasklet = tasklet_new();
739 if (!fconn->wait_event.tasklet)
740 goto fail;
741 fconn->wait_event.tasklet->process = fcgi_io_cb;
742 fconn->wait_event.tasklet->context = fconn;
743 fconn->wait_event.events = 0;
744
745 /* Initialise the context. */
746 fconn->state = FCGI_CS_INIT;
747 fconn->conn = conn;
748 fconn->streams_limit = app->maxreqs;
749 fconn->max_id = -1;
750 fconn->nb_streams = 0;
751 fconn->nb_cs = 0;
752 fconn->nb_reserved = 0;
753 fconn->stream_cnt = 0;
754
755 fconn->dbuf = *input;
756 fconn->dsi = -1;
757
758 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
759 fconn->streams_by_id = EB_ROOT;
760 LIST_INIT(&fconn->send_list);
Willy Tarreau954827a2021-02-20 11:49:49 +0100761 LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200762
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200763 conn->ctx = fconn;
764
Christopher Faulet99eff652019-08-11 23:11:30 +0200765 if (t)
766 task_queue(t);
767
768 /* FIXME: this is temporary, for outgoing connections we need to
769 * immediately allocate a stream until the code is modified so that the
770 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200771 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200772 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200773 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200774 if (!fstrm)
775 goto fail;
776
Christopher Faulet99eff652019-08-11 23:11:30 +0200777
778 /* Repare to read something */
779 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200780 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200781 return 0;
782
783 fail:
784 task_destroy(t);
785 if (fconn->wait_event.tasklet)
786 tasklet_free(fconn->wait_event.tasklet);
787 pool_free(pool_head_fcgi_conn, fconn);
788 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200789 conn->ctx = conn_ctx; // restore saved ctx
790 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200791 return -1;
792}
793
794/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
795 * -1 if no more is allocatable.
796 */
797static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
798{
799 int32_t id = (fconn->max_id + 1) | 1;
800
801 if ((id & 0x80000000U))
802 id = -1;
803 return id;
804}
805
806/* Returns the stream associated with id <id> or NULL if not found */
807static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
808{
809 struct eb32_node *node;
810
811 if (id == 0)
812 return (struct fcgi_strm *)fcgi_mgmt_stream;
813
814 if (id > fconn->max_id)
815 return (struct fcgi_strm *)fcgi_unknown_stream;
816
817 node = eb32_lookup(&fconn->streams_by_id, id);
818 if (!node)
819 return (struct fcgi_strm *)fcgi_unknown_stream;
820 return container_of(node, struct fcgi_strm, by_id);
821}
822
823
824/* Release function. This one should be called to free all resources allocated
825 * to the mux.
826 */
827static void fcgi_release(struct fcgi_conn *fconn)
828{
William Dauchy477757c2020-08-07 22:19:23 +0200829 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200830
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200831 TRACE_POINT(FCGI_EV_FCONN_END);
832
Christopher Faulet99eff652019-08-11 23:11:30 +0200833 if (fconn) {
834 /* The connection must be attached to this mux to be released */
835 if (fconn->conn && fconn->conn->ctx == fconn)
836 conn = fconn->conn;
837
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200838 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
839
Willy Tarreau954827a2021-02-20 11:49:49 +0100840 if (LIST_ADDED(&fconn->buf_wait.list))
841 LIST_DEL_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200842
843 fcgi_release_buf(fconn, &fconn->dbuf);
844 fcgi_release_mbuf(fconn);
845
846 if (fconn->task) {
847 fconn->task->context = NULL;
848 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
849 fconn->task = NULL;
850 }
851 if (fconn->wait_event.tasklet)
852 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200853 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200854 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
855 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200856
857 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200858 }
859
860 if (conn) {
861 conn->mux = NULL;
862 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200863 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200864
865 conn_stop_tracking(conn);
866 conn_full_close(conn);
867 if (conn->destroy_cb)
868 conn->destroy_cb(conn);
869 conn_free(conn);
870 }
871}
872
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200873/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
874 * pending on the connection AND if there is no more data in the demux
875 * buffer. The function returns 1 to report a read0 or 0 otherwise.
876 */
877static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
878{
879 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
880 return 1;
881 return 0;
882}
883
Christopher Faulet99eff652019-08-11 23:11:30 +0200884
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500885/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200886static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
887{
888 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
889 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
890 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
891 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
892 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
893 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
894 conn_xprt_read0_pending(fconn->conn))))
895 return 1;
896 return 0;
897}
898
899
900/********************************************************/
901/* functions below are for the FCGI protocol processing */
902/********************************************************/
903
Christopher Faulet99eff652019-08-11 23:11:30 +0200904/* Marks an error on the stream. */
905static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
906{
907 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200908 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
909 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200910 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200911 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
912 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200913 if (fstrm->cs)
914 cs_set_error(fstrm->cs);
915 }
916}
917
918/* Attempts to notify the data layer of recv availability */
919static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
920{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100921 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200922 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100923 tasklet_wakeup(fstrm->subs->tasklet);
924 fstrm->subs->events &= ~SUB_RETRY_RECV;
925 if (!fstrm->subs->events)
926 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200927 }
928}
929
930/* Attempts to notify the data layer of send availability */
931static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
932{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100933 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200934 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100935 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100936 tasklet_wakeup(fstrm->subs->tasklet);
937 fstrm->subs->events &= ~SUB_RETRY_SEND;
938 if (!fstrm->subs->events)
939 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200940 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100941 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
942 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
943 tasklet_wakeup(fstrm->shut_tl);
944 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200945}
946
947/* Alerts the data layer, trying to wake it up by all means, following
948 * this sequence :
949 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
950 * for recv
951 * - if its subscribed to send, then it's woken up for send
952 * - if it was subscribed to neither, its ->wake() callback is called
953 * It is safe to call this function with a closed stream which doesn't have a
954 * conn_stream anymore.
955 */
956static void fcgi_strm_alert(struct fcgi_strm *fstrm)
957{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200958 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100959 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100960 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200961 fcgi_strm_notify_recv(fstrm);
962 fcgi_strm_notify_send(fstrm);
963 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200964 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
965 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200966 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200967 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200968}
969
970/* Writes the 16-bit record size <len> at address <record> */
971static inline void fcgi_set_record_size(void *record, uint16_t len)
972{
973 uint8_t *out = (record + 4);
974
975 *out = (len >> 8);
976 *(out + 1) = (len & 0xff);
977}
978
979/* Writes the 16-bit stream id <id> at address <record> */
980static inline void fcgi_set_record_id(void *record, uint16_t id)
981{
982 uint8_t *out = (record + 2);
983
984 *out = (id >> 8);
985 *(out + 1) = (id & 0xff);
986}
987
988/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
989 * its connection if the stream was not yet closed. Please use this exclusively
990 * before closing a stream to ensure stream count is well maintained.
991 */
992static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
993{
994 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200995 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200996 fstrm->fconn->nb_streams--;
997 if (!fstrm->id)
998 fstrm->fconn->nb_reserved--;
999 if (fstrm->cs) {
1000 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1001 fcgi_strm_notify_recv(fstrm);
1002 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001003 fstrm->state = FCGI_SS_CLOSED;
1004 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1005 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001006 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001007}
1008
1009/* Detaches a FCGI stream from its FCGI connection and releases it to the
1010 * fcgi_strm pool.
1011 */
1012static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1013{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001014 struct connection *conn = fstrm->fconn->conn;
1015
1016 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1017
Christopher Faulet99eff652019-08-11 23:11:30 +02001018 fcgi_strm_close(fstrm);
1019 eb32_delete(&fstrm->by_id);
1020 if (b_size(&fstrm->rxbuf)) {
1021 b_free(&fstrm->rxbuf);
Willy Tarreau132b3a42021-02-20 12:02:46 +01001022 offer_buffers(NULL, 1);
Christopher Faulet99eff652019-08-11 23:11:30 +02001023 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001024 if (fstrm->subs)
1025 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001026 /* There's no need to explicitly call unsubscribe here, the only
1027 * reference left would be in the fconn send_list/fctl_list, and if
1028 * we're in it, we're getting out anyway
1029 */
1030 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001031 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001032 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001033
1034 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001035}
1036
1037/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1038 * stream tree. In case of error, nothing is added and NULL is returned. The
1039 * causes of errors can be any failed memory allocation. The caller is
1040 * responsible for checking if the connection may support an extra stream prior
1041 * to calling this function.
1042 */
1043static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1044{
1045 struct fcgi_strm *fstrm;
1046
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001047 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1048
Christopher Faulet99eff652019-08-11 23:11:30 +02001049 fstrm = pool_alloc(pool_head_fcgi_strm);
1050 if (!fstrm)
1051 goto out;
1052
Willy Tarreau7aad7032020-01-16 17:20:57 +01001053 fstrm->shut_tl = tasklet_new();
1054 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001055 pool_free(pool_head_fcgi_strm, fstrm);
1056 goto out;
1057 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001058 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001059 fstrm->shut_tl->process = fcgi_deferred_shut;
1060 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001061 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001062 fstrm->fconn = fconn;
1063 fstrm->cs = NULL;
1064 fstrm->flags = FCGI_SF_NONE;
1065 fstrm->proto_status = 0;
1066 fstrm->state = FCGI_SS_IDLE;
1067 fstrm->rxbuf = BUF_NULL;
1068
1069 h1m_init_res(&fstrm->h1m);
1070 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1071 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1072
1073 fstrm->by_id.key = fstrm->id = id;
1074 if (id > 0)
1075 fconn->max_id = id;
1076 else
1077 fconn->nb_reserved++;
1078
1079 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1080 fconn->nb_streams++;
1081 fconn->stream_cnt++;
1082
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001083 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001084 return fstrm;
1085
1086 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001087 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 +02001088 return NULL;
1089}
1090
1091/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1092 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1093 * highest possible stream ID was reached.
1094 */
1095static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1096 struct session *sess)
1097{
1098 struct fcgi_strm *fstrm = NULL;
1099
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001100 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1101 if (fconn->nb_streams >= fconn->streams_limit) {
1102 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 +02001103 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001104 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001105
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001106 if (fcgi_streams_left(fconn) < 1) {
1107 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 +02001108 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001109 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001110
1111 /* Defer choosing the ID until we send the first message to create the stream */
1112 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001113 if (!fstrm) {
1114 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 +02001115 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001116 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001117
1118 fstrm->cs = cs;
1119 fstrm->sess = sess;
1120 cs->ctx = fstrm;
1121 fconn->nb_cs++;
1122
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001123 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001124 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001125
1126 out:
1127 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001128}
1129
1130/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1131 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1132 * automatically updated accordingly. If the stream is orphaned, it is
1133 * destroyed.
1134 */
1135static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1136{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001137 struct fcgi_conn *fconn = fstrm->fconn;
1138
1139 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1140
Christopher Faulet99eff652019-08-11 23:11:30 +02001141 if (!fstrm->cs) {
1142 /* this stream was already orphaned */
1143 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001144 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001145 return;
1146 }
1147
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001148 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001150 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001151 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1152 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 else if (fstrm->state == FCGI_SS_HLOC)
1154 fcgi_strm_close(fstrm);
1155 }
1156
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001157 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001158 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1159 if (fstrm->cs->flags & CS_FL_EOS)
1160 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001161
1162 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001163 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1165 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001166 }
1167
1168 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001169
1170 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001171}
1172
1173/* Wakes unassigned streams (ID == 0) attached to the connection. */
1174static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1175{
1176 struct eb32_node *node;
1177 struct fcgi_strm *fstrm;
1178
1179 node = eb32_lookup(&fconn->streams_by_id, 0);
1180 while (node) {
1181 fstrm = container_of(node, struct fcgi_strm, by_id);
1182 if (fstrm->id > 0)
1183 break;
1184 node = eb32_next(node);
1185 fcgi_strm_wake_one_stream(fstrm);
1186 }
1187}
1188
1189/* Wakes the streams attached to the connection, whose id is greater than <last>
1190 * or unassigned.
1191 */
1192static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1193{
1194 struct eb32_node *node;
1195 struct fcgi_strm *fstrm;
1196
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001197 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1198
Christopher Faulet99eff652019-08-11 23:11:30 +02001199 /* Wake all streams with ID > last */
1200 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1201 while (node) {
1202 fstrm = container_of(node, struct fcgi_strm, by_id);
1203 node = eb32_next(node);
1204 fcgi_strm_wake_one_stream(fstrm);
1205 }
1206 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001207
1208 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001209}
1210
1211static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1212 struct htx *htx, struct htx_sl *sl,
1213 struct fcgi_strm_params *params)
1214{
1215 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1216 struct ist p;
1217
1218 if (!sl)
1219 goto error;
1220
1221 if (!(params->mask & FCGI_SP_DOC_ROOT))
1222 params->docroot = fconn->app->docroot;
1223
1224 if (!(params->mask & FCGI_SP_REQ_METH)) {
1225 p = htx_sl_req_meth(sl);
1226 params->meth = ist2(b_tail(params->p), p.len);
1227 chunk_memcat(params->p, p.ptr, p.len);
1228 }
1229 if (!(params->mask & FCGI_SP_REQ_URI)) {
1230 p = htx_sl_req_uri(sl);
1231 params->uri = ist2(b_tail(params->p), p.len);
1232 chunk_memcat(params->p, p.ptr, p.len);
1233 }
1234 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1235 p = htx_sl_req_vsn(sl);
1236 params->vsn = ist2(b_tail(params->p), p.len);
1237 chunk_memcat(params->p, p.ptr, p.len);
1238 }
1239 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1240 char *end;
1241 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001242 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001243 port = get_host_port(cli_conn->dst);
1244 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1245 if (!end)
1246 goto error;
1247 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1248 params->p->data += params->srv_port.len;
1249 }
1250 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1251 /* If no Host header found, use the server address to fill
1252 * srv_name */
1253 if (!istlen(params->srv_name)) {
1254 char *ptr = NULL;
1255
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001256 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001257 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1258 ptr = b_tail(params->p);
1259 if (ptr) {
1260 params->srv_name = ist2(ptr, strlen(ptr));
1261 params->p->data += params->srv_name.len;
1262 }
1263 }
1264 }
1265 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1266 char *ptr = NULL;
1267
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001268 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001269 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1270 ptr = b_tail(params->p);
1271 if (ptr) {
1272 params->rem_addr = ist2(ptr, strlen(ptr));
1273 params->p->data += params->rem_addr.len;
1274 }
1275 }
1276 if (!(params->mask & FCGI_SP_REM_PORT)) {
1277 char *end;
1278 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001279 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001280 port = get_host_port(cli_conn->src);
1281 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1282 if (!end)
1283 goto error;
1284 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1285 params->p->data += params->rem_port.len;
1286 }
1287 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1288 struct htx_blk *blk;
1289 enum htx_blk_type type;
1290 char *end;
1291 size_t len = 0;
1292
1293 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1294 type = htx_get_blk_type(blk);
1295
1296 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1297 break;
1298 if (type == HTX_BLK_DATA)
1299 len += htx_get_blksz(blk);
1300 }
1301 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1302 if (!end)
1303 goto error;
1304 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1305 params->p->data += params->cont_len.len;
1306 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001307#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001308 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001309 if (cli_conn)
1310 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001311 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001312#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001313 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1314 /* one of scriptname, pathinfo or query_string is no set */
1315 struct ist path = http_get_path(params->uri);
1316 int len;
1317
Christopher Faulet99eff652019-08-11 23:11:30 +02001318 /* No scrit_name set but no valid path ==> error */
1319 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1320 goto error;
1321
Christopher Faulet99eff652019-08-11 23:11:30 +02001322 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001323 if (!(params->mask & FCGI_SP_REQ_QS)) {
1324 struct ist qs = istfind(path, '?');
1325
1326 /* Update the path length */
1327 path.len -= qs.len;
1328
1329 /* Set the query-string skipping the '?', if any */
1330 if (istlen(qs))
1331 params->qs = istnext(qs);
1332 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001333
1334 /* If the script_name is set, don't try to deduce the path_info
1335 * too. The opposite is not true.
1336 */
1337 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1338 params->mask |= FCGI_SP_PATH_INFO;
1339 goto end;
1340 }
1341
Christopher Faulet0f17a442020-07-23 15:44:37 +02001342 /* Decode the path. it must first be copied to keep the URI
1343 * untouched.
1344 */
1345 chunk_memcat(params->p, path.ptr, path.len);
1346 path.ptr = b_tail(params->p) - path.len;
1347 len = url_decode(ist0(path), 0);
1348 if (len < 0)
1349 goto error;
1350 path.len = len;
1351
Christopher Faulet99eff652019-08-11 23:11:30 +02001352 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001353 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001354
1355 /* If there is no regex to match the pathinfo, just to the last
1356 * part and see if the index must be used.
1357 */
1358 if (!fconn->app->pathinfo_re)
1359 goto check_index;
1360
Christopher Faulet28cb3662020-02-14 14:47:37 +01001361 /* If some special characters are found in the decoded path (\n
1362 * or \0), the PATH_INFO regex cannot match. This is theorically
1363 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001364 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001365 * case.
1366 */
1367 if (istchr(path, '\n') || istchr(path, '\0'))
1368 goto error;
1369
Christopher Faulet99eff652019-08-11 23:11:30 +02001370 /* The regex does not match, just to the last part and see if
1371 * the index must be used.
1372 */
1373 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1374 goto check_index;
1375
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001376 /* We must have at least 1 capture for the script name,
1377 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001378 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001379 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001380 goto check_index;
1381
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001382 /* Finally we can set the script_name and the path_info. The
1383 * path_info is set if not already defined, and if it was
1384 * captured
1385 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001386 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001387 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1388 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001389
1390 check_index:
1391 len = params->scriptname.len;
1392 /* the script_name if finished by a '/' so we can add the index
1393 * part, if any.
1394 */
1395 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1396 struct ist sn = params->scriptname;
1397
1398 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1399 chunk_memcat(params->p, sn.ptr, sn.len);
1400 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1401 }
1402 }
1403
1404 end:
1405 return 1;
1406 error:
1407 return 0;
1408}
1409
1410static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1411 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1412{
1413 struct fcgi_param p;
1414
1415 if (params->mask & flag)
1416 return 1;
1417
1418 chunk_reset(&trash);
1419
1420 switch (flag) {
1421 case FCGI_SP_CGI_GATEWAY:
1422 p.n = ist("GATEWAY_INTERFACE");
1423 p.v = ist("CGI/1.1");
1424 goto encode;
1425 case FCGI_SP_DOC_ROOT:
1426 p.n = ist("DOCUMENT_ROOT");
1427 p.v = params->docroot;
1428 goto encode;
1429 case FCGI_SP_SCRIPT_NAME:
1430 p.n = ist("SCRIPT_NAME");
1431 p.v = params->scriptname;
1432 goto encode;
1433 case FCGI_SP_PATH_INFO:
1434 p.n = ist("PATH_INFO");
1435 p.v = params->pathinfo;
1436 goto encode;
1437 case FCGI_SP_REQ_URI:
1438 p.n = ist("REQUEST_URI");
1439 p.v = params->uri;
1440 goto encode;
1441 case FCGI_SP_REQ_METH:
1442 p.n = ist("REQUEST_METHOD");
1443 p.v = params->meth;
1444 goto encode;
1445 case FCGI_SP_REQ_QS:
1446 p.n = ist("QUERY_STRING");
1447 p.v = params->qs;
1448 goto encode;
1449 case FCGI_SP_SRV_NAME:
1450 p.n = ist("SERVER_NAME");
1451 p.v = params->srv_name;
1452 goto encode;
1453 case FCGI_SP_SRV_PORT:
1454 p.n = ist("SERVER_PORT");
1455 p.v = params->srv_port;
1456 goto encode;
1457 case FCGI_SP_SRV_PROTO:
1458 p.n = ist("SERVER_PROTOCOL");
1459 p.v = params->vsn;
1460 goto encode;
1461 case FCGI_SP_REM_ADDR:
1462 p.n = ist("REMOTE_ADDR");
1463 p.v = params->rem_addr;
1464 goto encode;
1465 case FCGI_SP_REM_PORT:
1466 p.n = ist("REMOTE_PORT");
1467 p.v = params->rem_port;
1468 goto encode;
1469 case FCGI_SP_SCRIPT_FILE:
1470 p.n = ist("SCRIPT_FILENAME");
1471 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1472 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1473 p.v = ist2(b_head(&trash), b_data(&trash));
1474 goto encode;
1475 case FCGI_SP_PATH_TRANS:
1476 if (!istlen(params->pathinfo))
1477 goto skip;
1478 p.n = ist("PATH_TRANSLATED");
1479 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1480 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1481 p.v = ist2(b_head(&trash), b_data(&trash));
1482 goto encode;
1483 case FCGI_SP_CONT_LEN:
1484 p.n = ist("CONTENT_LENGTH");
1485 p.v = params->cont_len;
1486 goto encode;
1487 case FCGI_SP_HTTPS:
1488 if (!params->https)
1489 goto skip;
1490 p.n = ist("HTTPS");
1491 p.v = ist("on");
1492 goto encode;
1493 default:
1494 goto skip;
1495 }
1496
1497 encode:
1498 if (!istlen(p.v))
1499 goto skip;
1500 if (!fcgi_encode_param(outbuf, &p))
1501 return 0;
1502 skip:
1503 params->mask |= flag;
1504 return 1;
1505}
1506
1507/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1508 * anything. It is highly unexpected, but if the record is larger than a buffer
1509 * and cannot be encoded in one time, an error is triggered and the connection is
1510 * closed. GET_VALUES record cannot be split.
1511 */
1512static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1513{
1514 struct buffer outbuf;
1515 struct buffer *mbuf;
1516 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1517 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001518 int ret = 0;
1519
1520 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001521
1522 mbuf = br_tail(fconn->mbuf);
1523 retry:
1524 if (!fcgi_get_buf(fconn, mbuf)) {
1525 fconn->flags |= FCGI_CF_MUX_MALLOC;
1526 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001527 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1528 ret = 0;
1529 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001530 }
1531
1532 while (1) {
1533 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1534 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1535 break;
1536 realign_again:
1537 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1538 }
1539
1540 if (outbuf.size < 8)
1541 goto full;
1542
1543 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1544 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1545 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1546 outbuf.data = 8;
1547
1548 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1549 * handled by HAProxy.
1550 */
1551 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1552 goto full;
1553
1554 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001555 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 +02001556 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1557 b_add(mbuf, outbuf.data);
1558 ret = 1;
1559
1560 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001561 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001562 return ret;
1563 full:
1564 /* Too large to be encoded. For GET_VALUES records, it is an error */
1565 if (!b_data(mbuf))
1566 goto fail;
1567
1568 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1569 goto retry;
1570 fconn->flags |= FCGI_CF_MUX_MFULL;
1571 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001572 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001573 ret = 0;
1574 goto end;
1575 fail:
1576 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001577 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1578 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1579 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001580}
1581
1582/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1583 * couldn't do anything. It is highly unexpected, but if the record is larger
1584 * than a buffer and cannot be decoded in one time, an error is triggered and
1585 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1586 */
1587static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1588{
1589 struct buffer inbuf;
1590 struct buffer *dbuf;
1591 size_t offset;
1592
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001593 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1594
Christopher Faulet99eff652019-08-11 23:11:30 +02001595 dbuf = &fconn->dbuf;
1596
1597 /* Record too large to be fully decoded */
1598 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1599 goto fail;
1600
1601 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001602 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1603 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001604 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001605 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001606
1607 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1608 /* Realign the dmux buffer if the record wraps. It is unexpected
1609 * at this stage because it should be the first record received
1610 * from the FCGI application.
1611 */
1612 b_slow_realign(dbuf, trash.area, 0);
1613 }
1614
1615 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1616
1617 for (offset = 0; offset < b_data(&inbuf); ) {
1618 struct fcgi_param p;
1619 size_t ret;
1620
1621 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1622 if (!ret) {
1623 /* name or value too large to be decoded at once */
1624 goto fail;
1625 }
1626 offset += ret;
1627
1628 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001629 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001630 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 +02001631 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001632 }
1633 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001634 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 +02001635 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001636 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001637 }
1638 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1639 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001640 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 +02001641 }
1642 /*
1643 * Ignore all other params
1644 */
1645 }
1646
1647 /* Reset the number of concurrent streams supported if the FCGI
1648 * application does not support connection multiplexing
1649 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001650 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001651 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001652 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1653 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001654
1655 /* We must be sure to have read exactly the announced record length, no
1656 * more no less
1657 */
1658 if (offset != fconn->drl)
1659 goto fail;
1660
Willy Tarreau022e5e52020-09-10 09:33:15 +02001661 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 +02001662 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1663 fconn->drl = 0;
1664 fconn->drp = 0;
1665 fconn->state = FCGI_CS_RECORD_H;
1666 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001667 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1668 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001669 return 1;
1670 fail:
1671 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001672 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1673 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 +02001674 return 0;
1675}
1676
1677/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1678 * excluded, as the streams which already received the end-of-stream. It returns
1679 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1680 */
1681static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1682{
1683 struct eb32_node *node;
1684 struct fcgi_strm *fstrm;
1685
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001686 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1687
Christopher Faulet99eff652019-08-11 23:11:30 +02001688 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1689 while (node) {
1690 fstrm = container_of(node, struct fcgi_strm, by_id);
1691 node = eb32_next(node);
1692 if (fstrm->state != FCGI_SS_CLOSED &&
1693 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1694 !fcgi_strm_send_abort(fconn, fstrm))
1695 return 0;
1696 }
1697 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001698 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1699 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001700 return 1;
1701}
1702
1703/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1704 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1705 * space to proceed. It is small enough to be encoded in an empty buffer.
1706 */
1707static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1708{
1709 struct buffer outbuf;
1710 struct buffer *mbuf;
1711 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1712 int ret;
1713
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001714 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1715
Christopher Faulet99eff652019-08-11 23:11:30 +02001716 mbuf = br_tail(fconn->mbuf);
1717 retry:
1718 if (!fcgi_get_buf(fconn, mbuf)) {
1719 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001720 fstrm->flags |= FCGI_SF_BLK_MROOM;
1721 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1722 ret = 0;
1723 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001724 }
1725
1726 while (1) {
1727 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1728 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1729 break;
1730 realign_again:
1731 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1732 }
1733
1734 if (outbuf.size < 8)
1735 goto full;
1736
1737 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1738 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1739 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1740 fcgi_set_record_id(outbuf.area, fstrm->id);
1741 outbuf.data = 8;
1742
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001743 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1744 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001745 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001746 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001747 if (!fcgi_encode_begin_request(&outbuf, &rec))
1748 goto full;
1749
1750 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001751 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 +02001752 b_add(mbuf, outbuf.data);
1753 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1754 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001755 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001756 ret = 1;
1757
1758 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001759 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001760 return ret;
1761 full:
1762 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1763 goto retry;
1764 fconn->flags |= FCGI_CF_MUX_MFULL;
1765 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001766 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 +02001767 ret = 0;
1768 goto end;
1769}
1770
1771/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1772 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1773 * space to proceed. It is small enough to be encoded in an empty buffer.
1774 */
1775static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1776 enum fcgi_record_type rtype)
1777{
1778 struct buffer outbuf;
1779 struct buffer *mbuf;
1780 int ret;
1781
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001782 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001783 mbuf = br_tail(fconn->mbuf);
1784 retry:
1785 if (!fcgi_get_buf(fconn, mbuf)) {
1786 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001787 fstrm->flags |= FCGI_SF_BLK_MROOM;
1788 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1789 ret = 0;
1790 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001791 }
1792
1793 while (1) {
1794 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1795 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1796 break;
1797 realign_again:
1798 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1799 }
1800
1801 if (outbuf.size < 8)
1802 goto full;
1803
1804 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1805 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1806 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1807 outbuf.area[1] = rtype;
1808 fcgi_set_record_id(outbuf.area, fstrm->id);
1809 outbuf.data = 8;
1810
1811 /* commit the record */
1812 b_add(mbuf, outbuf.data);
1813 ret = 1;
1814
1815 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001816 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001817 return ret;
1818 full:
1819 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1820 goto retry;
1821 fconn->flags |= FCGI_CF_MUX_MFULL;
1822 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001823 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 +02001824 ret = 0;
1825 goto end;
1826}
1827
1828
1829/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1830 * marks the end of params.
1831 */
1832static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1833{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001834 int ret;
1835
1836 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1837 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1838 if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02001839 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 +02001840 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001841}
1842
1843/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1844 * marks the end of input. On success, all the request was successfully sent.
1845 */
1846static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1847{
1848 int ret;
1849
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001850 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001851 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001852 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001853 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001854 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 +02001855 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1856 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1857 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001858 return ret;
1859}
1860
1861/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1862 * stops the request processing.
1863 */
1864static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1865{
1866 int ret;
1867
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001868 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001869 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001870 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001871 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001872 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 +02001873 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1874 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1875 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001876 return ret;
1877}
1878
1879/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1880 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1881 * several records are sent. However, a K/V param cannot be split between 2
1882 * records.
1883 */
1884static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1885 struct htx *htx)
1886{
1887 struct buffer outbuf;
1888 struct buffer *mbuf;
1889 struct htx_blk *blk;
1890 struct htx_sl *sl = NULL;
1891 struct fcgi_strm_params params;
1892 size_t total = 0;
1893
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001894 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1895
Christopher Faulet99eff652019-08-11 23:11:30 +02001896 memset(&params, 0, sizeof(params));
1897 params.p = get_trash_chunk();
1898
1899 mbuf = br_tail(fconn->mbuf);
1900 retry:
1901 if (!fcgi_get_buf(fconn, mbuf)) {
1902 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001903 fstrm->flags |= FCGI_SF_BLK_MROOM;
1904 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1905 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001906 }
1907
1908 while (1) {
1909 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1910 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1911 break;
1912 realign_again:
1913 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1914 }
1915
1916 if (outbuf.size < 8)
1917 goto full;
1918
1919 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1920 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1921 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1922 fcgi_set_record_id(outbuf.area, fstrm->id);
1923 outbuf.data = 8;
1924
1925 blk = htx_get_head_blk(htx);
1926 while (blk) {
1927 enum htx_blk_type type;
1928 uint32_t size = htx_get_blksz(blk);
1929 struct fcgi_param p;
1930
1931 type = htx_get_blk_type(blk);
1932 switch (type) {
1933 case HTX_BLK_REQ_SL:
1934 sl = htx_get_blk_ptr(htx, blk);
1935 if (sl->info.req.meth == HTTP_METH_HEAD)
1936 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1937 if (sl->flags & HTX_SL_F_VER_11)
1938 fstrm->h1m.flags |= H1_MF_VER_11;
1939 break;
1940
1941 case HTX_BLK_HDR:
1942 p.n = htx_get_blk_name(htx, blk);
1943 p.v = htx_get_blk_value(htx, blk);
1944
1945 if (istmatch(p.n, ist(":fcgi-"))) {
1946 p.n.ptr += 6;
1947 p.n.len -= 6;
1948 if (isteq(p.n, ist("gateway_interface")))
1949 params.mask |= FCGI_SP_CGI_GATEWAY;
1950 else if (isteq(p.n, ist("document_root"))) {
1951 params.mask |= FCGI_SP_DOC_ROOT;
1952 params.docroot = p.v;
1953 }
1954 else if (isteq(p.n, ist("script_name"))) {
1955 params.mask |= FCGI_SP_SCRIPT_NAME;
1956 params.scriptname = p.v;
1957 }
1958 else if (isteq(p.n, ist("path_info"))) {
1959 params.mask |= FCGI_SP_PATH_INFO;
1960 params.pathinfo = p.v;
1961 }
1962 else if (isteq(p.n, ist("request_uri"))) {
1963 params.mask |= FCGI_SP_REQ_URI;
1964 params.uri = p.v;
1965 }
1966 else if (isteq(p.n, ist("request_meth")))
1967 params.mask |= FCGI_SP_REQ_METH;
1968 else if (isteq(p.n, ist("query_string")))
1969 params.mask |= FCGI_SP_REQ_QS;
1970 else if (isteq(p.n, ist("server_name")))
1971 params.mask |= FCGI_SP_SRV_NAME;
1972 else if (isteq(p.n, ist("server_port")))
1973 params.mask |= FCGI_SP_SRV_PORT;
1974 else if (isteq(p.n, ist("server_protocol")))
1975 params.mask |= FCGI_SP_SRV_PROTO;
1976 else if (isteq(p.n, ist("remote_addr")))
1977 params.mask |= FCGI_SP_REM_ADDR;
1978 else if (isteq(p.n, ist("remote_port")))
1979 params.mask |= FCGI_SP_REM_PORT;
1980 else if (isteq(p.n, ist("script_filename")))
1981 params.mask |= FCGI_SP_SCRIPT_FILE;
1982 else if (isteq(p.n, ist("path_translated")))
1983 params.mask |= FCGI_SP_PATH_TRANS;
1984 else if (isteq(p.n, ist("https")))
1985 params.mask |= FCGI_SP_HTTPS;
1986 }
1987 else if (isteq(p.n, ist("content-length"))) {
1988 p.n = ist("CONTENT_LENGTH");
1989 params.mask |= FCGI_SP_CONT_LEN;
1990 }
1991 else if (isteq(p.n, ist("content-type")))
1992 p.n = ist("CONTENT_TYPE");
1993 else {
1994 if (isteq(p.n, ist("host")))
1995 params.srv_name = p.v;
1996
Christopher Faulet67d58092019-10-02 10:51:38 +02001997 /* Skip header if same name is used to add the server name */
1998 if (fconn->proxy->server_id_hdr_name &&
1999 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
2000 break;
2001
Christopher Faulet99eff652019-08-11 23:11:30 +02002002 memcpy(trash.area, "http_", 5);
2003 memcpy(trash.area+5, p.n.ptr, p.n.len);
2004 p.n = ist2(trash.area, p.n.len+5);
2005 }
2006
2007 if (!fcgi_encode_param(&outbuf, &p)) {
2008 if (b_space_wraps(mbuf))
2009 goto realign_again;
2010 if (outbuf.data == 8)
2011 goto full;
2012 goto done;
2013 }
2014 break;
2015
2016 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002017 if (fconn->proxy->server_id_hdr_name) {
2018 struct server *srv = objt_server(fconn->conn->target);
2019
2020 if (!srv)
2021 goto done;
2022
2023 memcpy(trash.area, "http_", 5);
2024 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2025 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2026 p.v = ist(srv->id);
2027
2028 if (!fcgi_encode_param(&outbuf, &p)) {
2029 if (b_space_wraps(mbuf))
2030 goto realign_again;
2031 if (outbuf.data == 8)
2032 goto full;
2033 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002034 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002035 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002036 goto done;
2037
2038 default:
2039 break;
2040 }
2041 total += size;
2042 blk = htx_remove_blk(htx, blk);
2043 }
2044
2045 done:
2046 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2047 goto error;
2048
2049 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2051 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2052 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2053 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2054 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2055 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2056 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2057 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2058 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2059 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2060 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2061 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2062 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2063 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2064 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2065 goto error;
2066
2067 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002068 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 +02002069 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2070 b_add(mbuf, outbuf.data);
2071
2072 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002073 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002074 return total;
2075 full:
2076 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2077 goto retry;
2078 fconn->flags |= FCGI_CF_MUX_MFULL;
2079 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002080 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 +02002081 if (total)
2082 goto error;
2083 goto end;
2084
2085 error:
2086 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002087 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002088 fcgi_strm_error(fstrm);
2089 goto end;
2090}
2091
2092/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2093 * anything. STDIN records contain the request body.
2094 */
2095static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2096 struct htx *htx, size_t count, struct buffer *buf)
2097{
2098 struct buffer outbuf;
2099 struct buffer *mbuf;
2100 struct htx_blk *blk;
2101 enum htx_blk_type type;
2102 uint32_t size;
2103 size_t total = 0;
2104
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002105 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002106 if (!count)
2107 goto end;
2108
2109 mbuf = br_tail(fconn->mbuf);
2110 retry:
2111 if (!fcgi_get_buf(fconn, mbuf)) {
2112 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002113 fstrm->flags |= FCGI_SF_BLK_MROOM;
2114 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2115 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002116 }
2117
2118 /* Perform some optimizations to reduce the number of buffer copies.
2119 * First, if the mux's buffer is empty and the htx area contains exactly
2120 * one data block of the same size as the requested count, and this
2121 * count fits within the record size, then it's possible to simply swap
2122 * the caller's buffer with the mux's output buffer and adjust offsets
2123 * and length to match the entire DATA HTX block in the middle. In this
2124 * case we perform a true zero-copy operation from end-to-end. This is
2125 * the situation that happens all the time with large files. Second, if
2126 * this is not possible, but the mux's output buffer is empty, we still
2127 * have an opportunity to avoid the copy to the intermediary buffer, by
2128 * making the intermediary buffer's area point to the output buffer's
2129 * area. In this case we want to skip the HTX header to make sure that
2130 * copies remain aligned and that this operation remains possible all
2131 * the time. This goes for headers, data blocks and any data extracted
2132 * from the HTX blocks.
2133 */
2134 blk = htx_get_head_blk(htx);
2135 if (!blk)
2136 goto end;
2137 type = htx_get_blk_type(blk);
2138 size = htx_get_blksz(blk);
2139 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2140 void *old_area = mbuf->area;
2141
2142 if (b_data(mbuf)) {
2143 /* Too bad there are data left there. We're willing to memcpy/memmove
2144 * up to 1/4 of the buffer, which means that it's OK to copy a large
2145 * record into a buffer containing few data if it needs to be realigned,
2146 * and that it's also OK to copy few data without realigning. Otherwise
2147 * we'll pretend the mbuf is full and wait for it to become empty.
2148 */
2149 if (size + 8 <= b_room(mbuf) &&
2150 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2151 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2152 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002153 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002154 }
2155
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002156 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 +02002157 /* map a FCGI record to the HTX block so that we can put the
2158 * record header there.
2159 */
2160 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2161 outbuf.area = b_head(mbuf);
2162
2163 /* prepend a FCGI record header just before the DATA block */
2164 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2165 fcgi_set_record_id(outbuf.area, fstrm->id);
2166 fcgi_set_record_size(outbuf.area, size);
2167
2168 /* and exchange with our old area */
2169 buf->area = old_area;
2170 buf->data = buf->head = 0;
2171 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002172
2173 htx = (struct htx *)buf->area;
2174 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002175 goto end;
2176 }
2177
2178 copy:
2179 while (1) {
2180 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2181 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2182 break;
2183 realign_again:
2184 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2185 }
2186
2187 if (outbuf.size < 8)
2188 goto full;
2189
2190 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2191 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2192 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2193 fcgi_set_record_id(outbuf.area, fstrm->id);
2194 outbuf.data = 8;
2195
2196 blk = htx_get_head_blk(htx);
2197 while (blk && count) {
2198 enum htx_blk_type type = htx_get_blk_type(blk);
2199 uint32_t size = htx_get_blksz(blk);
2200 struct ist v;
2201
2202 switch (type) {
2203 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002204 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 +02002205 v = htx_get_blk_value(htx, blk);
2206 if (v.len > count)
2207 v.len = count;
2208
2209 if (v.len > b_room(&outbuf)) {
2210 /* It doesn't fit at once. If it at least fits once split and
2211 * the amount of data to move is low, let's defragment the
2212 * buffer now.
2213 */
2214 if (b_space_wraps(mbuf) &&
2215 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2216 b_data(mbuf) <= MAX_DATA_REALIGN)
2217 goto realign_again;
2218 v.len = b_room(&outbuf);
2219 }
2220 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2221 if (outbuf.data == 8)
2222 goto full;
2223 goto done;
2224 }
2225 if (v.len != size) {
2226 total += v.len;
2227 count -= v.len;
2228 htx_cut_data_blk(htx, blk, v.len);
2229 goto done;
2230 }
2231 break;
2232
2233 case HTX_BLK_EOM:
2234 goto done;
2235
2236 default:
2237 break;
2238 }
2239 total += size;
2240 count -= size;
2241 blk = htx_remove_blk(htx, blk);
2242 }
2243
2244 done:
2245 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002246 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 +02002247 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2248 b_add(mbuf, outbuf.data);
2249
2250 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002251 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002252 return total;
2253 full:
2254 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2255 goto retry;
2256 fconn->flags |= FCGI_CF_MUX_MFULL;
2257 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002258 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 +02002259 goto end;
2260}
2261
2262/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2263 * anything. STDOUT records contain the entire response. All the content is
2264 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2265 */
2266static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2267{
2268 struct buffer *dbuf;
2269 size_t ret;
2270 size_t max;
2271
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002272 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2273
Christopher Faulet99eff652019-08-11 23:11:30 +02002274 dbuf = &fconn->dbuf;
2275
2276 /* Only padding remains */
2277 if (fconn->state == FCGI_CS_RECORD_P)
2278 goto end_transfer;
2279
2280 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2281 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2282 buf_room_for_htx_data(dbuf))
2283 goto fail; // incomplete record
2284
2285 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2286 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002287 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2288 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002289 }
2290
2291 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2292 max = buf_room_for_htx_data(&fstrm->rxbuf);
2293 if (!b_data(&fstrm->rxbuf))
2294 fstrm->rxbuf.head = sizeof(struct htx);
2295 if (max > fconn->drl)
2296 max = fconn->drl;
2297
2298 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2299 if (!ret)
2300 goto fail;
2301 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002302 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2303 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 +02002304
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002305 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002306 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002307 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2308 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002309
2310 if (fconn->drl)
2311 goto fail;
2312
2313 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002314 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002315 fconn->drl += fconn->drp;
2316 fconn->drp = 0;
2317 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2318 b_del(&fconn->dbuf, ret);
2319 fconn->drl -= ret;
2320 if (fconn->drl)
2321 goto fail;
2322
2323 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002324 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2325 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002326 return 1;
2327 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002328 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 +02002329 return 0;
2330}
2331
2332
2333/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2334 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002335 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002336 */
2337static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2338{
2339 int ret;
2340
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002341 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2342
Christopher Faulet99eff652019-08-11 23:11:30 +02002343 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002344 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002345 fconn->drl += fconn->drp;
2346 fconn->drp = 0;
2347 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2348 b_del(&fconn->dbuf, ret);
2349 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002350 if (fconn->drl) {
2351 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 +02002352 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002353 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002354 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002355 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002356 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 +02002357 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);
2358 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002359 return 1;
2360}
2361
2362/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2363 * anything.
2364 */
2365static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2366{
2367 struct buffer *dbuf;
2368 struct buffer tag;
2369 size_t ret;
2370
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002371 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002372 dbuf = &fconn->dbuf;
2373
2374 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002375 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002376 goto end_transfer;
2377
2378 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2379 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2380 buf_room_for_htx_data(dbuf))
2381 goto fail; // incomplete record
2382
2383 chunk_reset(&trash);
2384 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2385 if (!ret)
2386 goto fail;
2387 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002388 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 +02002389
2390 trash.area[ret] = '\n';
2391 trash.area[ret+1] = '\0';
2392 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002393 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002394
2395 if (fconn->drl)
2396 goto fail;
2397
2398 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002399 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002400 fconn->drl += fconn->drp;
2401 fconn->drp = 0;
2402 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2403 b_del(&fconn->dbuf, ret);
2404 fconn->drl -= ret;
2405 if (fconn->drl)
2406 goto fail;
2407 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002408 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2409 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002410 return 1;
2411 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 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 +02002413 return 0;
2414}
2415
2416/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2417 * anything. If the empty STDOUT record is not already received, this one marks
2418 * the end of the response. It is highly unexpected, but if the record is larger
2419 * than a buffer and cannot be decoded in one time, an error is triggered and
2420 * the connection is closed. END_REQUEST record cannot be split.
2421 */
2422static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2423{
2424 struct buffer inbuf;
2425 struct buffer *dbuf;
2426 struct fcgi_end_request endreq;
2427
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002428 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002429 dbuf = &fconn->dbuf;
2430
2431 /* Record too large to be fully decoded */
2432 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2433 goto fail;
2434
2435 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002436 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2437 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002438 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002439 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002440
2441 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2442 /* Realign the dmux buffer if the record wraps. It is unexpected
2443 * at this stage because it should be the first record received
2444 * from the FCGI application.
2445 */
2446 b_slow_realign(dbuf, trash.area, 0);
2447 }
2448
2449 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2450
2451 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2452 goto fail;
2453
2454 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002455 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 +02002456 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 +02002457 fstrm->proto_status = endreq.errcode;
2458 fcgi_strm_close(fstrm);
2459
2460 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2461 fconn->drl = 0;
2462 fconn->drp = 0;
2463 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002464 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2465 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002466 return 1;
2467
2468 fail:
2469 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002470 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 +02002471 return 0;
2472}
2473
2474/* process Rx records to be demultiplexed */
2475static void fcgi_process_demux(struct fcgi_conn *fconn)
2476{
2477 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2478 struct fcgi_header hdr;
2479 int ret;
2480
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2482
Christopher Faulet99eff652019-08-11 23:11:30 +02002483 if (fconn->state == FCGI_CS_CLOSED)
2484 return;
2485
2486 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002487 if (fconn->state == FCGI_CS_INIT) {
2488 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2489 return;
2490 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002491 if (fconn->state == FCGI_CS_SETTINGS) {
2492 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002493 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002494 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2495 if (!ret)
2496 goto fail;
2497 b_del(&fconn->dbuf, ret);
2498
2499 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2500 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002501 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);
2502 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 +02002503 goto fail;
2504 }
2505 goto new_record;
2506 }
2507 }
2508
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002509 /* process as many incoming records as possible below */
2510 while (1) {
2511 if (!b_data(&fconn->dbuf)) {
2512 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2513 break;
2514 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002515
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002516 if (fconn->state == FCGI_CS_CLOSED) {
2517 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002518 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002519 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002520
2521 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002522 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002523 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2524 if (!ret)
2525 break;
2526 b_del(&fconn->dbuf, ret);
2527
2528 new_record:
2529 fconn->dsi = hdr.id;
2530 fconn->drt = hdr.type;
2531 fconn->drl = hdr.len;
2532 fconn->drp = hdr.padding;
2533 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002534 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 +02002535 }
2536
2537 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2538 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2539
2540 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2541 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002542 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002543 fstrm->state == FCGI_SS_CLOSED ||
2544 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2545 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2546 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002547 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 +02002548 fstrm->cs->flags |= CS_FL_RCV_MORE;
2549 fcgi_strm_notify_recv(fstrm);
2550 }
2551 fstrm = tmp_fstrm;
2552
2553 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2554 /* ignore all record for closed streams */
2555 goto ignore_record;
2556 }
2557 if (fstrm->state == FCGI_SS_IDLE) {
2558 /* ignore all record for unknown streams */
2559 goto ignore_record;
2560 }
2561
2562 switch (fconn->drt) {
2563 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002564 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 +02002565 ret = fcgi_conn_handle_values_result(fconn);
2566 break;
2567
2568 case FCGI_STDOUT:
2569 if (fstrm->flags & FCGI_SF_ES_RCVD)
2570 goto ignore_record;
2571
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002572 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002573 if (fconn->drl)
2574 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2575 else
2576 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2577 break;
2578
2579 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002580 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002581 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2582 break;
2583
2584 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002585 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 +02002586 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2587 break;
2588
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002589 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002590 default:
2591 ignore_record:
2592 /* drop records that we ignore. They may be
2593 * larger than the buffer so we drain all of
2594 * their contents until we reach the end.
2595 */
2596 fconn->state = FCGI_CS_RECORD_P;
2597 fconn->drl += fconn->drp;
2598 fconn->drp = 0;
2599 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002600 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002602 b_del(&fconn->dbuf, ret);
2603 fconn->drl -= ret;
2604 ret = (fconn->drl == 0);
2605 }
2606
2607 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002608 if (ret <= 0) {
2609 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002610 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002611 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002612
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002613 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002614 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002615 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2616 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002617 }
2618
2619 fail:
2620 /* we can go here on missing data, blocked response or error */
2621 if (fstrm && fstrm->cs &&
2622 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002623 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002624 fstrm->state == FCGI_SS_CLOSED ||
2625 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2626 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2627 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002628 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 +02002629 fstrm->cs->flags |= CS_FL_RCV_MORE;
2630 fcgi_strm_notify_recv(fstrm);
2631 }
2632
2633 fcgi_conn_restart_reading(fconn, 0);
2634}
2635
2636/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2637 * the end.
2638 */
2639static int fcgi_process_mux(struct fcgi_conn *fconn)
2640{
2641 struct fcgi_strm *fstrm, *fstrm_back;
2642
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002643 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2644
Christopher Faulet99eff652019-08-11 23:11:30 +02002645 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2646 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2647 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2648 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002649 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 +02002650 fcgi_wake_unassigned_streams(fconn);
2651 goto mux;
2652 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002653 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002654 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2655 goto fail;
2656 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002657 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 +02002658 }
2659 /* need to wait for the other side */
2660 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002661 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002662 }
2663
2664 mux:
2665 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2666 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2667 break;
2668
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002669 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002670 continue;
2671
Willy Tarreau7aad7032020-01-16 17:20:57 +01002672 /* If the sender changed his mind and unsubscribed, let's just
2673 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002674 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002675 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2676 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002677 LIST_DEL_INIT(&fstrm->send_list);
2678 continue;
2679 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002680
2681 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002682 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2683 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002684 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002685 tasklet_wakeup(fstrm->subs->tasklet);
2686 fstrm->subs->events &= ~SUB_RETRY_SEND;
2687 if (!fstrm->subs->events)
2688 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002689 } else {
2690 /* it's the shut request that was queued */
2691 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2692 tasklet_wakeup(fstrm->shut_tl);
2693 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 }
2695
2696 fail:
2697 if (fconn->state == FCGI_CS_CLOSED) {
2698 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2699 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002700 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2701 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002702 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002703 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002704 }
2705 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002706
2707 done:
2708 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002709 return 1;
2710}
2711
2712
2713/* Attempt to read data, and subscribe if none available.
2714 * The function returns 1 if data has been received, otherwise zero.
2715 */
2716static int fcgi_recv(struct fcgi_conn *fconn)
2717{
2718 struct connection *conn = fconn->conn;
2719 struct buffer *buf;
2720 int max;
2721 size_t ret;
2722
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002723 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2724
2725 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2726 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002727 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002728 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002729
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002730 if (!fcgi_recv_allowed(fconn)) {
2731 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002732 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002733 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002734
2735 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2736 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002737 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002738 fconn->flags |= FCGI_CF_DEM_DALLOC;
2739 return 0;
2740 }
2741
2742 b_realign_if_empty(buf);
2743 if (!b_data(buf)) {
2744 /* try to pre-align the buffer like the
2745 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002746 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002747 * HTX block to alias it upon recv. We cannot use the
2748 * head because rcv_buf() will realign the buffer if
2749 * it's empty. Thus we cheat and pretend we already
2750 * have a few bytes there.
2751 */
2752 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2753 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2754 }
2755 else
2756 max = buf_room_for_htx_data(buf);
2757
2758 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2759
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002760 if (max && !ret && fcgi_recv_allowed(fconn)) {
2761 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002762 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002763 }
2764 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002765 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002766
2767 if (!b_data(buf)) {
2768 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002769 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002770 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2771 }
2772
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002773 if (ret == max) {
2774 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002775 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002776 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002777
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002778 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002779 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2780}
2781
2782
2783/* Try to send data if possible.
2784 * The function returns 1 if data have been sent, otherwise zero.
2785 */
2786static int fcgi_send(struct fcgi_conn *fconn)
2787{
2788 struct connection *conn = fconn->conn;
2789 int done;
2790 int sent = 0;
2791
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002792 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2793
2794 if (conn->flags & CO_FL_ERROR) {
2795 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002796 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002797 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002798
2799
Willy Tarreau911db9b2020-01-23 16:27:54 +01002800 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002801 /* a handshake was requested */
2802 goto schedule;
2803 }
2804
2805 /* This loop is quite simple : it tries to fill as much as it can from
2806 * pending streams into the existing buffer until it's reportedly full
2807 * or the end of send requests is reached. Then it tries to send this
2808 * buffer's contents out, marks it not full if at least one byte could
2809 * be sent, and tries again.
2810 *
2811 * The snd_buf() function normally takes a "flags" argument which may
2812 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2813 * data immediately comes and CO_SFL_STREAMER to indicate that the
2814 * connection is streaming lots of data (used to increase TLS record
2815 * size at the expense of latency). The former can be sent any time
2816 * there's a buffer full flag, as it indicates at least one stream
2817 * attempted to send and failed so there are pending data. An
2818 * alternative would be to set it as long as there's an active stream
2819 * but that would be problematic for ACKs until we have an absolute
2820 * guarantee that all waiters have at least one byte to send. The
2821 * latter should possibly not be set for now.
2822 */
2823
2824 done = 0;
2825 while (!done) {
2826 unsigned int flags = 0;
2827 unsigned int released = 0;
2828 struct buffer *buf;
2829
2830 /* fill as much as we can into the current buffer */
2831 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2832 done = fcgi_process_mux(fconn);
2833
2834 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2835 done = 1; // we won't go further without extra buffers
2836
2837 if (conn->flags & CO_FL_ERROR)
2838 break;
2839
2840 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2841 flags |= CO_SFL_MSG_MORE;
2842
2843 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2844 if (b_data(buf)) {
2845 int ret;
2846
2847 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2848 if (!ret) {
2849 done = 1;
2850 break;
2851 }
2852 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002853 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002854 b_del(buf, ret);
2855 if (b_data(buf)) {
2856 done = 1;
2857 break;
2858 }
2859 }
2860 b_free(buf);
2861 released++;
2862 }
2863
2864 if (released)
Willy Tarreau132b3a42021-02-20 12:02:46 +01002865 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02002866
2867 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002868 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2869 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002870 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2871 }
2872
2873 if (conn->flags & CO_FL_SOCK_WR_SH) {
2874 /* output closed, nothing to send, clear the buffer to release it */
2875 b_reset(br_tail(fconn->mbuf));
2876 }
2877 /* We're not full anymore, so we can wake any task that are waiting
2878 * for us.
2879 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002880 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002881 struct fcgi_strm *fstrm;
2882
2883 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2884 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2885 break;
2886
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002887 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002888 continue;
2889
Willy Tarreau7aad7032020-01-16 17:20:57 +01002890 /* If the sender changed his mind and unsubscribed, let's just
2891 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002892 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002893 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2894 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002895 LIST_DEL_INIT(&fstrm->send_list);
2896 continue;
2897 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002898
2899 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002900 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002901 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002902 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002903 tasklet_wakeup(fstrm->subs->tasklet);
2904 fstrm->subs->events &= ~SUB_RETRY_SEND;
2905 if (!fstrm->subs->events)
2906 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002907 } else {
2908 /* it's the shut request that was queued */
2909 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2910 tasklet_wakeup(fstrm->shut_tl);
2911 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002912 }
2913 }
2914 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002915 if (!br_data(fconn->mbuf)) {
2916 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002917 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002918 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002919schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002920 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2921 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002922 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002923 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002924
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002925 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002926 return sent;
2927}
2928
2929/* this is the tasklet referenced in fconn->wait_event.tasklet */
Willy Tarreau9a5a0e02021-01-20 14:55:01 +01002930struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet99eff652019-08-11 23:11:30 +02002931{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002932 struct connection *conn;
Willy Tarreaub360bb82021-03-02 16:51:09 +01002933 struct fcgi_conn *fconn = ctx;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002934 struct tasklet *tl = (struct tasklet *)t;
2935 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002936 int ret = 0;
2937
Willy Tarreaub360bb82021-03-02 16:51:09 +01002938 if (status & TASK_F_USR1) {
2939 /* the tasklet was idling on an idle connection, it might have
2940 * been stolen, let's be careful!
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002941 */
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002942
Willy Tarreaub360bb82021-03-02 16:51:09 +01002943 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2944 if (tl->context == NULL) {
2945 /* The connection has been taken over by another thread,
2946 * we're no longer responsible for it, so just free the
2947 * tasklet, and do nothing.
2948 */
2949 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2950 tasklet_free(tl);
2951 return NULL;
2952 }
2953 conn = fconn->conn;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002954
Willy Tarreaub360bb82021-03-02 16:51:09 +01002955 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002956
Willy Tarreaub360bb82021-03-02 16:51:09 +01002957 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2958 if (conn_in_list)
2959 MT_LIST_DEL(&conn->list);
2960 } else {
2961 /* we're certain the connection was not in an idle list */
2962 conn = fconn->conn;
2963 TRACE_ENTER(FCGI_EV_FCONN_WAKE, conn);
2964 conn_in_list = 0;
2965 }
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002966 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002967
Christopher Faulet99eff652019-08-11 23:11:30 +02002968 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2969 ret = fcgi_send(fconn);
2970 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2971 ret |= fcgi_recv(fconn);
2972 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002973 ret = fcgi_process(fconn);
2974
2975 /* If we were in an idle list, we want to add it back into it,
2976 * unless fcgi_process() returned -1, which mean it has destroyed
2977 * the connection (testing !ret is enough, if fcgi_process() wasn't
2978 * called then ret will be 0 anyway.
2979 */
2980 if (!ret && conn_in_list) {
2981 struct server *srv = objt_server(conn->target);
2982
2983 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002984 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002985 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002986 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002987 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002988 return NULL;
2989}
2990
2991/* callback called on any event by the connection handler.
2992 * It applies changes and returns zero, or < 0 if it wants immediate
2993 * destruction of the connection (which normally doesn not happen in FCGI).
2994 */
2995static int fcgi_process(struct fcgi_conn *fconn)
2996{
2997 struct connection *conn = fconn->conn;
2998
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002999 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
3000
Christopher Faulet99eff652019-08-11 23:11:30 +02003001 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
3002 fcgi_process_demux(fconn);
3003
3004 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
3005 b_reset(&fconn->dbuf);
3006
3007 if (buf_room_for_htx_data(&fconn->dbuf))
3008 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3009 }
3010 fcgi_send(fconn);
3011
Willy Tarreauc3914d42020-09-24 08:39:22 +02003012 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003013 /* frontend is stopping, reload likely in progress, let's try
3014 * to announce a graceful shutdown if not yet done. We don't
3015 * care if it fails, it will be tried again later.
3016 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003017 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 +02003018 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3019 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3020 fcgi_conn_send_aborts(fconn);
3021 }
3022 }
3023
3024 /*
3025 * If we received early data, and the handshake is done, wake
3026 * any stream that was waiting for it.
3027 */
3028 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003029 (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 +02003030 struct eb32_node *node;
3031 struct fcgi_strm *fstrm;
3032
3033 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3034 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3035
3036 while (node) {
3037 fstrm = container_of(node, struct fcgi_strm, by_id);
3038 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3039 fcgi_strm_notify_recv(fstrm);
3040 node = eb32_next(node);
3041 }
3042 }
3043
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003044 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003045 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3046 eb_is_empty(&fconn->streams_by_id)) {
3047 fcgi_wake_some_streams(fconn, 0);
3048
3049 if (eb_is_empty(&fconn->streams_by_id)) {
3050 /* no more stream, kill the connection now */
3051 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003052 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003053 return -1;
3054 }
3055 }
3056
3057 if (!b_data(&fconn->dbuf))
3058 fcgi_release_buf(fconn, &fconn->dbuf);
3059
3060 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3061 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3062 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3063 fcgi_release_mbuf(fconn);
3064
3065 if (fconn->task) {
3066 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3067 task_queue(fconn->task);
3068 }
3069
3070 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003071 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003072 return 0;
3073}
3074
3075
3076/* wake-up function called by the connection layer (mux_ops.wake) */
3077static int fcgi_wake(struct connection *conn)
3078{
3079 struct fcgi_conn *fconn = conn->ctx;
3080
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003081 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003082 return (fcgi_process(fconn));
3083}
3084
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003085
3086static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3087{
3088 int ret = 0;
3089 switch (mux_ctl) {
3090 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003091 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003092 ret |= MUX_STATUS_READY;
3093 return ret;
3094 default:
3095 return -1;
3096 }
3097}
3098
Christopher Faulet99eff652019-08-11 23:11:30 +02003099/* Connection timeout management. The principle is that if there's no receipt
3100 * nor sending for a certain amount of time, the connection is closed. If the
3101 * MUX buffer still has lying data or is not allocatable, the connection is
3102 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003103 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003104 */
3105static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3106{
3107 struct fcgi_conn *fconn = context;
3108 int expired = tick_is_expired(t->expire, now_ms);
3109
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003110 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3111
Willy Tarreau60814ff2020-06-30 11:19:23 +02003112 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003113 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3114
3115 /* Somebody already stole the connection from us, so we should not
3116 * free it, we just have to free the task.
3117 */
3118 if (!t->context) {
3119 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3120 fconn = NULL;
3121 goto do_leave;
3122 }
3123
Willy Tarreau60814ff2020-06-30 11:19:23 +02003124 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003125 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003126 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3127 return t;
3128 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003129
Willy Tarreau60814ff2020-06-30 11:19:23 +02003130 /* We're about to destroy the connection, so make sure nobody attempts
3131 * to steal it from us.
3132 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003133 if (fconn->conn->flags & CO_FL_LIST_MASK)
3134 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003135
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003136 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003137 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003138
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003139do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003140 task_destroy(t);
3141
3142 if (!fconn) {
3143 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003144 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003145 return NULL;
3146 }
3147
3148 fconn->task = NULL;
3149 fconn->state = FCGI_CS_CLOSED;
3150 fcgi_wake_some_streams(fconn, 0);
3151
3152 if (br_data(fconn->mbuf)) {
3153 /* don't even try to send aborts, the buffer is stuck */
3154 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3155 goto end;
3156 }
3157
3158 /* try to send but no need to insist */
3159 if (!fcgi_conn_send_aborts(fconn))
3160 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3161
3162 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3163 conn_xprt_ready(fconn->conn)) {
3164 unsigned int released = 0;
3165 struct buffer *buf;
3166
3167 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3168 if (b_data(buf)) {
3169 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3170 buf, b_data(buf), 0);
3171 if (!ret)
3172 break;
3173 b_del(buf, ret);
3174 if (b_data(buf))
3175 break;
3176 b_free(buf);
3177 released++;
3178 }
3179 }
3180
3181 if (released)
Willy Tarreau132b3a42021-02-20 12:02:46 +01003182 offer_buffers(NULL, released);
Christopher Faulet99eff652019-08-11 23:11:30 +02003183 }
3184
3185 end:
3186 /* either we can release everything now or it will be done later once
3187 * the last stream closes.
3188 */
3189 if (eb_is_empty(&fconn->streams_by_id))
3190 fcgi_release(fconn);
3191
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003192 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003193 return NULL;
3194}
3195
3196
3197/*******************************************/
3198/* functions below are used by the streams */
3199/*******************************************/
3200
3201/* Append the description of what is present in error snapshot <es> into <out>.
3202 * The description must be small enough to always fit in a buffer. The output
3203 * buffer may be the trash so the trash must not be used inside this function.
3204 */
3205static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3206{
3207 chunk_appendf(out,
3208 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3209 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3210 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3211 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3212 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3213 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3214}
3215/*
3216 * Capture a bad response and archive it in the proxy's structure. By default
3217 * it tries to report the error position as h1m->err_pos. However if this one is
3218 * not set, it will then report h1m->next, which is the last known parsing
3219 * point. The function is able to deal with wrapping buffers. It always displays
3220 * buffers as a contiguous area starting at buf->p. The direction is determined
3221 * thanks to the h1m's flags.
3222 */
3223static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3224 struct h1m *h1m, struct buffer *buf)
3225{
3226 struct session *sess = fstrm->sess;
3227 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003228 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003229 union error_snapshot_ctx ctx;
3230
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003231 if (fstrm->cs && fstrm->cs->data) {
3232 if (sess == NULL)
3233 sess = si_strm(fstrm->cs->data)->sess;
3234 if (!(h1m->flags & H1_MF_RESP))
3235 other_end = si_strm(fstrm->cs->data)->be;
3236 else
3237 other_end = sess->fe;
3238 } else
3239 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003240 /* http-specific part now */
3241 ctx.h1.state = h1m->state;
3242 ctx.h1.c_flags = fconn->flags;
3243 ctx.h1.s_flags = fstrm->flags;
3244 ctx.h1.m_flags = h1m->flags;
3245 ctx.h1.m_clen = h1m->curr_len;
3246 ctx.h1.m_blen = h1m->body_len;
3247
3248 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3249 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3250 &ctx, fcgi_show_error_snapshot);
3251}
3252
3253static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3254 struct buffer *buf, size_t *ofs, size_t max)
3255{
3256 int ret;
3257
Willy Tarreau022e5e52020-09-10 09:33:15 +02003258 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 +02003259 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3260 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003261 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 +02003262 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003263 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 +02003264 fcgi_strm_error(fstrm);
3265 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3266 }
3267 goto end;
3268 }
3269
3270 *ofs += ret;
3271 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003272 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 +02003273 return ret;
3274
3275}
3276
Christopher Fauletaf542632019-10-01 21:52:49 +02003277static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003278 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3279{
3280 int ret;
3281
Willy Tarreau022e5e52020-09-10 09:33:15 +02003282 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 +02003283 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003284 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003285 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 +02003286 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003287 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 +02003288 fcgi_strm_error(fstrm);
3289 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3290 }
3291 goto end;
3292 }
3293 *ofs += ret;
3294 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003295 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 +02003296 return ret;
3297}
3298
3299static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3300 struct buffer *buf, size_t *ofs, size_t max)
3301{
3302 int ret;
3303
Willy Tarreau022e5e52020-09-10 09:33:15 +02003304 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 +02003305 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003306 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003307 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 +02003308 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003309 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 +02003310 fcgi_strm_error(fstrm);
3311 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3312 }
3313 goto end;
3314 }
3315 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003316 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003317 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 +02003318 return ret;
3319}
3320
3321static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003322 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003323{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003324 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003325
Willy Tarreau022e5e52020-09-10 09:33:15 +02003326 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 +01003327 ret = h1_parse_msg_eom(h1m, htx, max);
3328 if (!ret) {
3329 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3330 if (htx->flags & HTX_FL_PARSING_ERROR) {
3331 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3332 fcgi_strm_error(fstrm);
3333 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3334 }
3335 goto end;
3336 }
3337 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3338 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003339 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 +01003340 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003341}
3342
3343static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3344{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003345 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003346 struct htx *htx;
3347 struct h1m *h1m = &fstrm->h1m;
3348 size_t ret, data, total = 0;
3349
3350 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003351 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3352
Christopher Faulet99eff652019-08-11 23:11:30 +02003353 data = htx->data;
3354 if (fstrm->state == FCGI_SS_ERROR)
3355 goto end;
3356
3357 do {
3358 size_t used = htx_used_space(htx);
3359
3360 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003361 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003362 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3363 if (!ret)
3364 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003365
3366 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3367
Christopher Faulet99eff652019-08-11 23:11:30 +02003368 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3369 struct htx_blk *blk = htx_get_head_blk(htx);
3370 struct htx_sl *sl;
3371
3372 if (!blk)
3373 break;
3374 sl = htx_get_blk_ptr(htx, blk);
3375 sl->flags |= HTX_SL_F_XFER_LEN;
3376 htx->extra = 0;
3377 }
3378 }
3379 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003380 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003381 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003382 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003383 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003384
3385 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 +02003386 }
3387 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003388 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3389 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3390 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003391 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003392
Christopher Faulet76014fd2019-12-10 11:47:22 +01003393 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 +02003394 }
3395 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003396 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3397 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3398 break;
3399
3400 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3401 }
3402
Christopher Faulet99eff652019-08-11 23:11:30 +02003403 if (b_data(&fstrm->rxbuf) > total) {
3404 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003405 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003406 fcgi_strm_error(fstrm);
3407 }
3408 break;
3409 }
3410 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003411 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003412 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003413
Christopher Faulet99eff652019-08-11 23:11:30 +02003414 if (fstrm->state != FCGI_SS_ERROR &&
3415 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003416 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003417 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3418 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3419 h1m->state = H1_MSG_DONE;
3420 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 +02003421 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003422 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003423 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003424
3425 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003426 }
3427 else {
3428 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003429 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003430 fcgi_strm_error(fstrm);
3431 break;
3432 }
3433
3434 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003436
3437 if (fstrm->state == FCGI_SS_ERROR) {
3438 b_reset(&fstrm->rxbuf);
3439 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003440 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003441 return 0;
3442 }
3443
3444 b_del(&fstrm->rxbuf, total);
3445
3446 end:
3447 htx_to_buf(htx, buf);
3448 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003449 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003450 return ret;
3451}
3452
3453/*
3454 * Attach a new stream to a connection
3455 * (Used for outgoing connections)
3456 */
3457static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3458{
3459 struct conn_stream *cs;
3460 struct fcgi_strm *fstrm;
3461 struct fcgi_conn *fconn = conn->ctx;
3462
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003463 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003464 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003465 if (!cs) {
3466 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003467 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003468 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003469 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3470 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003471 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003472 cs_free(cs);
3473 return NULL;
3474 }
Willy Tarreaub360bb82021-03-02 16:51:09 +01003475
3476 /* the connection is not idle anymore, let's mark this */
3477 HA_ATOMIC_AND(&fconn->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01003478 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaub360bb82021-03-02 16:51:09 +01003479
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003480 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003481 return cs;
3482}
3483
3484/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3485 * We have to scan because we may have some orphan streams. It might be
3486 * beneficial to scan backwards from the end to reduce the likeliness to find
3487 * orphans.
3488 */
3489static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3490{
3491 struct fcgi_conn *fconn = conn->ctx;
3492 struct fcgi_strm *fstrm;
3493 struct eb32_node *node;
3494
3495 node = eb32_first(&fconn->streams_by_id);
3496 while (node) {
3497 fstrm = container_of(node, struct fcgi_strm, by_id);
3498 if (fstrm->cs)
3499 return fstrm->cs;
3500 node = eb32_next(node);
3501 }
3502 return NULL;
3503}
3504
3505/*
3506 * Destroy the mux and the associated connection, if it is no longer used
3507 */
3508static void fcgi_destroy(void *ctx)
3509{
3510 struct fcgi_conn *fconn = ctx;
3511
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003512 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003513 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3514 fcgi_release(fconn);
3515}
3516
3517/*
3518 * Detach the stream from the connection and possibly release the connection.
3519 */
3520static void fcgi_detach(struct conn_stream *cs)
3521{
3522 struct fcgi_strm *fstrm = cs->ctx;
3523 struct fcgi_conn *fconn;
3524 struct session *sess;
3525
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003526 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3527
Christopher Faulet99eff652019-08-11 23:11:30 +02003528 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003529 if (!fstrm) {
3530 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003531 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003532 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003533
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003534 /* there's no txbuf so we're certain no to be able to send anything */
3535 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003536
3537 sess = fstrm->sess;
3538 fconn = fstrm->fconn;
3539 fstrm->cs = NULL;
3540 fconn->nb_cs--;
3541
3542 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3543 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3544 fconn->streams_limit = 1;
3545 }
3546 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3547 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3548 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3549 fconn->state = FCGI_CS_CLOSED;
3550 }
3551
3552 /* this stream may be blocked waiting for some data to leave, so orphan
3553 * it in this case.
3554 */
3555 if (!(cs->conn->flags & CO_FL_ERROR) &&
3556 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003557 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003558 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003559 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003560 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003561 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003562
3563 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3564 /* unblock the connection if it was blocked on this stream. */
3565 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3566 fcgi_conn_restart_reading(fconn, 1);
3567 }
3568
3569 fcgi_strm_destroy(fstrm);
3570
3571 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003572 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003573 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003574 /* Add the connection in the session serverlist, if not already done */
3575 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3576 fconn->conn->owner = NULL;
3577 if (eb_is_empty(&fconn->streams_by_id)) {
3578 /* let's kill the connection right away */
3579 fconn->conn->mux->destroy(fconn);
3580 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3581 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003582 }
3583 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003584 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003585 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3586 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003587 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003588 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003589 }
3590 }
3591 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003592 else {
3593 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003594 /* If the connection is owned by the session, first remove it
3595 * from its list
3596 */
3597 if (fconn->conn->owner) {
3598 session_unown_conn(fconn->conn->owner, fconn->conn);
3599 fconn->conn->owner = NULL;
3600 }
3601
Willy Tarreaub360bb82021-03-02 16:51:09 +01003602 /* mark that the tasklet may lose its context to another thread and
3603 * that the handler needs to check it under the idle conns lock.
3604 */
3605 HA_ATOMIC_OR(&fconn->wait_event.tasklet->state, TASK_F_USR1);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01003606 xprt_set_idle(fconn->conn, fconn->conn->xprt, fconn->conn->xprt_ctx);
3607
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003608 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003609 /* The server doesn't want it, let's kill the connection right away */
3610 fconn->conn->mux->destroy(fconn);
3611 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3612 return;
3613 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003614 /* At this point, the connection has been added to the
3615 * server idle list, so another thread may already have
3616 * hijacked it, so we can't do anything with it.
3617 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003618 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3619 return;
3620 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003621 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003622 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3623 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003624 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003625 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003626 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 }
3628
3629 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003630 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003631 */
3632 if (fcgi_conn_is_dead(fconn)) {
3633 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003634 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 fcgi_release(fconn);
3636 }
3637 else if (fconn->task) {
3638 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3639 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003640 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003641 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003642 else
3643 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003644}
3645
3646
3647/* Performs a synchronous or asynchronous shutr(). */
3648static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3649{
3650 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003651
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003652 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3653
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 if (fstrm->state == FCGI_SS_CLOSED)
3655 goto done;
3656
3657 /* a connstream may require us to immediately kill the whole connection
3658 * for example because of a "tcp-request content reject" rule that is
3659 * normally used to limit abuse.
3660 */
3661 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003662 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3663 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003664 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003665 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003666 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003667 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003668 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3669 !fcgi_strm_send_abort(fconn, fstrm))
3670 goto add_to_list;
3671 }
3672
3673 fcgi_strm_close(fstrm);
3674
3675 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3676 tasklet_wakeup(fconn->wait_event.tasklet);
3677 done:
3678 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003679 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003680 return;
3681
3682 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003683 /* Let the handler know we want to shutr, and add ourselves to the
3684 * send list if not yet done. fcgi_deferred_shut() will be
3685 * automatically called via the shut_tl tasklet when there's room
3686 * again.
3687 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003688 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003689 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003690 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3691 }
3692 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003694 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003695 return;
3696}
3697
3698/* Performs a synchronous or asynchronous shutw(). */
3699static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3700{
3701 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003703 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3704
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3706 goto done;
3707
3708 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3709 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3710 !fcgi_strm_send_abort(fconn, fstrm))
3711 goto add_to_list;
3712
3713 if (fstrm->state == FCGI_SS_HREM)
3714 fcgi_strm_close(fstrm);
3715 else
3716 fstrm->state = FCGI_SS_HLOC;
3717 } else {
3718 /* a connstream may require us to immediately kill the whole connection
3719 * for example because of a "tcp-request content reject" rule that is
3720 * normally used to limit abuse.
3721 */
3722 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003723 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3724 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003725 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003726 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003727
3728 fcgi_strm_close(fstrm);
3729 }
3730
3731 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3732 tasklet_wakeup(fconn->wait_event.tasklet);
3733 done:
3734 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003735 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003736 return;
3737
3738 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003739 /* Let the handler know we want to shutr, and add ourselves to the
3740 * send list if not yet done. fcgi_deferred_shut() will be
3741 * automatically called via the shut_tl tasklet when there's room
3742 * again.
3743 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003744 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003745 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003746 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3747 }
3748 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003749 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003750 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003751 return;
3752}
3753
Willy Tarreau7aad7032020-01-16 17:20:57 +01003754/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003755 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003756 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 */
3758static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3759{
3760 struct fcgi_strm *fstrm = ctx;
3761 struct fcgi_conn *fconn = fstrm->fconn;
3762
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003763 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3764
Willy Tarreau7aad7032020-01-16 17:20:57 +01003765 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3766 /* some data processing remains to be done first */
3767 goto end;
3768 }
3769
Christopher Faulet99eff652019-08-11 23:11:30 +02003770 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3771 fcgi_do_shutw(fstrm);
3772
3773 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3774 fcgi_do_shutr(fstrm);
3775
3776 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3777 /* We're done trying to send, remove ourself from the send_list */
3778 LIST_DEL_INIT(&fstrm->send_list);
3779
3780 if (!fstrm->cs) {
3781 fcgi_strm_destroy(fstrm);
3782 if (fcgi_conn_is_dead(fconn))
3783 fcgi_release(fconn);
3784 }
3785 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003786 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003787 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 return NULL;
3789}
3790
3791/* shutr() called by the conn_stream (mux_ops.shutr) */
3792static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3793{
3794 struct fcgi_strm *fstrm = cs->ctx;
3795
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003796 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 if (cs->flags & CS_FL_KILL_CONN)
3798 fstrm->flags |= FCGI_SF_KILL_CONN;
3799
3800 if (!mode)
3801 return;
3802
3803 fcgi_do_shutr(fstrm);
3804}
3805
3806/* shutw() called by the conn_stream (mux_ops.shutw) */
3807static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3808{
3809 struct fcgi_strm *fstrm = cs->ctx;
3810
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003811 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003812 if (cs->flags & CS_FL_KILL_CONN)
3813 fstrm->flags |= FCGI_SF_KILL_CONN;
3814
3815 fcgi_do_shutw(fstrm);
3816}
3817
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003818/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3819 * event subscriber <es> is not allowed to change from a previous call as long
3820 * as at least one event is still subscribed. The <event_type> must only be a
3821 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003822 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003823static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003824{
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 struct fcgi_strm *fstrm = cs->ctx;
3826 struct fcgi_conn *fconn = fstrm->fconn;
3827
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003828 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003829 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003830
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003831 es->events |= event_type;
3832 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003833
3834 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003835 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003836
Christopher Faulet99eff652019-08-11 23:11:30 +02003837 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003838 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003839 if (!LIST_ADDED(&fstrm->send_list))
3840 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003841 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003842 return 0;
3843}
3844
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003845/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3846 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3847 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003848 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003849static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003850{
Christopher Faulet99eff652019-08-11 23:11:30 +02003851 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003852 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003853
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003854 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003855 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003856
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003857 es->events &= ~event_type;
3858 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003859 fstrm->subs = NULL;
3860
3861 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003862 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003863
Christopher Faulet99eff652019-08-11 23:11:30 +02003864 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003865 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003866 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003867 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3868 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003869 }
3870 return 0;
3871}
3872
3873/* Called from the upper layer, to receive data */
3874static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3875{
3876 struct fcgi_strm *fstrm = cs->ctx;
3877 struct fcgi_conn *fconn = fstrm->fconn;
3878 size_t ret = 0;
3879
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003880 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3881
Christopher Faulet99eff652019-08-11 23:11:30 +02003882 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3883 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003884 else
3885 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003886
Christopher Faulet76014fd2019-12-10 11:47:22 +01003887 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 +02003888 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3889 else {
3890 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003891 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 cs->flags |= CS_FL_EOI;
3893 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3894 cs->flags |= CS_FL_EOS;
3895 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003896 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003897 cs->flags |= CS_FL_EOS;
3898 if (cs->flags & CS_FL_ERR_PENDING)
3899 cs->flags |= CS_FL_ERROR;
3900 fcgi_release_buf(fconn, &fstrm->rxbuf);
3901 }
3902
3903 if (ret && fconn->dsi == fstrm->id) {
3904 /* demux is blocking on this stream's buffer */
3905 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3906 fcgi_conn_restart_reading(fconn, 1);
3907 }
3908
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003909 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003910 return ret;
3911}
3912
3913
Christopher Faulet99eff652019-08-11 23:11:30 +02003914/* Called from the upper layer, to send data from buffer <buf> for no more than
3915 * <count> bytes. Returns the number of bytes effectively sent. Some status
3916 * flags may be updated on the conn_stream.
3917 */
3918static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3919{
3920 struct fcgi_strm *fstrm = cs->ctx;
3921 struct fcgi_conn *fconn = fstrm->fconn;
3922 size_t total = 0;
3923 size_t ret;
3924 struct htx *htx = NULL;
3925 struct htx_sl *sl;
3926 struct htx_blk *blk;
3927 uint32_t bsize;
3928
Willy Tarreau022e5e52020-09-10 09:33:15 +02003929 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003930
Christopher Faulet99eff652019-08-11 23:11:30 +02003931 /* If we were not just woken because we wanted to send but couldn't,
3932 * and there's somebody else that is waiting to send, do nothing,
3933 * we will subscribe later and be put at the end of the list
3934 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003935 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003936 TRACE_STATE("other streams already waiting, going to the queue and leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003937 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003938 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003939 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003940
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003941 if (fconn->state < FCGI_CS_RECORD_H) {
3942 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003943 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003944 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003945
3946 htx = htxbuf(buf);
3947 if (fstrm->id == 0) {
3948 int32_t id = fcgi_conn_get_next_sid(fconn);
3949
3950 if (id < 0) {
3951 fcgi_strm_close(fstrm);
3952 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953 TRACE_DEVEL("couldn't get a stream ID, leaving in error", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_ERR|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003954 return 0;
3955 }
3956
3957 eb32_delete(&fstrm->by_id);
3958 fstrm->by_id.key = fstrm->id = id;
3959 fconn->max_id = id;
3960 fconn->nb_reserved--;
3961 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3962
3963
3964 /* Check if length of the body is known or if the message is
3965 * full. Otherwise, the request is invalid.
3966 */
3967 sl = http_get_stline(htx);
3968 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3969 htx->flags |= HTX_FL_PARSING_ERROR;
3970 fcgi_strm_error(fstrm);
3971 goto done;
3972 }
3973 }
3974
3975 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 TRACE_PROTO("sending FCGI BEGIN_REQUEST record", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003977 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3978 goto done;
3979 }
3980
3981 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3982 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3983
Christopher Fauletfe410d62020-05-19 15:13:00 +02003984 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003985 count && !htx_is_empty(htx)) {
3986 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003987 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 bsize = htx_get_blksz(blk);
3989
3990 switch (htx_get_blk_type(blk)) {
3991 case HTX_BLK_REQ_SL:
3992 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993 TRACE_USER("sending FCGI PARAMS record", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003994 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3995 if (!ret) {
3996 goto done;
3997 }
3998 total += ret;
3999 count -= ret;
4000 break;
4001
4002 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004003 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 +02004004 ret = fcgi_strm_send_empty_params(fconn, fstrm);
4005 if (!ret)
4006 goto done;
4007 goto remove_blk;
4008
4009 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004010 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 +02004011 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
4012 if (ret > 0) {
4013 htx = htx_from_buf(buf);
4014 total += ret;
4015 count -= ret;
4016 if (ret < bsize)
4017 goto done;
4018 }
4019 break;
4020
4021 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004022 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 +02004023 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4024 if (!ret)
4025 goto done;
4026 goto remove_blk;
4027
4028 default:
4029 remove_blk:
4030 htx_remove_blk(htx, blk);
4031 total += bsize;
4032 count -= bsize;
4033 break;
4034 }
4035 }
4036
4037 done:
4038 if (fstrm->state >= FCGI_SS_HLOC) {
4039 /* trim any possibly pending data after we close (extra CR-LF,
4040 * unprocessed trailers, abnormal extra data, ...)
4041 */
4042 total += count;
4043 count = 0;
4044 }
4045
4046 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004047 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 +02004048 cs_set_error(cs);
4049 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4050 fcgi_strm_close(fstrm);
4051 }
4052
4053 if (htx)
4054 htx_to_buf(htx, buf);
4055
Christopher Faulet99eff652019-08-11 23:11:30 +02004056 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004057 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4058 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 +02004059 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004060 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004061
4062 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004063 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4064 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004065 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004066
4067 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004068 return total;
4069}
4070
4071/* for debugging with CLI's "show fd" command */
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004072static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004073{
4074 struct fcgi_conn *fconn = conn->ctx;
4075 struct fcgi_strm *fstrm = NULL;
4076 struct eb32_node *node;
4077 int send_cnt = 0;
4078 int tree_cnt = 0;
4079 int orph_cnt = 0;
4080 struct buffer *hmbuf, *tmbuf;
4081
4082 if (!fconn)
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004083 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004084
4085 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4086 send_cnt++;
4087
4088 fstrm = NULL;
4089 node = eb32_first(&fconn->streams_by_id);
4090 while (node) {
4091 fstrm = container_of(node, struct fcgi_strm, by_id);
4092 tree_cnt++;
4093 if (!fstrm->cs)
4094 orph_cnt++;
4095 node = eb32_next(node);
4096 }
4097
4098 hmbuf = br_head(fconn->mbuf);
4099 tmbuf = br_tail(fconn->mbuf);
4100 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4101 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4102 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4103 fconn->state, fconn->max_id, fconn->flags,
4104 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4105 fconn->wait_event.events, fconn->dsi,
4106 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4107 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4108 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4109 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4110 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4111 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4112 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4113
4114 if (fstrm) {
4115 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4116 fstrm, fstrm->id, fstrm->flags,
4117 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4118 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4119 fstrm->cs);
4120 if (fstrm->cs)
4121 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4122 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau5a090d62021-01-20 17:10:46 +01004123 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4124 if (fstrm->subs) {
Christopher Faulet70da1072021-02-25 10:06:29 +01004125 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4126 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4127 fstrm->subs->tasklet->calls,
4128 fstrm->subs->tasklet->context);
4129 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4130 chunk_appendf(&trash, ")");
Willy Tarreau5a090d62021-01-20 17:10:46 +01004131 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004132 }
Willy Tarreau6cb467e2021-01-21 08:26:06 +01004133 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004134}
4135
4136/* Migrate the the connection to the current thread.
4137 * Return 0 if successful, non-zero otherwise.
4138 * Expected to be called with the old thread lock held.
4139 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004140static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004141{
4142 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004143 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004144
4145 if (fd_takeover(conn->handle.fd, conn) != 0)
4146 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004147
4148 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4149 /* We failed to takeover the xprt, even if the connection may
4150 * still be valid, flag it as error'd, as we have already
4151 * taken over the fd, and wake the tasklet, so that it will
4152 * destroy it.
4153 */
4154 conn->flags |= CO_FL_ERROR;
4155 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4156 return -1;
4157 }
4158
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004159 if (fcgi->wait_event.events)
4160 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4161 fcgi->wait_event.events, &fcgi->wait_event);
4162 /* To let the tasklet know it should free itself, and do nothing else,
4163 * set its context to NULL;
4164 */
4165 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004166 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004167
4168 task = fcgi->task;
4169 if (task) {
4170 task->context = NULL;
4171 fcgi->task = NULL;
4172 __ha_barrier_store();
4173 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004174
4175 fcgi->task = task_new(tid_bit);
4176 if (!fcgi->task) {
4177 fcgi_release(fcgi);
4178 return -1;
4179 }
4180 fcgi->task->process = fcgi_timeout_task;
4181 fcgi->task->context = fcgi;
4182 }
4183 fcgi->wait_event.tasklet = tasklet_new();
4184 if (!fcgi->wait_event.tasklet) {
4185 fcgi_release(fcgi);
4186 return -1;
4187 }
4188 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4189 fcgi->wait_event.tasklet->context = fcgi;
4190 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4191 SUB_RETRY_RECV, &fcgi->wait_event);
4192
4193 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004194}
4195
4196/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004197/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004198/****************************************/
4199
4200/* The mux operations */
4201static const struct mux_ops mux_fcgi_ops = {
4202 .init = fcgi_init,
4203 .wake = fcgi_wake,
4204 .attach = fcgi_attach,
4205 .get_first_cs = fcgi_get_first_cs,
4206 .detach = fcgi_detach,
4207 .destroy = fcgi_destroy,
4208 .avail_streams = fcgi_avail_streams,
4209 .used_streams = fcgi_used_streams,
4210 .rcv_buf = fcgi_rcv_buf,
4211 .snd_buf = fcgi_snd_buf,
4212 .subscribe = fcgi_subscribe,
4213 .unsubscribe = fcgi_unsubscribe,
4214 .shutr = fcgi_shutr,
4215 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004216 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004217 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004218 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004219 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004220 .name = "FCGI",
4221};
4222
4223
4224/* this mux registers FCGI proto */
4225static struct mux_proto_list mux_proto_fcgi =
4226{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4227
4228INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4229
4230/*
4231 * Local variables:
4232 * c-indent-level: 8
4233 * c-basic-offset: 8
4234 * End:
4235 */