blob: 765cff3b8800190a5804b1ce3e6f23cbb7c2a928 [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 Tarreau691d5032021-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 Tarreau21046592020-02-26 10:39:36 +0100605 if (likely(!MT_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 Tarreau86891272020-07-10 08:22:26 +0200609 MT_LIST_ADDQ(&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);
618 offer_buffers(NULL, tasks_run_queue);
619 }
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)
632 offer_buffers(NULL, tasks_run_queue);
633}
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 Tarreau21046592020-02-26 10:39:36 +0100761 MT_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 Tarreau21046592020-02-26 10:39:36 +0100840 if (MT_LIST_ADDED(&fconn->buf_wait.list))
841 MT_LIST_DEL(&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);
1022 offer_buffers(NULL, tasks_run_queue);
1023 }
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;
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001151 TRACE_STATE("switching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001152 }
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)
2865 offer_buffers(NULL, tasks_run_queue);
2866
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 Tarreau691d5032021-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;
2933 struct fcgi_conn *fconn;
2934 struct tasklet *tl = (struct tasklet *)t;
2935 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002936 int ret = 0;
2937
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002938
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002939 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002940 if (tl->context == NULL) {
2941 /* The connection has been taken over by another thread,
2942 * we're no longer responsible for it, so just free the
2943 * tasklet, and do nothing.
2944 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002945 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002946 tasklet_free(tl);
2947 return NULL;
2948
2949 }
2950 fconn = ctx;
2951 conn = fconn->conn;
2952
2953 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2954
2955 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2956 if (conn_in_list)
2957 MT_LIST_DEL(&conn->list);
2958
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002959 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002960
Christopher Faulet99eff652019-08-11 23:11:30 +02002961 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2962 ret = fcgi_send(fconn);
2963 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2964 ret |= fcgi_recv(fconn);
2965 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002966 ret = fcgi_process(fconn);
2967
2968 /* If we were in an idle list, we want to add it back into it,
2969 * unless fcgi_process() returned -1, which mean it has destroyed
2970 * the connection (testing !ret is enough, if fcgi_process() wasn't
2971 * called then ret will be 0 anyway.
2972 */
2973 if (!ret && conn_in_list) {
2974 struct server *srv = objt_server(conn->target);
2975
2976 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002977 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002978 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002979 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002980 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002981 return NULL;
2982}
2983
2984/* callback called on any event by the connection handler.
2985 * It applies changes and returns zero, or < 0 if it wants immediate
2986 * destruction of the connection (which normally doesn not happen in FCGI).
2987 */
2988static int fcgi_process(struct fcgi_conn *fconn)
2989{
2990 struct connection *conn = fconn->conn;
2991
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002992 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2993
Christopher Faulet99eff652019-08-11 23:11:30 +02002994 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2995 fcgi_process_demux(fconn);
2996
2997 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2998 b_reset(&fconn->dbuf);
2999
3000 if (buf_room_for_htx_data(&fconn->dbuf))
3001 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3002 }
3003 fcgi_send(fconn);
3004
Willy Tarreauc3914d42020-09-24 08:39:22 +02003005 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003006 /* frontend is stopping, reload likely in progress, let's try
3007 * to announce a graceful shutdown if not yet done. We don't
3008 * care if it fails, it will be tried again later.
3009 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003010 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 +02003011 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3012 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3013 fcgi_conn_send_aborts(fconn);
3014 }
3015 }
3016
3017 /*
3018 * If we received early data, and the handshake is done, wake
3019 * any stream that was waiting for it.
3020 */
3021 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003022 (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 +02003023 struct eb32_node *node;
3024 struct fcgi_strm *fstrm;
3025
3026 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3027 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3028
3029 while (node) {
3030 fstrm = container_of(node, struct fcgi_strm, by_id);
3031 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3032 fcgi_strm_notify_recv(fstrm);
3033 node = eb32_next(node);
3034 }
3035 }
3036
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003037 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003038 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3039 eb_is_empty(&fconn->streams_by_id)) {
3040 fcgi_wake_some_streams(fconn, 0);
3041
3042 if (eb_is_empty(&fconn->streams_by_id)) {
3043 /* no more stream, kill the connection now */
3044 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003045 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003046 return -1;
3047 }
3048 }
3049
3050 if (!b_data(&fconn->dbuf))
3051 fcgi_release_buf(fconn, &fconn->dbuf);
3052
3053 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3054 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3055 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3056 fcgi_release_mbuf(fconn);
3057
3058 if (fconn->task) {
3059 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3060 task_queue(fconn->task);
3061 }
3062
3063 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003064 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003065 return 0;
3066}
3067
3068
3069/* wake-up function called by the connection layer (mux_ops.wake) */
3070static int fcgi_wake(struct connection *conn)
3071{
3072 struct fcgi_conn *fconn = conn->ctx;
3073
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003074 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003075 return (fcgi_process(fconn));
3076}
3077
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003078
3079static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3080{
3081 int ret = 0;
3082 switch (mux_ctl) {
3083 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003084 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003085 ret |= MUX_STATUS_READY;
3086 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003087 case MUX_EXIT_STATUS:
3088 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003089 default:
3090 return -1;
3091 }
3092}
3093
Christopher Faulet99eff652019-08-11 23:11:30 +02003094/* Connection timeout management. The principle is that if there's no receipt
3095 * nor sending for a certain amount of time, the connection is closed. If the
3096 * MUX buffer still has lying data or is not allocatable, the connection is
3097 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003098 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003099 */
3100static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3101{
3102 struct fcgi_conn *fconn = context;
3103 int expired = tick_is_expired(t->expire, now_ms);
3104
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003105 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3106
Willy Tarreau60814ff2020-06-30 11:19:23 +02003107 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003108 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3109
3110 /* Somebody already stole the connection from us, so we should not
3111 * free it, we just have to free the task.
3112 */
3113 if (!t->context) {
3114 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3115 fconn = NULL;
3116 goto do_leave;
3117 }
3118
Willy Tarreau60814ff2020-06-30 11:19:23 +02003119 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003120 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003121 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3122 return t;
3123 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003124
Willy Tarreau60814ff2020-06-30 11:19:23 +02003125 /* We're about to destroy the connection, so make sure nobody attempts
3126 * to steal it from us.
3127 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003128 if (fconn->conn->flags & CO_FL_LIST_MASK)
3129 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003130
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003131 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003132 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003133
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003134do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003135 task_destroy(t);
3136
3137 if (!fconn) {
3138 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003139 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003140 return NULL;
3141 }
3142
3143 fconn->task = NULL;
3144 fconn->state = FCGI_CS_CLOSED;
3145 fcgi_wake_some_streams(fconn, 0);
3146
3147 if (br_data(fconn->mbuf)) {
3148 /* don't even try to send aborts, the buffer is stuck */
3149 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3150 goto end;
3151 }
3152
3153 /* try to send but no need to insist */
3154 if (!fcgi_conn_send_aborts(fconn))
3155 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3156
3157 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3158 conn_xprt_ready(fconn->conn)) {
3159 unsigned int released = 0;
3160 struct buffer *buf;
3161
3162 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3163 if (b_data(buf)) {
3164 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3165 buf, b_data(buf), 0);
3166 if (!ret)
3167 break;
3168 b_del(buf, ret);
3169 if (b_data(buf))
3170 break;
3171 b_free(buf);
3172 released++;
3173 }
3174 }
3175
3176 if (released)
3177 offer_buffers(NULL, tasks_run_queue);
3178 }
3179
3180 end:
3181 /* either we can release everything now or it will be done later once
3182 * the last stream closes.
3183 */
3184 if (eb_is_empty(&fconn->streams_by_id))
3185 fcgi_release(fconn);
3186
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003187 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003188 return NULL;
3189}
3190
3191
3192/*******************************************/
3193/* functions below are used by the streams */
3194/*******************************************/
3195
3196/* Append the description of what is present in error snapshot <es> into <out>.
3197 * The description must be small enough to always fit in a buffer. The output
3198 * buffer may be the trash so the trash must not be used inside this function.
3199 */
3200static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3201{
3202 chunk_appendf(out,
3203 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3204 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3205 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3206 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3207 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3208 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3209}
3210/*
3211 * Capture a bad response and archive it in the proxy's structure. By default
3212 * it tries to report the error position as h1m->err_pos. However if this one is
3213 * not set, it will then report h1m->next, which is the last known parsing
3214 * point. The function is able to deal with wrapping buffers. It always displays
3215 * buffers as a contiguous area starting at buf->p. The direction is determined
3216 * thanks to the h1m's flags.
3217 */
3218static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3219 struct h1m *h1m, struct buffer *buf)
3220{
3221 struct session *sess = fstrm->sess;
3222 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003223 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003224 union error_snapshot_ctx ctx;
3225
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003226 if (fstrm->cs && fstrm->cs->data) {
3227 if (sess == NULL)
3228 sess = si_strm(fstrm->cs->data)->sess;
3229 if (!(h1m->flags & H1_MF_RESP))
3230 other_end = si_strm(fstrm->cs->data)->be;
3231 else
3232 other_end = sess->fe;
3233 } else
3234 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003235 /* http-specific part now */
3236 ctx.h1.state = h1m->state;
3237 ctx.h1.c_flags = fconn->flags;
3238 ctx.h1.s_flags = fstrm->flags;
3239 ctx.h1.m_flags = h1m->flags;
3240 ctx.h1.m_clen = h1m->curr_len;
3241 ctx.h1.m_blen = h1m->body_len;
3242
3243 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3244 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3245 &ctx, fcgi_show_error_snapshot);
3246}
3247
3248static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3249 struct buffer *buf, size_t *ofs, size_t max)
3250{
3251 int ret;
3252
Willy Tarreau022e5e52020-09-10 09:33:15 +02003253 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 +02003254 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3255 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003256 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 +02003257 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003258 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 +02003259 fcgi_strm_error(fstrm);
3260 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3261 }
3262 goto end;
3263 }
3264
3265 *ofs += ret;
3266 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003267 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 +02003268 return ret;
3269
3270}
3271
Christopher Fauletaf542632019-10-01 21:52:49 +02003272static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003273 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3274{
3275 int ret;
3276
Willy Tarreau022e5e52020-09-10 09:33:15 +02003277 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 +02003278 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003279 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003280 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003281 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003282 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003283 fcgi_strm_error(fstrm);
3284 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3285 }
3286 goto end;
3287 }
3288 *ofs += ret;
3289 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003290 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 +02003291 return ret;
3292}
3293
3294static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3295 struct buffer *buf, size_t *ofs, size_t max)
3296{
3297 int ret;
3298
Willy Tarreau022e5e52020-09-10 09:33:15 +02003299 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 +02003300 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003301 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003302 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003303 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003304 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003305 fcgi_strm_error(fstrm);
3306 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3307 }
3308 goto end;
3309 }
3310 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003311 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003312 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 +02003313 return ret;
3314}
3315
3316static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003317 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003318{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003319 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003320
Willy Tarreau022e5e52020-09-10 09:33:15 +02003321 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 +01003322 ret = h1_parse_msg_eom(h1m, htx, max);
3323 if (!ret) {
3324 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3325 if (htx->flags & HTX_FL_PARSING_ERROR) {
3326 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3327 fcgi_strm_error(fstrm);
3328 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3329 }
3330 goto end;
3331 }
3332 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3333 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003334 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 +01003335 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003336}
3337
3338static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3339{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003340 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003341 struct htx *htx;
3342 struct h1m *h1m = &fstrm->h1m;
3343 size_t ret, data, total = 0;
3344
3345 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003346 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3347
Christopher Faulet99eff652019-08-11 23:11:30 +02003348 data = htx->data;
3349 if (fstrm->state == FCGI_SS_ERROR)
3350 goto end;
3351
3352 do {
3353 size_t used = htx_used_space(htx);
3354
3355 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003356 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003357 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3358 if (!ret)
3359 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003360
3361 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3362
Christopher Faulet99eff652019-08-11 23:11:30 +02003363 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3364 struct htx_blk *blk = htx_get_head_blk(htx);
3365 struct htx_sl *sl;
3366
3367 if (!blk)
3368 break;
3369 sl = htx_get_blk_ptr(htx, blk);
3370 sl->flags |= HTX_SL_F_XFER_LEN;
3371 htx->extra = 0;
3372 }
3373 }
3374 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003375 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003376 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003377 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003378 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003379
3380 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 +02003381 }
3382 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003383 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3384 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3385 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003386 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003387
Christopher Faulet76014fd2019-12-10 11:47:22 +01003388 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 +02003389 }
3390 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003391 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3392 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3393 break;
3394
3395 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3396 }
3397
Christopher Faulet99eff652019-08-11 23:11:30 +02003398 if (b_data(&fstrm->rxbuf) > total) {
3399 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003400 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003401 fcgi_strm_error(fstrm);
3402 }
3403 break;
3404 }
3405 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003406 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003407 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003408
Christopher Faulet99eff652019-08-11 23:11:30 +02003409 if (fstrm->state != FCGI_SS_ERROR &&
3410 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003411 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003412 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3413 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3414 h1m->state = H1_MSG_DONE;
3415 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 +02003416 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003417 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003418 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003419
3420 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003421 }
3422 else {
3423 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003424 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003425 fcgi_strm_error(fstrm);
3426 break;
3427 }
3428
3429 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003430 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003431
3432 if (fstrm->state == FCGI_SS_ERROR) {
3433 b_reset(&fstrm->rxbuf);
3434 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003436 return 0;
3437 }
3438
3439 b_del(&fstrm->rxbuf, total);
3440
3441 end:
3442 htx_to_buf(htx, buf);
3443 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003444 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003445 return ret;
3446}
3447
3448/*
3449 * Attach a new stream to a connection
3450 * (Used for outgoing connections)
3451 */
3452static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3453{
3454 struct conn_stream *cs;
3455 struct fcgi_strm *fstrm;
3456 struct fcgi_conn *fconn = conn->ctx;
3457
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003458 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003459 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003460 if (!cs) {
3461 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003462 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003463 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003464 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3465 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003466 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003467 cs_free(cs);
3468 return NULL;
3469 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003470 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003471 return cs;
3472}
3473
3474/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3475 * We have to scan because we may have some orphan streams. It might be
3476 * beneficial to scan backwards from the end to reduce the likeliness to find
3477 * orphans.
3478 */
3479static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3480{
3481 struct fcgi_conn *fconn = conn->ctx;
3482 struct fcgi_strm *fstrm;
3483 struct eb32_node *node;
3484
3485 node = eb32_first(&fconn->streams_by_id);
3486 while (node) {
3487 fstrm = container_of(node, struct fcgi_strm, by_id);
3488 if (fstrm->cs)
3489 return fstrm->cs;
3490 node = eb32_next(node);
3491 }
3492 return NULL;
3493}
3494
3495/*
3496 * Destroy the mux and the associated connection, if it is no longer used
3497 */
3498static void fcgi_destroy(void *ctx)
3499{
3500 struct fcgi_conn *fconn = ctx;
3501
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003502 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003503 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3504 fcgi_release(fconn);
3505}
3506
3507/*
3508 * Detach the stream from the connection and possibly release the connection.
3509 */
3510static void fcgi_detach(struct conn_stream *cs)
3511{
3512 struct fcgi_strm *fstrm = cs->ctx;
3513 struct fcgi_conn *fconn;
3514 struct session *sess;
3515
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003516 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3517
Christopher Faulet99eff652019-08-11 23:11:30 +02003518 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003519 if (!fstrm) {
3520 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003521 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003522 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003523
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003524 /* there's no txbuf so we're certain no to be able to send anything */
3525 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003526
3527 sess = fstrm->sess;
3528 fconn = fstrm->fconn;
3529 fstrm->cs = NULL;
3530 fconn->nb_cs--;
3531
3532 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3533 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3534 fconn->streams_limit = 1;
3535 }
3536 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3537 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3538 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3539 fconn->state = FCGI_CS_CLOSED;
3540 }
3541
3542 /* this stream may be blocked waiting for some data to leave, so orphan
3543 * it in this case.
3544 */
3545 if (!(cs->conn->flags & CO_FL_ERROR) &&
3546 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003547 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003548 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003549 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003550 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003551 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003552
3553 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3554 /* unblock the connection if it was blocked on this stream. */
3555 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3556 fcgi_conn_restart_reading(fconn, 1);
3557 }
3558
3559 fcgi_strm_destroy(fstrm);
3560
3561 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003562 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003563 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003564 /* Add the connection in the session serverlist, if not already done */
3565 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3566 fconn->conn->owner = NULL;
3567 if (eb_is_empty(&fconn->streams_by_id)) {
3568 /* let's kill the connection right away */
3569 fconn->conn->mux->destroy(fconn);
3570 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3571 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003572 }
3573 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003574 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003575 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3576 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003577 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003578 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003579 }
3580 }
3581 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003582 else {
3583 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003584 /* If the connection is owned by the session, first remove it
3585 * from its list
3586 */
3587 if (fconn->conn->owner) {
3588 session_unown_conn(fconn->conn->owner, fconn->conn);
3589 fconn->conn->owner = NULL;
3590 }
3591
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003592 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003593 /* The server doesn't want it, let's kill the connection right away */
3594 fconn->conn->mux->destroy(fconn);
3595 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3596 return;
3597 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003598 /* At this point, the connection has been added to the
3599 * server idle list, so another thread may already have
3600 * hijacked it, so we can't do anything with it.
3601 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003602 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3603 return;
3604 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003605 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003606 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3607 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003608 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003609 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003610 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003611 }
3612
3613 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003614 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003615 */
3616 if (fcgi_conn_is_dead(fconn)) {
3617 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003618 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003619 fcgi_release(fconn);
3620 }
3621 else if (fconn->task) {
3622 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3623 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003624 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003625 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003626 else
3627 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003628}
3629
3630
3631/* Performs a synchronous or asynchronous shutr(). */
3632static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3633{
3634 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003635
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003636 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3637
Christopher Faulet99eff652019-08-11 23:11:30 +02003638 if (fstrm->state == FCGI_SS_CLOSED)
3639 goto done;
3640
3641 /* a connstream may require us to immediately kill the whole connection
3642 * for example because of a "tcp-request content reject" rule that is
3643 * normally used to limit abuse.
3644 */
3645 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003646 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3647 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003648 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003649 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003650 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003651 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3653 !fcgi_strm_send_abort(fconn, fstrm))
3654 goto add_to_list;
3655 }
3656
3657 fcgi_strm_close(fstrm);
3658
3659 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3660 tasklet_wakeup(fconn->wait_event.tasklet);
3661 done:
3662 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003663 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003664 return;
3665
3666 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003667 /* Let the handler know we want to shutr, and add ourselves to the
3668 * send list if not yet done. fcgi_deferred_shut() will be
3669 * automatically called via the shut_tl tasklet when there's room
3670 * again.
3671 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003672 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003673 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003674 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3675 }
3676 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003678 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003679 return;
3680}
3681
3682/* Performs a synchronous or asynchronous shutw(). */
3683static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3684{
3685 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003686
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003687 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3688
Christopher Faulet99eff652019-08-11 23:11:30 +02003689 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3690 goto done;
3691
3692 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3693 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3694 !fcgi_strm_send_abort(fconn, fstrm))
3695 goto add_to_list;
3696
3697 if (fstrm->state == FCGI_SS_HREM)
3698 fcgi_strm_close(fstrm);
3699 else
3700 fstrm->state = FCGI_SS_HLOC;
3701 } else {
3702 /* a connstream may require us to immediately kill the whole connection
3703 * for example because of a "tcp-request content reject" rule that is
3704 * normally used to limit abuse.
3705 */
3706 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003707 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3708 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003709 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003710 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003711
3712 fcgi_strm_close(fstrm);
3713 }
3714
3715 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3716 tasklet_wakeup(fconn->wait_event.tasklet);
3717 done:
3718 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003719 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003720 return;
3721
3722 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003723 /* Let the handler know we want to shutr, and add ourselves to the
3724 * send list if not yet done. fcgi_deferred_shut() will be
3725 * automatically called via the shut_tl tasklet when there's room
3726 * again.
3727 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003728 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003729 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003730 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3731 }
3732 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003733 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003734 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003735 return;
3736}
3737
Willy Tarreau7aad7032020-01-16 17:20:57 +01003738/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003739 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003740 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003741 */
3742static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3743{
3744 struct fcgi_strm *fstrm = ctx;
3745 struct fcgi_conn *fconn = fstrm->fconn;
3746
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003747 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3748
Willy Tarreau7aad7032020-01-16 17:20:57 +01003749 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3750 /* some data processing remains to be done first */
3751 goto end;
3752 }
3753
Christopher Faulet99eff652019-08-11 23:11:30 +02003754 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3755 fcgi_do_shutw(fstrm);
3756
3757 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3758 fcgi_do_shutr(fstrm);
3759
3760 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3761 /* We're done trying to send, remove ourself from the send_list */
3762 LIST_DEL_INIT(&fstrm->send_list);
3763
3764 if (!fstrm->cs) {
3765 fcgi_strm_destroy(fstrm);
3766 if (fcgi_conn_is_dead(fconn))
3767 fcgi_release(fconn);
3768 }
3769 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003770 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003771 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003772 return NULL;
3773}
3774
3775/* shutr() called by the conn_stream (mux_ops.shutr) */
3776static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3777{
3778 struct fcgi_strm *fstrm = cs->ctx;
3779
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003780 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003781 if (cs->flags & CS_FL_KILL_CONN)
3782 fstrm->flags |= FCGI_SF_KILL_CONN;
3783
3784 if (!mode)
3785 return;
3786
3787 fcgi_do_shutr(fstrm);
3788}
3789
3790/* shutw() called by the conn_stream (mux_ops.shutw) */
3791static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3792{
3793 struct fcgi_strm *fstrm = cs->ctx;
3794
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003795 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003796 if (cs->flags & CS_FL_KILL_CONN)
3797 fstrm->flags |= FCGI_SF_KILL_CONN;
3798
3799 fcgi_do_shutw(fstrm);
3800}
3801
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003802/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3803 * event subscriber <es> is not allowed to change from a previous call as long
3804 * as at least one event is still subscribed. The <event_type> must only be a
3805 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003806 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003807static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003808{
Christopher Faulet99eff652019-08-11 23:11:30 +02003809 struct fcgi_strm *fstrm = cs->ctx;
3810 struct fcgi_conn *fconn = fstrm->fconn;
3811
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003812 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003813 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003814
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003815 es->events |= event_type;
3816 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003817
3818 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003819 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003820
Christopher Faulet99eff652019-08-11 23:11:30 +02003821 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003822 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003823 if (!LIST_ADDED(&fstrm->send_list))
3824 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003826 return 0;
3827}
3828
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003829/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3830 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3831 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003832 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003833static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003834{
Christopher Faulet99eff652019-08-11 23:11:30 +02003835 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003836 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003837
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003838 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003839 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003840
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003841 es->events &= ~event_type;
3842 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003843 fstrm->subs = NULL;
3844
3845 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003846 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003847
Christopher Faulet99eff652019-08-11 23:11:30 +02003848 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003849 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003850 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003851 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3852 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003853 }
3854 return 0;
3855}
3856
3857/* Called from the upper layer, to receive data */
3858static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3859{
3860 struct fcgi_strm *fstrm = cs->ctx;
3861 struct fcgi_conn *fconn = fstrm->fconn;
3862 size_t ret = 0;
3863
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003864 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3865
Christopher Faulet99eff652019-08-11 23:11:30 +02003866 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3867 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003868 else
3869 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003870
Christopher Faulet76014fd2019-12-10 11:47:22 +01003871 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 +02003872 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3873 else {
3874 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003875 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003876 cs->flags |= CS_FL_EOI;
3877 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3878 cs->flags |= CS_FL_EOS;
3879 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003880 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003881 cs->flags |= CS_FL_EOS;
3882 if (cs->flags & CS_FL_ERR_PENDING)
3883 cs->flags |= CS_FL_ERROR;
3884 fcgi_release_buf(fconn, &fstrm->rxbuf);
3885 }
3886
3887 if (ret && fconn->dsi == fstrm->id) {
3888 /* demux is blocking on this stream's buffer */
3889 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3890 fcgi_conn_restart_reading(fconn, 1);
3891 }
3892
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003893 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003894 return ret;
3895}
3896
3897
Christopher Faulet99eff652019-08-11 23:11:30 +02003898/* Called from the upper layer, to send data from buffer <buf> for no more than
3899 * <count> bytes. Returns the number of bytes effectively sent. Some status
3900 * flags may be updated on the conn_stream.
3901 */
3902static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3903{
3904 struct fcgi_strm *fstrm = cs->ctx;
3905 struct fcgi_conn *fconn = fstrm->fconn;
3906 size_t total = 0;
3907 size_t ret;
3908 struct htx *htx = NULL;
3909 struct htx_sl *sl;
3910 struct htx_blk *blk;
3911 uint32_t bsize;
3912
Willy Tarreau022e5e52020-09-10 09:33:15 +02003913 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003914
Christopher Faulet99eff652019-08-11 23:11:30 +02003915 /* If we were not just woken because we wanted to send but couldn't,
3916 * and there's somebody else that is waiting to send, do nothing,
3917 * we will subscribe later and be put at the end of the list
3918 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003919 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003920 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 +02003921 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003922 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003923 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003924
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003925 if (fconn->state < FCGI_CS_RECORD_H) {
3926 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003927 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003928 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003929
3930 htx = htxbuf(buf);
3931 if (fstrm->id == 0) {
3932 int32_t id = fcgi_conn_get_next_sid(fconn);
3933
3934 if (id < 0) {
3935 fcgi_strm_close(fstrm);
3936 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003937 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 +02003938 return 0;
3939 }
3940
3941 eb32_delete(&fstrm->by_id);
3942 fstrm->by_id.key = fstrm->id = id;
3943 fconn->max_id = id;
3944 fconn->nb_reserved--;
3945 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3946
3947
3948 /* Check if length of the body is known or if the message is
3949 * full. Otherwise, the request is invalid.
3950 */
3951 sl = http_get_stline(htx);
3952 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3953 htx->flags |= HTX_FL_PARSING_ERROR;
3954 fcgi_strm_error(fstrm);
3955 goto done;
3956 }
3957 }
3958
3959 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003960 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 +02003961 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3962 goto done;
3963 }
3964
3965 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3966 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3967
Christopher Fauletfe410d62020-05-19 15:13:00 +02003968 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003969 count && !htx_is_empty(htx)) {
3970 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003971 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003972 bsize = htx_get_blksz(blk);
3973
3974 switch (htx_get_blk_type(blk)) {
3975 case HTX_BLK_REQ_SL:
3976 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003977 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 +02003978 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3979 if (!ret) {
3980 goto done;
3981 }
3982 total += ret;
3983 count -= ret;
3984 break;
3985
3986 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003987 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 +02003988 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3989 if (!ret)
3990 goto done;
3991 goto remove_blk;
3992
3993 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003994 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 +02003995 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3996 if (ret > 0) {
3997 htx = htx_from_buf(buf);
3998 total += ret;
3999 count -= ret;
4000 if (ret < bsize)
4001 goto done;
4002 }
4003 break;
4004
4005 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004006 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 +02004007 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4008 if (!ret)
4009 goto done;
4010 goto remove_blk;
4011
4012 default:
4013 remove_blk:
4014 htx_remove_blk(htx, blk);
4015 total += bsize;
4016 count -= bsize;
4017 break;
4018 }
4019 }
4020
4021 done:
4022 if (fstrm->state >= FCGI_SS_HLOC) {
4023 /* trim any possibly pending data after we close (extra CR-LF,
4024 * unprocessed trailers, abnormal extra data, ...)
4025 */
4026 total += count;
4027 count = 0;
4028 }
4029
4030 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004031 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 +02004032 cs_set_error(cs);
4033 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4034 fcgi_strm_close(fstrm);
4035 }
4036
4037 if (htx)
4038 htx_to_buf(htx, buf);
4039
Christopher Faulet99eff652019-08-11 23:11:30 +02004040 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004041 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4042 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 +02004043 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004044 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004045
4046 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004047 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4048 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004049 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004050
4051 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004052 return total;
4053}
4054
4055/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01004056static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
Christopher Faulet99eff652019-08-11 23:11:30 +02004057{
4058 struct fcgi_conn *fconn = conn->ctx;
4059 struct fcgi_strm *fstrm = NULL;
4060 struct eb32_node *node;
4061 int send_cnt = 0;
4062 int tree_cnt = 0;
4063 int orph_cnt = 0;
4064 struct buffer *hmbuf, *tmbuf;
4065
4066 if (!fconn)
Willy Tarreau8050efe2021-01-21 08:26:06 +01004067 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004068
4069 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4070 send_cnt++;
4071
4072 fstrm = NULL;
4073 node = eb32_first(&fconn->streams_by_id);
4074 while (node) {
4075 fstrm = container_of(node, struct fcgi_strm, by_id);
4076 tree_cnt++;
4077 if (!fstrm->cs)
4078 orph_cnt++;
4079 node = eb32_next(node);
4080 }
4081
4082 hmbuf = br_head(fconn->mbuf);
4083 tmbuf = br_tail(fconn->mbuf);
4084 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4085 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4086 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4087 fconn->state, fconn->max_id, fconn->flags,
4088 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4089 fconn->wait_event.events, fconn->dsi,
4090 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4091 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4092 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4093 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4094 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4095 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4096 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4097
4098 if (fstrm) {
4099 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4100 fstrm, fstrm->id, fstrm->flags,
4101 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4102 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4103 fstrm->cs);
4104 if (fstrm->cs)
4105 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4106 fstrm->cs->flags, fstrm->cs->data);
Willy Tarreau1776ffb2021-01-20 17:10:46 +01004107 chunk_appendf(&trash, " .subs=%p", fstrm->subs);
4108 if (fstrm->subs) {
4109 if (fstrm->subs) {
4110 chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
4111 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
4112 fstrm->subs->tasklet->calls,
4113 fstrm->subs->tasklet->context);
4114 resolve_sym_name(&trash, NULL, fstrm->subs->tasklet->process);
4115 chunk_appendf(&trash, ")");
4116 }
4117 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004118 }
Willy Tarreau8050efe2021-01-21 08:26:06 +01004119 return 0;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004120}
4121
4122/* Migrate the the connection to the current thread.
4123 * Return 0 if successful, non-zero otherwise.
4124 * Expected to be called with the old thread lock held.
4125 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004126static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004127{
4128 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004129 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004130
4131 if (fd_takeover(conn->handle.fd, conn) != 0)
4132 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004133
4134 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4135 /* We failed to takeover the xprt, even if the connection may
4136 * still be valid, flag it as error'd, as we have already
4137 * taken over the fd, and wake the tasklet, so that it will
4138 * destroy it.
4139 */
4140 conn->flags |= CO_FL_ERROR;
4141 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4142 return -1;
4143 }
4144
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004145 if (fcgi->wait_event.events)
4146 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4147 fcgi->wait_event.events, &fcgi->wait_event);
4148 /* To let the tasklet know it should free itself, and do nothing else,
4149 * set its context to NULL;
4150 */
4151 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004152 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004153
4154 task = fcgi->task;
4155 if (task) {
4156 task->context = NULL;
4157 fcgi->task = NULL;
4158 __ha_barrier_store();
4159 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004160
4161 fcgi->task = task_new(tid_bit);
4162 if (!fcgi->task) {
4163 fcgi_release(fcgi);
4164 return -1;
4165 }
4166 fcgi->task->process = fcgi_timeout_task;
4167 fcgi->task->context = fcgi;
4168 }
4169 fcgi->wait_event.tasklet = tasklet_new();
4170 if (!fcgi->wait_event.tasklet) {
4171 fcgi_release(fcgi);
4172 return -1;
4173 }
4174 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4175 fcgi->wait_event.tasklet->context = fcgi;
4176 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4177 SUB_RETRY_RECV, &fcgi->wait_event);
4178
4179 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004180}
4181
4182/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004183/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004184/****************************************/
4185
4186/* The mux operations */
4187static const struct mux_ops mux_fcgi_ops = {
4188 .init = fcgi_init,
4189 .wake = fcgi_wake,
4190 .attach = fcgi_attach,
4191 .get_first_cs = fcgi_get_first_cs,
4192 .detach = fcgi_detach,
4193 .destroy = fcgi_destroy,
4194 .avail_streams = fcgi_avail_streams,
4195 .used_streams = fcgi_used_streams,
4196 .rcv_buf = fcgi_rcv_buf,
4197 .snd_buf = fcgi_snd_buf,
4198 .subscribe = fcgi_subscribe,
4199 .unsubscribe = fcgi_unsubscribe,
4200 .shutr = fcgi_shutr,
4201 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004202 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004203 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004204 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004205 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004206 .name = "FCGI",
4207};
4208
4209
4210/* this mux registers FCGI proto */
4211static struct mux_proto_list mux_proto_fcgi =
4212{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4213
4214INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4215
4216/*
4217 * Local variables:
4218 * c-indent-level: 8
4219 * c-basic-offset: 8
4220 * End:
4221 */