blob: 1b0495d9072a42e88756ec482d640faa21c99da9 [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 */
Christopher Faulet99eff652019-08-11 23:11:30 +0200110
111 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
112 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
113};
114
115
116/* FCGI stream state, in fcgi_strm->state */
117enum fcgi_strm_st {
118 FCGI_SS_IDLE = 0,
119 FCGI_SS_OPEN,
120 FCGI_SS_HREM, // half-closed(remote)
121 FCGI_SS_HLOC, // half-closed(local)
122 FCGI_SS_ERROR,
123 FCGI_SS_CLOSED,
124 FCGI_SS_ENTRIES
125} __attribute__((packed));
126
127
128/* FCGI stream flags (32 bits) */
129#define FCGI_SF_NONE 0x00000000
130#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
131#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
132#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
133
134/* Stream flags indicating the reason the stream is blocked */
135#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
136#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
137#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
138
139#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
140#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100141#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200142
143#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
144#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
145#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
146
147/* Other flags */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100148#define FCGI_SF_H1_PARSING_DONE 0x00010000
Christopher Faulet99eff652019-08-11 23:11:30 +0200149
150/* FCGI stream descriptor */
151struct fcgi_strm {
152 struct conn_stream *cs;
153 struct session *sess;
154 struct fcgi_conn *fconn;
155
156 int32_t id; /* stream ID */
157
158 uint32_t flags; /* Connection flags: FCGI_SF_* */
159 enum fcgi_strm_st state; /* FCGI stream state */
160 int proto_status; /* FCGI_PS_* */
161
162 struct h1m h1m; /* response parser state for H1 */
163
164 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
165
166 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
167 struct wait_event wait_event; /* Wait list, when we're attempting to send an ABORT but we can't send */
168 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
169 struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */
170 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Christopher Faulet99eff652019-08-11 23:11:30 +0200171};
172
173/* Flags representing all default FCGI parameters */
174#define FCGI_SP_CGI_GATEWAY 0x00000001
175#define FCGI_SP_DOC_ROOT 0x00000002
176#define FCGI_SP_SCRIPT_NAME 0x00000004
177#define FCGI_SP_PATH_INFO 0x00000008
178#define FCGI_SP_REQ_URI 0x00000010
179#define FCGI_SP_REQ_METH 0x00000020
180#define FCGI_SP_REQ_QS 0x00000040
181#define FCGI_SP_SRV_PORT 0x00000080
182#define FCGI_SP_SRV_PROTO 0x00000100
183#define FCGI_SP_SRV_NAME 0x00000200
184#define FCGI_SP_REM_ADDR 0x00000400
185#define FCGI_SP_REM_PORT 0x00000800
186#define FCGI_SP_SCRIPT_FILE 0x00001000
187#define FCGI_SP_PATH_TRANS 0x00002000
188#define FCGI_SP_CONT_LEN 0x00004000
189#define FCGI_SP_HTTPS 0x00008000
190#define FCGI_SP_MASK 0x0000FFFF
191#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
192
193/* FCGI parameters used when PARAMS record is sent */
194struct fcgi_strm_params {
195 uint32_t mask;
196 struct ist docroot;
197 struct ist scriptname;
198 struct ist pathinfo;
199 struct ist meth;
200 struct ist uri;
201 struct ist vsn;
202 struct ist qs;
203 struct ist srv_name;
204 struct ist srv_port;
205 struct ist rem_addr;
206 struct ist rem_port;
207 struct ist cont_len;
208 int https;
209 struct buffer *p;
210};
211
212/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
213#define MAX_DATA_REALIGN 1024
214
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200215/* trace source and events */
216static void fcgi_trace(enum trace_level level, uint64_t mask,
217 const struct trace_source *src,
218 const struct ist where, const struct ist func,
219 const void *a1, const void *a2, const void *a3, const void *a4);
220
221/* The event representation is split like this :
222 * fconn - internal FCGI connection
223 * fstrm - internal FCGI stream
224 * strm - application layer
225 * rx - data receipt
226 * tx - data transmission
227 * rsp - response parsing
228 */
229static const struct trace_event fcgi_trace_events[] = {
230#define FCGI_EV_FCONN_NEW (1ULL << 0)
231 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
232#define FCGI_EV_FCONN_RECV (1ULL << 1)
233 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
234#define FCGI_EV_FCONN_SEND (1ULL << 2)
235 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
236#define FCGI_EV_FCONN_BLK (1ULL << 3)
237 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
238#define FCGI_EV_FCONN_WAKE (1ULL << 4)
239 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
240#define FCGI_EV_FCONN_END (1ULL << 5)
241 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
242#define FCGI_EV_FCONN_ERR (1ULL << 6)
243 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
244
245#define FCGI_EV_RX_FHDR (1ULL << 7)
246 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
247#define FCGI_EV_RX_RECORD (1ULL << 8)
248 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
249#define FCGI_EV_RX_EOI (1ULL << 9)
250 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
251#define FCGI_EV_RX_GETVAL (1ULL << 10)
252 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
253#define FCGI_EV_RX_STDOUT (1ULL << 11)
254 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
255#define FCGI_EV_RX_STDERR (1ULL << 12)
256 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
257#define FCGI_EV_RX_ENDREQ (1ULL << 13)
258 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
259
260#define FCGI_EV_TX_RECORD (1ULL << 14)
261 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
262#define FCGI_EV_TX_EOI (1ULL << 15)
263 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
264#define FCGI_EV_TX_BEGREQ (1ULL << 16)
265 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
266#define FCGI_EV_TX_GETVAL (1ULL << 17)
267 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
268#define FCGI_EV_TX_PARAMS (1ULL << 18)
269 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
270#define FCGI_EV_TX_STDIN (1ULL << 19)
271 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
272#define FCGI_EV_TX_ABORT (1ULL << 20)
273 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
274
275#define FCGI_EV_RSP_DATA (1ULL << 21)
276 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
277#define FCGI_EV_RSP_EOM (1ULL << 22)
278 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
279#define FCGI_EV_RSP_HDRS (1ULL << 23)
280 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
281#define FCGI_EV_RSP_BODY (1ULL << 24)
282 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
283#define FCGI_EV_RSP_TLRS (1ULL << 25)
284 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
285
286#define FCGI_EV_FSTRM_NEW (1ULL << 26)
287 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
288#define FCGI_EV_FSTRM_BLK (1ULL << 27)
289 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
290#define FCGI_EV_FSTRM_END (1ULL << 28)
291 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
292#define FCGI_EV_FSTRM_ERR (1ULL << 29)
293 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
294
295#define FCGI_EV_STRM_NEW (1ULL << 30)
296 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
297#define FCGI_EV_STRM_RECV (1ULL << 31)
298 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
299#define FCGI_EV_STRM_SEND (1ULL << 32)
300 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
301#define FCGI_EV_STRM_FULL (1ULL << 33)
302 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
303#define FCGI_EV_STRM_WAKE (1ULL << 34)
304 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
305#define FCGI_EV_STRM_SHUT (1ULL << 35)
306 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
307#define FCGI_EV_STRM_END (1ULL << 36)
308 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
309#define FCGI_EV_STRM_ERR (1ULL << 37)
310 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
311
312 { }
313};
314
315static const struct name_desc fcgi_trace_lockon_args[4] = {
316 /* arg1 */ { /* already used by the connection */ },
317 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
318 /* arg3 */ { },
319 /* arg4 */ { }
320};
321
322
323static const struct name_desc fcgi_trace_decoding[] = {
324#define FCGI_VERB_CLEAN 1
325 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
326#define FCGI_VERB_MINIMAL 2
327 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
328#define FCGI_VERB_SIMPLE 3
329 { .name="simple", .desc="add request/response status line or htx info when available" },
330#define FCGI_VERB_ADVANCED 4
331 { .name="advanced", .desc="add header fields or record decoding when available" },
332#define FCGI_VERB_COMPLETE 5
333 { .name="complete", .desc="add full data dump when available" },
334 { /* end */ }
335};
336
337static struct trace_source trace_fcgi = {
338 .name = IST("fcgi"),
339 .desc = "FastCGI multiplexer",
340 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
341 .default_cb = fcgi_trace,
342 .known_events = fcgi_trace_events,
343 .lockon_args = fcgi_trace_lockon_args,
344 .decoding = fcgi_trace_decoding,
345 .report_events = ~0, // report everything by default
346};
347
348#define TRACE_SOURCE &trace_fcgi
349INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
350
Christopher Faulet99eff652019-08-11 23:11:30 +0200351/* FCGI connection and stream pools */
352DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
353DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
354
355static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
356static int fcgi_process(struct fcgi_conn *fconn);
357static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
358static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
359static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
360static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200361static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
362static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200363static void fcgi_strm_alert(struct fcgi_strm *fstrm);
364static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
365
366/* a dmumy management stream */
367static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
368 .cs = NULL,
369 .fconn = NULL,
370 .state = FCGI_SS_CLOSED,
371 .flags = FCGI_SF_NONE,
372 .id = 0,
373};
374
375/* and a dummy idle stream for use with any unknown stream */
376static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
377 .cs = NULL,
378 .fconn = NULL,
379 .state = FCGI_SS_IDLE,
380 .flags = FCGI_SF_NONE,
381 .id = 0,
382};
383
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200384/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
385static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
386{
387 switch (st) {
388 case FCGI_CS_INIT : return "INI";
389 case FCGI_CS_SETTINGS : return "STG";
390 case FCGI_CS_RECORD_H : return "RDH";
391 case FCGI_CS_RECORD_D : return "RDD";
392 case FCGI_CS_RECORD_P : return "RDP";
393 case FCGI_CS_CLOSED : return "CLO";
394 default : return "???";
395 }
396}
397
398/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
399static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
400{
401 switch (st) {
402 case FCGI_SS_IDLE : return "IDL";
403 case FCGI_SS_OPEN : return "OPN";
404 case FCGI_SS_HREM : return "RCL";
405 case FCGI_SS_HLOC : return "HCL";
406 case FCGI_SS_ERROR : return "ERR";
407 case FCGI_SS_CLOSED : return "CLO";
408 default : return "???";
409 }
410}
411
412
413/* the FCGI traces always expect that arg1, if non-null, is of type connection
414 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
415 * and that arg3, if non-null, is a htx for rx/tx headers.
416 */
417static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
418 const struct ist where, const struct ist func,
419 const void *a1, const void *a2, const void *a3, const void *a4)
420{
421 const struct connection *conn = a1;
422 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
423 const struct fcgi_strm *fstrm = a2;
424 const struct htx *htx = a3;
425 const size_t *val = a4;
426
427 if (!fconn)
428 fconn = (fstrm ? fstrm->fconn : NULL);
429
430 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
431 return;
432
433 /* Display the response state if fstrm is defined */
434 if (fstrm)
435 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
436
437 if (src->verbosity == FCGI_VERB_CLEAN)
438 return;
439
440 /* Display the value to the 4th argument (level > STATE) */
441 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100442 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200443
444 /* Display status-line if possible (verbosity > MINIMAL) */
445 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
446 const struct htx_blk *blk = htx_get_head_blk(htx);
447 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
448 enum htx_blk_type type = htx_get_blk_type(blk);
449
450 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
451 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
452 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
453 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
454 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
455 }
456
457 /* Display fconn info and, if defined, fstrm info */
458 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
459 if (fstrm)
460 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
461
462 if (!fstrm || fstrm->id <= 0)
463 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
464 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
465 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
466
467 if (src->verbosity == FCGI_VERB_MINIMAL)
468 return;
469
470 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
471 if (src->level > TRACE_LEVEL_USER) {
472 if (src->verbosity == FCGI_VERB_COMPLETE ||
473 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
474 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
475 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
476 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
477 if (src->verbosity == FCGI_VERB_COMPLETE ||
478 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
479 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
480 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
481
482 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
483 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
484 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
485 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
486 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
487 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
488 }
489
490 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
491 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
492 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
493 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
494 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
495 }
496
497 /* Display htx info if defined (level > USER) */
498 if (src->level > TRACE_LEVEL_USER && htx) {
499 int full = 0;
500
501 /* Full htx info (level > STATE && verbosity > SIMPLE) */
502 if (src->level > TRACE_LEVEL_STATE) {
503 if (src->verbosity == FCGI_VERB_COMPLETE)
504 full = 1;
505 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
506 full = 1;
507 }
508
509 chunk_memcat(&trace_buf, "\n\t", 2);
510 htx_dump(&trace_buf, htx, full);
511 }
512}
Christopher Faulet99eff652019-08-11 23:11:30 +0200513
514/*****************************************************/
515/* functions below are for dynamic buffer management */
516/*****************************************************/
517
518/* Indicates whether or not the we may call the fcgi_recv() function to attempt
519 * to receive data into the buffer and/or demux pending data. The condition is
520 * a bit complex due to some API limits for now. The rules are the following :
521 * - if an error or a shutdown was detected on the connection and the buffer
522 * is empty, we must not attempt to receive
523 * - if the demux buf failed to be allocated, we must not try to receive and
524 * we know there is nothing pending
525 * - if no flag indicates a blocking condition, we may attempt to receive,
526 * regardless of whether the demux buffer is full or not, so that only
527 * de demux part decides whether or not to block. This is needed because
528 * the connection API indeed prevents us from re-enabling receipt that is
529 * already enabled in a polled state, so we must always immediately stop
530 * as soon as the demux can't proceed so as never to hit an end of read
531 * with data pending in the buffers.
532 * - otherwise must may not attempt
533 */
534static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
535{
536 if (b_data(&fconn->dbuf) == 0 &&
537 (fconn->state == FCGI_CS_CLOSED ||
538 fconn->conn->flags & CO_FL_ERROR ||
539 conn_xprt_read0_pending(fconn->conn)))
540 return 0;
541
542 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
543 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
544 return 1;
545
546 return 0;
547}
548
549/* Restarts reading on the connection if it was not enabled */
550static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
551{
552 if (!fcgi_recv_allowed(fconn))
553 return;
554 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
555 (fconn->wait_event.events & SUB_RETRY_RECV))
556 return;
557 tasklet_wakeup(fconn->wait_event.tasklet);
558}
559
560
561/* Tries to grab a buffer and to re-enable processing on mux <target>. The
562 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
563 * the allocation succeeds, in which case the connection is woken up, or 0 if
564 * it's impossible to wake up and we prefer to be woken up later.
565 */
566static int fcgi_buf_available(void *target)
567{
568 struct fcgi_conn *fconn = target;
569 struct fcgi_strm *fstrm;
570
571 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200572 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200573 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
574 fcgi_conn_restart_reading(fconn, 1);
575 return 1;
576 }
577
578 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200579 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200580 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200581 if (fconn->flags & FCGI_CF_DEM_MROOM) {
582 fconn->flags &= ~FCGI_CF_DEM_MROOM;
583 fcgi_conn_restart_reading(fconn, 1);
584 }
585 return 1;
586 }
587
588 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
589 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
590 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200591 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200592 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
593 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200594 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200595 return 1;
596 }
597
598 return 0;
599}
600
601static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
602{
603 struct buffer *buf = NULL;
604
605 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
606 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
607 fconn->buf_wait.target = fconn;
608 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
609 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
610 LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
611 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
612 __conn_xprt_stop_recv(fconn->conn);
613 }
614 return buf;
615}
616
617static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
618{
619 if (bptr->size) {
620 b_free(bptr);
621 offer_buffers(NULL, tasks_run_queue);
622 }
623}
624
625static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
626{
627 struct buffer *buf;
628 unsigned int count = 0;
629
630 while (b_size(buf = br_head_pick(fconn->mbuf))) {
631 b_free(buf);
632 count++;
633 }
634 if (count)
635 offer_buffers(NULL, tasks_run_queue);
636}
637
638/* Returns the number of allocatable outgoing streams for the connection taking
639 * the number reserved streams into account.
640 */
641static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
642{
643 int ret;
644
645 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
646 if (ret < 0)
647 ret = 0;
648 return ret;
649}
650
651/* Returns the number of streams in use on a connection to figure if it's
652 * idle or not. We check nb_cs and not nb_streams as the caller will want
653 * to know if it was the last one after a detach().
654 */
655static int fcgi_used_streams(struct connection *conn)
656{
657 struct fcgi_conn *fconn = conn->ctx;
658
659 return fconn->nb_cs;
660}
661
662/* Returns the number of concurrent streams available on the connection */
663static int fcgi_avail_streams(struct connection *conn)
664{
665 struct server *srv = objt_server(conn->target);
666 struct fcgi_conn *fconn = conn->ctx;
667 int ret1, ret2;
668
669 /* Don't open new stream if the connection is closed */
670 if (fconn->state == FCGI_CS_CLOSED)
671 return 0;
672
673 /* May be negative if this setting has changed */
674 ret1 = (fconn->streams_limit - fconn->nb_streams);
675
676 /* we must also consider the limit imposed by stream IDs */
677 ret2 = fcgi_streams_left(fconn);
678 ret1 = MIN(ret1, ret2);
679 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
680 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
681 ret1 = MIN(ret1, ret2);
682 }
683 return ret1;
684}
685
686/*****************************************************************/
687/* functions below are dedicated to the mux setup and management */
688/*****************************************************************/
689
690/* Initializes the mux once it's attached. Only outgoing connections are
691 * supported. So the context is already initialized before installing the
692 * mux. <input> is always used as Input buffer and may contain data. It is the
693 * caller responsibility to not reuse it anymore. Returns < 0 on error.
694 */
695static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
696 struct buffer *input)
697{
698 struct fcgi_conn *fconn;
699 struct fcgi_strm *fstrm;
700 struct fcgi_app *app = get_px_fcgi_app(px);
701 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200702 void *conn_ctx = conn->ctx;
703
704 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200705
706 if (!app)
707 goto fail_conn;
708
709 fconn = pool_alloc(pool_head_fcgi_conn);
710 if (!fconn)
711 goto fail_conn;
712
713 fconn->shut_timeout = fconn->timeout = px->timeout.server;
714 if (tick_isset(px->timeout.serverfin))
715 fconn->shut_timeout = px->timeout.serverfin;
716
717 fconn->flags = FCGI_CF_NONE;
718
719 /* Retrieve usefull info from the FCGI app */
720 if (app->flags & FCGI_APP_FL_KEEP_CONN)
721 fconn->flags |= FCGI_CF_KEEP_CONN;
722 if (app->flags & FCGI_APP_FL_GET_VALUES)
723 fconn->flags |= FCGI_CF_GET_VALUES;
724 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
725 fconn->flags |= FCGI_CF_MPXS_CONNS;
726
727 fconn->proxy = px;
728 fconn->app = app;
729 fconn->task = NULL;
730 if (tick_isset(fconn->timeout)) {
731 t = task_new(tid_bit);
732 if (!t)
733 goto fail;
734
735 fconn->task = t;
736 t->process = fcgi_timeout_task;
737 t->context = fconn;
738 t->expire = tick_add(now_ms, fconn->timeout);
739 }
740
741 fconn->wait_event.tasklet = tasklet_new();
742 if (!fconn->wait_event.tasklet)
743 goto fail;
744 fconn->wait_event.tasklet->process = fcgi_io_cb;
745 fconn->wait_event.tasklet->context = fconn;
746 fconn->wait_event.events = 0;
747
748 /* Initialise the context. */
749 fconn->state = FCGI_CS_INIT;
750 fconn->conn = conn;
751 fconn->streams_limit = app->maxreqs;
752 fconn->max_id = -1;
753 fconn->nb_streams = 0;
754 fconn->nb_cs = 0;
755 fconn->nb_reserved = 0;
756 fconn->stream_cnt = 0;
757
758 fconn->dbuf = *input;
759 fconn->dsi = -1;
760
761 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
762 fconn->streams_by_id = EB_ROOT;
763 LIST_INIT(&fconn->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200764 LIST_INIT(&fconn->buf_wait.list);
765
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200766 conn->ctx = fconn;
767
Christopher Faulet99eff652019-08-11 23:11:30 +0200768 if (t)
769 task_queue(t);
770
771 /* FIXME: this is temporary, for outgoing connections we need to
772 * immediately allocate a stream until the code is modified so that the
773 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200774 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200775 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200776 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200777 if (!fstrm)
778 goto fail;
779
Christopher Faulet99eff652019-08-11 23:11:30 +0200780
781 /* Repare to read something */
782 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200783 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200784 return 0;
785
786 fail:
787 task_destroy(t);
788 if (fconn->wait_event.tasklet)
789 tasklet_free(fconn->wait_event.tasklet);
790 pool_free(pool_head_fcgi_conn, fconn);
791 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200792 conn->ctx = conn_ctx; // restore saved ctx
793 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200794 return -1;
795}
796
797/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
798 * -1 if no more is allocatable.
799 */
800static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
801{
802 int32_t id = (fconn->max_id + 1) | 1;
803
804 if ((id & 0x80000000U))
805 id = -1;
806 return id;
807}
808
809/* Returns the stream associated with id <id> or NULL if not found */
810static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
811{
812 struct eb32_node *node;
813
814 if (id == 0)
815 return (struct fcgi_strm *)fcgi_mgmt_stream;
816
817 if (id > fconn->max_id)
818 return (struct fcgi_strm *)fcgi_unknown_stream;
819
820 node = eb32_lookup(&fconn->streams_by_id, id);
821 if (!node)
822 return (struct fcgi_strm *)fcgi_unknown_stream;
823 return container_of(node, struct fcgi_strm, by_id);
824}
825
826
827/* Release function. This one should be called to free all resources allocated
828 * to the mux.
829 */
830static void fcgi_release(struct fcgi_conn *fconn)
831{
832 struct connection *conn = NULL;;
833
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200834 TRACE_POINT(FCGI_EV_FCONN_END);
835
Christopher Faulet99eff652019-08-11 23:11:30 +0200836 if (fconn) {
837 /* The connection must be attached to this mux to be released */
838 if (fconn->conn && fconn->conn->ctx == fconn)
839 conn = fconn->conn;
840
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200841 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
842
Christopher Faulet99eff652019-08-11 23:11:30 +0200843 if (LIST_ADDED(&fconn->buf_wait.list)) {
844 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
845 LIST_DEL(&fconn->buf_wait.list);
846 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
847 }
848
849 fcgi_release_buf(fconn, &fconn->dbuf);
850 fcgi_release_mbuf(fconn);
851
852 if (fconn->task) {
853 fconn->task->context = NULL;
854 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
855 fconn->task = NULL;
856 }
857 if (fconn->wait_event.tasklet)
858 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200859 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200860 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
861 &fconn->wait_event);
862 }
863
864 if (conn) {
865 conn->mux = NULL;
866 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200867 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200868
869 conn_stop_tracking(conn);
870 conn_full_close(conn);
871 if (conn->destroy_cb)
872 conn->destroy_cb(conn);
873 conn_free(conn);
874 }
875}
876
877
878/* Retruns true if the FCGI connection must be release */
879static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
880{
881 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
882 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
883 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
884 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
885 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
886 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
887 conn_xprt_read0_pending(fconn->conn))))
888 return 1;
889 return 0;
890}
891
892
893/********************************************************/
894/* functions below are for the FCGI protocol processing */
895/********************************************************/
896
Christopher Faulet99eff652019-08-11 23:11:30 +0200897/* Marks an error on the stream. */
898static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
899{
900 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200901 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
902 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200903 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200904 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
905 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200906 if (fstrm->cs)
907 cs_set_error(fstrm->cs);
908 }
909}
910
911/* Attempts to notify the data layer of recv availability */
912static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
913{
914 struct wait_event *sw;
915
916 if (fstrm->recv_wait) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200917 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200918 sw = fstrm->recv_wait;
919 sw->events &= ~SUB_RETRY_RECV;
920 tasklet_wakeup(sw->tasklet);
921 fstrm->recv_wait = NULL;
922 }
923}
924
925/* Attempts to notify the data layer of send availability */
926static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
927{
928 struct wait_event *sw;
929
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100930 if (fstrm->send_wait) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200931 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200932 sw = fstrm->send_wait;
933 sw->events &= ~SUB_RETRY_SEND;
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100934 fstrm->flags |= FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +0200935 tasklet_wakeup(sw->tasklet);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100936 fstrm->send_wait = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200937 }
938}
939
940/* Alerts the data layer, trying to wake it up by all means, following
941 * this sequence :
942 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
943 * for recv
944 * - if its subscribed to send, then it's woken up for send
945 * - if it was subscribed to neither, its ->wake() callback is called
946 * It is safe to call this function with a closed stream which doesn't have a
947 * conn_stream anymore.
948 */
949static void fcgi_strm_alert(struct fcgi_strm *fstrm)
950{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200951 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200952 if (fstrm->recv_wait || fstrm->send_wait) {
953 fcgi_strm_notify_recv(fstrm);
954 fcgi_strm_notify_send(fstrm);
955 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200956 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
957 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200958 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200959 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200960}
961
962/* Writes the 16-bit record size <len> at address <record> */
963static inline void fcgi_set_record_size(void *record, uint16_t len)
964{
965 uint8_t *out = (record + 4);
966
967 *out = (len >> 8);
968 *(out + 1) = (len & 0xff);
969}
970
971/* Writes the 16-bit stream id <id> at address <record> */
972static inline void fcgi_set_record_id(void *record, uint16_t id)
973{
974 uint8_t *out = (record + 2);
975
976 *out = (id >> 8);
977 *(out + 1) = (id & 0xff);
978}
979
980/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
981 * its connection if the stream was not yet closed. Please use this exclusively
982 * before closing a stream to ensure stream count is well maintained.
983 */
984static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
985{
986 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200987 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200988 fstrm->fconn->nb_streams--;
989 if (!fstrm->id)
990 fstrm->fconn->nb_reserved--;
991 if (fstrm->cs) {
992 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
993 fcgi_strm_notify_recv(fstrm);
994 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200995 fstrm->state = FCGI_SS_CLOSED;
996 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
997 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200998 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200999}
1000
1001/* Detaches a FCGI stream from its FCGI connection and releases it to the
1002 * fcgi_strm pool.
1003 */
1004static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1005{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001006 struct connection *conn = fstrm->fconn->conn;
1007
1008 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1009
Christopher Faulet99eff652019-08-11 23:11:30 +02001010 fcgi_strm_close(fstrm);
1011 eb32_delete(&fstrm->by_id);
1012 if (b_size(&fstrm->rxbuf)) {
1013 b_free(&fstrm->rxbuf);
1014 offer_buffers(NULL, tasks_run_queue);
1015 }
1016 if (fstrm->send_wait != NULL)
1017 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
1018 if (fstrm->recv_wait != NULL)
1019 fstrm->recv_wait->events &= ~SUB_RETRY_RECV;
1020 /* There's no need to explicitly call unsubscribe here, the only
1021 * reference left would be in the fconn send_list/fctl_list, and if
1022 * we're in it, we're getting out anyway
1023 */
1024 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001025 tasklet_free(fstrm->wait_event.tasklet);
1026 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001027
1028 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001029}
1030
1031/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1032 * stream tree. In case of error, nothing is added and NULL is returned. The
1033 * causes of errors can be any failed memory allocation. The caller is
1034 * responsible for checking if the connection may support an extra stream prior
1035 * to calling this function.
1036 */
1037static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1038{
1039 struct fcgi_strm *fstrm;
1040
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001041 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1042
Christopher Faulet99eff652019-08-11 23:11:30 +02001043 fstrm = pool_alloc(pool_head_fcgi_strm);
1044 if (!fstrm)
1045 goto out;
1046
1047 fstrm->wait_event.tasklet = tasklet_new();
1048 if (!fstrm->wait_event.tasklet) {
1049 pool_free(pool_head_fcgi_strm, fstrm);
1050 goto out;
1051 }
1052 fstrm->send_wait = NULL;
1053 fstrm->recv_wait = NULL;
1054 fstrm->wait_event.tasklet->process = fcgi_deferred_shut;
1055 fstrm->wait_event.tasklet->context = fstrm;
1056 fstrm->wait_event.events = 0;
1057 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001058 fstrm->fconn = fconn;
1059 fstrm->cs = NULL;
1060 fstrm->flags = FCGI_SF_NONE;
1061 fstrm->proto_status = 0;
1062 fstrm->state = FCGI_SS_IDLE;
1063 fstrm->rxbuf = BUF_NULL;
1064
1065 h1m_init_res(&fstrm->h1m);
1066 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1067 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1068
1069 fstrm->by_id.key = fstrm->id = id;
1070 if (id > 0)
1071 fconn->max_id = id;
1072 else
1073 fconn->nb_reserved++;
1074
1075 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1076 fconn->nb_streams++;
1077 fconn->stream_cnt++;
1078
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001079 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001080 return fstrm;
1081
1082 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001083 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 +02001084 return NULL;
1085}
1086
1087/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1088 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1089 * highest possible stream ID was reached.
1090 */
1091static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1092 struct session *sess)
1093{
1094 struct fcgi_strm *fstrm = NULL;
1095
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001096 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1097 if (fconn->nb_streams >= fconn->streams_limit) {
1098 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 +02001099 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001100 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001101
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001102 if (fcgi_streams_left(fconn) < 1) {
1103 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 +02001104 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001105 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001106
1107 /* Defer choosing the ID until we send the first message to create the stream */
1108 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001109 if (!fstrm) {
1110 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 +02001111 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001112 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001113
1114 fstrm->cs = cs;
1115 fstrm->sess = sess;
1116 cs->ctx = fstrm;
1117 fconn->nb_cs++;
1118
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001119 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001120 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001121
1122 out:
1123 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001124}
1125
1126/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1127 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1128 * automatically updated accordingly. If the stream is orphaned, it is
1129 * destroyed.
1130 */
1131static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1132{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001133 struct fcgi_conn *fconn = fstrm->fconn;
1134
1135 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1136
Christopher Faulet99eff652019-08-11 23:11:30 +02001137 if (!fstrm->cs) {
1138 /* this stream was already orphaned */
1139 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001140 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001141 return;
1142 }
1143
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001144 if (conn_xprt_read0_pending(fconn->conn)) {
1145 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001146 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001147 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1148 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001149 else if (fstrm->state == FCGI_SS_HLOC)
1150 fcgi_strm_close(fstrm);
1151 }
1152
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001153 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1155 if (fstrm->cs->flags & CS_FL_EOS)
1156 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001157
1158 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001159 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1161 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001162 }
1163
1164 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001165
1166 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001167}
1168
1169/* Wakes unassigned streams (ID == 0) attached to the connection. */
1170static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1171{
1172 struct eb32_node *node;
1173 struct fcgi_strm *fstrm;
1174
1175 node = eb32_lookup(&fconn->streams_by_id, 0);
1176 while (node) {
1177 fstrm = container_of(node, struct fcgi_strm, by_id);
1178 if (fstrm->id > 0)
1179 break;
1180 node = eb32_next(node);
1181 fcgi_strm_wake_one_stream(fstrm);
1182 }
1183}
1184
1185/* Wakes the streams attached to the connection, whose id is greater than <last>
1186 * or unassigned.
1187 */
1188static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1189{
1190 struct eb32_node *node;
1191 struct fcgi_strm *fstrm;
1192
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001193 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1194
Christopher Faulet99eff652019-08-11 23:11:30 +02001195 /* Wake all streams with ID > last */
1196 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1197 while (node) {
1198 fstrm = container_of(node, struct fcgi_strm, by_id);
1199 node = eb32_next(node);
1200 fcgi_strm_wake_one_stream(fstrm);
1201 }
1202 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001203
1204 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001205}
1206
1207static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1208 struct htx *htx, struct htx_sl *sl,
1209 struct fcgi_strm_params *params)
1210{
1211 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1212 struct ist p;
1213
1214 if (!sl)
1215 goto error;
1216
1217 if (!(params->mask & FCGI_SP_DOC_ROOT))
1218 params->docroot = fconn->app->docroot;
1219
1220 if (!(params->mask & FCGI_SP_REQ_METH)) {
1221 p = htx_sl_req_meth(sl);
1222 params->meth = ist2(b_tail(params->p), p.len);
1223 chunk_memcat(params->p, p.ptr, p.len);
1224 }
1225 if (!(params->mask & FCGI_SP_REQ_URI)) {
1226 p = htx_sl_req_uri(sl);
1227 params->uri = ist2(b_tail(params->p), p.len);
1228 chunk_memcat(params->p, p.ptr, p.len);
1229 }
1230 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1231 p = htx_sl_req_vsn(sl);
1232 params->vsn = ist2(b_tail(params->p), p.len);
1233 chunk_memcat(params->p, p.ptr, p.len);
1234 }
1235 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1236 char *end;
1237 int port = 0;
1238 if (conn_get_dst(cli_conn))
1239 port = get_host_port(cli_conn->dst);
1240 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1241 if (!end)
1242 goto error;
1243 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1244 params->p->data += params->srv_port.len;
1245 }
1246 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1247 /* If no Host header found, use the server address to fill
1248 * srv_name */
1249 if (!istlen(params->srv_name)) {
1250 char *ptr = NULL;
1251
1252 if (conn_get_dst(cli_conn))
1253 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1254 ptr = b_tail(params->p);
1255 if (ptr) {
1256 params->srv_name = ist2(ptr, strlen(ptr));
1257 params->p->data += params->srv_name.len;
1258 }
1259 }
1260 }
1261 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1262 char *ptr = NULL;
1263
1264 if (conn_get_src(cli_conn))
1265 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1266 ptr = b_tail(params->p);
1267 if (ptr) {
1268 params->rem_addr = ist2(ptr, strlen(ptr));
1269 params->p->data += params->rem_addr.len;
1270 }
1271 }
1272 if (!(params->mask & FCGI_SP_REM_PORT)) {
1273 char *end;
1274 int port = 0;
1275 if (conn_get_src(cli_conn))
1276 port = get_host_port(cli_conn->src);
1277 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1278 if (!end)
1279 goto error;
1280 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1281 params->p->data += params->rem_port.len;
1282 }
1283 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1284 struct htx_blk *blk;
1285 enum htx_blk_type type;
1286 char *end;
1287 size_t len = 0;
1288
1289 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1290 type = htx_get_blk_type(blk);
1291
1292 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1293 break;
1294 if (type == HTX_BLK_DATA)
1295 len += htx_get_blksz(blk);
1296 }
1297 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1298 if (!end)
1299 goto error;
1300 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1301 params->p->data += params->cont_len.len;
1302 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001303#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001304 if (!(params->mask & FCGI_SP_HTTPS)) {
1305 params->https = ssl_sock_is_ssl(cli_conn);
1306 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001307#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001308 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1309 /* one of scriptname, pathinfo or query_string is no set */
1310 struct ist path = http_get_path(params->uri);
1311 int len;
1312
1313 /* Decode the path. it must first be copied to keep the URI
1314 * untouched.
1315 */
1316 chunk_memcat(params->p, path.ptr, path.len);
1317 path.ptr = b_tail(params->p) - path.len;
1318 path.ptr[path.len] = '\0';
1319 len = url_decode(path.ptr);
1320 if (len < 0)
1321 goto error;
1322 path.len = len;
1323
1324 /* No scrit_name set but no valid path ==> error */
1325 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1326 goto error;
1327
1328 /* Find limit between the path and the query-string */
1329 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1330
1331 /* If there is a query-string, Set it if not already set */
1332 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1333 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1334
1335 /* If the script_name is set, don't try to deduce the path_info
1336 * too. The opposite is not true.
1337 */
1338 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1339 params->mask |= FCGI_SP_PATH_INFO;
1340 goto end;
1341 }
1342
1343 /* script_name not set, preset it with the path for now */
1344 params->scriptname = ist2(path.ptr, len);
1345
1346 /* If there is no regex to match the pathinfo, just to the last
1347 * part and see if the index must be used.
1348 */
1349 if (!fconn->app->pathinfo_re)
1350 goto check_index;
1351
1352 /* The regex does not match, just to the last part and see if
1353 * the index must be used.
1354 */
1355 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1356 goto check_index;
1357
1358 /* We must have at least 2 captures, otherwise we do nothing and
1359 * jump to the last part. Only first 2 ones will be considered
1360 */
1361 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1 ||
1362 pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1)
1363 goto check_index;
1364
1365 /* Finally we can set the script_name and the path_info */
1366 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
1367 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
1368
1369 check_index:
1370 len = params->scriptname.len;
1371 /* the script_name if finished by a '/' so we can add the index
1372 * part, if any.
1373 */
1374 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1375 struct ist sn = params->scriptname;
1376
1377 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1378 chunk_memcat(params->p, sn.ptr, sn.len);
1379 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1380 }
1381 }
1382
1383 end:
1384 return 1;
1385 error:
1386 return 0;
1387}
1388
1389static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1390 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1391{
1392 struct fcgi_param p;
1393
1394 if (params->mask & flag)
1395 return 1;
1396
1397 chunk_reset(&trash);
1398
1399 switch (flag) {
1400 case FCGI_SP_CGI_GATEWAY:
1401 p.n = ist("GATEWAY_INTERFACE");
1402 p.v = ist("CGI/1.1");
1403 goto encode;
1404 case FCGI_SP_DOC_ROOT:
1405 p.n = ist("DOCUMENT_ROOT");
1406 p.v = params->docroot;
1407 goto encode;
1408 case FCGI_SP_SCRIPT_NAME:
1409 p.n = ist("SCRIPT_NAME");
1410 p.v = params->scriptname;
1411 goto encode;
1412 case FCGI_SP_PATH_INFO:
1413 p.n = ist("PATH_INFO");
1414 p.v = params->pathinfo;
1415 goto encode;
1416 case FCGI_SP_REQ_URI:
1417 p.n = ist("REQUEST_URI");
1418 p.v = params->uri;
1419 goto encode;
1420 case FCGI_SP_REQ_METH:
1421 p.n = ist("REQUEST_METHOD");
1422 p.v = params->meth;
1423 goto encode;
1424 case FCGI_SP_REQ_QS:
1425 p.n = ist("QUERY_STRING");
1426 p.v = params->qs;
1427 goto encode;
1428 case FCGI_SP_SRV_NAME:
1429 p.n = ist("SERVER_NAME");
1430 p.v = params->srv_name;
1431 goto encode;
1432 case FCGI_SP_SRV_PORT:
1433 p.n = ist("SERVER_PORT");
1434 p.v = params->srv_port;
1435 goto encode;
1436 case FCGI_SP_SRV_PROTO:
1437 p.n = ist("SERVER_PROTOCOL");
1438 p.v = params->vsn;
1439 goto encode;
1440 case FCGI_SP_REM_ADDR:
1441 p.n = ist("REMOTE_ADDR");
1442 p.v = params->rem_addr;
1443 goto encode;
1444 case FCGI_SP_REM_PORT:
1445 p.n = ist("REMOTE_PORT");
1446 p.v = params->rem_port;
1447 goto encode;
1448 case FCGI_SP_SCRIPT_FILE:
1449 p.n = ist("SCRIPT_FILENAME");
1450 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1451 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1452 p.v = ist2(b_head(&trash), b_data(&trash));
1453 goto encode;
1454 case FCGI_SP_PATH_TRANS:
1455 if (!istlen(params->pathinfo))
1456 goto skip;
1457 p.n = ist("PATH_TRANSLATED");
1458 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1459 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1460 p.v = ist2(b_head(&trash), b_data(&trash));
1461 goto encode;
1462 case FCGI_SP_CONT_LEN:
1463 p.n = ist("CONTENT_LENGTH");
1464 p.v = params->cont_len;
1465 goto encode;
1466 case FCGI_SP_HTTPS:
1467 if (!params->https)
1468 goto skip;
1469 p.n = ist("HTTPS");
1470 p.v = ist("on");
1471 goto encode;
1472 default:
1473 goto skip;
1474 }
1475
1476 encode:
1477 if (!istlen(p.v))
1478 goto skip;
1479 if (!fcgi_encode_param(outbuf, &p))
1480 return 0;
1481 skip:
1482 params->mask |= flag;
1483 return 1;
1484}
1485
1486/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1487 * anything. It is highly unexpected, but if the record is larger than a buffer
1488 * and cannot be encoded in one time, an error is triggered and the connection is
1489 * closed. GET_VALUES record cannot be split.
1490 */
1491static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1492{
1493 struct buffer outbuf;
1494 struct buffer *mbuf;
1495 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1496 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001497 int ret = 0;
1498
1499 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001500
1501 mbuf = br_tail(fconn->mbuf);
1502 retry:
1503 if (!fcgi_get_buf(fconn, mbuf)) {
1504 fconn->flags |= FCGI_CF_MUX_MALLOC;
1505 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001506 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1507 ret = 0;
1508 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001509 }
1510
1511 while (1) {
1512 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1513 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1514 break;
1515 realign_again:
1516 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1517 }
1518
1519 if (outbuf.size < 8)
1520 goto full;
1521
1522 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1523 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1524 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1525 outbuf.data = 8;
1526
1527 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1528 * handled by HAProxy.
1529 */
1530 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1531 goto full;
1532
1533 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001534 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 +02001535 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1536 b_add(mbuf, outbuf.data);
1537 ret = 1;
1538
1539 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001540 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001541 return ret;
1542 full:
1543 /* Too large to be encoded. For GET_VALUES records, it is an error */
1544 if (!b_data(mbuf))
1545 goto fail;
1546
1547 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1548 goto retry;
1549 fconn->flags |= FCGI_CF_MUX_MFULL;
1550 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001551 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001552 ret = 0;
1553 goto end;
1554 fail:
1555 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001556 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1557 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1558 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001559}
1560
1561/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1562 * couldn't do anything. It is highly unexpected, but if the record is larger
1563 * than a buffer and cannot be decoded in one time, an error is triggered and
1564 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1565 */
1566static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1567{
1568 struct buffer inbuf;
1569 struct buffer *dbuf;
1570 size_t offset;
1571
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001572 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1573
Christopher Faulet99eff652019-08-11 23:11:30 +02001574 dbuf = &fconn->dbuf;
1575
1576 /* Record too large to be fully decoded */
1577 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1578 goto fail;
1579
1580 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001581 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1582 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001583 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001584 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001585
1586 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1587 /* Realign the dmux buffer if the record wraps. It is unexpected
1588 * at this stage because it should be the first record received
1589 * from the FCGI application.
1590 */
1591 b_slow_realign(dbuf, trash.area, 0);
1592 }
1593
1594 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1595
1596 for (offset = 0; offset < b_data(&inbuf); ) {
1597 struct fcgi_param p;
1598 size_t ret;
1599
1600 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1601 if (!ret) {
1602 /* name or value too large to be decoded at once */
1603 goto fail;
1604 }
1605 offset += ret;
1606
1607 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001608 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001609 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 +02001610 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001611 }
1612 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001613 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 +02001614 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001615 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001616 }
1617 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1618 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001619 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 +02001620 }
1621 /*
1622 * Ignore all other params
1623 */
1624 }
1625
1626 /* Reset the number of concurrent streams supported if the FCGI
1627 * application does not support connection multiplexing
1628 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001629 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001630 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001631 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1632 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001633
1634 /* We must be sure to have read exactly the announced record length, no
1635 * more no less
1636 */
1637 if (offset != fconn->drl)
1638 goto fail;
1639
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001640 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 +02001641 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1642 fconn->drl = 0;
1643 fconn->drp = 0;
1644 fconn->state = FCGI_CS_RECORD_H;
1645 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001646 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1647 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001648 return 1;
1649 fail:
1650 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001651 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1652 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 +02001653 return 0;
1654}
1655
1656/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1657 * excluded, as the streams which already received the end-of-stream. It returns
1658 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1659 */
1660static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1661{
1662 struct eb32_node *node;
1663 struct fcgi_strm *fstrm;
1664
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001665 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1666
Christopher Faulet99eff652019-08-11 23:11:30 +02001667 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1668 while (node) {
1669 fstrm = container_of(node, struct fcgi_strm, by_id);
1670 node = eb32_next(node);
1671 if (fstrm->state != FCGI_SS_CLOSED &&
1672 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1673 !fcgi_strm_send_abort(fconn, fstrm))
1674 return 0;
1675 }
1676 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001677 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1678 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001679 return 1;
1680}
1681
1682/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1683 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1684 * space to proceed. It is small enough to be encoded in an empty buffer.
1685 */
1686static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1687{
1688 struct buffer outbuf;
1689 struct buffer *mbuf;
1690 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1691 int ret;
1692
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001693 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1694
Christopher Faulet99eff652019-08-11 23:11:30 +02001695 mbuf = br_tail(fconn->mbuf);
1696 retry:
1697 if (!fcgi_get_buf(fconn, mbuf)) {
1698 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001699 fstrm->flags |= FCGI_SF_BLK_MROOM;
1700 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1701 ret = 0;
1702 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001703 }
1704
1705 while (1) {
1706 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1707 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1708 break;
1709 realign_again:
1710 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1711 }
1712
1713 if (outbuf.size < 8)
1714 goto full;
1715
1716 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1717 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1718 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1719 fcgi_set_record_id(outbuf.area, fstrm->id);
1720 outbuf.data = 8;
1721
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001722 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1723 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001724 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001725 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001726 if (!fcgi_encode_begin_request(&outbuf, &rec))
1727 goto full;
1728
1729 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001730 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 +02001731 b_add(mbuf, outbuf.data);
1732 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1733 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001734 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001735 ret = 1;
1736
1737 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001738 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001739 return ret;
1740 full:
1741 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1742 goto retry;
1743 fconn->flags |= FCGI_CF_MUX_MFULL;
1744 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001745 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 +02001746 ret = 0;
1747 goto end;
1748}
1749
1750/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1751 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1752 * space to proceed. It is small enough to be encoded in an empty buffer.
1753 */
1754static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1755 enum fcgi_record_type rtype)
1756{
1757 struct buffer outbuf;
1758 struct buffer *mbuf;
1759 int ret;
1760
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001761 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001762 mbuf = br_tail(fconn->mbuf);
1763 retry:
1764 if (!fcgi_get_buf(fconn, mbuf)) {
1765 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001766 fstrm->flags |= FCGI_SF_BLK_MROOM;
1767 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1768 ret = 0;
1769 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001770 }
1771
1772 while (1) {
1773 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1774 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1775 break;
1776 realign_again:
1777 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1778 }
1779
1780 if (outbuf.size < 8)
1781 goto full;
1782
1783 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1784 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1785 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1786 outbuf.area[1] = rtype;
1787 fcgi_set_record_id(outbuf.area, fstrm->id);
1788 outbuf.data = 8;
1789
1790 /* commit the record */
1791 b_add(mbuf, outbuf.data);
1792 ret = 1;
1793
1794 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001795 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001796 return ret;
1797 full:
1798 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1799 goto retry;
1800 fconn->flags |= FCGI_CF_MUX_MFULL;
1801 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001802 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 +02001803 ret = 0;
1804 goto end;
1805}
1806
1807
1808/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1809 * marks the end of params.
1810 */
1811static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1812{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001813 int ret;
1814
1815 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1816 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1817 if (ret)
1818 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1819 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001820}
1821
1822/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1823 * marks the end of input. On success, all the request was successfully sent.
1824 */
1825static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1826{
1827 int ret;
1828
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001829 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001830 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001831 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001832 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1834 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1835 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1836 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001837 return ret;
1838}
1839
1840/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1841 * stops the request processing.
1842 */
1843static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1844{
1845 int ret;
1846
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001847 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001848 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001849 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001850 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001851 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1852 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1853 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1854 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001855 return ret;
1856}
1857
1858/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1859 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1860 * several records are sent. However, a K/V param cannot be split between 2
1861 * records.
1862 */
1863static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1864 struct htx *htx)
1865{
1866 struct buffer outbuf;
1867 struct buffer *mbuf;
1868 struct htx_blk *blk;
1869 struct htx_sl *sl = NULL;
1870 struct fcgi_strm_params params;
1871 size_t total = 0;
1872
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001873 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1874
Christopher Faulet99eff652019-08-11 23:11:30 +02001875 memset(&params, 0, sizeof(params));
1876 params.p = get_trash_chunk();
1877
1878 mbuf = br_tail(fconn->mbuf);
1879 retry:
1880 if (!fcgi_get_buf(fconn, mbuf)) {
1881 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001882 fstrm->flags |= FCGI_SF_BLK_MROOM;
1883 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1884 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001885 }
1886
1887 while (1) {
1888 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1889 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1890 break;
1891 realign_again:
1892 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1893 }
1894
1895 if (outbuf.size < 8)
1896 goto full;
1897
1898 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1899 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1900 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1901 fcgi_set_record_id(outbuf.area, fstrm->id);
1902 outbuf.data = 8;
1903
1904 blk = htx_get_head_blk(htx);
1905 while (blk) {
1906 enum htx_blk_type type;
1907 uint32_t size = htx_get_blksz(blk);
1908 struct fcgi_param p;
1909
1910 type = htx_get_blk_type(blk);
1911 switch (type) {
1912 case HTX_BLK_REQ_SL:
1913 sl = htx_get_blk_ptr(htx, blk);
1914 if (sl->info.req.meth == HTTP_METH_HEAD)
1915 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1916 if (sl->flags & HTX_SL_F_VER_11)
1917 fstrm->h1m.flags |= H1_MF_VER_11;
1918 break;
1919
1920 case HTX_BLK_HDR:
1921 p.n = htx_get_blk_name(htx, blk);
1922 p.v = htx_get_blk_value(htx, blk);
1923
1924 if (istmatch(p.n, ist(":fcgi-"))) {
1925 p.n.ptr += 6;
1926 p.n.len -= 6;
1927 if (isteq(p.n, ist("gateway_interface")))
1928 params.mask |= FCGI_SP_CGI_GATEWAY;
1929 else if (isteq(p.n, ist("document_root"))) {
1930 params.mask |= FCGI_SP_DOC_ROOT;
1931 params.docroot = p.v;
1932 }
1933 else if (isteq(p.n, ist("script_name"))) {
1934 params.mask |= FCGI_SP_SCRIPT_NAME;
1935 params.scriptname = p.v;
1936 }
1937 else if (isteq(p.n, ist("path_info"))) {
1938 params.mask |= FCGI_SP_PATH_INFO;
1939 params.pathinfo = p.v;
1940 }
1941 else if (isteq(p.n, ist("request_uri"))) {
1942 params.mask |= FCGI_SP_REQ_URI;
1943 params.uri = p.v;
1944 }
1945 else if (isteq(p.n, ist("request_meth")))
1946 params.mask |= FCGI_SP_REQ_METH;
1947 else if (isteq(p.n, ist("query_string")))
1948 params.mask |= FCGI_SP_REQ_QS;
1949 else if (isteq(p.n, ist("server_name")))
1950 params.mask |= FCGI_SP_SRV_NAME;
1951 else if (isteq(p.n, ist("server_port")))
1952 params.mask |= FCGI_SP_SRV_PORT;
1953 else if (isteq(p.n, ist("server_protocol")))
1954 params.mask |= FCGI_SP_SRV_PROTO;
1955 else if (isteq(p.n, ist("remote_addr")))
1956 params.mask |= FCGI_SP_REM_ADDR;
1957 else if (isteq(p.n, ist("remote_port")))
1958 params.mask |= FCGI_SP_REM_PORT;
1959 else if (isteq(p.n, ist("script_filename")))
1960 params.mask |= FCGI_SP_SCRIPT_FILE;
1961 else if (isteq(p.n, ist("path_translated")))
1962 params.mask |= FCGI_SP_PATH_TRANS;
1963 else if (isteq(p.n, ist("https")))
1964 params.mask |= FCGI_SP_HTTPS;
1965 }
1966 else if (isteq(p.n, ist("content-length"))) {
1967 p.n = ist("CONTENT_LENGTH");
1968 params.mask |= FCGI_SP_CONT_LEN;
1969 }
1970 else if (isteq(p.n, ist("content-type")))
1971 p.n = ist("CONTENT_TYPE");
1972 else {
1973 if (isteq(p.n, ist("host")))
1974 params.srv_name = p.v;
1975
Christopher Faulet67d58092019-10-02 10:51:38 +02001976 /* Skip header if same name is used to add the server name */
1977 if (fconn->proxy->server_id_hdr_name &&
1978 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1979 break;
1980
Christopher Faulet99eff652019-08-11 23:11:30 +02001981 memcpy(trash.area, "http_", 5);
1982 memcpy(trash.area+5, p.n.ptr, p.n.len);
1983 p.n = ist2(trash.area, p.n.len+5);
1984 }
1985
1986 if (!fcgi_encode_param(&outbuf, &p)) {
1987 if (b_space_wraps(mbuf))
1988 goto realign_again;
1989 if (outbuf.data == 8)
1990 goto full;
1991 goto done;
1992 }
1993 break;
1994
1995 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001996 if (fconn->proxy->server_id_hdr_name) {
1997 struct server *srv = objt_server(fconn->conn->target);
1998
1999 if (!srv)
2000 goto done;
2001
2002 memcpy(trash.area, "http_", 5);
2003 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2004 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2005 p.v = ist(srv->id);
2006
2007 if (!fcgi_encode_param(&outbuf, &p)) {
2008 if (b_space_wraps(mbuf))
2009 goto realign_again;
2010 if (outbuf.data == 8)
2011 goto full;
2012 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002013 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002014 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002015 goto done;
2016
2017 default:
2018 break;
2019 }
2020 total += size;
2021 blk = htx_remove_blk(htx, blk);
2022 }
2023
2024 done:
2025 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2026 goto error;
2027
2028 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2029 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2030 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2031 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2032 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2033 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2034 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2035 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2044 goto error;
2045
2046 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002047 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 +02002048 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2049 b_add(mbuf, outbuf.data);
2050
2051 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002052 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002053 return total;
2054 full:
2055 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2056 goto retry;
2057 fconn->flags |= FCGI_CF_MUX_MFULL;
2058 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002059 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 +02002060 if (total)
2061 goto error;
2062 goto end;
2063
2064 error:
2065 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002066 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002067 fcgi_strm_error(fstrm);
2068 goto end;
2069}
2070
2071/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2072 * anything. STDIN records contain the request body.
2073 */
2074static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2075 struct htx *htx, size_t count, struct buffer *buf)
2076{
2077 struct buffer outbuf;
2078 struct buffer *mbuf;
2079 struct htx_blk *blk;
2080 enum htx_blk_type type;
2081 uint32_t size;
2082 size_t total = 0;
2083
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002084 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002085 if (!count)
2086 goto end;
2087
2088 mbuf = br_tail(fconn->mbuf);
2089 retry:
2090 if (!fcgi_get_buf(fconn, mbuf)) {
2091 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002092 fstrm->flags |= FCGI_SF_BLK_MROOM;
2093 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2094 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002095 }
2096
2097 /* Perform some optimizations to reduce the number of buffer copies.
2098 * First, if the mux's buffer is empty and the htx area contains exactly
2099 * one data block of the same size as the requested count, and this
2100 * count fits within the record size, then it's possible to simply swap
2101 * the caller's buffer with the mux's output buffer and adjust offsets
2102 * and length to match the entire DATA HTX block in the middle. In this
2103 * case we perform a true zero-copy operation from end-to-end. This is
2104 * the situation that happens all the time with large files. Second, if
2105 * this is not possible, but the mux's output buffer is empty, we still
2106 * have an opportunity to avoid the copy to the intermediary buffer, by
2107 * making the intermediary buffer's area point to the output buffer's
2108 * area. In this case we want to skip the HTX header to make sure that
2109 * copies remain aligned and that this operation remains possible all
2110 * the time. This goes for headers, data blocks and any data extracted
2111 * from the HTX blocks.
2112 */
2113 blk = htx_get_head_blk(htx);
2114 if (!blk)
2115 goto end;
2116 type = htx_get_blk_type(blk);
2117 size = htx_get_blksz(blk);
2118 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2119 void *old_area = mbuf->area;
2120
2121 if (b_data(mbuf)) {
2122 /* Too bad there are data left there. We're willing to memcpy/memmove
2123 * up to 1/4 of the buffer, which means that it's OK to copy a large
2124 * record into a buffer containing few data if it needs to be realigned,
2125 * and that it's also OK to copy few data without realigning. Otherwise
2126 * we'll pretend the mbuf is full and wait for it to become empty.
2127 */
2128 if (size + 8 <= b_room(mbuf) &&
2129 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2130 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2131 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002132 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002133 }
2134
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002135 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 +02002136 /* map a FCGI record to the HTX block so that we can put the
2137 * record header there.
2138 */
2139 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2140 outbuf.area = b_head(mbuf);
2141
2142 /* prepend a FCGI record header just before the DATA block */
2143 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2144 fcgi_set_record_id(outbuf.area, fstrm->id);
2145 fcgi_set_record_size(outbuf.area, size);
2146
2147 /* and exchange with our old area */
2148 buf->area = old_area;
2149 buf->data = buf->head = 0;
2150 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002151
2152 htx = (struct htx *)buf->area;
2153 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002154 goto end;
2155 }
2156
2157 copy:
2158 while (1) {
2159 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2160 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2161 break;
2162 realign_again:
2163 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2164 }
2165
2166 if (outbuf.size < 8)
2167 goto full;
2168
2169 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2170 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2171 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2172 fcgi_set_record_id(outbuf.area, fstrm->id);
2173 outbuf.data = 8;
2174
2175 blk = htx_get_head_blk(htx);
2176 while (blk && count) {
2177 enum htx_blk_type type = htx_get_blk_type(blk);
2178 uint32_t size = htx_get_blksz(blk);
2179 struct ist v;
2180
2181 switch (type) {
2182 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002183 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 +02002184 v = htx_get_blk_value(htx, blk);
2185 if (v.len > count)
2186 v.len = count;
2187
2188 if (v.len > b_room(&outbuf)) {
2189 /* It doesn't fit at once. If it at least fits once split and
2190 * the amount of data to move is low, let's defragment the
2191 * buffer now.
2192 */
2193 if (b_space_wraps(mbuf) &&
2194 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2195 b_data(mbuf) <= MAX_DATA_REALIGN)
2196 goto realign_again;
2197 v.len = b_room(&outbuf);
2198 }
2199 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2200 if (outbuf.data == 8)
2201 goto full;
2202 goto done;
2203 }
2204 if (v.len != size) {
2205 total += v.len;
2206 count -= v.len;
2207 htx_cut_data_blk(htx, blk, v.len);
2208 goto done;
2209 }
2210 break;
2211
2212 case HTX_BLK_EOM:
2213 goto done;
2214
2215 default:
2216 break;
2217 }
2218 total += size;
2219 count -= size;
2220 blk = htx_remove_blk(htx, blk);
2221 }
2222
2223 done:
2224 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002225 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 +02002226 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2227 b_add(mbuf, outbuf.data);
2228
2229 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002230 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002231 return total;
2232 full:
2233 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2234 goto retry;
2235 fconn->flags |= FCGI_CF_MUX_MFULL;
2236 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002237 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 +02002238 goto end;
2239}
2240
2241/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2242 * anything. STDOUT records contain the entire response. All the content is
2243 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2244 */
2245static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2246{
2247 struct buffer *dbuf;
2248 size_t ret;
2249 size_t max;
2250
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002251 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2252
Christopher Faulet99eff652019-08-11 23:11:30 +02002253 dbuf = &fconn->dbuf;
2254
2255 /* Only padding remains */
2256 if (fconn->state == FCGI_CS_RECORD_P)
2257 goto end_transfer;
2258
2259 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2260 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2261 buf_room_for_htx_data(dbuf))
2262 goto fail; // incomplete record
2263
2264 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2265 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002266 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2267 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002268 }
2269
2270 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2271 max = buf_room_for_htx_data(&fstrm->rxbuf);
2272 if (!b_data(&fstrm->rxbuf))
2273 fstrm->rxbuf.head = sizeof(struct htx);
2274 if (max > fconn->drl)
2275 max = fconn->drl;
2276
2277 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2278 if (!ret)
2279 goto fail;
2280 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002281 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2282 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 +02002283
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002284 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002285 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002286 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2287 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002288
2289 if (fconn->drl)
2290 goto fail;
2291
2292 end_transfer:
2293 fconn->drl += fconn->drp;
2294 fconn->drp = 0;
2295 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2296 b_del(&fconn->dbuf, ret);
2297 fconn->drl -= ret;
2298 if (fconn->drl)
2299 goto fail;
2300
2301 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002302 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2303 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002304 return 1;
2305 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002306 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 +02002307 return 0;
2308}
2309
2310
2311/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2312 * anything. It only skip the padding in fact, there is no payload for such
2313 * records. It makrs the end of the response.
2314 */
2315static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2316{
2317 int ret;
2318
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002319 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2320
Christopher Faulet99eff652019-08-11 23:11:30 +02002321 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002322 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002323 fconn->drl += fconn->drp;
2324 fconn->drp = 0;
2325 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2326 b_del(&fconn->dbuf, ret);
2327 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002328 if (fconn->drl) {
2329 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 +02002330 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002331 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002332 fconn->state = FCGI_CS_RECORD_H;
2333 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002334 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2335 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);
2336 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002337 return 1;
2338}
2339
2340/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2341 * anything.
2342 */
2343static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2344{
2345 struct buffer *dbuf;
2346 struct buffer tag;
2347 size_t ret;
2348
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002349 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002350 dbuf = &fconn->dbuf;
2351
2352 /* Only padding remains */
2353 if (fconn->state == FCGI_CS_RECORD_P)
2354 goto end_transfer;
2355
2356 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2357 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2358 buf_room_for_htx_data(dbuf))
2359 goto fail; // incomplete record
2360
2361 chunk_reset(&trash);
2362 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2363 if (!ret)
2364 goto fail;
2365 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002366 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 +02002367
2368 trash.area[ret] = '\n';
2369 trash.area[ret+1] = '\0';
2370 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002371 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002372
2373 if (fconn->drl)
2374 goto fail;
2375
2376 end_transfer:
2377 fconn->drl += fconn->drp;
2378 fconn->drp = 0;
2379 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2380 b_del(&fconn->dbuf, ret);
2381 fconn->drl -= ret;
2382 if (fconn->drl)
2383 goto fail;
2384 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002385 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2386 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002387 return 1;
2388 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002389 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 +02002390 return 0;
2391}
2392
2393/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2394 * anything. If the empty STDOUT record is not already received, this one marks
2395 * the end of the response. It is highly unexpected, but if the record is larger
2396 * than a buffer and cannot be decoded in one time, an error is triggered and
2397 * the connection is closed. END_REQUEST record cannot be split.
2398 */
2399static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2400{
2401 struct buffer inbuf;
2402 struct buffer *dbuf;
2403 struct fcgi_end_request endreq;
2404
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002405 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002406 dbuf = &fconn->dbuf;
2407
2408 /* Record too large to be fully decoded */
2409 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2410 goto fail;
2411
2412 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002413 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2414 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002415 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002416 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002417
2418 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2419 /* Realign the dmux buffer if the record wraps. It is unexpected
2420 * at this stage because it should be the first record received
2421 * from the FCGI application.
2422 */
2423 b_slow_realign(dbuf, trash.area, 0);
2424 }
2425
2426 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2427
2428 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2429 goto fail;
2430
2431 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002432 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2433 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 +02002434 fstrm->proto_status = endreq.errcode;
2435 fcgi_strm_close(fstrm);
2436
2437 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2438 fconn->drl = 0;
2439 fconn->drp = 0;
2440 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002441 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2442 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002443 return 1;
2444
2445 fail:
2446 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002447 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 +02002448 return 0;
2449}
2450
2451/* process Rx records to be demultiplexed */
2452static void fcgi_process_demux(struct fcgi_conn *fconn)
2453{
2454 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2455 struct fcgi_header hdr;
2456 int ret;
2457
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002458 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2459
Christopher Faulet99eff652019-08-11 23:11:30 +02002460 if (fconn->state == FCGI_CS_CLOSED)
2461 return;
2462
2463 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002464 if (fconn->state == FCGI_CS_INIT) {
2465 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2466 return;
2467 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002468 if (fconn->state == FCGI_CS_SETTINGS) {
2469 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002470 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002471 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2472 if (!ret)
2473 goto fail;
2474 b_del(&fconn->dbuf, ret);
2475
2476 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2477 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002478 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);
2479 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 +02002480 goto fail;
2481 }
2482 goto new_record;
2483 }
2484 }
2485
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002486 /* process as many incoming records as possible below */
2487 while (1) {
2488 if (!b_data(&fconn->dbuf)) {
2489 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2490 break;
2491 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002492
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002493 if (fconn->state == FCGI_CS_CLOSED) {
2494 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002495 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002496 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002497
2498 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002499 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002500 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2501 if (!ret)
2502 break;
2503 b_del(&fconn->dbuf, ret);
2504
2505 new_record:
2506 fconn->dsi = hdr.id;
2507 fconn->drt = hdr.type;
2508 fconn->drl = hdr.len;
2509 fconn->drp = hdr.padding;
2510 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002511 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 +02002512 }
2513
2514 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2515 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2516
2517 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2518 (b_data(&fstrm->rxbuf) ||
2519 conn_xprt_read0_pending(fconn->conn) ||
2520 fstrm->state == FCGI_SS_CLOSED ||
2521 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2522 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2523 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002524 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 +02002525 fstrm->cs->flags |= CS_FL_RCV_MORE;
2526 fcgi_strm_notify_recv(fstrm);
2527 }
2528 fstrm = tmp_fstrm;
2529
2530 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2531 /* ignore all record for closed streams */
2532 goto ignore_record;
2533 }
2534 if (fstrm->state == FCGI_SS_IDLE) {
2535 /* ignore all record for unknown streams */
2536 goto ignore_record;
2537 }
2538
2539 switch (fconn->drt) {
2540 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002541 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 +02002542 ret = fcgi_conn_handle_values_result(fconn);
2543 break;
2544
2545 case FCGI_STDOUT:
2546 if (fstrm->flags & FCGI_SF_ES_RCVD)
2547 goto ignore_record;
2548
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002549 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002550 if (fconn->drl)
2551 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2552 else
2553 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2554 break;
2555
2556 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002557 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002558 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2559 break;
2560
2561 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002562 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 +02002563 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2564 break;
2565
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002566 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002567 default:
2568 ignore_record:
2569 /* drop records that we ignore. They may be
2570 * larger than the buffer so we drain all of
2571 * their contents until we reach the end.
2572 */
2573 fconn->state = FCGI_CS_RECORD_P;
2574 fconn->drl += fconn->drp;
2575 fconn->drp = 0;
2576 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002577 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2578 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002579 b_del(&fconn->dbuf, ret);
2580 fconn->drl -= ret;
2581 ret = (fconn->drl == 0);
2582 }
2583
2584 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002585 if (ret <= 0) {
2586 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002587 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002589
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002590 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002591 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002592 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2593 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002594 }
2595
2596 fail:
2597 /* we can go here on missing data, blocked response or error */
2598 if (fstrm && fstrm->cs &&
2599 (b_data(&fstrm->rxbuf) ||
2600 conn_xprt_read0_pending(fconn->conn) ||
2601 fstrm->state == FCGI_SS_CLOSED ||
2602 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2603 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2604 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002605 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 +02002606 fstrm->cs->flags |= CS_FL_RCV_MORE;
2607 fcgi_strm_notify_recv(fstrm);
2608 }
2609
2610 fcgi_conn_restart_reading(fconn, 0);
2611}
2612
2613/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2614 * the end.
2615 */
2616static int fcgi_process_mux(struct fcgi_conn *fconn)
2617{
2618 struct fcgi_strm *fstrm, *fstrm_back;
2619
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002620 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2621
Christopher Faulet99eff652019-08-11 23:11:30 +02002622 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2623 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2624 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2625 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002626 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 +02002627 fcgi_wake_unassigned_streams(fconn);
2628 goto mux;
2629 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002630 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002631 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2632 goto fail;
2633 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 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 +02002635 }
2636 /* need to wait for the other side */
2637 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002639 }
2640
2641 mux:
2642 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2643 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2644 break;
2645
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002646 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002647 continue;
2648
2649 /* For some reason, the upper layer failed to subsribe again,
2650 * so remove it from the send_list
2651 */
2652 if (!fstrm->send_wait) {
2653 LIST_DEL_INIT(&fstrm->send_list);
2654 continue;
2655 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002656 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002657 fstrm->flags &= ~FCGI_SF_BLK_ANY;
2658 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002659 fstrm->flags |= FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02002660 tasklet_wakeup(fstrm->send_wait->tasklet);
2661 }
2662
2663 fail:
2664 if (fconn->state == FCGI_CS_CLOSED) {
2665 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2666 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002667 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2668 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002669 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002670 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002671 }
2672 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002673
2674 done:
2675 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002676 return 1;
2677}
2678
2679
2680/* Attempt to read data, and subscribe if none available.
2681 * The function returns 1 if data has been received, otherwise zero.
2682 */
2683static int fcgi_recv(struct fcgi_conn *fconn)
2684{
2685 struct connection *conn = fconn->conn;
2686 struct buffer *buf;
2687 int max;
2688 size_t ret;
2689
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002690 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2691
2692 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2693 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002695 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002696
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002697 if (!fcgi_recv_allowed(fconn)) {
2698 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002699 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002700 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002701
2702 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2703 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002704 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002705 fconn->flags |= FCGI_CF_DEM_DALLOC;
2706 return 0;
2707 }
2708
2709 b_realign_if_empty(buf);
2710 if (!b_data(buf)) {
2711 /* try to pre-align the buffer like the
2712 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002713 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002714 * HTX block to alias it upon recv. We cannot use the
2715 * head because rcv_buf() will realign the buffer if
2716 * it's empty. Thus we cheat and pretend we already
2717 * have a few bytes there.
2718 */
2719 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2720 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2721 }
2722 else
2723 max = buf_room_for_htx_data(buf);
2724
2725 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2726
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 if (max && !ret && fcgi_recv_allowed(fconn)) {
2728 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002729 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002730 }
2731 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002732 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002733
2734 if (!b_data(buf)) {
2735 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002736 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002737 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2738 }
2739
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002740 if (ret == max) {
2741 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002742 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002743 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002744
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002745 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002746 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2747}
2748
2749
2750/* Try to send data if possible.
2751 * The function returns 1 if data have been sent, otherwise zero.
2752 */
2753static int fcgi_send(struct fcgi_conn *fconn)
2754{
2755 struct connection *conn = fconn->conn;
2756 int done;
2757 int sent = 0;
2758
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002759 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2760
2761 if (conn->flags & CO_FL_ERROR) {
2762 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002763 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002764 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002765
2766
2767 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2768 /* a handshake was requested */
2769 goto schedule;
2770 }
2771
2772 /* This loop is quite simple : it tries to fill as much as it can from
2773 * pending streams into the existing buffer until it's reportedly full
2774 * or the end of send requests is reached. Then it tries to send this
2775 * buffer's contents out, marks it not full if at least one byte could
2776 * be sent, and tries again.
2777 *
2778 * The snd_buf() function normally takes a "flags" argument which may
2779 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2780 * data immediately comes and CO_SFL_STREAMER to indicate that the
2781 * connection is streaming lots of data (used to increase TLS record
2782 * size at the expense of latency). The former can be sent any time
2783 * there's a buffer full flag, as it indicates at least one stream
2784 * attempted to send and failed so there are pending data. An
2785 * alternative would be to set it as long as there's an active stream
2786 * but that would be problematic for ACKs until we have an absolute
2787 * guarantee that all waiters have at least one byte to send. The
2788 * latter should possibly not be set for now.
2789 */
2790
2791 done = 0;
2792 while (!done) {
2793 unsigned int flags = 0;
2794 unsigned int released = 0;
2795 struct buffer *buf;
2796
2797 /* fill as much as we can into the current buffer */
2798 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2799 done = fcgi_process_mux(fconn);
2800
2801 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2802 done = 1; // we won't go further without extra buffers
2803
2804 if (conn->flags & CO_FL_ERROR)
2805 break;
2806
2807 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2808 flags |= CO_SFL_MSG_MORE;
2809
2810 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2811 if (b_data(buf)) {
2812 int ret;
2813
2814 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2815 if (!ret) {
2816 done = 1;
2817 break;
2818 }
2819 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002820 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002821 b_del(buf, ret);
2822 if (b_data(buf)) {
2823 done = 1;
2824 break;
2825 }
2826 }
2827 b_free(buf);
2828 released++;
2829 }
2830
2831 if (released)
2832 offer_buffers(NULL, tasks_run_queue);
2833
2834 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002835 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2836 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002837 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2838 }
2839
2840 if (conn->flags & CO_FL_SOCK_WR_SH) {
2841 /* output closed, nothing to send, clear the buffer to release it */
2842 b_reset(br_tail(fconn->mbuf));
2843 }
2844 /* We're not full anymore, so we can wake any task that are waiting
2845 * for us.
2846 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002847 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002848 struct fcgi_strm *fstrm;
2849
2850 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2851 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2852 break;
2853
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002854 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002855 continue;
2856
2857 /* For some reason, the upper layer failed to subsribe again,
2858 * so remove it from the send_list
2859 */
2860 if (!fstrm->send_wait) {
2861 LIST_DEL_INIT(&fstrm->send_list);
2862 continue;
2863 }
2864 fstrm->flags &= ~FCGI_SF_BLK_ANY;
2865 fstrm->send_wait->events &= ~SUB_RETRY_SEND;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002866 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002867 tasklet_wakeup(fstrm->send_wait->tasklet);
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002868 fstrm->flags |= FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 }
2870 }
2871 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002872 if (!br_data(fconn->mbuf)) {
2873 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002874 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002875 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002876schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002877 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2878 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002879 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002880 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002881
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002882 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002883 return sent;
2884}
2885
2886/* this is the tasklet referenced in fconn->wait_event.tasklet */
2887static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2888{
2889 struct fcgi_conn *fconn = ctx;
2890 int ret = 0;
2891
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002892 TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn);
2893
Christopher Faulet99eff652019-08-11 23:11:30 +02002894 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2895 ret = fcgi_send(fconn);
2896 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2897 ret |= fcgi_recv(fconn);
2898 if (ret || b_data(&fconn->dbuf))
2899 fcgi_process(fconn);
2900 return NULL;
2901}
2902
2903/* callback called on any event by the connection handler.
2904 * It applies changes and returns zero, or < 0 if it wants immediate
2905 * destruction of the connection (which normally doesn not happen in FCGI).
2906 */
2907static int fcgi_process(struct fcgi_conn *fconn)
2908{
2909 struct connection *conn = fconn->conn;
2910
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002911 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2912
Christopher Faulet99eff652019-08-11 23:11:30 +02002913 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2914 fcgi_process_demux(fconn);
2915
2916 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2917 b_reset(&fconn->dbuf);
2918
2919 if (buf_room_for_htx_data(&fconn->dbuf))
2920 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2921 }
2922 fcgi_send(fconn);
2923
2924 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2925 /* frontend is stopping, reload likely in progress, let's try
2926 * to announce a graceful shutdown if not yet done. We don't
2927 * care if it fails, it will be tried again later.
2928 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002929 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 +02002930 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2931 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2932 fcgi_conn_send_aborts(fconn);
2933 }
2934 }
2935
2936 /*
2937 * If we received early data, and the handshake is done, wake
2938 * any stream that was waiting for it.
2939 */
2940 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
2941 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2942 struct eb32_node *node;
2943 struct fcgi_strm *fstrm;
2944
2945 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
2946 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
2947
2948 while (node) {
2949 fstrm = container_of(node, struct fcgi_strm, by_id);
2950 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
2951 fcgi_strm_notify_recv(fstrm);
2952 node = eb32_next(node);
2953 }
2954 }
2955
2956 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
2957 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2958 eb_is_empty(&fconn->streams_by_id)) {
2959 fcgi_wake_some_streams(fconn, 0);
2960
2961 if (eb_is_empty(&fconn->streams_by_id)) {
2962 /* no more stream, kill the connection now */
2963 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002964 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002965 return -1;
2966 }
2967 }
2968
2969 if (!b_data(&fconn->dbuf))
2970 fcgi_release_buf(fconn, &fconn->dbuf);
2971
2972 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2973 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2974 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
2975 fcgi_release_mbuf(fconn);
2976
2977 if (fconn->task) {
2978 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
2979 task_queue(fconn->task);
2980 }
2981
2982 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002983 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002984 return 0;
2985}
2986
2987
2988/* wake-up function called by the connection layer (mux_ops.wake) */
2989static int fcgi_wake(struct connection *conn)
2990{
2991 struct fcgi_conn *fconn = conn->ctx;
2992
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002993 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002994 return (fcgi_process(fconn));
2995}
2996
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002997
2998static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2999{
3000 int ret = 0;
3001 switch (mux_ctl) {
3002 case MUX_STATUS:
3003 if (conn->flags & CO_FL_CONNECTED)
3004 ret |= MUX_STATUS_READY;
3005 return ret;
3006 default:
3007 return -1;
3008 }
3009}
3010
Christopher Faulet99eff652019-08-11 23:11:30 +02003011/* Connection timeout management. The principle is that if there's no receipt
3012 * nor sending for a certain amount of time, the connection is closed. If the
3013 * MUX buffer still has lying data or is not allocatable, the connection is
3014 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003015 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003016 */
3017static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3018{
3019 struct fcgi_conn *fconn = context;
3020 int expired = tick_is_expired(t->expire, now_ms);
3021
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003022 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3023
3024 if (!expired && fconn) {
3025 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003026 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003027 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003028
3029 task_destroy(t);
3030
3031 if (!fconn) {
3032 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003033 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003034 return NULL;
3035 }
3036
3037 fconn->task = NULL;
3038 fconn->state = FCGI_CS_CLOSED;
3039 fcgi_wake_some_streams(fconn, 0);
3040
3041 if (br_data(fconn->mbuf)) {
3042 /* don't even try to send aborts, the buffer is stuck */
3043 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3044 goto end;
3045 }
3046
3047 /* try to send but no need to insist */
3048 if (!fcgi_conn_send_aborts(fconn))
3049 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3050
3051 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3052 conn_xprt_ready(fconn->conn)) {
3053 unsigned int released = 0;
3054 struct buffer *buf;
3055
3056 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3057 if (b_data(buf)) {
3058 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3059 buf, b_data(buf), 0);
3060 if (!ret)
3061 break;
3062 b_del(buf, ret);
3063 if (b_data(buf))
3064 break;
3065 b_free(buf);
3066 released++;
3067 }
3068 }
3069
3070 if (released)
3071 offer_buffers(NULL, tasks_run_queue);
3072 }
3073
3074 end:
3075 /* either we can release everything now or it will be done later once
3076 * the last stream closes.
3077 */
3078 if (eb_is_empty(&fconn->streams_by_id))
3079 fcgi_release(fconn);
3080
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003081 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003082 return NULL;
3083}
3084
3085
3086/*******************************************/
3087/* functions below are used by the streams */
3088/*******************************************/
3089
3090/* Append the description of what is present in error snapshot <es> into <out>.
3091 * The description must be small enough to always fit in a buffer. The output
3092 * buffer may be the trash so the trash must not be used inside this function.
3093 */
3094static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3095{
3096 chunk_appendf(out,
3097 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3098 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3099 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3100 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3101 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3102 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3103}
3104/*
3105 * Capture a bad response and archive it in the proxy's structure. By default
3106 * it tries to report the error position as h1m->err_pos. However if this one is
3107 * not set, it will then report h1m->next, which is the last known parsing
3108 * point. The function is able to deal with wrapping buffers. It always displays
3109 * buffers as a contiguous area starting at buf->p. The direction is determined
3110 * thanks to the h1m's flags.
3111 */
3112static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3113 struct h1m *h1m, struct buffer *buf)
3114{
3115 struct session *sess = fstrm->sess;
3116 struct proxy *proxy = fconn->proxy;
3117 struct proxy *other_end = sess->fe;
3118 union error_snapshot_ctx ctx;
3119
3120 /* http-specific part now */
3121 ctx.h1.state = h1m->state;
3122 ctx.h1.c_flags = fconn->flags;
3123 ctx.h1.s_flags = fstrm->flags;
3124 ctx.h1.m_flags = h1m->flags;
3125 ctx.h1.m_clen = h1m->curr_len;
3126 ctx.h1.m_blen = h1m->body_len;
3127
3128 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3129 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3130 &ctx, fcgi_show_error_snapshot);
3131}
3132
3133static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3134 struct buffer *buf, size_t *ofs, size_t max)
3135{
3136 int ret;
3137
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003138 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003139 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3140 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003141 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 +02003142 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003143 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 +02003144 fcgi_strm_error(fstrm);
3145 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3146 }
3147 goto end;
3148 }
3149
3150 *ofs += ret;
3151 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003152 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003153 return ret;
3154
3155}
3156
Christopher Fauletaf542632019-10-01 21:52:49 +02003157static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003158 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3159{
3160 int ret;
3161
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003162 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003163 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003164 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003165 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 +02003166 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003167 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 +02003168 fcgi_strm_error(fstrm);
3169 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3170 }
3171 goto end;
3172 }
3173 *ofs += ret;
3174 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003175 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003176 return ret;
3177}
3178
3179static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3180 struct buffer *buf, size_t *ofs, size_t max)
3181{
3182 int ret;
3183
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003184 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003185 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003186 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003187 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 +02003188 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003189 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 +02003190 fcgi_strm_error(fstrm);
3191 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3192 }
3193 goto end;
3194 }
3195 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003196 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003197 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003198 return ret;
3199}
3200
3201static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003202 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003203{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003204 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003205
Christopher Faulet76014fd2019-12-10 11:47:22 +01003206 TRACE_ENTER(FCGI_EV_RSP_DATA||FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
3207 ret = h1_parse_msg_eom(h1m, htx, max);
3208 if (!ret) {
3209 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3210 if (htx->flags & HTX_FL_PARSING_ERROR) {
3211 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3212 fcgi_strm_error(fstrm);
3213 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3214 }
3215 goto end;
3216 }
3217 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3218 end:
3219 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3220 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003221}
3222
3223static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3224{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003225 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003226 struct htx *htx;
3227 struct h1m *h1m = &fstrm->h1m;
3228 size_t ret, data, total = 0;
3229
3230 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003231 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3232
Christopher Faulet99eff652019-08-11 23:11:30 +02003233 data = htx->data;
3234 if (fstrm->state == FCGI_SS_ERROR)
3235 goto end;
3236
3237 do {
3238 size_t used = htx_used_space(htx);
3239
3240 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003241 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003242 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3243 if (!ret)
3244 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003245
3246 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3247
Christopher Faulet99eff652019-08-11 23:11:30 +02003248 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3249 struct htx_blk *blk = htx_get_head_blk(htx);
3250 struct htx_sl *sl;
3251
3252 if (!blk)
3253 break;
3254 sl = htx_get_blk_ptr(htx, blk);
3255 sl->flags |= HTX_SL_F_XFER_LEN;
3256 htx->extra = 0;
3257 }
3258 }
3259 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003260 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003261 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003262 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003263 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003264
3265 TRACE_PROTO("rcvd response payload data", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003266 }
3267 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003268 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3269 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3270 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003271 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003272
Christopher Faulet76014fd2019-12-10 11:47:22 +01003273 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 +02003274 }
3275 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003276 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3277 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3278 break;
3279
3280 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3281 }
3282
Christopher Faulet99eff652019-08-11 23:11:30 +02003283 if (b_data(&fstrm->rxbuf) > total) {
3284 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003285 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003286 fcgi_strm_error(fstrm);
3287 }
3288 break;
3289 }
3290 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003291 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003292 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003293
Christopher Faulet99eff652019-08-11 23:11:30 +02003294 if (fstrm->state != FCGI_SS_ERROR &&
3295 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003296 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003297 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3298 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3299 h1m->state = H1_MSG_DONE;
3300 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 +02003301 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003302 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003303 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003304
3305 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003306 }
3307 else {
3308 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003309 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 fcgi_strm_error(fstrm);
3311 break;
3312 }
3313
3314 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003315 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003316
3317 if (fstrm->state == FCGI_SS_ERROR) {
3318 b_reset(&fstrm->rxbuf);
3319 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003320 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003321 return 0;
3322 }
3323
3324 b_del(&fstrm->rxbuf, total);
3325
3326 end:
3327 htx_to_buf(htx, buf);
3328 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003329 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003330 return ret;
3331}
3332
3333/*
3334 * Attach a new stream to a connection
3335 * (Used for outgoing connections)
3336 */
3337static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3338{
3339 struct conn_stream *cs;
3340 struct fcgi_strm *fstrm;
3341 struct fcgi_conn *fconn = conn->ctx;
3342
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003343 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003344 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003345 if (!cs) {
3346 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003347 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003348 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003349 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3350 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003351 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003352 cs_free(cs);
3353 return NULL;
3354 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003355 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003356 return cs;
3357}
3358
3359/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3360 * We have to scan because we may have some orphan streams. It might be
3361 * beneficial to scan backwards from the end to reduce the likeliness to find
3362 * orphans.
3363 */
3364static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3365{
3366 struct fcgi_conn *fconn = conn->ctx;
3367 struct fcgi_strm *fstrm;
3368 struct eb32_node *node;
3369
3370 node = eb32_first(&fconn->streams_by_id);
3371 while (node) {
3372 fstrm = container_of(node, struct fcgi_strm, by_id);
3373 if (fstrm->cs)
3374 return fstrm->cs;
3375 node = eb32_next(node);
3376 }
3377 return NULL;
3378}
3379
3380/*
3381 * Destroy the mux and the associated connection, if it is no longer used
3382 */
3383static void fcgi_destroy(void *ctx)
3384{
3385 struct fcgi_conn *fconn = ctx;
3386
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003387 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003388 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3389 fcgi_release(fconn);
3390}
3391
3392/*
3393 * Detach the stream from the connection and possibly release the connection.
3394 */
3395static void fcgi_detach(struct conn_stream *cs)
3396{
3397 struct fcgi_strm *fstrm = cs->ctx;
3398 struct fcgi_conn *fconn;
3399 struct session *sess;
3400
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003401 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3402
Christopher Faulet99eff652019-08-11 23:11:30 +02003403 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003404 if (!fstrm) {
3405 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003406 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003407 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003408
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003409 /* there's no txbuf so we're certain no to be able to send anything */
3410 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003411
3412 sess = fstrm->sess;
3413 fconn = fstrm->fconn;
3414 fstrm->cs = NULL;
3415 fconn->nb_cs--;
3416
3417 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3418 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3419 fconn->streams_limit = 1;
3420 }
3421 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3422 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3423 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3424 fconn->state = FCGI_CS_CLOSED;
3425 }
3426
3427 /* this stream may be blocked waiting for some data to leave, so orphan
3428 * it in this case.
3429 */
3430 if (!(cs->conn->flags & CO_FL_ERROR) &&
3431 (fconn->state != FCGI_CS_CLOSED) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003432 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) && (fstrm->send_wait || fstrm->recv_wait)) {
3433 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003434 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003436
3437 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3438 /* unblock the connection if it was blocked on this stream. */
3439 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3440 fcgi_conn_restart_reading(fconn, 1);
3441 }
3442
3443 fcgi_strm_destroy(fstrm);
3444
3445 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3446 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
3447 if (!fconn->conn->owner) {
3448 fconn->conn->owner = sess;
3449 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3450 fconn->conn->owner = NULL;
3451 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003452 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003453 /* The server doesn't want it, let's kill the connection right away */
3454 fconn->conn->mux->destroy(fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003455 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3456 }
3457 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003458 return;
3459 }
3460 }
3461 }
3462 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003463 int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn);
3464 if (ret == -1) {
3465 /* The connection is destroyed, let's leave */
3466 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003467 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003468 }
3469 else if (ret == 1) {
3470 /* The connection was added to the server idle list, just stop */
3471 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3472 return;
3473 }
3474 TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003475 }
3476 /* Never ever allow to reuse a connection from a non-reuse backend */
3477 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3478 fconn->conn->flags |= CO_FL_PRIVATE;
3479 if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) {
3480 struct server *srv = objt_server(fconn->conn->target);
3481
3482 if (srv) {
3483 if (fconn->conn->flags & CO_FL_PRIVATE)
3484 LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list);
3485 else
3486 LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list);
3487 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003488 TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 }
3490 }
3491
3492 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003493 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003494 */
3495 if (fcgi_conn_is_dead(fconn)) {
3496 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003497 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003498 fcgi_release(fconn);
3499 }
3500 else if (fconn->task) {
3501 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3502 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003503 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003504 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003505 else
3506 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003507}
3508
3509
3510/* Performs a synchronous or asynchronous shutr(). */
3511static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3512{
3513 struct fcgi_conn *fconn = fstrm->fconn;
3514 struct wait_event *sw = &fstrm->wait_event;
3515
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003516 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3517
Christopher Faulet99eff652019-08-11 23:11:30 +02003518 if (fstrm->state == FCGI_SS_CLOSED)
3519 goto done;
3520
3521 /* a connstream may require us to immediately kill the whole connection
3522 * for example because of a "tcp-request content reject" rule that is
3523 * normally used to limit abuse.
3524 */
3525 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003526 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3527 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003528 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003529 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003530 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003531 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003532 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3533 !fcgi_strm_send_abort(fconn, fstrm))
3534 goto add_to_list;
3535 }
3536
3537 fcgi_strm_close(fstrm);
3538
3539 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3540 tasklet_wakeup(fconn->wait_event.tasklet);
3541 done:
3542 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003543 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003544 return;
3545
3546 add_to_list:
3547 if (!LIST_ADDED(&fstrm->send_list)) {
3548 sw->events |= SUB_RETRY_SEND;
3549 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
3550 fstrm->send_wait = sw;
3551 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3552 }
3553 }
3554 /* Let the handler know we want shutr */
3555 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003556 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003557 return;
3558}
3559
3560/* Performs a synchronous or asynchronous shutw(). */
3561static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3562{
3563 struct fcgi_conn *fconn = fstrm->fconn;
3564 struct wait_event *sw = &fstrm->wait_event;
3565
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003566 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3567
Christopher Faulet99eff652019-08-11 23:11:30 +02003568 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3569 goto done;
3570
3571 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3572 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3573 !fcgi_strm_send_abort(fconn, fstrm))
3574 goto add_to_list;
3575
3576 if (fstrm->state == FCGI_SS_HREM)
3577 fcgi_strm_close(fstrm);
3578 else
3579 fstrm->state = FCGI_SS_HLOC;
3580 } else {
3581 /* a connstream may require us to immediately kill the whole connection
3582 * for example because of a "tcp-request content reject" rule that is
3583 * normally used to limit abuse.
3584 */
3585 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003586 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3587 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003588 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003589 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003590
3591 fcgi_strm_close(fstrm);
3592 }
3593
3594 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3595 tasklet_wakeup(fconn->wait_event.tasklet);
3596 done:
3597 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003598 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003599 return;
3600
3601 add_to_list:
3602 if (!LIST_ADDED(&fstrm->send_list)) {
3603 sw->events |= SUB_RETRY_SEND;
3604 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
3605 fstrm->send_wait = sw;
3606 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3607 }
3608 }
3609 /* let the handler know we want to shutw */
3610 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003611 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003612 return;
3613}
3614
3615/* This is the tasklet referenced in fstrm->wait_event.tasklet, it is used for
3616 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003617 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003618 */
3619static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3620{
3621 struct fcgi_strm *fstrm = ctx;
3622 struct fcgi_conn *fconn = fstrm->fconn;
3623
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003624 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3625
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003626 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3628 fcgi_do_shutw(fstrm);
3629
3630 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3631 fcgi_do_shutr(fstrm);
3632
3633 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3634 /* We're done trying to send, remove ourself from the send_list */
3635 LIST_DEL_INIT(&fstrm->send_list);
3636
3637 if (!fstrm->cs) {
3638 fcgi_strm_destroy(fstrm);
3639 if (fcgi_conn_is_dead(fconn))
3640 fcgi_release(fconn);
3641 }
3642 }
3643
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003644 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003645 return NULL;
3646}
3647
3648/* shutr() called by the conn_stream (mux_ops.shutr) */
3649static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3650{
3651 struct fcgi_strm *fstrm = cs->ctx;
3652
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003653 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 if (cs->flags & CS_FL_KILL_CONN)
3655 fstrm->flags |= FCGI_SF_KILL_CONN;
3656
3657 if (!mode)
3658 return;
3659
3660 fcgi_do_shutr(fstrm);
3661}
3662
3663/* shutw() called by the conn_stream (mux_ops.shutw) */
3664static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3665{
3666 struct fcgi_strm *fstrm = cs->ctx;
3667
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003668 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003669 if (cs->flags & CS_FL_KILL_CONN)
3670 fstrm->flags |= FCGI_SF_KILL_CONN;
3671
3672 fcgi_do_shutw(fstrm);
3673}
3674
3675/* Called from the upper layer, to subscribe to events, such as being able to send.
3676 * The <param> argument here is supposed to be a pointer to a wait_event struct
3677 * which will be passed to fstrm->recv_wait or fstrm->send_wait depending on the
3678 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
3679 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
3680 * returns 0 except for the error above.
3681 */
3682static int fcgi_subscribe(struct conn_stream *cs, int event_type, void *param)
3683{
3684 struct wait_event *sw;
3685 struct fcgi_strm *fstrm = cs->ctx;
3686 struct fcgi_conn *fconn = fstrm->fconn;
3687
3688 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003689 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003690 sw = param;
3691 BUG_ON(fstrm->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
3692 sw->events |= SUB_RETRY_RECV;
3693 fstrm->recv_wait = sw;
3694 event_type &= ~SUB_RETRY_RECV;
3695 }
3696 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003697 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003698 sw = param;
3699 BUG_ON(fstrm->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
3700 sw->events |= SUB_RETRY_SEND;
3701 fstrm->send_wait = sw;
3702 if (!LIST_ADDED(&fstrm->send_list))
3703 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3704 event_type &= ~SUB_RETRY_SEND;
3705 }
3706 if (event_type != 0)
3707 return -1;
3708 return 0;
3709}
3710
3711/* Called from the upper layer, to unsubscribe some events (undo fcgi_subscribe).
3712 * The <param> argument here is supposed to be a pointer to the same wait_event
3713 * struct that was passed to fcgi_subscribe() otherwise nothing will be changed.
3714 * It always returns zero.
3715 */
3716static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3717{
3718 struct wait_event *sw;
3719 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003720 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003721
3722 if (event_type & SUB_RETRY_RECV) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003723 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003724 sw = param;
3725 BUG_ON(fstrm->recv_wait != sw);
3726 sw->events &= ~SUB_RETRY_RECV;
3727 fstrm->recv_wait = NULL;
3728 }
3729 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003730 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003731 sw = param;
3732 BUG_ON(fstrm->send_wait != sw);
3733 LIST_DEL(&fstrm->send_list);
3734 LIST_INIT(&fstrm->send_list);
3735 sw->events &= ~SUB_RETRY_SEND;
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003736 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003737 fstrm->send_wait = NULL;
3738 }
3739 return 0;
3740}
3741
3742/* Called from the upper layer, to receive data */
3743static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3744{
3745 struct fcgi_strm *fstrm = cs->ctx;
3746 struct fcgi_conn *fconn = fstrm->fconn;
3747 size_t ret = 0;
3748
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003749 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3750
Christopher Faulet99eff652019-08-11 23:11:30 +02003751 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3752 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003753 else
3754 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003755
Christopher Faulet76014fd2019-12-10 11:47:22 +01003756 if (b_data(&fstrm->rxbuf) || (fstrm->h1m.state == H1_MSG_DONE && !(fstrm->flags & FCGI_SF_H1_PARSING_DONE)))
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3758 else {
3759 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003760 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003761 cs->flags |= CS_FL_EOI;
3762 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3763 cs->flags |= CS_FL_EOS;
3764 }
3765 if (conn_xprt_read0_pending(fconn->conn))
3766 cs->flags |= CS_FL_EOS;
3767 if (cs->flags & CS_FL_ERR_PENDING)
3768 cs->flags |= CS_FL_ERROR;
3769 fcgi_release_buf(fconn, &fstrm->rxbuf);
3770 }
3771
3772 if (ret && fconn->dsi == fstrm->id) {
3773 /* demux is blocking on this stream's buffer */
3774 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3775 fcgi_conn_restart_reading(fconn, 1);
3776 }
3777
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003778 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003779 return ret;
3780}
3781
3782
Christopher Faulet99eff652019-08-11 23:11:30 +02003783/* Called from the upper layer, to send data from buffer <buf> for no more than
3784 * <count> bytes. Returns the number of bytes effectively sent. Some status
3785 * flags may be updated on the conn_stream.
3786 */
3787static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3788{
3789 struct fcgi_strm *fstrm = cs->ctx;
3790 struct fcgi_conn *fconn = fstrm->fconn;
3791 size_t total = 0;
3792 size_t ret;
3793 struct htx *htx = NULL;
3794 struct htx_sl *sl;
3795 struct htx_blk *blk;
3796 uint32_t bsize;
3797
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003798 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3799
Christopher Faulet99eff652019-08-11 23:11:30 +02003800 /* If we were not just woken because we wanted to send but couldn't,
3801 * and there's somebody else that is waiting to send, do nothing,
3802 * we will subscribe later and be put at the end of the list
3803 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003804 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003805 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 +02003806 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003807 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003808 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003809
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003810 if (fconn->state < FCGI_CS_RECORD_H) {
3811 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003812 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003813 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003814
3815 htx = htxbuf(buf);
3816 if (fstrm->id == 0) {
3817 int32_t id = fcgi_conn_get_next_sid(fconn);
3818
3819 if (id < 0) {
3820 fcgi_strm_close(fstrm);
3821 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003822 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 +02003823 return 0;
3824 }
3825
3826 eb32_delete(&fstrm->by_id);
3827 fstrm->by_id.key = fstrm->id = id;
3828 fconn->max_id = id;
3829 fconn->nb_reserved--;
3830 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3831
3832
3833 /* Check if length of the body is known or if the message is
3834 * full. Otherwise, the request is invalid.
3835 */
3836 sl = http_get_stline(htx);
3837 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3838 htx->flags |= HTX_FL_PARSING_ERROR;
3839 fcgi_strm_error(fstrm);
3840 goto done;
3841 }
3842 }
3843
3844 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003845 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 +02003846 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3847 goto done;
3848 }
3849
3850 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3851 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3852
3853 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3854 count && !htx_is_empty(htx)) {
3855 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003856 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003857 bsize = htx_get_blksz(blk);
3858
3859 switch (htx_get_blk_type(blk)) {
3860 case HTX_BLK_REQ_SL:
3861 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003862 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 +02003863 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3864 if (!ret) {
3865 goto done;
3866 }
3867 total += ret;
3868 count -= ret;
3869 break;
3870
3871 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003872 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 +02003873 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3874 if (!ret)
3875 goto done;
3876 goto remove_blk;
3877
3878 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003879 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 +02003880 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3881 if (ret > 0) {
3882 htx = htx_from_buf(buf);
3883 total += ret;
3884 count -= ret;
3885 if (ret < bsize)
3886 goto done;
3887 }
3888 break;
3889
3890 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003891 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 +02003892 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3893 if (!ret)
3894 goto done;
3895 goto remove_blk;
3896
3897 default:
3898 remove_blk:
3899 htx_remove_blk(htx, blk);
3900 total += bsize;
3901 count -= bsize;
3902 break;
3903 }
3904 }
3905
3906 done:
3907 if (fstrm->state >= FCGI_SS_HLOC) {
3908 /* trim any possibly pending data after we close (extra CR-LF,
3909 * unprocessed trailers, abnormal extra data, ...)
3910 */
3911 total += count;
3912 count = 0;
3913 }
3914
3915 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003916 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 +02003917 cs_set_error(cs);
3918 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3919 fcgi_strm_close(fstrm);
3920 }
3921
3922 if (htx)
3923 htx_to_buf(htx, buf);
3924
Christopher Faulet99eff652019-08-11 23:11:30 +02003925 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003926 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
3927 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 +02003928 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003929 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003930
3931 /* Ok we managed to send something, leave the send_list */
3932 LIST_DEL_INIT(&fstrm->send_list);
3933 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003934
3935 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02003936 return total;
3937}
3938
3939/* for debugging with CLI's "show fd" command */
3940static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
3941{
3942 struct fcgi_conn *fconn = conn->ctx;
3943 struct fcgi_strm *fstrm = NULL;
3944 struct eb32_node *node;
3945 int send_cnt = 0;
3946 int tree_cnt = 0;
3947 int orph_cnt = 0;
3948 struct buffer *hmbuf, *tmbuf;
3949
3950 if (!fconn)
3951 return;
3952
3953 list_for_each_entry(fstrm, &fconn->send_list, send_list)
3954 send_cnt++;
3955
3956 fstrm = NULL;
3957 node = eb32_first(&fconn->streams_by_id);
3958 while (node) {
3959 fstrm = container_of(node, struct fcgi_strm, by_id);
3960 tree_cnt++;
3961 if (!fstrm->cs)
3962 orph_cnt++;
3963 node = eb32_next(node);
3964 }
3965
3966 hmbuf = br_head(fconn->mbuf);
3967 tmbuf = br_tail(fconn->mbuf);
3968 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
3969 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
3970 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
3971 fconn->state, fconn->max_id, fconn->flags,
3972 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
3973 fconn->wait_event.events, fconn->dsi,
3974 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
3975 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
3976 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
3977 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
3978 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
3979 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
3980 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
3981
3982 if (fstrm) {
3983 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
3984 fstrm, fstrm->id, fstrm->flags,
3985 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
3986 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
3987 fstrm->cs);
3988 if (fstrm->cs)
3989 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3990 fstrm->cs->flags, fstrm->cs->data);
3991 }
3992}
3993
3994/****************************************/
3995/* MUX initialization and instanciation */
3996/****************************************/
3997
3998/* The mux operations */
3999static const struct mux_ops mux_fcgi_ops = {
4000 .init = fcgi_init,
4001 .wake = fcgi_wake,
4002 .attach = fcgi_attach,
4003 .get_first_cs = fcgi_get_first_cs,
4004 .detach = fcgi_detach,
4005 .destroy = fcgi_destroy,
4006 .avail_streams = fcgi_avail_streams,
4007 .used_streams = fcgi_used_streams,
4008 .rcv_buf = fcgi_rcv_buf,
4009 .snd_buf = fcgi_snd_buf,
4010 .subscribe = fcgi_subscribe,
4011 .unsubscribe = fcgi_unsubscribe,
4012 .shutr = fcgi_shutr,
4013 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004014 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004015 .show_fd = fcgi_show_fd,
4016 .flags = MX_FL_HTX,
4017 .name = "FCGI",
4018};
4019
4020
4021/* this mux registers FCGI proto */
4022static struct mux_proto_list mux_proto_fcgi =
4023{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4024
4025INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4026
4027/*
4028 * Local variables:
4029 * c-indent-level: 8
4030 * c-basic-offset: 8
4031 * End:
4032 */