blob: f8ee0b7e4b3e01061c5853670e94fa830ebfd6ca [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
13#include <common/cfgparse.h>
14#include <common/config.h>
15#include <common/fcgi.h>
16#include <common/h1.h>
17#include <common/htx.h>
18#include <common/initcall.h>
19#include <common/ist.h>
20#include <common/mini-clist.h>
21#include <common/net_helper.h>
22
23#include <types/proxy.h>
24#include <types/session.h>
25
26#include <proto/connection.h>
27#include <proto/fcgi-app.h>
28#include <proto/h1_htx.h>
29#include <proto/http_htx.h>
30#include <proto/log.h>
31#include <proto/session.h>
32#include <proto/ssl_sock.h>
33#include <proto/stream.h>
34#include <proto/stream_interface.h>
Christopher Faulet5c0f8592019-10-04 15:21:17 +020035#include <proto/trace.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020036
37/* 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 */
110 struct list sending_list; /* list of fcgi_strm scheduled to send data */
111
112 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
113 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
114};
115
116
117/* FCGI stream state, in fcgi_strm->state */
118enum fcgi_strm_st {
119 FCGI_SS_IDLE = 0,
120 FCGI_SS_OPEN,
121 FCGI_SS_HREM, // half-closed(remote)
122 FCGI_SS_HLOC, // half-closed(local)
123 FCGI_SS_ERROR,
124 FCGI_SS_CLOSED,
125 FCGI_SS_ENTRIES
126} __attribute__((packed));
127
128
129/* FCGI stream flags (32 bits) */
130#define FCGI_SF_NONE 0x00000000
131#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
132#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
133#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
134
135/* Stream flags indicating the reason the stream is blocked */
136#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
137#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
138#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
139
140#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
141#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
142
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 */
148#define FCGI_SF_HAVE_I_TLR 0x00010000 /* Set during input process to know the trailers were processed */
Christopher Fauletf950c2e2019-12-06 16:20:49 +0100149#define FCGI_SF_APPEND_EOM 0x00020000 /* Send EOM to the HTX buffer */
Christopher Faulet99eff652019-08-11 23:11:30 +0200150
151/* FCGI stream descriptor */
152struct fcgi_strm {
153 struct conn_stream *cs;
154 struct session *sess;
155 struct fcgi_conn *fconn;
156
157 int32_t id; /* stream ID */
158
159 uint32_t flags; /* Connection flags: FCGI_SF_* */
160 enum fcgi_strm_st state; /* FCGI stream state */
161 int proto_status; /* FCGI_PS_* */
162
163 struct h1m h1m; /* response parser state for H1 */
164
165 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
166
167 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
168 struct wait_event wait_event; /* Wait list, when we're attempting to send an ABORT but we can't send */
169 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
170 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
171 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
172 struct list sending_list; /* To be used when adding in fcgi_conn->sending_list */
173};
174
175/* Flags representing all default FCGI parameters */
176#define FCGI_SP_CGI_GATEWAY 0x00000001
177#define FCGI_SP_DOC_ROOT 0x00000002
178#define FCGI_SP_SCRIPT_NAME 0x00000004
179#define FCGI_SP_PATH_INFO 0x00000008
180#define FCGI_SP_REQ_URI 0x00000010
181#define FCGI_SP_REQ_METH 0x00000020
182#define FCGI_SP_REQ_QS 0x00000040
183#define FCGI_SP_SRV_PORT 0x00000080
184#define FCGI_SP_SRV_PROTO 0x00000100
185#define FCGI_SP_SRV_NAME 0x00000200
186#define FCGI_SP_REM_ADDR 0x00000400
187#define FCGI_SP_REM_PORT 0x00000800
188#define FCGI_SP_SCRIPT_FILE 0x00001000
189#define FCGI_SP_PATH_TRANS 0x00002000
190#define FCGI_SP_CONT_LEN 0x00004000
191#define FCGI_SP_HTTPS 0x00008000
192#define FCGI_SP_MASK 0x0000FFFF
193#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
194
195/* FCGI parameters used when PARAMS record is sent */
196struct fcgi_strm_params {
197 uint32_t mask;
198 struct ist docroot;
199 struct ist scriptname;
200 struct ist pathinfo;
201 struct ist meth;
202 struct ist uri;
203 struct ist vsn;
204 struct ist qs;
205 struct ist srv_name;
206 struct ist srv_port;
207 struct ist rem_addr;
208 struct ist rem_port;
209 struct ist cont_len;
210 int https;
211 struct buffer *p;
212};
213
214/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
215#define MAX_DATA_REALIGN 1024
216
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200217/* trace source and events */
218static void fcgi_trace(enum trace_level level, uint64_t mask,
219 const struct trace_source *src,
220 const struct ist where, const struct ist func,
221 const void *a1, const void *a2, const void *a3, const void *a4);
222
223/* The event representation is split like this :
224 * fconn - internal FCGI connection
225 * fstrm - internal FCGI stream
226 * strm - application layer
227 * rx - data receipt
228 * tx - data transmission
229 * rsp - response parsing
230 */
231static const struct trace_event fcgi_trace_events[] = {
232#define FCGI_EV_FCONN_NEW (1ULL << 0)
233 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
234#define FCGI_EV_FCONN_RECV (1ULL << 1)
235 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
236#define FCGI_EV_FCONN_SEND (1ULL << 2)
237 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
238#define FCGI_EV_FCONN_BLK (1ULL << 3)
239 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
240#define FCGI_EV_FCONN_WAKE (1ULL << 4)
241 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
242#define FCGI_EV_FCONN_END (1ULL << 5)
243 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
244#define FCGI_EV_FCONN_ERR (1ULL << 6)
245 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
246
247#define FCGI_EV_RX_FHDR (1ULL << 7)
248 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
249#define FCGI_EV_RX_RECORD (1ULL << 8)
250 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
251#define FCGI_EV_RX_EOI (1ULL << 9)
252 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
253#define FCGI_EV_RX_GETVAL (1ULL << 10)
254 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
255#define FCGI_EV_RX_STDOUT (1ULL << 11)
256 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
257#define FCGI_EV_RX_STDERR (1ULL << 12)
258 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
259#define FCGI_EV_RX_ENDREQ (1ULL << 13)
260 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
261
262#define FCGI_EV_TX_RECORD (1ULL << 14)
263 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
264#define FCGI_EV_TX_EOI (1ULL << 15)
265 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
266#define FCGI_EV_TX_BEGREQ (1ULL << 16)
267 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
268#define FCGI_EV_TX_GETVAL (1ULL << 17)
269 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
270#define FCGI_EV_TX_PARAMS (1ULL << 18)
271 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
272#define FCGI_EV_TX_STDIN (1ULL << 19)
273 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
274#define FCGI_EV_TX_ABORT (1ULL << 20)
275 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
276
277#define FCGI_EV_RSP_DATA (1ULL << 21)
278 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
279#define FCGI_EV_RSP_EOM (1ULL << 22)
280 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
281#define FCGI_EV_RSP_HDRS (1ULL << 23)
282 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
283#define FCGI_EV_RSP_BODY (1ULL << 24)
284 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
285#define FCGI_EV_RSP_TLRS (1ULL << 25)
286 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
287
288#define FCGI_EV_FSTRM_NEW (1ULL << 26)
289 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
290#define FCGI_EV_FSTRM_BLK (1ULL << 27)
291 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
292#define FCGI_EV_FSTRM_END (1ULL << 28)
293 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
294#define FCGI_EV_FSTRM_ERR (1ULL << 29)
295 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
296
297#define FCGI_EV_STRM_NEW (1ULL << 30)
298 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
299#define FCGI_EV_STRM_RECV (1ULL << 31)
300 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
301#define FCGI_EV_STRM_SEND (1ULL << 32)
302 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
303#define FCGI_EV_STRM_FULL (1ULL << 33)
304 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
305#define FCGI_EV_STRM_WAKE (1ULL << 34)
306 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
307#define FCGI_EV_STRM_SHUT (1ULL << 35)
308 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
309#define FCGI_EV_STRM_END (1ULL << 36)
310 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
311#define FCGI_EV_STRM_ERR (1ULL << 37)
312 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
313
314 { }
315};
316
317static const struct name_desc fcgi_trace_lockon_args[4] = {
318 /* arg1 */ { /* already used by the connection */ },
319 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
320 /* arg3 */ { },
321 /* arg4 */ { }
322};
323
324
325static const struct name_desc fcgi_trace_decoding[] = {
326#define FCGI_VERB_CLEAN 1
327 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
328#define FCGI_VERB_MINIMAL 2
329 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
330#define FCGI_VERB_SIMPLE 3
331 { .name="simple", .desc="add request/response status line or htx info when available" },
332#define FCGI_VERB_ADVANCED 4
333 { .name="advanced", .desc="add header fields or record decoding when available" },
334#define FCGI_VERB_COMPLETE 5
335 { .name="complete", .desc="add full data dump when available" },
336 { /* end */ }
337};
338
339static struct trace_source trace_fcgi = {
340 .name = IST("fcgi"),
341 .desc = "FastCGI multiplexer",
342 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
343 .default_cb = fcgi_trace,
344 .known_events = fcgi_trace_events,
345 .lockon_args = fcgi_trace_lockon_args,
346 .decoding = fcgi_trace_decoding,
347 .report_events = ~0, // report everything by default
348};
349
350#define TRACE_SOURCE &trace_fcgi
351INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
352
Christopher Faulet99eff652019-08-11 23:11:30 +0200353/* FCGI connection and stream pools */
354DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
355DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
356
357static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
358static int fcgi_process(struct fcgi_conn *fconn);
359static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
360static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
361static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
362static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200363static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
364static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200365static void fcgi_strm_alert(struct fcgi_strm *fstrm);
366static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
367
368/* a dmumy management stream */
369static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
370 .cs = NULL,
371 .fconn = NULL,
372 .state = FCGI_SS_CLOSED,
373 .flags = FCGI_SF_NONE,
374 .id = 0,
375};
376
377/* and a dummy idle stream for use with any unknown stream */
378static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
379 .cs = NULL,
380 .fconn = NULL,
381 .state = FCGI_SS_IDLE,
382 .flags = FCGI_SF_NONE,
383 .id = 0,
384};
385
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200386/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
387static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
388{
389 switch (st) {
390 case FCGI_CS_INIT : return "INI";
391 case FCGI_CS_SETTINGS : return "STG";
392 case FCGI_CS_RECORD_H : return "RDH";
393 case FCGI_CS_RECORD_D : return "RDD";
394 case FCGI_CS_RECORD_P : return "RDP";
395 case FCGI_CS_CLOSED : return "CLO";
396 default : return "???";
397 }
398}
399
400/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
401static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
402{
403 switch (st) {
404 case FCGI_SS_IDLE : return "IDL";
405 case FCGI_SS_OPEN : return "OPN";
406 case FCGI_SS_HREM : return "RCL";
407 case FCGI_SS_HLOC : return "HCL";
408 case FCGI_SS_ERROR : return "ERR";
409 case FCGI_SS_CLOSED : return "CLO";
410 default : return "???";
411 }
412}
413
414
415/* the FCGI traces always expect that arg1, if non-null, is of type connection
416 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
417 * and that arg3, if non-null, is a htx for rx/tx headers.
418 */
419static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
420 const struct ist where, const struct ist func,
421 const void *a1, const void *a2, const void *a3, const void *a4)
422{
423 const struct connection *conn = a1;
424 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
425 const struct fcgi_strm *fstrm = a2;
426 const struct htx *htx = a3;
427 const size_t *val = a4;
428
429 if (!fconn)
430 fconn = (fstrm ? fstrm->fconn : NULL);
431
432 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
433 return;
434
435 /* Display the response state if fstrm is defined */
436 if (fstrm)
437 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
438
439 if (src->verbosity == FCGI_VERB_CLEAN)
440 return;
441
442 /* Display the value to the 4th argument (level > STATE) */
443 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100444 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200445
446 /* Display status-line if possible (verbosity > MINIMAL) */
447 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
448 const struct htx_blk *blk = htx_get_head_blk(htx);
449 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
450 enum htx_blk_type type = htx_get_blk_type(blk);
451
452 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
453 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
454 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
455 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
456 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
457 }
458
459 /* Display fconn info and, if defined, fstrm info */
460 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
461 if (fstrm)
462 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
463
464 if (!fstrm || fstrm->id <= 0)
465 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
466 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
467 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
468
469 if (src->verbosity == FCGI_VERB_MINIMAL)
470 return;
471
472 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
473 if (src->level > TRACE_LEVEL_USER) {
474 if (src->verbosity == FCGI_VERB_COMPLETE ||
475 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
476 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
477 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
478 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
479 if (src->verbosity == FCGI_VERB_COMPLETE ||
480 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
481 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
482 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
483
484 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
485 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
486 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
487 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
488 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
489 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
490 }
491
492 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
493 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
494 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
495 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
496 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
497 }
498
499 /* Display htx info if defined (level > USER) */
500 if (src->level > TRACE_LEVEL_USER && htx) {
501 int full = 0;
502
503 /* Full htx info (level > STATE && verbosity > SIMPLE) */
504 if (src->level > TRACE_LEVEL_STATE) {
505 if (src->verbosity == FCGI_VERB_COMPLETE)
506 full = 1;
507 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
508 full = 1;
509 }
510
511 chunk_memcat(&trace_buf, "\n\t", 2);
512 htx_dump(&trace_buf, htx, full);
513 }
514}
Christopher Faulet99eff652019-08-11 23:11:30 +0200515
516/*****************************************************/
517/* functions below are for dynamic buffer management */
518/*****************************************************/
519
520/* Indicates whether or not the we may call the fcgi_recv() function to attempt
521 * to receive data into the buffer and/or demux pending data. The condition is
522 * a bit complex due to some API limits for now. The rules are the following :
523 * - if an error or a shutdown was detected on the connection and the buffer
524 * is empty, we must not attempt to receive
525 * - if the demux buf failed to be allocated, we must not try to receive and
526 * we know there is nothing pending
527 * - if no flag indicates a blocking condition, we may attempt to receive,
528 * regardless of whether the demux buffer is full or not, so that only
529 * de demux part decides whether or not to block. This is needed because
530 * the connection API indeed prevents us from re-enabling receipt that is
531 * already enabled in a polled state, so we must always immediately stop
532 * as soon as the demux can't proceed so as never to hit an end of read
533 * with data pending in the buffers.
534 * - otherwise must may not attempt
535 */
536static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
537{
538 if (b_data(&fconn->dbuf) == 0 &&
539 (fconn->state == FCGI_CS_CLOSED ||
540 fconn->conn->flags & CO_FL_ERROR ||
541 conn_xprt_read0_pending(fconn->conn)))
542 return 0;
543
544 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
545 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
546 return 1;
547
548 return 0;
549}
550
551/* Restarts reading on the connection if it was not enabled */
552static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
553{
554 if (!fcgi_recv_allowed(fconn))
555 return;
556 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
557 (fconn->wait_event.events & SUB_RETRY_RECV))
558 return;
559 tasklet_wakeup(fconn->wait_event.tasklet);
560}
561
562
563/* Tries to grab a buffer and to re-enable processing on mux <target>. The
564 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
565 * the allocation succeeds, in which case the connection is woken up, or 0 if
566 * it's impossible to wake up and we prefer to be woken up later.
567 */
568static int fcgi_buf_available(void *target)
569{
570 struct fcgi_conn *fconn = target;
571 struct fcgi_strm *fstrm;
572
573 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200574 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200575 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
576 fcgi_conn_restart_reading(fconn, 1);
577 return 1;
578 }
579
580 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200581 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200582 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200583 if (fconn->flags & FCGI_CF_DEM_MROOM) {
584 fconn->flags &= ~FCGI_CF_DEM_MROOM;
585 fcgi_conn_restart_reading(fconn, 1);
586 }
587 return 1;
588 }
589
590 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
591 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
592 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200593 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200594 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
595 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200596 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200597 return 1;
598 }
599
600 return 0;
601}
602
603static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
604{
605 struct buffer *buf = NULL;
606
607 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
608 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
609 fconn->buf_wait.target = fconn;
610 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
611 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
612 LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
613 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
614 __conn_xprt_stop_recv(fconn->conn);
615 }
616 return buf;
617}
618
619static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
620{
621 if (bptr->size) {
622 b_free(bptr);
623 offer_buffers(NULL, tasks_run_queue);
624 }
625}
626
627static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
628{
629 struct buffer *buf;
630 unsigned int count = 0;
631
632 while (b_size(buf = br_head_pick(fconn->mbuf))) {
633 b_free(buf);
634 count++;
635 }
636 if (count)
637 offer_buffers(NULL, tasks_run_queue);
638}
639
640/* Returns the number of allocatable outgoing streams for the connection taking
641 * the number reserved streams into account.
642 */
643static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
644{
645 int ret;
646
647 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
648 if (ret < 0)
649 ret = 0;
650 return ret;
651}
652
653/* Returns the number of streams in use on a connection to figure if it's
654 * idle or not. We check nb_cs and not nb_streams as the caller will want
655 * to know if it was the last one after a detach().
656 */
657static int fcgi_used_streams(struct connection *conn)
658{
659 struct fcgi_conn *fconn = conn->ctx;
660
661 return fconn->nb_cs;
662}
663
664/* Returns the number of concurrent streams available on the connection */
665static int fcgi_avail_streams(struct connection *conn)
666{
667 struct server *srv = objt_server(conn->target);
668 struct fcgi_conn *fconn = conn->ctx;
669 int ret1, ret2;
670
671 /* Don't open new stream if the connection is closed */
672 if (fconn->state == FCGI_CS_CLOSED)
673 return 0;
674
675 /* May be negative if this setting has changed */
676 ret1 = (fconn->streams_limit - fconn->nb_streams);
677
678 /* we must also consider the limit imposed by stream IDs */
679 ret2 = fcgi_streams_left(fconn);
680 ret1 = MIN(ret1, ret2);
681 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
682 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
683 ret1 = MIN(ret1, ret2);
684 }
685 return ret1;
686}
687
688/*****************************************************************/
689/* functions below are dedicated to the mux setup and management */
690/*****************************************************************/
691
692/* Initializes the mux once it's attached. Only outgoing connections are
693 * supported. So the context is already initialized before installing the
694 * mux. <input> is always used as Input buffer and may contain data. It is the
695 * caller responsibility to not reuse it anymore. Returns < 0 on error.
696 */
697static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
698 struct buffer *input)
699{
700 struct fcgi_conn *fconn;
701 struct fcgi_strm *fstrm;
702 struct fcgi_app *app = get_px_fcgi_app(px);
703 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200704 void *conn_ctx = conn->ctx;
705
706 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200707
708 if (!app)
709 goto fail_conn;
710
711 fconn = pool_alloc(pool_head_fcgi_conn);
712 if (!fconn)
713 goto fail_conn;
714
715 fconn->shut_timeout = fconn->timeout = px->timeout.server;
716 if (tick_isset(px->timeout.serverfin))
717 fconn->shut_timeout = px->timeout.serverfin;
718
719 fconn->flags = FCGI_CF_NONE;
720
721 /* Retrieve usefull info from the FCGI app */
722 if (app->flags & FCGI_APP_FL_KEEP_CONN)
723 fconn->flags |= FCGI_CF_KEEP_CONN;
724 if (app->flags & FCGI_APP_FL_GET_VALUES)
725 fconn->flags |= FCGI_CF_GET_VALUES;
726 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
727 fconn->flags |= FCGI_CF_MPXS_CONNS;
728
729 fconn->proxy = px;
730 fconn->app = app;
731 fconn->task = NULL;
732 if (tick_isset(fconn->timeout)) {
733 t = task_new(tid_bit);
734 if (!t)
735 goto fail;
736
737 fconn->task = t;
738 t->process = fcgi_timeout_task;
739 t->context = fconn;
740 t->expire = tick_add(now_ms, fconn->timeout);
741 }
742
743 fconn->wait_event.tasklet = tasklet_new();
744 if (!fconn->wait_event.tasklet)
745 goto fail;
746 fconn->wait_event.tasklet->process = fcgi_io_cb;
747 fconn->wait_event.tasklet->context = fconn;
748 fconn->wait_event.events = 0;
749
750 /* Initialise the context. */
751 fconn->state = FCGI_CS_INIT;
752 fconn->conn = conn;
753 fconn->streams_limit = app->maxreqs;
754 fconn->max_id = -1;
755 fconn->nb_streams = 0;
756 fconn->nb_cs = 0;
757 fconn->nb_reserved = 0;
758 fconn->stream_cnt = 0;
759
760 fconn->dbuf = *input;
761 fconn->dsi = -1;
762
763 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
764 fconn->streams_by_id = EB_ROOT;
765 LIST_INIT(&fconn->send_list);
766 LIST_INIT(&fconn->sending_list);
767 LIST_INIT(&fconn->buf_wait.list);
768
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200769 conn->ctx = fconn;
770
Christopher Faulet99eff652019-08-11 23:11:30 +0200771 if (t)
772 task_queue(t);
773
774 /* FIXME: this is temporary, for outgoing connections we need to
775 * immediately allocate a stream until the code is modified so that the
776 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200777 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200778 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200779 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 if (!fstrm)
781 goto fail;
782
Christopher Faulet99eff652019-08-11 23:11:30 +0200783
784 /* Repare to read something */
785 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200786 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200787 return 0;
788
789 fail:
790 task_destroy(t);
791 if (fconn->wait_event.tasklet)
792 tasklet_free(fconn->wait_event.tasklet);
793 pool_free(pool_head_fcgi_conn, fconn);
794 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200795 conn->ctx = conn_ctx; // restore saved ctx
796 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200797 return -1;
798}
799
800/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
801 * -1 if no more is allocatable.
802 */
803static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
804{
805 int32_t id = (fconn->max_id + 1) | 1;
806
807 if ((id & 0x80000000U))
808 id = -1;
809 return id;
810}
811
812/* Returns the stream associated with id <id> or NULL if not found */
813static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
814{
815 struct eb32_node *node;
816
817 if (id == 0)
818 return (struct fcgi_strm *)fcgi_mgmt_stream;
819
820 if (id > fconn->max_id)
821 return (struct fcgi_strm *)fcgi_unknown_stream;
822
823 node = eb32_lookup(&fconn->streams_by_id, id);
824 if (!node)
825 return (struct fcgi_strm *)fcgi_unknown_stream;
826 return container_of(node, struct fcgi_strm, by_id);
827}
828
829
830/* Release function. This one should be called to free all resources allocated
831 * to the mux.
832 */
833static void fcgi_release(struct fcgi_conn *fconn)
834{
835 struct connection *conn = NULL;;
836
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200837 TRACE_POINT(FCGI_EV_FCONN_END);
838
Christopher Faulet99eff652019-08-11 23:11:30 +0200839 if (fconn) {
840 /* The connection must be attached to this mux to be released */
841 if (fconn->conn && fconn->conn->ctx == fconn)
842 conn = fconn->conn;
843
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200844 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
845
Christopher Faulet99eff652019-08-11 23:11:30 +0200846 if (LIST_ADDED(&fconn->buf_wait.list)) {
847 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
848 LIST_DEL(&fconn->buf_wait.list);
849 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
850 }
851
852 fcgi_release_buf(fconn, &fconn->dbuf);
853 fcgi_release_mbuf(fconn);
854
855 if (fconn->task) {
856 fconn->task->context = NULL;
857 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
858 fconn->task = NULL;
859 }
860 if (fconn->wait_event.tasklet)
861 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200862 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200863 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
864 &fconn->wait_event);
865 }
866
867 if (conn) {
868 conn->mux = NULL;
869 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200870 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200871
872 conn_stop_tracking(conn);
873 conn_full_close(conn);
874 if (conn->destroy_cb)
875 conn->destroy_cb(conn);
876 conn_free(conn);
877 }
878}
879
880
881/* Retruns true if the FCGI connection must be release */
882static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
883{
884 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
885 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
886 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
887 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
888 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
889 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
890 conn_xprt_read0_pending(fconn->conn))))
891 return 1;
892 return 0;
893}
894
895
896/********************************************************/
897/* functions below are for the FCGI protocol processing */
898/********************************************************/
899
Christopher Faulet99eff652019-08-11 23:11:30 +0200900/* Marks an error on the stream. */
901static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
902{
903 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200904 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
905 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200906 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200907 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
908 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200909 if (fstrm->cs)
910 cs_set_error(fstrm->cs);
911 }
912}
913
914/* Attempts to notify the data layer of recv availability */
915static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
916{
917 struct wait_event *sw;
918
919 if (fstrm->recv_wait) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200920 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200921 sw = fstrm->recv_wait;
922 sw->events &= ~SUB_RETRY_RECV;
923 tasklet_wakeup(sw->tasklet);
924 fstrm->recv_wait = NULL;
925 }
926}
927
928/* Attempts to notify the data layer of send availability */
929static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
930{
931 struct wait_event *sw;
932
933 if (fstrm->send_wait && !LIST_ADDED(&fstrm->sending_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200934 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200935 sw = fstrm->send_wait;
936 sw->events &= ~SUB_RETRY_SEND;
937 LIST_ADDQ(&fstrm->fconn->sending_list, &fstrm->sending_list);
938 tasklet_wakeup(sw->tasklet);
939 }
940}
941
942/* Alerts the data layer, trying to wake it up by all means, following
943 * this sequence :
944 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
945 * for recv
946 * - if its subscribed to send, then it's woken up for send
947 * - if it was subscribed to neither, its ->wake() callback is called
948 * It is safe to call this function with a closed stream which doesn't have a
949 * conn_stream anymore.
950 */
951static void fcgi_strm_alert(struct fcgi_strm *fstrm)
952{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200953 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200954 if (fstrm->recv_wait || fstrm->send_wait) {
955 fcgi_strm_notify_recv(fstrm);
956 fcgi_strm_notify_send(fstrm);
957 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200958 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
959 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200960 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200961 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200962}
963
964/* Writes the 16-bit record size <len> at address <record> */
965static inline void fcgi_set_record_size(void *record, uint16_t len)
966{
967 uint8_t *out = (record + 4);
968
969 *out = (len >> 8);
970 *(out + 1) = (len & 0xff);
971}
972
973/* Writes the 16-bit stream id <id> at address <record> */
974static inline void fcgi_set_record_id(void *record, uint16_t id)
975{
976 uint8_t *out = (record + 2);
977
978 *out = (id >> 8);
979 *(out + 1) = (id & 0xff);
980}
981
982/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
983 * its connection if the stream was not yet closed. Please use this exclusively
984 * before closing a stream to ensure stream count is well maintained.
985 */
986static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
987{
988 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200989 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200990 fstrm->fconn->nb_streams--;
991 if (!fstrm->id)
992 fstrm->fconn->nb_reserved--;
993 if (fstrm->cs) {
994 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
995 fcgi_strm_notify_recv(fstrm);
996 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200997 fstrm->state = FCGI_SS_CLOSED;
998 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
999 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001000 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001001}
1002
1003/* Detaches a FCGI stream from its FCGI connection and releases it to the
1004 * fcgi_strm pool.
1005 */
1006static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1007{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001008 struct connection *conn = fstrm->fconn->conn;
1009
1010 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1011
Christopher Faulet99eff652019-08-11 23:11:30 +02001012 fcgi_strm_close(fstrm);
1013 eb32_delete(&fstrm->by_id);
1014 if (b_size(&fstrm->rxbuf)) {
1015 b_free(&fstrm->rxbuf);
1016 offer_buffers(NULL, tasks_run_queue);
1017 }
1018 if (fstrm->send_wait != NULL)
1019 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
1020 if (fstrm->recv_wait != NULL)
1021 fstrm->recv_wait->events &= ~SUB_RETRY_RECV;
1022 /* There's no need to explicitly call unsubscribe here, the only
1023 * reference left would be in the fconn send_list/fctl_list, and if
1024 * we're in it, we're getting out anyway
1025 */
1026 LIST_DEL_INIT(&fstrm->send_list);
1027 if (LIST_ADDED(&fstrm->sending_list)) {
1028 tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet);
1029 LIST_DEL_INIT(&fstrm->sending_list);
1030 }
1031 tasklet_free(fstrm->wait_event.tasklet);
1032 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
1053 fstrm->wait_event.tasklet = tasklet_new();
1054 if (!fstrm->wait_event.tasklet) {
1055 pool_free(pool_head_fcgi_strm, fstrm);
1056 goto out;
1057 }
1058 fstrm->send_wait = NULL;
1059 fstrm->recv_wait = NULL;
1060 fstrm->wait_event.tasklet->process = fcgi_deferred_shut;
1061 fstrm->wait_event.tasklet->context = fstrm;
1062 fstrm->wait_event.events = 0;
1063 LIST_INIT(&fstrm->send_list);
1064 LIST_INIT(&fstrm->sending_list);
1065 fstrm->fconn = fconn;
1066 fstrm->cs = NULL;
1067 fstrm->flags = FCGI_SF_NONE;
1068 fstrm->proto_status = 0;
1069 fstrm->state = FCGI_SS_IDLE;
1070 fstrm->rxbuf = BUF_NULL;
1071
1072 h1m_init_res(&fstrm->h1m);
1073 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1074 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1075
1076 fstrm->by_id.key = fstrm->id = id;
1077 if (id > 0)
1078 fconn->max_id = id;
1079 else
1080 fconn->nb_reserved++;
1081
1082 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1083 fconn->nb_streams++;
1084 fconn->stream_cnt++;
1085
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001086 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001087 return fstrm;
1088
1089 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001090 TRACE_DEVEL("leaving in error", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001091 return NULL;
1092}
1093
1094/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1095 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1096 * highest possible stream ID was reached.
1097 */
1098static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1099 struct session *sess)
1100{
1101 struct fcgi_strm *fstrm = NULL;
1102
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001103 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1104 if (fconn->nb_streams >= fconn->streams_limit) {
1105 TRACE_DEVEL("leaving on streams_limit reached", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001106 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001107 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001108
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001109 if (fcgi_streams_left(fconn) < 1) {
1110 TRACE_DEVEL("leaving on !streams_left", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001111 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001112 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001113
1114 /* Defer choosing the ID until we send the first message to create the stream */
1115 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001116 if (!fstrm) {
1117 TRACE_DEVEL("leaving on fstrm creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_END|FCGI_EV_FSTRM_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001118 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001119 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001120
1121 fstrm->cs = cs;
1122 fstrm->sess = sess;
1123 cs->ctx = fstrm;
1124 fconn->nb_cs++;
1125
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001126 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001127 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001128
1129 out:
1130 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001131}
1132
1133/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1134 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1135 * automatically updated accordingly. If the stream is orphaned, it is
1136 * destroyed.
1137 */
1138static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1139{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001140 struct fcgi_conn *fconn = fstrm->fconn;
1141
1142 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1143
Christopher Faulet99eff652019-08-11 23:11:30 +02001144 if (!fstrm->cs) {
1145 /* this stream was already orphaned */
1146 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001147 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001148 return;
1149 }
1150
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001151 if (conn_xprt_read0_pending(fconn->conn)) {
1152 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001154 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1155 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001156 else if (fstrm->state == FCGI_SS_HLOC)
1157 fcgi_strm_close(fstrm);
1158 }
1159
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001161 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1162 if (fstrm->cs->flags & CS_FL_EOS)
1163 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001164
1165 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001166 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001167 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1168 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001169 }
1170
1171 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001172
1173 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001174}
1175
1176/* Wakes unassigned streams (ID == 0) attached to the connection. */
1177static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1178{
1179 struct eb32_node *node;
1180 struct fcgi_strm *fstrm;
1181
1182 node = eb32_lookup(&fconn->streams_by_id, 0);
1183 while (node) {
1184 fstrm = container_of(node, struct fcgi_strm, by_id);
1185 if (fstrm->id > 0)
1186 break;
1187 node = eb32_next(node);
1188 fcgi_strm_wake_one_stream(fstrm);
1189 }
1190}
1191
1192/* Wakes the streams attached to the connection, whose id is greater than <last>
1193 * or unassigned.
1194 */
1195static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1196{
1197 struct eb32_node *node;
1198 struct fcgi_strm *fstrm;
1199
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001200 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1201
Christopher Faulet99eff652019-08-11 23:11:30 +02001202 /* Wake all streams with ID > last */
1203 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1204 while (node) {
1205 fstrm = container_of(node, struct fcgi_strm, by_id);
1206 node = eb32_next(node);
1207 fcgi_strm_wake_one_stream(fstrm);
1208 }
1209 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001210
1211 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001212}
1213
1214static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1215 struct htx *htx, struct htx_sl *sl,
1216 struct fcgi_strm_params *params)
1217{
1218 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1219 struct ist p;
1220
1221 if (!sl)
1222 goto error;
1223
1224 if (!(params->mask & FCGI_SP_DOC_ROOT))
1225 params->docroot = fconn->app->docroot;
1226
1227 if (!(params->mask & FCGI_SP_REQ_METH)) {
1228 p = htx_sl_req_meth(sl);
1229 params->meth = ist2(b_tail(params->p), p.len);
1230 chunk_memcat(params->p, p.ptr, p.len);
1231 }
1232 if (!(params->mask & FCGI_SP_REQ_URI)) {
1233 p = htx_sl_req_uri(sl);
1234 params->uri = ist2(b_tail(params->p), p.len);
1235 chunk_memcat(params->p, p.ptr, p.len);
1236 }
1237 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1238 p = htx_sl_req_vsn(sl);
1239 params->vsn = ist2(b_tail(params->p), p.len);
1240 chunk_memcat(params->p, p.ptr, p.len);
1241 }
1242 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1243 char *end;
1244 int port = 0;
1245 if (conn_get_dst(cli_conn))
1246 port = get_host_port(cli_conn->dst);
1247 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1248 if (!end)
1249 goto error;
1250 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1251 params->p->data += params->srv_port.len;
1252 }
1253 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1254 /* If no Host header found, use the server address to fill
1255 * srv_name */
1256 if (!istlen(params->srv_name)) {
1257 char *ptr = NULL;
1258
1259 if (conn_get_dst(cli_conn))
1260 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1261 ptr = b_tail(params->p);
1262 if (ptr) {
1263 params->srv_name = ist2(ptr, strlen(ptr));
1264 params->p->data += params->srv_name.len;
1265 }
1266 }
1267 }
1268 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1269 char *ptr = NULL;
1270
1271 if (conn_get_src(cli_conn))
1272 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1273 ptr = b_tail(params->p);
1274 if (ptr) {
1275 params->rem_addr = ist2(ptr, strlen(ptr));
1276 params->p->data += params->rem_addr.len;
1277 }
1278 }
1279 if (!(params->mask & FCGI_SP_REM_PORT)) {
1280 char *end;
1281 int port = 0;
1282 if (conn_get_src(cli_conn))
1283 port = get_host_port(cli_conn->src);
1284 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1285 if (!end)
1286 goto error;
1287 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1288 params->p->data += params->rem_port.len;
1289 }
1290 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1291 struct htx_blk *blk;
1292 enum htx_blk_type type;
1293 char *end;
1294 size_t len = 0;
1295
1296 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1297 type = htx_get_blk_type(blk);
1298
1299 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1300 break;
1301 if (type == HTX_BLK_DATA)
1302 len += htx_get_blksz(blk);
1303 }
1304 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1305 if (!end)
1306 goto error;
1307 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1308 params->p->data += params->cont_len.len;
1309 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001310#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001311 if (!(params->mask & FCGI_SP_HTTPS)) {
1312 params->https = ssl_sock_is_ssl(cli_conn);
1313 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001314#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001315 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1316 /* one of scriptname, pathinfo or query_string is no set */
1317 struct ist path = http_get_path(params->uri);
1318 int len;
1319
1320 /* Decode the path. it must first be copied to keep the URI
1321 * untouched.
1322 */
1323 chunk_memcat(params->p, path.ptr, path.len);
1324 path.ptr = b_tail(params->p) - path.len;
1325 path.ptr[path.len] = '\0';
1326 len = url_decode(path.ptr);
1327 if (len < 0)
1328 goto error;
1329 path.len = len;
1330
1331 /* No scrit_name set but no valid path ==> error */
1332 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1333 goto error;
1334
1335 /* Find limit between the path and the query-string */
1336 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1337
1338 /* If there is a query-string, Set it if not already set */
1339 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1340 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1341
1342 /* If the script_name is set, don't try to deduce the path_info
1343 * too. The opposite is not true.
1344 */
1345 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1346 params->mask |= FCGI_SP_PATH_INFO;
1347 goto end;
1348 }
1349
1350 /* script_name not set, preset it with the path for now */
1351 params->scriptname = ist2(path.ptr, len);
1352
1353 /* If there is no regex to match the pathinfo, just to the last
1354 * part and see if the index must be used.
1355 */
1356 if (!fconn->app->pathinfo_re)
1357 goto check_index;
1358
1359 /* The regex does not match, just to the last part and see if
1360 * the index must be used.
1361 */
1362 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1363 goto check_index;
1364
1365 /* We must have at least 2 captures, otherwise we do nothing and
1366 * jump to the last part. Only first 2 ones will be considered
1367 */
1368 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1 ||
1369 pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1)
1370 goto check_index;
1371
1372 /* Finally we can set the script_name and the path_info */
1373 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
1374 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
1375
1376 check_index:
1377 len = params->scriptname.len;
1378 /* the script_name if finished by a '/' so we can add the index
1379 * part, if any.
1380 */
1381 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1382 struct ist sn = params->scriptname;
1383
1384 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1385 chunk_memcat(params->p, sn.ptr, sn.len);
1386 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1387 }
1388 }
1389
1390 end:
1391 return 1;
1392 error:
1393 return 0;
1394}
1395
1396static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1397 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1398{
1399 struct fcgi_param p;
1400
1401 if (params->mask & flag)
1402 return 1;
1403
1404 chunk_reset(&trash);
1405
1406 switch (flag) {
1407 case FCGI_SP_CGI_GATEWAY:
1408 p.n = ist("GATEWAY_INTERFACE");
1409 p.v = ist("CGI/1.1");
1410 goto encode;
1411 case FCGI_SP_DOC_ROOT:
1412 p.n = ist("DOCUMENT_ROOT");
1413 p.v = params->docroot;
1414 goto encode;
1415 case FCGI_SP_SCRIPT_NAME:
1416 p.n = ist("SCRIPT_NAME");
1417 p.v = params->scriptname;
1418 goto encode;
1419 case FCGI_SP_PATH_INFO:
1420 p.n = ist("PATH_INFO");
1421 p.v = params->pathinfo;
1422 goto encode;
1423 case FCGI_SP_REQ_URI:
1424 p.n = ist("REQUEST_URI");
1425 p.v = params->uri;
1426 goto encode;
1427 case FCGI_SP_REQ_METH:
1428 p.n = ist("REQUEST_METHOD");
1429 p.v = params->meth;
1430 goto encode;
1431 case FCGI_SP_REQ_QS:
1432 p.n = ist("QUERY_STRING");
1433 p.v = params->qs;
1434 goto encode;
1435 case FCGI_SP_SRV_NAME:
1436 p.n = ist("SERVER_NAME");
1437 p.v = params->srv_name;
1438 goto encode;
1439 case FCGI_SP_SRV_PORT:
1440 p.n = ist("SERVER_PORT");
1441 p.v = params->srv_port;
1442 goto encode;
1443 case FCGI_SP_SRV_PROTO:
1444 p.n = ist("SERVER_PROTOCOL");
1445 p.v = params->vsn;
1446 goto encode;
1447 case FCGI_SP_REM_ADDR:
1448 p.n = ist("REMOTE_ADDR");
1449 p.v = params->rem_addr;
1450 goto encode;
1451 case FCGI_SP_REM_PORT:
1452 p.n = ist("REMOTE_PORT");
1453 p.v = params->rem_port;
1454 goto encode;
1455 case FCGI_SP_SCRIPT_FILE:
1456 p.n = ist("SCRIPT_FILENAME");
1457 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1458 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1459 p.v = ist2(b_head(&trash), b_data(&trash));
1460 goto encode;
1461 case FCGI_SP_PATH_TRANS:
1462 if (!istlen(params->pathinfo))
1463 goto skip;
1464 p.n = ist("PATH_TRANSLATED");
1465 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1466 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1467 p.v = ist2(b_head(&trash), b_data(&trash));
1468 goto encode;
1469 case FCGI_SP_CONT_LEN:
1470 p.n = ist("CONTENT_LENGTH");
1471 p.v = params->cont_len;
1472 goto encode;
1473 case FCGI_SP_HTTPS:
1474 if (!params->https)
1475 goto skip;
1476 p.n = ist("HTTPS");
1477 p.v = ist("on");
1478 goto encode;
1479 default:
1480 goto skip;
1481 }
1482
1483 encode:
1484 if (!istlen(p.v))
1485 goto skip;
1486 if (!fcgi_encode_param(outbuf, &p))
1487 return 0;
1488 skip:
1489 params->mask |= flag;
1490 return 1;
1491}
1492
1493/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1494 * anything. It is highly unexpected, but if the record is larger than a buffer
1495 * and cannot be encoded in one time, an error is triggered and the connection is
1496 * closed. GET_VALUES record cannot be split.
1497 */
1498static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1499{
1500 struct buffer outbuf;
1501 struct buffer *mbuf;
1502 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1503 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001504 int ret = 0;
1505
1506 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001507
1508 mbuf = br_tail(fconn->mbuf);
1509 retry:
1510 if (!fcgi_get_buf(fconn, mbuf)) {
1511 fconn->flags |= FCGI_CF_MUX_MALLOC;
1512 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001513 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1514 ret = 0;
1515 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001516 }
1517
1518 while (1) {
1519 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1520 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1521 break;
1522 realign_again:
1523 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1524 }
1525
1526 if (outbuf.size < 8)
1527 goto full;
1528
1529 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1530 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1531 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1532 outbuf.data = 8;
1533
1534 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1535 * handled by HAProxy.
1536 */
1537 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1538 goto full;
1539
1540 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001541 TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn,,, (size_t[]){outbuf.data-8});
Christopher Faulet99eff652019-08-11 23:11:30 +02001542 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1543 b_add(mbuf, outbuf.data);
1544 ret = 1;
1545
1546 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001547 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001548 return ret;
1549 full:
1550 /* Too large to be encoded. For GET_VALUES records, it is an error */
1551 if (!b_data(mbuf))
1552 goto fail;
1553
1554 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1555 goto retry;
1556 fconn->flags |= FCGI_CF_MUX_MFULL;
1557 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001558 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001559 ret = 0;
1560 goto end;
1561 fail:
1562 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001563 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1564 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1565 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001566}
1567
1568/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1569 * couldn't do anything. It is highly unexpected, but if the record is larger
1570 * than a buffer and cannot be decoded in one time, an error is triggered and
1571 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1572 */
1573static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1574{
1575 struct buffer inbuf;
1576 struct buffer *dbuf;
1577 size_t offset;
1578
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001579 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1580
Christopher Faulet99eff652019-08-11 23:11:30 +02001581 dbuf = &fconn->dbuf;
1582
1583 /* Record too large to be fully decoded */
1584 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1585 goto fail;
1586
1587 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001588 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1589 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001590 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001591 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001592
1593 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1594 /* Realign the dmux buffer if the record wraps. It is unexpected
1595 * at this stage because it should be the first record received
1596 * from the FCGI application.
1597 */
1598 b_slow_realign(dbuf, trash.area, 0);
1599 }
1600
1601 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1602
1603 for (offset = 0; offset < b_data(&inbuf); ) {
1604 struct fcgi_param p;
1605 size_t ret;
1606
1607 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1608 if (!ret) {
1609 /* name or value too large to be decoded at once */
1610 goto fail;
1611 }
1612 offset += ret;
1613
1614 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001615 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001616 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001617 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001618 }
1619 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001620 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001621 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001622 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001623 }
1624 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1625 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001626 TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->streams_limit});
Christopher Faulet99eff652019-08-11 23:11:30 +02001627 }
1628 /*
1629 * Ignore all other params
1630 */
1631 }
1632
1633 /* Reset the number of concurrent streams supported if the FCGI
1634 * application does not support connection multiplexing
1635 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001636 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001637 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001638 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1639 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001640
1641 /* We must be sure to have read exactly the announced record length, no
1642 * more no less
1643 */
1644 if (offset != fconn->drl)
1645 goto fail;
1646
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001647 TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02001648 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1649 fconn->drl = 0;
1650 fconn->drp = 0;
1651 fconn->state = FCGI_CS_RECORD_H;
1652 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001653 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1654 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001655 return 1;
1656 fail:
1657 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001658 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1659 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 +02001660 return 0;
1661}
1662
1663/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1664 * excluded, as the streams which already received the end-of-stream. It returns
1665 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1666 */
1667static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1668{
1669 struct eb32_node *node;
1670 struct fcgi_strm *fstrm;
1671
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001672 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1673
Christopher Faulet99eff652019-08-11 23:11:30 +02001674 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1675 while (node) {
1676 fstrm = container_of(node, struct fcgi_strm, by_id);
1677 node = eb32_next(node);
1678 if (fstrm->state != FCGI_SS_CLOSED &&
1679 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1680 !fcgi_strm_send_abort(fconn, fstrm))
1681 return 0;
1682 }
1683 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001684 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1685 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 return 1;
1687}
1688
1689/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1690 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1691 * space to proceed. It is small enough to be encoded in an empty buffer.
1692 */
1693static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1694{
1695 struct buffer outbuf;
1696 struct buffer *mbuf;
1697 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1698 int ret;
1699
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001700 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1701
Christopher Faulet99eff652019-08-11 23:11:30 +02001702 mbuf = br_tail(fconn->mbuf);
1703 retry:
1704 if (!fcgi_get_buf(fconn, mbuf)) {
1705 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001706 fstrm->flags |= FCGI_SF_BLK_MROOM;
1707 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1708 ret = 0;
1709 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001710 }
1711
1712 while (1) {
1713 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1714 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1715 break;
1716 realign_again:
1717 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1718 }
1719
1720 if (outbuf.size < 8)
1721 goto full;
1722
1723 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1724 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1725 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1726 fcgi_set_record_id(outbuf.area, fstrm->id);
1727 outbuf.data = 8;
1728
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001729 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1730 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001731 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001732 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 if (!fcgi_encode_begin_request(&outbuf, &rec))
1734 goto full;
1735
1736 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001737 TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001738 b_add(mbuf, outbuf.data);
1739 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1740 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001741 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001742 ret = 1;
1743
1744 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001745 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 return ret;
1747 full:
1748 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1749 goto retry;
1750 fconn->flags |= FCGI_CF_MUX_MFULL;
1751 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001752 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 +02001753 ret = 0;
1754 goto end;
1755}
1756
1757/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1758 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1759 * space to proceed. It is small enough to be encoded in an empty buffer.
1760 */
1761static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1762 enum fcgi_record_type rtype)
1763{
1764 struct buffer outbuf;
1765 struct buffer *mbuf;
1766 int ret;
1767
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001768 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001769 mbuf = br_tail(fconn->mbuf);
1770 retry:
1771 if (!fcgi_get_buf(fconn, mbuf)) {
1772 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001773 fstrm->flags |= FCGI_SF_BLK_MROOM;
1774 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1775 ret = 0;
1776 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 }
1778
1779 while (1) {
1780 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1781 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1782 break;
1783 realign_again:
1784 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1785 }
1786
1787 if (outbuf.size < 8)
1788 goto full;
1789
1790 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1791 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1792 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1793 outbuf.area[1] = rtype;
1794 fcgi_set_record_id(outbuf.area, fstrm->id);
1795 outbuf.data = 8;
1796
1797 /* commit the record */
1798 b_add(mbuf, outbuf.data);
1799 ret = 1;
1800
1801 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001802 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001803 return ret;
1804 full:
1805 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1806 goto retry;
1807 fconn->flags |= FCGI_CF_MUX_MFULL;
1808 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001809 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 +02001810 ret = 0;
1811 goto end;
1812}
1813
1814
1815/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1816 * marks the end of params.
1817 */
1818static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1819{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001820 int ret;
1821
1822 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1823 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1824 if (ret)
1825 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1826 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001827}
1828
1829/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1830 * marks the end of input. On success, all the request was successfully sent.
1831 */
1832static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1833{
1834 int ret;
1835
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001836 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001837 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001838 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001839 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001840 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1841 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1842 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1843 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001844 return ret;
1845}
1846
1847/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1848 * stops the request processing.
1849 */
1850static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1851{
1852 int ret;
1853
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001854 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001855 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001856 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001858 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1859 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1860 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1861 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001862 return ret;
1863}
1864
1865/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1866 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1867 * several records are sent. However, a K/V param cannot be split between 2
1868 * records.
1869 */
1870static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1871 struct htx *htx)
1872{
1873 struct buffer outbuf;
1874 struct buffer *mbuf;
1875 struct htx_blk *blk;
1876 struct htx_sl *sl = NULL;
1877 struct fcgi_strm_params params;
1878 size_t total = 0;
1879
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001880 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1881
Christopher Faulet99eff652019-08-11 23:11:30 +02001882 memset(&params, 0, sizeof(params));
1883 params.p = get_trash_chunk();
1884
1885 mbuf = br_tail(fconn->mbuf);
1886 retry:
1887 if (!fcgi_get_buf(fconn, mbuf)) {
1888 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001889 fstrm->flags |= FCGI_SF_BLK_MROOM;
1890 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1891 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001892 }
1893
1894 while (1) {
1895 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1896 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1897 break;
1898 realign_again:
1899 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1900 }
1901
1902 if (outbuf.size < 8)
1903 goto full;
1904
1905 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1906 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1907 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1908 fcgi_set_record_id(outbuf.area, fstrm->id);
1909 outbuf.data = 8;
1910
1911 blk = htx_get_head_blk(htx);
1912 while (blk) {
1913 enum htx_blk_type type;
1914 uint32_t size = htx_get_blksz(blk);
1915 struct fcgi_param p;
1916
1917 type = htx_get_blk_type(blk);
1918 switch (type) {
1919 case HTX_BLK_REQ_SL:
1920 sl = htx_get_blk_ptr(htx, blk);
1921 if (sl->info.req.meth == HTTP_METH_HEAD)
1922 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1923 if (sl->flags & HTX_SL_F_VER_11)
1924 fstrm->h1m.flags |= H1_MF_VER_11;
1925 break;
1926
1927 case HTX_BLK_HDR:
1928 p.n = htx_get_blk_name(htx, blk);
1929 p.v = htx_get_blk_value(htx, blk);
1930
1931 if (istmatch(p.n, ist(":fcgi-"))) {
1932 p.n.ptr += 6;
1933 p.n.len -= 6;
1934 if (isteq(p.n, ist("gateway_interface")))
1935 params.mask |= FCGI_SP_CGI_GATEWAY;
1936 else if (isteq(p.n, ist("document_root"))) {
1937 params.mask |= FCGI_SP_DOC_ROOT;
1938 params.docroot = p.v;
1939 }
1940 else if (isteq(p.n, ist("script_name"))) {
1941 params.mask |= FCGI_SP_SCRIPT_NAME;
1942 params.scriptname = p.v;
1943 }
1944 else if (isteq(p.n, ist("path_info"))) {
1945 params.mask |= FCGI_SP_PATH_INFO;
1946 params.pathinfo = p.v;
1947 }
1948 else if (isteq(p.n, ist("request_uri"))) {
1949 params.mask |= FCGI_SP_REQ_URI;
1950 params.uri = p.v;
1951 }
1952 else if (isteq(p.n, ist("request_meth")))
1953 params.mask |= FCGI_SP_REQ_METH;
1954 else if (isteq(p.n, ist("query_string")))
1955 params.mask |= FCGI_SP_REQ_QS;
1956 else if (isteq(p.n, ist("server_name")))
1957 params.mask |= FCGI_SP_SRV_NAME;
1958 else if (isteq(p.n, ist("server_port")))
1959 params.mask |= FCGI_SP_SRV_PORT;
1960 else if (isteq(p.n, ist("server_protocol")))
1961 params.mask |= FCGI_SP_SRV_PROTO;
1962 else if (isteq(p.n, ist("remote_addr")))
1963 params.mask |= FCGI_SP_REM_ADDR;
1964 else if (isteq(p.n, ist("remote_port")))
1965 params.mask |= FCGI_SP_REM_PORT;
1966 else if (isteq(p.n, ist("script_filename")))
1967 params.mask |= FCGI_SP_SCRIPT_FILE;
1968 else if (isteq(p.n, ist("path_translated")))
1969 params.mask |= FCGI_SP_PATH_TRANS;
1970 else if (isteq(p.n, ist("https")))
1971 params.mask |= FCGI_SP_HTTPS;
1972 }
1973 else if (isteq(p.n, ist("content-length"))) {
1974 p.n = ist("CONTENT_LENGTH");
1975 params.mask |= FCGI_SP_CONT_LEN;
1976 }
1977 else if (isteq(p.n, ist("content-type")))
1978 p.n = ist("CONTENT_TYPE");
1979 else {
1980 if (isteq(p.n, ist("host")))
1981 params.srv_name = p.v;
1982
Christopher Faulet67d58092019-10-02 10:51:38 +02001983 /* Skip header if same name is used to add the server name */
1984 if (fconn->proxy->server_id_hdr_name &&
1985 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1986 break;
1987
Christopher Faulet99eff652019-08-11 23:11:30 +02001988 memcpy(trash.area, "http_", 5);
1989 memcpy(trash.area+5, p.n.ptr, p.n.len);
1990 p.n = ist2(trash.area, p.n.len+5);
1991 }
1992
1993 if (!fcgi_encode_param(&outbuf, &p)) {
1994 if (b_space_wraps(mbuf))
1995 goto realign_again;
1996 if (outbuf.data == 8)
1997 goto full;
1998 goto done;
1999 }
2000 break;
2001
2002 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002003 if (fconn->proxy->server_id_hdr_name) {
2004 struct server *srv = objt_server(fconn->conn->target);
2005
2006 if (!srv)
2007 goto done;
2008
2009 memcpy(trash.area, "http_", 5);
2010 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2011 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2012 p.v = ist(srv->id);
2013
2014 if (!fcgi_encode_param(&outbuf, &p)) {
2015 if (b_space_wraps(mbuf))
2016 goto realign_again;
2017 if (outbuf.data == 8)
2018 goto full;
2019 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002020 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002022 goto done;
2023
2024 default:
2025 break;
2026 }
2027 total += size;
2028 blk = htx_remove_blk(htx, blk);
2029 }
2030
2031 done:
2032 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2033 goto error;
2034
2035 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2047 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2048 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2049 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2051 goto error;
2052
2053 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002054 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002055 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2056 b_add(mbuf, outbuf.data);
2057
2058 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002059 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002060 return total;
2061 full:
2062 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2063 goto retry;
2064 fconn->flags |= FCGI_CF_MUX_MFULL;
2065 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002066 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 +02002067 if (total)
2068 goto error;
2069 goto end;
2070
2071 error:
2072 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002073 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002074 fcgi_strm_error(fstrm);
2075 goto end;
2076}
2077
2078/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2079 * anything. STDIN records contain the request body.
2080 */
2081static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2082 struct htx *htx, size_t count, struct buffer *buf)
2083{
2084 struct buffer outbuf;
2085 struct buffer *mbuf;
2086 struct htx_blk *blk;
2087 enum htx_blk_type type;
2088 uint32_t size;
2089 size_t total = 0;
2090
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002091 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002092 if (!count)
2093 goto end;
2094
2095 mbuf = br_tail(fconn->mbuf);
2096 retry:
2097 if (!fcgi_get_buf(fconn, mbuf)) {
2098 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002099 fstrm->flags |= FCGI_SF_BLK_MROOM;
2100 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2101 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002102 }
2103
2104 /* Perform some optimizations to reduce the number of buffer copies.
2105 * First, if the mux's buffer is empty and the htx area contains exactly
2106 * one data block of the same size as the requested count, and this
2107 * count fits within the record size, then it's possible to simply swap
2108 * the caller's buffer with the mux's output buffer and adjust offsets
2109 * and length to match the entire DATA HTX block in the middle. In this
2110 * case we perform a true zero-copy operation from end-to-end. This is
2111 * the situation that happens all the time with large files. Second, if
2112 * this is not possible, but the mux's output buffer is empty, we still
2113 * have an opportunity to avoid the copy to the intermediary buffer, by
2114 * making the intermediary buffer's area point to the output buffer's
2115 * area. In this case we want to skip the HTX header to make sure that
2116 * copies remain aligned and that this operation remains possible all
2117 * the time. This goes for headers, data blocks and any data extracted
2118 * from the HTX blocks.
2119 */
2120 blk = htx_get_head_blk(htx);
2121 if (!blk)
2122 goto end;
2123 type = htx_get_blk_type(blk);
2124 size = htx_get_blksz(blk);
2125 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2126 void *old_area = mbuf->area;
2127
2128 if (b_data(mbuf)) {
2129 /* Too bad there are data left there. We're willing to memcpy/memmove
2130 * up to 1/4 of the buffer, which means that it's OK to copy a large
2131 * record into a buffer containing few data if it needs to be realigned,
2132 * and that it's also OK to copy few data without realigning. Otherwise
2133 * we'll pretend the mbuf is full and wait for it to become empty.
2134 */
2135 if (size + 8 <= b_room(mbuf) &&
2136 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2137 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2138 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002139 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002140 }
2141
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002142 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 +02002143 /* map a FCGI record to the HTX block so that we can put the
2144 * record header there.
2145 */
2146 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2147 outbuf.area = b_head(mbuf);
2148
2149 /* prepend a FCGI record header just before the DATA block */
2150 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2151 fcgi_set_record_id(outbuf.area, fstrm->id);
2152 fcgi_set_record_size(outbuf.area, size);
2153
2154 /* and exchange with our old area */
2155 buf->area = old_area;
2156 buf->data = buf->head = 0;
2157 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002158
2159 htx = (struct htx *)buf->area;
2160 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002161 goto end;
2162 }
2163
2164 copy:
2165 while (1) {
2166 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2167 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2168 break;
2169 realign_again:
2170 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2171 }
2172
2173 if (outbuf.size < 8)
2174 goto full;
2175
2176 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2177 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2178 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2179 fcgi_set_record_id(outbuf.area, fstrm->id);
2180 outbuf.data = 8;
2181
2182 blk = htx_get_head_blk(htx);
2183 while (blk && count) {
2184 enum htx_blk_type type = htx_get_blk_type(blk);
2185 uint32_t size = htx_get_blksz(blk);
2186 struct ist v;
2187
2188 switch (type) {
2189 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002190 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 +02002191 v = htx_get_blk_value(htx, blk);
2192 if (v.len > count)
2193 v.len = count;
2194
2195 if (v.len > b_room(&outbuf)) {
2196 /* It doesn't fit at once. If it at least fits once split and
2197 * the amount of data to move is low, let's defragment the
2198 * buffer now.
2199 */
2200 if (b_space_wraps(mbuf) &&
2201 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2202 b_data(mbuf) <= MAX_DATA_REALIGN)
2203 goto realign_again;
2204 v.len = b_room(&outbuf);
2205 }
2206 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2207 if (outbuf.data == 8)
2208 goto full;
2209 goto done;
2210 }
2211 if (v.len != size) {
2212 total += v.len;
2213 count -= v.len;
2214 htx_cut_data_blk(htx, blk, v.len);
2215 goto done;
2216 }
2217 break;
2218
2219 case HTX_BLK_EOM:
2220 goto done;
2221
2222 default:
2223 break;
2224 }
2225 total += size;
2226 count -= size;
2227 blk = htx_remove_blk(htx, blk);
2228 }
2229
2230 done:
2231 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002232 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002233 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2234 b_add(mbuf, outbuf.data);
2235
2236 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002237 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002238 return total;
2239 full:
2240 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2241 goto retry;
2242 fconn->flags |= FCGI_CF_MUX_MFULL;
2243 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002244 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 +02002245 goto end;
2246}
2247
2248/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2249 * anything. STDOUT records contain the entire response. All the content is
2250 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2251 */
2252static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2253{
2254 struct buffer *dbuf;
2255 size_t ret;
2256 size_t max;
2257
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002258 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2259
Christopher Faulet99eff652019-08-11 23:11:30 +02002260 dbuf = &fconn->dbuf;
2261
2262 /* Only padding remains */
2263 if (fconn->state == FCGI_CS_RECORD_P)
2264 goto end_transfer;
2265
2266 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2267 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2268 buf_room_for_htx_data(dbuf))
2269 goto fail; // incomplete record
2270
2271 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2272 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002273 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2274 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002275 }
2276
2277 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2278 max = buf_room_for_htx_data(&fstrm->rxbuf);
2279 if (!b_data(&fstrm->rxbuf))
2280 fstrm->rxbuf.head = sizeof(struct htx);
2281 if (max > fconn->drl)
2282 max = fconn->drl;
2283
2284 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2285 if (!ret)
2286 goto fail;
2287 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002288 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2289 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002290
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002291 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002292 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002293 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2294 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002295
2296 if (fconn->drl)
2297 goto fail;
2298
2299 end_transfer:
2300 fconn->drl += fconn->drp;
2301 fconn->drp = 0;
2302 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2303 b_del(&fconn->dbuf, ret);
2304 fconn->drl -= ret;
2305 if (fconn->drl)
2306 goto fail;
2307
2308 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002309 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2310 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002311 return 1;
2312 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002313 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 +02002314 return 0;
2315}
2316
2317
2318/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2319 * anything. It only skip the padding in fact, there is no payload for such
2320 * records. It makrs the end of the response.
2321 */
2322static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2323{
2324 int ret;
2325
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002326 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2327
Christopher Faulet99eff652019-08-11 23:11:30 +02002328 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002329 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002330 fconn->drl += fconn->drp;
2331 fconn->drp = 0;
2332 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2333 b_del(&fconn->dbuf, ret);
2334 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002335 if (fconn->drl) {
2336 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 +02002337 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002338 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002339 fconn->state = FCGI_CS_RECORD_H;
2340 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002341 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2342 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);
2343 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002344 return 1;
2345}
2346
2347/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2348 * anything.
2349 */
2350static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2351{
2352 struct buffer *dbuf;
2353 struct buffer tag;
2354 size_t ret;
2355
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002356 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002357 dbuf = &fconn->dbuf;
2358
2359 /* Only padding remains */
2360 if (fconn->state == FCGI_CS_RECORD_P)
2361 goto end_transfer;
2362
2363 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2364 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2365 buf_room_for_htx_data(dbuf))
2366 goto fail; // incomplete record
2367
2368 chunk_reset(&trash);
2369 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2370 if (!ret)
2371 goto fail;
2372 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002373 TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002374
2375 trash.area[ret] = '\n';
2376 trash.area[ret+1] = '\0';
2377 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002378 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002379
2380 if (fconn->drl)
2381 goto fail;
2382
2383 end_transfer:
2384 fconn->drl += fconn->drp;
2385 fconn->drp = 0;
2386 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2387 b_del(&fconn->dbuf, ret);
2388 fconn->drl -= ret;
2389 if (fconn->drl)
2390 goto fail;
2391 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2393 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002394 return 1;
2395 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 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 +02002397 return 0;
2398}
2399
2400/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2401 * anything. If the empty STDOUT record is not already received, this one marks
2402 * the end of the response. It is highly unexpected, but if the record is larger
2403 * than a buffer and cannot be decoded in one time, an error is triggered and
2404 * the connection is closed. END_REQUEST record cannot be split.
2405 */
2406static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2407{
2408 struct buffer inbuf;
2409 struct buffer *dbuf;
2410 struct fcgi_end_request endreq;
2411
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002413 dbuf = &fconn->dbuf;
2414
2415 /* Record too large to be fully decoded */
2416 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2417 goto fail;
2418
2419 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2421 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002422 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002423 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002424
2425 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2426 /* Realign the dmux buffer if the record wraps. It is unexpected
2427 * at this stage because it should be the first record received
2428 * from the FCGI application.
2429 */
2430 b_slow_realign(dbuf, trash.area, 0);
2431 }
2432
2433 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2434
2435 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2436 goto fail;
2437
2438 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002439 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2440 TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm,, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02002441 fstrm->proto_status = endreq.errcode;
2442 fcgi_strm_close(fstrm);
2443
2444 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2445 fconn->drl = 0;
2446 fconn->drp = 0;
2447 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002448 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2449 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002450 return 1;
2451
2452 fail:
2453 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002454 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 +02002455 return 0;
2456}
2457
2458/* process Rx records to be demultiplexed */
2459static void fcgi_process_demux(struct fcgi_conn *fconn)
2460{
2461 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2462 struct fcgi_header hdr;
2463 int ret;
2464
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002465 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2466
Christopher Faulet99eff652019-08-11 23:11:30 +02002467 if (fconn->state == FCGI_CS_CLOSED)
2468 return;
2469
2470 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 if (fconn->state == FCGI_CS_INIT) {
2472 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2473 return;
2474 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002475 if (fconn->state == FCGI_CS_SETTINGS) {
2476 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002477 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002478 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2479 if (!ret)
2480 goto fail;
2481 b_del(&fconn->dbuf, ret);
2482
2483 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2484 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002485 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);
2486 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 +02002487 goto fail;
2488 }
2489 goto new_record;
2490 }
2491 }
2492
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002493 /* process as many incoming records as possible below */
2494 while (1) {
2495 if (!b_data(&fconn->dbuf)) {
2496 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2497 break;
2498 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002499
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002500 if (fconn->state == FCGI_CS_CLOSED) {
2501 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002502 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002503 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002504
2505 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002506 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002507 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2508 if (!ret)
2509 break;
2510 b_del(&fconn->dbuf, ret);
2511
2512 new_record:
2513 fconn->dsi = hdr.id;
2514 fconn->drt = hdr.type;
2515 fconn->drl = hdr.len;
2516 fconn->drp = hdr.padding;
2517 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002518 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 +02002519 }
2520
2521 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2522 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2523
2524 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2525 (b_data(&fstrm->rxbuf) ||
2526 conn_xprt_read0_pending(fconn->conn) ||
2527 fstrm->state == FCGI_SS_CLOSED ||
2528 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2529 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2530 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002531 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 +02002532 fstrm->cs->flags |= CS_FL_RCV_MORE;
2533 fcgi_strm_notify_recv(fstrm);
2534 }
2535 fstrm = tmp_fstrm;
2536
2537 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2538 /* ignore all record for closed streams */
2539 goto ignore_record;
2540 }
2541 if (fstrm->state == FCGI_SS_IDLE) {
2542 /* ignore all record for unknown streams */
2543 goto ignore_record;
2544 }
2545
2546 switch (fconn->drt) {
2547 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002548 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 +02002549 ret = fcgi_conn_handle_values_result(fconn);
2550 break;
2551
2552 case FCGI_STDOUT:
2553 if (fstrm->flags & FCGI_SF_ES_RCVD)
2554 goto ignore_record;
2555
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002556 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002557 if (fconn->drl)
2558 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2559 else
2560 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2561 break;
2562
2563 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002564 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002565 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2566 break;
2567
2568 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002569 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 +02002570 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2571 break;
2572
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002573 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002574 default:
2575 ignore_record:
2576 /* drop records that we ignore. They may be
2577 * larger than the buffer so we drain all of
2578 * their contents until we reach the end.
2579 */
2580 fconn->state = FCGI_CS_RECORD_P;
2581 fconn->drl += fconn->drp;
2582 fconn->drp = 0;
2583 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002584 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2585 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002586 b_del(&fconn->dbuf, ret);
2587 fconn->drl -= ret;
2588 ret = (fconn->drl == 0);
2589 }
2590
2591 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002592 if (ret <= 0) {
2593 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002594 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002595 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002596
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002597 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002598 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002599 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2600 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002601 }
2602
2603 fail:
2604 /* we can go here on missing data, blocked response or error */
2605 if (fstrm && fstrm->cs &&
2606 (b_data(&fstrm->rxbuf) ||
2607 conn_xprt_read0_pending(fconn->conn) ||
2608 fstrm->state == FCGI_SS_CLOSED ||
2609 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2610 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2611 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002612 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 +02002613 fstrm->cs->flags |= CS_FL_RCV_MORE;
2614 fcgi_strm_notify_recv(fstrm);
2615 }
2616
2617 fcgi_conn_restart_reading(fconn, 0);
2618}
2619
2620/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2621 * the end.
2622 */
2623static int fcgi_process_mux(struct fcgi_conn *fconn)
2624{
2625 struct fcgi_strm *fstrm, *fstrm_back;
2626
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002627 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2628
Christopher Faulet99eff652019-08-11 23:11:30 +02002629 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2630 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2631 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2632 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002633 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 +02002634 fcgi_wake_unassigned_streams(fconn);
2635 goto mux;
2636 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002638 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2639 goto fail;
2640 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002641 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 +02002642 }
2643 /* need to wait for the other side */
2644 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002645 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002646 }
2647
2648 mux:
2649 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2650 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2651 break;
2652
2653 if (LIST_ADDED(&fstrm->sending_list))
2654 continue;
2655
2656 /* For some reason, the upper layer failed to subsribe again,
2657 * so remove it from the send_list
2658 */
2659 if (!fstrm->send_wait) {
2660 LIST_DEL_INIT(&fstrm->send_list);
2661 continue;
2662 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002663 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002664 fstrm->flags &= ~FCGI_SF_BLK_ANY;
2665 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
2666 LIST_ADDQ(&fconn->sending_list, &fstrm->sending_list);
2667 tasklet_wakeup(fstrm->send_wait->tasklet);
2668 }
2669
2670 fail:
2671 if (fconn->state == FCGI_CS_CLOSED) {
2672 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2673 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002674 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2675 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002676 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002677 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 }
2679 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002680
2681 done:
2682 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002683 return 1;
2684}
2685
2686
2687/* Attempt to read data, and subscribe if none available.
2688 * The function returns 1 if data has been received, otherwise zero.
2689 */
2690static int fcgi_recv(struct fcgi_conn *fconn)
2691{
2692 struct connection *conn = fconn->conn;
2693 struct buffer *buf;
2694 int max;
2695 size_t ret;
2696
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002697 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2698
2699 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2700 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002701 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002702 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002703
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002704 if (!fcgi_recv_allowed(fconn)) {
2705 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002706 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002707 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002708
2709 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2710 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002711 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002712 fconn->flags |= FCGI_CF_DEM_DALLOC;
2713 return 0;
2714 }
2715
2716 b_realign_if_empty(buf);
2717 if (!b_data(buf)) {
2718 /* try to pre-align the buffer like the
2719 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002720 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002721 * HTX block to alias it upon recv. We cannot use the
2722 * head because rcv_buf() will realign the buffer if
2723 * it's empty. Thus we cheat and pretend we already
2724 * have a few bytes there.
2725 */
2726 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2727 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2728 }
2729 else
2730 max = buf_room_for_htx_data(buf);
2731
2732 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2733
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002734 if (max && !ret && fcgi_recv_allowed(fconn)) {
2735 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002736 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002737 }
2738 else
2739 TRACE_DATA("send data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002740
2741 if (!b_data(buf)) {
2742 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002743 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002744 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2745 }
2746
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002747 if (ret == max) {
2748 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002749 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002750 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002751
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002752 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002753 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2754}
2755
2756
2757/* Try to send data if possible.
2758 * The function returns 1 if data have been sent, otherwise zero.
2759 */
2760static int fcgi_send(struct fcgi_conn *fconn)
2761{
2762 struct connection *conn = fconn->conn;
2763 int done;
2764 int sent = 0;
2765
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002766 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2767
2768 if (conn->flags & CO_FL_ERROR) {
2769 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002770 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002771 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002772
2773
2774 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2775 /* a handshake was requested */
2776 goto schedule;
2777 }
2778
2779 /* This loop is quite simple : it tries to fill as much as it can from
2780 * pending streams into the existing buffer until it's reportedly full
2781 * or the end of send requests is reached. Then it tries to send this
2782 * buffer's contents out, marks it not full if at least one byte could
2783 * be sent, and tries again.
2784 *
2785 * The snd_buf() function normally takes a "flags" argument which may
2786 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2787 * data immediately comes and CO_SFL_STREAMER to indicate that the
2788 * connection is streaming lots of data (used to increase TLS record
2789 * size at the expense of latency). The former can be sent any time
2790 * there's a buffer full flag, as it indicates at least one stream
2791 * attempted to send and failed so there are pending data. An
2792 * alternative would be to set it as long as there's an active stream
2793 * but that would be problematic for ACKs until we have an absolute
2794 * guarantee that all waiters have at least one byte to send. The
2795 * latter should possibly not be set for now.
2796 */
2797
2798 done = 0;
2799 while (!done) {
2800 unsigned int flags = 0;
2801 unsigned int released = 0;
2802 struct buffer *buf;
2803
2804 /* fill as much as we can into the current buffer */
2805 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2806 done = fcgi_process_mux(fconn);
2807
2808 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2809 done = 1; // we won't go further without extra buffers
2810
2811 if (conn->flags & CO_FL_ERROR)
2812 break;
2813
2814 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2815 flags |= CO_SFL_MSG_MORE;
2816
2817 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2818 if (b_data(buf)) {
2819 int ret;
2820
2821 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2822 if (!ret) {
2823 done = 1;
2824 break;
2825 }
2826 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002827 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002828 b_del(buf, ret);
2829 if (b_data(buf)) {
2830 done = 1;
2831 break;
2832 }
2833 }
2834 b_free(buf);
2835 released++;
2836 }
2837
2838 if (released)
2839 offer_buffers(NULL, tasks_run_queue);
2840
2841 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002842 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2843 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002844 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2845 }
2846
2847 if (conn->flags & CO_FL_SOCK_WR_SH) {
2848 /* output closed, nothing to send, clear the buffer to release it */
2849 b_reset(br_tail(fconn->mbuf));
2850 }
2851 /* We're not full anymore, so we can wake any task that are waiting
2852 * for us.
2853 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002854 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002855 struct fcgi_strm *fstrm;
2856
2857 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2858 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2859 break;
2860
2861 if (LIST_ADDED(&fstrm->sending_list))
2862 continue;
2863
2864 /* For some reason, the upper layer failed to subsribe again,
2865 * so remove it from the send_list
2866 */
2867 if (!fstrm->send_wait) {
2868 LIST_DEL_INIT(&fstrm->send_list);
2869 continue;
2870 }
2871 fstrm->flags &= ~FCGI_SF_BLK_ANY;
2872 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002873 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002874 tasklet_wakeup(fstrm->send_wait->tasklet);
2875 LIST_ADDQ(&fconn->sending_list, &fstrm->sending_list);
2876 }
2877 }
2878 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002879 if (!br_data(fconn->mbuf)) {
2880 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002881 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002882 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002883schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002884 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2885 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002886 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002887 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002888
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002889 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002890 return sent;
2891}
2892
2893/* this is the tasklet referenced in fconn->wait_event.tasklet */
2894static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2895{
2896 struct fcgi_conn *fconn = ctx;
2897 int ret = 0;
2898
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002899 TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn);
2900
Christopher Faulet99eff652019-08-11 23:11:30 +02002901 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2902 ret = fcgi_send(fconn);
2903 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2904 ret |= fcgi_recv(fconn);
2905 if (ret || b_data(&fconn->dbuf))
2906 fcgi_process(fconn);
2907 return NULL;
2908}
2909
2910/* callback called on any event by the connection handler.
2911 * It applies changes and returns zero, or < 0 if it wants immediate
2912 * destruction of the connection (which normally doesn not happen in FCGI).
2913 */
2914static int fcgi_process(struct fcgi_conn *fconn)
2915{
2916 struct connection *conn = fconn->conn;
2917
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002918 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2919
Christopher Faulet99eff652019-08-11 23:11:30 +02002920 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2921 fcgi_process_demux(fconn);
2922
2923 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2924 b_reset(&fconn->dbuf);
2925
2926 if (buf_room_for_htx_data(&fconn->dbuf))
2927 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2928 }
2929 fcgi_send(fconn);
2930
2931 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2932 /* frontend is stopping, reload likely in progress, let's try
2933 * to announce a graceful shutdown if not yet done. We don't
2934 * care if it fails, it will be tried again later.
2935 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002936 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 +02002937 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2938 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2939 fcgi_conn_send_aborts(fconn);
2940 }
2941 }
2942
2943 /*
2944 * If we received early data, and the handshake is done, wake
2945 * any stream that was waiting for it.
2946 */
2947 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
2948 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2949 struct eb32_node *node;
2950 struct fcgi_strm *fstrm;
2951
2952 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
2953 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
2954
2955 while (node) {
2956 fstrm = container_of(node, struct fcgi_strm, by_id);
2957 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
2958 fcgi_strm_notify_recv(fstrm);
2959 node = eb32_next(node);
2960 }
2961 }
2962
2963 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
2964 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2965 eb_is_empty(&fconn->streams_by_id)) {
2966 fcgi_wake_some_streams(fconn, 0);
2967
2968 if (eb_is_empty(&fconn->streams_by_id)) {
2969 /* no more stream, kill the connection now */
2970 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002971 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002972 return -1;
2973 }
2974 }
2975
2976 if (!b_data(&fconn->dbuf))
2977 fcgi_release_buf(fconn, &fconn->dbuf);
2978
2979 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2980 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2981 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
2982 fcgi_release_mbuf(fconn);
2983
2984 if (fconn->task) {
2985 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
2986 task_queue(fconn->task);
2987 }
2988
2989 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002990 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002991 return 0;
2992}
2993
2994
2995/* wake-up function called by the connection layer (mux_ops.wake) */
2996static int fcgi_wake(struct connection *conn)
2997{
2998 struct fcgi_conn *fconn = conn->ctx;
2999
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003000 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003001 return (fcgi_process(fconn));
3002}
3003
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003004
3005static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3006{
3007 int ret = 0;
3008 switch (mux_ctl) {
3009 case MUX_STATUS:
3010 if (conn->flags & CO_FL_CONNECTED)
3011 ret |= MUX_STATUS_READY;
3012 return ret;
3013 default:
3014 return -1;
3015 }
3016}
3017
Christopher Faulet99eff652019-08-11 23:11:30 +02003018/* Connection timeout management. The principle is that if there's no receipt
3019 * nor sending for a certain amount of time, the connection is closed. If the
3020 * MUX buffer still has lying data or is not allocatable, the connection is
3021 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003022 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003023 */
3024static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3025{
3026 struct fcgi_conn *fconn = context;
3027 int expired = tick_is_expired(t->expire, now_ms);
3028
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003029 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3030
3031 if (!expired && fconn) {
3032 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003033 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003034 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003035
3036 task_destroy(t);
3037
3038 if (!fconn) {
3039 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003040 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003041 return NULL;
3042 }
3043
3044 fconn->task = NULL;
3045 fconn->state = FCGI_CS_CLOSED;
3046 fcgi_wake_some_streams(fconn, 0);
3047
3048 if (br_data(fconn->mbuf)) {
3049 /* don't even try to send aborts, the buffer is stuck */
3050 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3051 goto end;
3052 }
3053
3054 /* try to send but no need to insist */
3055 if (!fcgi_conn_send_aborts(fconn))
3056 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3057
3058 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3059 conn_xprt_ready(fconn->conn)) {
3060 unsigned int released = 0;
3061 struct buffer *buf;
3062
3063 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3064 if (b_data(buf)) {
3065 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3066 buf, b_data(buf), 0);
3067 if (!ret)
3068 break;
3069 b_del(buf, ret);
3070 if (b_data(buf))
3071 break;
3072 b_free(buf);
3073 released++;
3074 }
3075 }
3076
3077 if (released)
3078 offer_buffers(NULL, tasks_run_queue);
3079 }
3080
3081 end:
3082 /* either we can release everything now or it will be done later once
3083 * the last stream closes.
3084 */
3085 if (eb_is_empty(&fconn->streams_by_id))
3086 fcgi_release(fconn);
3087
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003088 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003089 return NULL;
3090}
3091
3092
3093/*******************************************/
3094/* functions below are used by the streams */
3095/*******************************************/
3096
3097/* Append the description of what is present in error snapshot <es> into <out>.
3098 * The description must be small enough to always fit in a buffer. The output
3099 * buffer may be the trash so the trash must not be used inside this function.
3100 */
3101static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3102{
3103 chunk_appendf(out,
3104 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3105 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3106 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3107 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3108 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3109 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3110}
3111/*
3112 * Capture a bad response and archive it in the proxy's structure. By default
3113 * it tries to report the error position as h1m->err_pos. However if this one is
3114 * not set, it will then report h1m->next, which is the last known parsing
3115 * point. The function is able to deal with wrapping buffers. It always displays
3116 * buffers as a contiguous area starting at buf->p. The direction is determined
3117 * thanks to the h1m's flags.
3118 */
3119static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3120 struct h1m *h1m, struct buffer *buf)
3121{
3122 struct session *sess = fstrm->sess;
3123 struct proxy *proxy = fconn->proxy;
3124 struct proxy *other_end = sess->fe;
3125 union error_snapshot_ctx ctx;
3126
3127 /* http-specific part now */
3128 ctx.h1.state = h1m->state;
3129 ctx.h1.c_flags = fconn->flags;
3130 ctx.h1.s_flags = fstrm->flags;
3131 ctx.h1.m_flags = h1m->flags;
3132 ctx.h1.m_clen = h1m->curr_len;
3133 ctx.h1.m_blen = h1m->body_len;
3134
3135 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3136 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3137 &ctx, fcgi_show_error_snapshot);
3138}
3139
3140static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3141 struct buffer *buf, size_t *ofs, size_t max)
3142{
3143 int ret;
3144
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003145 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003146 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3147 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003148 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 +02003149 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003150 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 +02003151 fcgi_strm_error(fstrm);
3152 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3153 }
3154 goto end;
3155 }
3156
3157 *ofs += ret;
3158 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003159 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003160 return ret;
3161
3162}
3163
Christopher Fauletaf542632019-10-01 21:52:49 +02003164static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003165 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3166{
3167 int ret;
3168
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003169 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003170 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
3171 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003172 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 +02003173 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003174 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 +02003175 fcgi_strm_error(fstrm);
3176 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3177 }
3178 goto end;
3179 }
3180 *ofs += ret;
3181 end:
Christopher Fauletf950c2e2019-12-06 16:20:49 +01003182 if (h1m->state == H1_MSG_DONE) {
3183 fstrm->flags &= ~FCGI_SF_APPEND_EOM;
3184 TRACE_STATE("end of message", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn);
3185 }
3186 else if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_XFER_LEN) && h1m->curr_len == 0) {
3187 fstrm->flags |= FCGI_SF_APPEND_EOM;
3188 TRACE_STATE("add append_eom", FCGI_EV_RSP_DATA, fstrm->fconn->conn);
3189 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003190 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003191 return ret;
3192}
3193
3194static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3195 struct buffer *buf, size_t *ofs, size_t max)
3196{
3197 int ret;
3198
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003199 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003200 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
3201 if (ret <= 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003202 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 +02003203 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003204 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 +02003205 fcgi_strm_error(fstrm);
3206 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3207 }
3208 goto end;
3209 }
3210 *ofs += ret;
3211 fstrm->flags |= FCGI_SF_HAVE_I_TLR;
3212 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003213 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003214 return ret;
3215}
3216
3217static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3218 size_t max)
3219{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003220 TRACE_ENTER(FCGI_EV_RSP_DATA, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Fauletf950c2e2019-12-06 16:20:49 +01003221 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) {
3222 fstrm->flags |= FCGI_SF_APPEND_EOM;
3223 TRACE_STATE("leaving on append_eom", FCGI_EV_RSP_DATA, fstrm->fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003224 return 0;
Christopher Fauletf950c2e2019-12-06 16:20:49 +01003225 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003226
3227 h1m->state = H1_MSG_DONE;
Christopher Fauletf950c2e2019-12-06 16:20:49 +01003228 fstrm->flags &= ~FCGI_SF_APPEND_EOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003229 TRACE_STATE("end of response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3230 TRACE_LEAVE(FCGI_EV_RSP_DATA, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003231 return (sizeof(struct htx_blk) + 1);
3232}
3233
3234static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3235{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003236 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003237 struct htx *htx;
3238 struct h1m *h1m = &fstrm->h1m;
3239 size_t ret, data, total = 0;
3240
3241 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003242 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3243
Christopher Faulet99eff652019-08-11 23:11:30 +02003244 data = htx->data;
3245 if (fstrm->state == FCGI_SS_ERROR)
3246 goto end;
3247
3248 do {
3249 size_t used = htx_used_space(htx);
3250
3251 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003252 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003253 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3254 if (!ret)
3255 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003256
3257 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3258
Christopher Faulet99eff652019-08-11 23:11:30 +02003259 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3260 struct htx_blk *blk = htx_get_head_blk(htx);
3261 struct htx_sl *sl;
3262
3263 if (!blk)
3264 break;
3265 sl = htx_get_blk_ptr(htx, blk);
3266 sl->flags |= HTX_SL_F_XFER_LEN;
3267 htx->extra = 0;
3268 }
3269 }
3270 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003271 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003272 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet99eff652019-08-11 23:11:30 +02003273 if (!ret)
3274 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003275
3276 TRACE_PROTO("rcvd response payload data", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm, htx);
3277
3278 if (h1m->state == H1_MSG_DONE)
3279 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 +02003280 }
3281 else if (h1m->state == H1_MSG_TRAILERS) {
3282 if (!(fstrm->flags & FCGI_SF_HAVE_I_TLR)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003283 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003284 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3285 if (!ret)
3286 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003287
3288 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 +02003289 }
3290 else if (!fcgi_strm_add_eom(fstrm, h1m, htx, count))
3291 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003292
3293 if (h1m->state == H1_MSG_DONE)
3294 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 +02003295 }
3296 else if (h1m->state == H1_MSG_DONE) {
3297 if (b_data(&fstrm->rxbuf) > total) {
3298 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003299 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003300 fcgi_strm_error(fstrm);
3301 }
3302 break;
3303 }
3304 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003305 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003306 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet99eff652019-08-11 23:11:30 +02003307 if (fstrm->state != FCGI_SS_ERROR &&
3308 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003309 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3311 if (!fcgi_strm_add_eom(fstrm, h1m, htx, count))
3312 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003313 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 +02003314 }
3315 else {
3316 h1m->state = H1_MSG_DONE;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003317 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 +02003318 break;
3319 }
3320 }
3321 if (!ret)
3322 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003323
3324 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003325 }
3326 else {
3327 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003328 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003329 fcgi_strm_error(fstrm);
3330 break;
3331 }
3332
3333 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003334 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003335
3336 if (fstrm->state == FCGI_SS_ERROR) {
3337 b_reset(&fstrm->rxbuf);
3338 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003339 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003340 return 0;
3341 }
3342
3343 b_del(&fstrm->rxbuf, total);
3344
3345 end:
3346 htx_to_buf(htx, buf);
3347 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003348 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003349 return ret;
3350}
3351
3352/*
3353 * Attach a new stream to a connection
3354 * (Used for outgoing connections)
3355 */
3356static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3357{
3358 struct conn_stream *cs;
3359 struct fcgi_strm *fstrm;
3360 struct fcgi_conn *fconn = conn->ctx;
3361
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003362 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003363 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003364 if (!cs) {
3365 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003366 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003367 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003368 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3369 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003370 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003371 cs_free(cs);
3372 return NULL;
3373 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003374 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003375 return cs;
3376}
3377
3378/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3379 * We have to scan because we may have some orphan streams. It might be
3380 * beneficial to scan backwards from the end to reduce the likeliness to find
3381 * orphans.
3382 */
3383static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3384{
3385 struct fcgi_conn *fconn = conn->ctx;
3386 struct fcgi_strm *fstrm;
3387 struct eb32_node *node;
3388
3389 node = eb32_first(&fconn->streams_by_id);
3390 while (node) {
3391 fstrm = container_of(node, struct fcgi_strm, by_id);
3392 if (fstrm->cs)
3393 return fstrm->cs;
3394 node = eb32_next(node);
3395 }
3396 return NULL;
3397}
3398
3399/*
3400 * Destroy the mux and the associated connection, if it is no longer used
3401 */
3402static void fcgi_destroy(void *ctx)
3403{
3404 struct fcgi_conn *fconn = ctx;
3405
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003406 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003407 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3408 fcgi_release(fconn);
3409}
3410
3411/*
3412 * Detach the stream from the connection and possibly release the connection.
3413 */
3414static void fcgi_detach(struct conn_stream *cs)
3415{
3416 struct fcgi_strm *fstrm = cs->ctx;
3417 struct fcgi_conn *fconn;
3418 struct session *sess;
3419
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003420 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3421
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003423 if (!fstrm) {
3424 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003425 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003426 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003427
3428 /* The stream is about to die, so no need to attempt to run its task */
3429 if (LIST_ADDED(&fstrm->sending_list) &&
3430 fstrm->send_wait != &fstrm->wait_event) {
3431 tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet);
3432 LIST_DEL_INIT(&fstrm->sending_list);
3433 /*
3434 * At this point, the stream_interface is supposed to have called
3435 * fcgi_unsubscribe(), so the only way there's still a
3436 * subscription that came from the stream_interface (as we
3437 * can subscribe ourself, in fcgi_do_shutw() and fcgi_do_shutr(),
3438 * without the stream_interface involved) is that we subscribed
3439 * for sending, we woke the tasklet up and removed the
3440 * SUB_RETRY_SEND flag, so the stream_interface would not
3441 * know it has to unsubscribe for send, but the tasklet hasn't
3442 * run yet. Make sure to handle that by explicitely setting
3443 * send_wait to NULL, as nothing else will do it for us.
3444 */
3445 fstrm->send_wait = NULL;
3446 }
3447
3448 sess = fstrm->sess;
3449 fconn = fstrm->fconn;
3450 fstrm->cs = NULL;
3451 fconn->nb_cs--;
3452
3453 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3454 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3455 fconn->streams_limit = 1;
3456 }
3457 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3458 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3459 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3460 fconn->state = FCGI_CS_CLOSED;
3461 }
3462
3463 /* this stream may be blocked waiting for some data to leave, so orphan
3464 * it in this case.
3465 */
3466 if (!(cs->conn->flags & CO_FL_ERROR) &&
3467 (fconn->state != FCGI_CS_CLOSED) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003468 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) && (fstrm->send_wait || fstrm->recv_wait)) {
3469 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003470 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003471 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003472
3473 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3474 /* unblock the connection if it was blocked on this stream. */
3475 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3476 fcgi_conn_restart_reading(fconn, 1);
3477 }
3478
3479 fcgi_strm_destroy(fstrm);
3480
3481 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3482 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
3483 if (!fconn->conn->owner) {
3484 fconn->conn->owner = sess;
3485 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3486 fconn->conn->owner = NULL;
3487 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003488 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 /* The server doesn't want it, let's kill the connection right away */
3490 fconn->conn->mux->destroy(fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003491 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3492 }
3493 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003494 return;
3495 }
3496 }
3497 }
3498 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003499 int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn);
3500 if (ret == -1) {
3501 /* The connection is destroyed, let's leave */
3502 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003503 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003504 }
3505 else if (ret == 1) {
3506 /* The connection was added to the server idle list, just stop */
3507 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3508 return;
3509 }
3510 TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003511 }
3512 /* Never ever allow to reuse a connection from a non-reuse backend */
3513 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3514 fconn->conn->flags |= CO_FL_PRIVATE;
3515 if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) {
3516 struct server *srv = objt_server(fconn->conn->target);
3517
3518 if (srv) {
3519 if (fconn->conn->flags & CO_FL_PRIVATE)
3520 LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list);
3521 else
3522 LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list);
3523 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003524 TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003525 }
3526 }
3527
3528 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003529 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003530 */
3531 if (fcgi_conn_is_dead(fconn)) {
3532 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003533 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003534 fcgi_release(fconn);
3535 }
3536 else if (fconn->task) {
3537 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3538 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003539 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003540 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003541 else
3542 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003543}
3544
3545
3546/* Performs a synchronous or asynchronous shutr(). */
3547static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3548{
3549 struct fcgi_conn *fconn = fstrm->fconn;
3550 struct wait_event *sw = &fstrm->wait_event;
3551
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003552 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3553
Christopher Faulet99eff652019-08-11 23:11:30 +02003554 if (fstrm->state == FCGI_SS_CLOSED)
3555 goto done;
3556
3557 /* a connstream may require us to immediately kill the whole connection
3558 * for example because of a "tcp-request content reject" rule that is
3559 * normally used to limit abuse.
3560 */
3561 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003562 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3563 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003564 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003565 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003566 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003567 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003568 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3569 !fcgi_strm_send_abort(fconn, fstrm))
3570 goto add_to_list;
3571 }
3572
3573 fcgi_strm_close(fstrm);
3574
3575 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3576 tasklet_wakeup(fconn->wait_event.tasklet);
3577 done:
3578 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003579 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003580 return;
3581
3582 add_to_list:
3583 if (!LIST_ADDED(&fstrm->send_list)) {
3584 sw->events |= SUB_RETRY_SEND;
3585 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
3586 fstrm->send_wait = sw;
3587 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3588 }
3589 }
3590 /* Let the handler know we want shutr */
3591 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003592 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003593 return;
3594}
3595
3596/* Performs a synchronous or asynchronous shutw(). */
3597static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3598{
3599 struct fcgi_conn *fconn = fstrm->fconn;
3600 struct wait_event *sw = &fstrm->wait_event;
3601
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003602 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3603
Christopher Faulet99eff652019-08-11 23:11:30 +02003604 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3605 goto done;
3606
3607 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3608 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3609 !fcgi_strm_send_abort(fconn, fstrm))
3610 goto add_to_list;
3611
3612 if (fstrm->state == FCGI_SS_HREM)
3613 fcgi_strm_close(fstrm);
3614 else
3615 fstrm->state = FCGI_SS_HLOC;
3616 } else {
3617 /* a connstream may require us to immediately kill the whole connection
3618 * for example because of a "tcp-request content reject" rule that is
3619 * normally used to limit abuse.
3620 */
3621 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003622 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3623 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003624 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003625 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003626
3627 fcgi_strm_close(fstrm);
3628 }
3629
3630 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3631 tasklet_wakeup(fconn->wait_event.tasklet);
3632 done:
3633 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003634 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 return;
3636
3637 add_to_list:
3638 if (!LIST_ADDED(&fstrm->send_list)) {
3639 sw->events |= SUB_RETRY_SEND;
3640 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
3641 fstrm->send_wait = sw;
3642 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3643 }
3644 }
3645 /* let the handler know we want to shutw */
3646 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003647 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003648 return;
3649}
3650
3651/* This is the tasklet referenced in fstrm->wait_event.tasklet, it is used for
3652 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003653 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 */
3655static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3656{
3657 struct fcgi_strm *fstrm = ctx;
3658 struct fcgi_conn *fconn = fstrm->fconn;
3659
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003660 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3661
Christopher Faulet99eff652019-08-11 23:11:30 +02003662 LIST_DEL_INIT(&fstrm->sending_list);
3663 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3664 fcgi_do_shutw(fstrm);
3665
3666 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3667 fcgi_do_shutr(fstrm);
3668
3669 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3670 /* We're done trying to send, remove ourself from the send_list */
3671 LIST_DEL_INIT(&fstrm->send_list);
3672
3673 if (!fstrm->cs) {
3674 fcgi_strm_destroy(fstrm);
3675 if (fcgi_conn_is_dead(fconn))
3676 fcgi_release(fconn);
3677 }
3678 }
3679
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003680 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003681 return NULL;
3682}
3683
3684/* shutr() called by the conn_stream (mux_ops.shutr) */
3685static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3686{
3687 struct fcgi_strm *fstrm = cs->ctx;
3688
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003689 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003690 if (cs->flags & CS_FL_KILL_CONN)
3691 fstrm->flags |= FCGI_SF_KILL_CONN;
3692
3693 if (!mode)
3694 return;
3695
3696 fcgi_do_shutr(fstrm);
3697}
3698
3699/* shutw() called by the conn_stream (mux_ops.shutw) */
3700static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3701{
3702 struct fcgi_strm *fstrm = cs->ctx;
3703
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003704 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 if (cs->flags & CS_FL_KILL_CONN)
3706 fstrm->flags |= FCGI_SF_KILL_CONN;
3707
3708 fcgi_do_shutw(fstrm);
3709}
3710
3711/* Called from the upper layer, to subscribe to events, such as being able to send.
3712 * The <param> argument here is supposed to be a pointer to a wait_event struct
3713 * which will be passed to fstrm->recv_wait or fstrm->send_wait depending on the
3714 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
3715 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
3716 * returns 0 except for the error above.
3717 */
3718static int fcgi_subscribe(struct conn_stream *cs, int event_type, void *param)
3719{
3720 struct wait_event *sw;
3721 struct fcgi_strm *fstrm = cs->ctx;
3722 struct fcgi_conn *fconn = fstrm->fconn;
3723
3724 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003725 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003726 sw = param;
3727 BUG_ON(fstrm->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
3728 sw->events |= SUB_RETRY_RECV;
3729 fstrm->recv_wait = sw;
3730 event_type &= ~SUB_RETRY_RECV;
3731 }
3732 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003733 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003734 sw = param;
3735 BUG_ON(fstrm->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
3736 sw->events |= SUB_RETRY_SEND;
3737 fstrm->send_wait = sw;
3738 if (!LIST_ADDED(&fstrm->send_list))
3739 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3740 event_type &= ~SUB_RETRY_SEND;
3741 }
3742 if (event_type != 0)
3743 return -1;
3744 return 0;
3745}
3746
3747/* Called from the upper layer, to unsubscribe some events (undo fcgi_subscribe).
3748 * The <param> argument here is supposed to be a pointer to the same wait_event
3749 * struct that was passed to fcgi_subscribe() otherwise nothing will be changed.
3750 * It always returns zero.
3751 */
3752static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3753{
3754 struct wait_event *sw;
3755 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003756 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003757
3758 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003759 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003760 sw = param;
3761 BUG_ON(fstrm->recv_wait != sw);
3762 sw->events &= ~SUB_RETRY_RECV;
3763 fstrm->recv_wait = NULL;
3764 }
3765 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003766 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003767 sw = param;
3768 BUG_ON(fstrm->send_wait != sw);
3769 LIST_DEL(&fstrm->send_list);
3770 LIST_INIT(&fstrm->send_list);
3771 sw->events &= ~SUB_RETRY_SEND;
3772 /* We were about to send, make sure it does not happen */
3773 if (LIST_ADDED(&fstrm->sending_list) && fstrm->send_wait != &fstrm->wait_event) {
3774 tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet);
3775 LIST_DEL_INIT(&fstrm->sending_list);
3776 }
3777 fstrm->send_wait = NULL;
3778 }
3779 return 0;
3780}
3781
3782/* Called from the upper layer, to receive data */
3783static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3784{
3785 struct fcgi_strm *fstrm = cs->ctx;
3786 struct fcgi_conn *fconn = fstrm->fconn;
3787 size_t ret = 0;
3788
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003789 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3790
Christopher Faulet99eff652019-08-11 23:11:30 +02003791 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3792 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003793 else
3794 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003795
Christopher Fauletf950c2e2019-12-06 16:20:49 +01003796 if (b_data(&fstrm->rxbuf) || (fstrm->flags & FCGI_SF_APPEND_EOM))
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3798 else {
3799 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3800 if (fstrm->state == FCGI_SS_ERROR || fstrm->h1m.state == H1_MSG_DONE) {
3801 cs->flags |= CS_FL_EOI;
3802 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3803 cs->flags |= CS_FL_EOS;
3804 }
3805 if (conn_xprt_read0_pending(fconn->conn))
3806 cs->flags |= CS_FL_EOS;
3807 if (cs->flags & CS_FL_ERR_PENDING)
3808 cs->flags |= CS_FL_ERROR;
3809 fcgi_release_buf(fconn, &fstrm->rxbuf);
3810 }
3811
3812 if (ret && fconn->dsi == fstrm->id) {
3813 /* demux is blocking on this stream's buffer */
3814 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3815 fcgi_conn_restart_reading(fconn, 1);
3816 }
3817
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003818 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003819 return ret;
3820}
3821
3822
3823/* stops all senders of this connection for example when the mux buffer is full.
3824 * They are moved from the sending_list to send_list.
3825 */
3826static void fcgi_stop_senders(struct fcgi_conn *fconn)
3827{
3828 struct fcgi_strm *fstrm, *fstrm_back;
3829
3830 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->sending_list, sending_list) {
3831 LIST_DEL_INIT(&fstrm->sending_list);
3832 tasklet_remove_from_tasklet_list(fstrm->send_wait->tasklet);
3833 fstrm->send_wait->events |= SUB_RETRY_SEND;
3834 }
3835}
3836
3837
3838/* Called from the upper layer, to send data from buffer <buf> for no more than
3839 * <count> bytes. Returns the number of bytes effectively sent. Some status
3840 * flags may be updated on the conn_stream.
3841 */
3842static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3843{
3844 struct fcgi_strm *fstrm = cs->ctx;
3845 struct fcgi_conn *fconn = fstrm->fconn;
3846 size_t total = 0;
3847 size_t ret;
3848 struct htx *htx = NULL;
3849 struct htx_sl *sl;
3850 struct htx_blk *blk;
3851 uint32_t bsize;
3852
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003853 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3854
Christopher Faulet99eff652019-08-11 23:11:30 +02003855 /* If we were not just woken because we wanted to send but couldn't,
3856 * and there's somebody else that is waiting to send, do nothing,
3857 * we will subscribe later and be put at the end of the list
3858 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003859 if (!LIST_ADDED(&fstrm->sending_list) && !LIST_ISEMPTY(&fconn->send_list)) {
3860 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 +02003861 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003862 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003863 LIST_DEL_INIT(&fstrm->sending_list);
3864
3865 /* We couldn't set it to NULL before, because we needed it in case
3866 * we had to cancel the tasklet
3867 */
3868 fstrm->send_wait = NULL;
3869
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003870 if (fconn->state < FCGI_CS_RECORD_H) {
3871 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003872 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003873 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003874
3875 htx = htxbuf(buf);
3876 if (fstrm->id == 0) {
3877 int32_t id = fcgi_conn_get_next_sid(fconn);
3878
3879 if (id < 0) {
3880 fcgi_strm_close(fstrm);
3881 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003882 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 +02003883 return 0;
3884 }
3885
3886 eb32_delete(&fstrm->by_id);
3887 fstrm->by_id.key = fstrm->id = id;
3888 fconn->max_id = id;
3889 fconn->nb_reserved--;
3890 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3891
3892
3893 /* Check if length of the body is known or if the message is
3894 * full. Otherwise, the request is invalid.
3895 */
3896 sl = http_get_stline(htx);
3897 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3898 htx->flags |= HTX_FL_PARSING_ERROR;
3899 fcgi_strm_error(fstrm);
3900 goto done;
3901 }
3902 }
3903
3904 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003905 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 +02003906 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3907 goto done;
3908 }
3909
3910 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3911 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3912
3913 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3914 count && !htx_is_empty(htx)) {
3915 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003916 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003917 bsize = htx_get_blksz(blk);
3918
3919 switch (htx_get_blk_type(blk)) {
3920 case HTX_BLK_REQ_SL:
3921 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003922 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 +02003923 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3924 if (!ret) {
3925 goto done;
3926 }
3927 total += ret;
3928 count -= ret;
3929 break;
3930
3931 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003932 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 +02003933 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3934 if (!ret)
3935 goto done;
3936 goto remove_blk;
3937
3938 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003939 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 +02003940 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3941 if (ret > 0) {
3942 htx = htx_from_buf(buf);
3943 total += ret;
3944 count -= ret;
3945 if (ret < bsize)
3946 goto done;
3947 }
3948 break;
3949
3950 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003951 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 +02003952 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3953 if (!ret)
3954 goto done;
3955 goto remove_blk;
3956
3957 default:
3958 remove_blk:
3959 htx_remove_blk(htx, blk);
3960 total += bsize;
3961 count -= bsize;
3962 break;
3963 }
3964 }
3965
3966 done:
3967 if (fstrm->state >= FCGI_SS_HLOC) {
3968 /* trim any possibly pending data after we close (extra CR-LF,
3969 * unprocessed trailers, abnormal extra data, ...)
3970 */
3971 total += count;
3972 count = 0;
3973 }
3974
3975 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 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 +02003977 cs_set_error(cs);
3978 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3979 fcgi_strm_close(fstrm);
3980 }
3981
3982 if (htx)
3983 htx_to_buf(htx, buf);
3984
3985 /* The mux is full, cancel the pending tasks */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003986 if ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || (fstrm->flags & FCGI_SF_BLK_MBUSY)) {
3987 TRACE_DEVEL("mux full, stopping senders", FCGI_EV_STRM_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003988 fcgi_stop_senders(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003989 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003990
3991 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003992 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
3993 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 +02003994 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003995 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003996
3997 /* Ok we managed to send something, leave the send_list */
3998 LIST_DEL_INIT(&fstrm->send_list);
3999 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004000
4001 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004002 return total;
4003}
4004
4005/* for debugging with CLI's "show fd" command */
4006static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
4007{
4008 struct fcgi_conn *fconn = conn->ctx;
4009 struct fcgi_strm *fstrm = NULL;
4010 struct eb32_node *node;
4011 int send_cnt = 0;
4012 int tree_cnt = 0;
4013 int orph_cnt = 0;
4014 struct buffer *hmbuf, *tmbuf;
4015
4016 if (!fconn)
4017 return;
4018
4019 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4020 send_cnt++;
4021
4022 fstrm = NULL;
4023 node = eb32_first(&fconn->streams_by_id);
4024 while (node) {
4025 fstrm = container_of(node, struct fcgi_strm, by_id);
4026 tree_cnt++;
4027 if (!fstrm->cs)
4028 orph_cnt++;
4029 node = eb32_next(node);
4030 }
4031
4032 hmbuf = br_head(fconn->mbuf);
4033 tmbuf = br_tail(fconn->mbuf);
4034 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4035 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4036 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4037 fconn->state, fconn->max_id, fconn->flags,
4038 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4039 fconn->wait_event.events, fconn->dsi,
4040 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4041 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4042 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4043 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4044 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4045 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4046 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4047
4048 if (fstrm) {
4049 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4050 fstrm, fstrm->id, fstrm->flags,
4051 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4052 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4053 fstrm->cs);
4054 if (fstrm->cs)
4055 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4056 fstrm->cs->flags, fstrm->cs->data);
4057 }
4058}
4059
4060/****************************************/
4061/* MUX initialization and instanciation */
4062/****************************************/
4063
4064/* The mux operations */
4065static const struct mux_ops mux_fcgi_ops = {
4066 .init = fcgi_init,
4067 .wake = fcgi_wake,
4068 .attach = fcgi_attach,
4069 .get_first_cs = fcgi_get_first_cs,
4070 .detach = fcgi_detach,
4071 .destroy = fcgi_destroy,
4072 .avail_streams = fcgi_avail_streams,
4073 .used_streams = fcgi_used_streams,
4074 .rcv_buf = fcgi_rcv_buf,
4075 .snd_buf = fcgi_snd_buf,
4076 .subscribe = fcgi_subscribe,
4077 .unsubscribe = fcgi_unsubscribe,
4078 .shutr = fcgi_shutr,
4079 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004080 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004081 .show_fd = fcgi_show_fd,
4082 .flags = MX_FL_HTX,
4083 .name = "FCGI",
4084};
4085
4086
4087/* this mux registers FCGI proto */
4088static struct mux_proto_list mux_proto_fcgi =
4089{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4090
4091INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4092
4093/*
4094 * Local variables:
4095 * c-indent-level: 8
4096 * c-basic-offset: 8
4097 * End:
4098 */