blob: cef3074a3c287d1651ff0a26b68a26b1a7f3ac44 [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 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100167 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200168 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100169 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to close after we failed to by lack of space */
Christopher Faulet99eff652019-08-11 23:11:30 +0200170};
171
172/* Flags representing all default FCGI parameters */
173#define FCGI_SP_CGI_GATEWAY 0x00000001
174#define FCGI_SP_DOC_ROOT 0x00000002
175#define FCGI_SP_SCRIPT_NAME 0x00000004
176#define FCGI_SP_PATH_INFO 0x00000008
177#define FCGI_SP_REQ_URI 0x00000010
178#define FCGI_SP_REQ_METH 0x00000020
179#define FCGI_SP_REQ_QS 0x00000040
180#define FCGI_SP_SRV_PORT 0x00000080
181#define FCGI_SP_SRV_PROTO 0x00000100
182#define FCGI_SP_SRV_NAME 0x00000200
183#define FCGI_SP_REM_ADDR 0x00000400
184#define FCGI_SP_REM_PORT 0x00000800
185#define FCGI_SP_SCRIPT_FILE 0x00001000
186#define FCGI_SP_PATH_TRANS 0x00002000
187#define FCGI_SP_CONT_LEN 0x00004000
188#define FCGI_SP_HTTPS 0x00008000
189#define FCGI_SP_MASK 0x0000FFFF
190#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
191
192/* FCGI parameters used when PARAMS record is sent */
193struct fcgi_strm_params {
194 uint32_t mask;
195 struct ist docroot;
196 struct ist scriptname;
197 struct ist pathinfo;
198 struct ist meth;
199 struct ist uri;
200 struct ist vsn;
201 struct ist qs;
202 struct ist srv_name;
203 struct ist srv_port;
204 struct ist rem_addr;
205 struct ist rem_port;
206 struct ist cont_len;
207 int https;
208 struct buffer *p;
209};
210
211/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
212#define MAX_DATA_REALIGN 1024
213
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200214/* trace source and events */
215static void fcgi_trace(enum trace_level level, uint64_t mask,
216 const struct trace_source *src,
217 const struct ist where, const struct ist func,
218 const void *a1, const void *a2, const void *a3, const void *a4);
219
220/* The event representation is split like this :
221 * fconn - internal FCGI connection
222 * fstrm - internal FCGI stream
223 * strm - application layer
224 * rx - data receipt
225 * tx - data transmission
226 * rsp - response parsing
227 */
228static const struct trace_event fcgi_trace_events[] = {
229#define FCGI_EV_FCONN_NEW (1ULL << 0)
230 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
231#define FCGI_EV_FCONN_RECV (1ULL << 1)
232 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
233#define FCGI_EV_FCONN_SEND (1ULL << 2)
234 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
235#define FCGI_EV_FCONN_BLK (1ULL << 3)
236 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
237#define FCGI_EV_FCONN_WAKE (1ULL << 4)
238 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
239#define FCGI_EV_FCONN_END (1ULL << 5)
240 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
241#define FCGI_EV_FCONN_ERR (1ULL << 6)
242 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
243
244#define FCGI_EV_RX_FHDR (1ULL << 7)
245 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
246#define FCGI_EV_RX_RECORD (1ULL << 8)
247 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
248#define FCGI_EV_RX_EOI (1ULL << 9)
249 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
250#define FCGI_EV_RX_GETVAL (1ULL << 10)
251 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
252#define FCGI_EV_RX_STDOUT (1ULL << 11)
253 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
254#define FCGI_EV_RX_STDERR (1ULL << 12)
255 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
256#define FCGI_EV_RX_ENDREQ (1ULL << 13)
257 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
258
259#define FCGI_EV_TX_RECORD (1ULL << 14)
260 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
261#define FCGI_EV_TX_EOI (1ULL << 15)
262 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
263#define FCGI_EV_TX_BEGREQ (1ULL << 16)
264 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
265#define FCGI_EV_TX_GETVAL (1ULL << 17)
266 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
267#define FCGI_EV_TX_PARAMS (1ULL << 18)
268 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
269#define FCGI_EV_TX_STDIN (1ULL << 19)
270 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
271#define FCGI_EV_TX_ABORT (1ULL << 20)
272 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
273
274#define FCGI_EV_RSP_DATA (1ULL << 21)
275 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
276#define FCGI_EV_RSP_EOM (1ULL << 22)
277 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
278#define FCGI_EV_RSP_HDRS (1ULL << 23)
279 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
280#define FCGI_EV_RSP_BODY (1ULL << 24)
281 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
282#define FCGI_EV_RSP_TLRS (1ULL << 25)
283 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
284
285#define FCGI_EV_FSTRM_NEW (1ULL << 26)
286 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
287#define FCGI_EV_FSTRM_BLK (1ULL << 27)
288 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
289#define FCGI_EV_FSTRM_END (1ULL << 28)
290 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
291#define FCGI_EV_FSTRM_ERR (1ULL << 29)
292 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
293
294#define FCGI_EV_STRM_NEW (1ULL << 30)
295 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
296#define FCGI_EV_STRM_RECV (1ULL << 31)
297 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
298#define FCGI_EV_STRM_SEND (1ULL << 32)
299 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
300#define FCGI_EV_STRM_FULL (1ULL << 33)
301 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
302#define FCGI_EV_STRM_WAKE (1ULL << 34)
303 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
304#define FCGI_EV_STRM_SHUT (1ULL << 35)
305 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
306#define FCGI_EV_STRM_END (1ULL << 36)
307 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
308#define FCGI_EV_STRM_ERR (1ULL << 37)
309 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
310
311 { }
312};
313
314static const struct name_desc fcgi_trace_lockon_args[4] = {
315 /* arg1 */ { /* already used by the connection */ },
316 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
317 /* arg3 */ { },
318 /* arg4 */ { }
319};
320
321
322static const struct name_desc fcgi_trace_decoding[] = {
323#define FCGI_VERB_CLEAN 1
324 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
325#define FCGI_VERB_MINIMAL 2
326 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
327#define FCGI_VERB_SIMPLE 3
328 { .name="simple", .desc="add request/response status line or htx info when available" },
329#define FCGI_VERB_ADVANCED 4
330 { .name="advanced", .desc="add header fields or record decoding when available" },
331#define FCGI_VERB_COMPLETE 5
332 { .name="complete", .desc="add full data dump when available" },
333 { /* end */ }
334};
335
336static struct trace_source trace_fcgi = {
337 .name = IST("fcgi"),
338 .desc = "FastCGI multiplexer",
339 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
340 .default_cb = fcgi_trace,
341 .known_events = fcgi_trace_events,
342 .lockon_args = fcgi_trace_lockon_args,
343 .decoding = fcgi_trace_decoding,
344 .report_events = ~0, // report everything by default
345};
346
347#define TRACE_SOURCE &trace_fcgi
348INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
349
Christopher Faulet99eff652019-08-11 23:11:30 +0200350/* FCGI connection and stream pools */
351DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
352DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
353
354static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
355static int fcgi_process(struct fcgi_conn *fconn);
356static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
357static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
358static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
359static 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 +0200360static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
361static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200362static void fcgi_strm_alert(struct fcgi_strm *fstrm);
363static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
364
365/* a dmumy management stream */
366static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
367 .cs = NULL,
368 .fconn = NULL,
369 .state = FCGI_SS_CLOSED,
370 .flags = FCGI_SF_NONE,
371 .id = 0,
372};
373
374/* and a dummy idle stream for use with any unknown stream */
375static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
376 .cs = NULL,
377 .fconn = NULL,
378 .state = FCGI_SS_IDLE,
379 .flags = FCGI_SF_NONE,
380 .id = 0,
381};
382
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200383/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
384static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
385{
386 switch (st) {
387 case FCGI_CS_INIT : return "INI";
388 case FCGI_CS_SETTINGS : return "STG";
389 case FCGI_CS_RECORD_H : return "RDH";
390 case FCGI_CS_RECORD_D : return "RDD";
391 case FCGI_CS_RECORD_P : return "RDP";
392 case FCGI_CS_CLOSED : return "CLO";
393 default : return "???";
394 }
395}
396
397/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
398static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
399{
400 switch (st) {
401 case FCGI_SS_IDLE : return "IDL";
402 case FCGI_SS_OPEN : return "OPN";
403 case FCGI_SS_HREM : return "RCL";
404 case FCGI_SS_HLOC : return "HCL";
405 case FCGI_SS_ERROR : return "ERR";
406 case FCGI_SS_CLOSED : return "CLO";
407 default : return "???";
408 }
409}
410
411
412/* the FCGI traces always expect that arg1, if non-null, is of type connection
413 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
414 * and that arg3, if non-null, is a htx for rx/tx headers.
415 */
416static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
417 const struct ist where, const struct ist func,
418 const void *a1, const void *a2, const void *a3, const void *a4)
419{
420 const struct connection *conn = a1;
421 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
422 const struct fcgi_strm *fstrm = a2;
423 const struct htx *htx = a3;
424 const size_t *val = a4;
425
426 if (!fconn)
427 fconn = (fstrm ? fstrm->fconn : NULL);
428
429 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
430 return;
431
432 /* Display the response state if fstrm is defined */
433 if (fstrm)
434 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
435
436 if (src->verbosity == FCGI_VERB_CLEAN)
437 return;
438
439 /* Display the value to the 4th argument (level > STATE) */
440 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100441 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200442
443 /* Display status-line if possible (verbosity > MINIMAL) */
444 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
445 const struct htx_blk *blk = htx_get_head_blk(htx);
446 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
447 enum htx_blk_type type = htx_get_blk_type(blk);
448
449 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
450 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
451 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
452 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
453 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
454 }
455
456 /* Display fconn info and, if defined, fstrm info */
457 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
458 if (fstrm)
459 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
460
461 if (!fstrm || fstrm->id <= 0)
462 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
463 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
464 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
465
466 if (src->verbosity == FCGI_VERB_MINIMAL)
467 return;
468
469 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
470 if (src->level > TRACE_LEVEL_USER) {
471 if (src->verbosity == FCGI_VERB_COMPLETE ||
472 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
473 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
474 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
475 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
476 if (src->verbosity == FCGI_VERB_COMPLETE ||
477 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
478 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
479 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
480
481 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
482 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
483 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
484 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
485 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
486 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
487 }
488
489 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
490 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
491 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
492 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
493 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
494 }
495
496 /* Display htx info if defined (level > USER) */
497 if (src->level > TRACE_LEVEL_USER && htx) {
498 int full = 0;
499
500 /* Full htx info (level > STATE && verbosity > SIMPLE) */
501 if (src->level > TRACE_LEVEL_STATE) {
502 if (src->verbosity == FCGI_VERB_COMPLETE)
503 full = 1;
504 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
505 full = 1;
506 }
507
508 chunk_memcat(&trace_buf, "\n\t", 2);
509 htx_dump(&trace_buf, htx, full);
510 }
511}
Christopher Faulet99eff652019-08-11 23:11:30 +0200512
513/*****************************************************/
514/* functions below are for dynamic buffer management */
515/*****************************************************/
516
517/* Indicates whether or not the we may call the fcgi_recv() function to attempt
518 * to receive data into the buffer and/or demux pending data. The condition is
519 * a bit complex due to some API limits for now. The rules are the following :
520 * - if an error or a shutdown was detected on the connection and the buffer
521 * is empty, we must not attempt to receive
522 * - if the demux buf failed to be allocated, we must not try to receive and
523 * we know there is nothing pending
524 * - if no flag indicates a blocking condition, we may attempt to receive,
525 * regardless of whether the demux buffer is full or not, so that only
526 * de demux part decides whether or not to block. This is needed because
527 * the connection API indeed prevents us from re-enabling receipt that is
528 * already enabled in a polled state, so we must always immediately stop
529 * as soon as the demux can't proceed so as never to hit an end of read
530 * with data pending in the buffers.
531 * - otherwise must may not attempt
532 */
533static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
534{
535 if (b_data(&fconn->dbuf) == 0 &&
536 (fconn->state == FCGI_CS_CLOSED ||
537 fconn->conn->flags & CO_FL_ERROR ||
538 conn_xprt_read0_pending(fconn->conn)))
539 return 0;
540
541 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
542 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
543 return 1;
544
545 return 0;
546}
547
548/* Restarts reading on the connection if it was not enabled */
549static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
550{
551 if (!fcgi_recv_allowed(fconn))
552 return;
553 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
554 (fconn->wait_event.events & SUB_RETRY_RECV))
555 return;
556 tasklet_wakeup(fconn->wait_event.tasklet);
557}
558
559
560/* Tries to grab a buffer and to re-enable processing on mux <target>. The
561 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
562 * the allocation succeeds, in which case the connection is woken up, or 0 if
563 * it's impossible to wake up and we prefer to be woken up later.
564 */
565static int fcgi_buf_available(void *target)
566{
567 struct fcgi_conn *fconn = target;
568 struct fcgi_strm *fstrm;
569
570 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200571 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 +0200572 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
573 fcgi_conn_restart_reading(fconn, 1);
574 return 1;
575 }
576
577 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200578 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 +0200579 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200580 if (fconn->flags & FCGI_CF_DEM_MROOM) {
581 fconn->flags &= ~FCGI_CF_DEM_MROOM;
582 fcgi_conn_restart_reading(fconn, 1);
583 }
584 return 1;
585 }
586
587 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
588 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
589 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200590 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 +0200591 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
592 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200593 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200594 return 1;
595 }
596
597 return 0;
598}
599
600static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
601{
602 struct buffer *buf = NULL;
603
Willy Tarreau21046592020-02-26 10:39:36 +0100604 if (likely(!MT_LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200605 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
606 fconn->buf_wait.target = fconn;
607 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau21046592020-02-26 10:39:36 +0100608 MT_LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200609 }
610 return buf;
611}
612
613static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
614{
615 if (bptr->size) {
616 b_free(bptr);
617 offer_buffers(NULL, tasks_run_queue);
618 }
619}
620
621static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
622{
623 struct buffer *buf;
624 unsigned int count = 0;
625
626 while (b_size(buf = br_head_pick(fconn->mbuf))) {
627 b_free(buf);
628 count++;
629 }
630 if (count)
631 offer_buffers(NULL, tasks_run_queue);
632}
633
634/* Returns the number of allocatable outgoing streams for the connection taking
635 * the number reserved streams into account.
636 */
637static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
638{
639 int ret;
640
641 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
642 if (ret < 0)
643 ret = 0;
644 return ret;
645}
646
647/* Returns the number of streams in use on a connection to figure if it's
648 * idle or not. We check nb_cs and not nb_streams as the caller will want
649 * to know if it was the last one after a detach().
650 */
651static int fcgi_used_streams(struct connection *conn)
652{
653 struct fcgi_conn *fconn = conn->ctx;
654
655 return fconn->nb_cs;
656}
657
658/* Returns the number of concurrent streams available on the connection */
659static int fcgi_avail_streams(struct connection *conn)
660{
661 struct server *srv = objt_server(conn->target);
662 struct fcgi_conn *fconn = conn->ctx;
663 int ret1, ret2;
664
665 /* Don't open new stream if the connection is closed */
666 if (fconn->state == FCGI_CS_CLOSED)
667 return 0;
668
669 /* May be negative if this setting has changed */
670 ret1 = (fconn->streams_limit - fconn->nb_streams);
671
672 /* we must also consider the limit imposed by stream IDs */
673 ret2 = fcgi_streams_left(fconn);
674 ret1 = MIN(ret1, ret2);
675 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
676 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
677 ret1 = MIN(ret1, ret2);
678 }
679 return ret1;
680}
681
682/*****************************************************************/
683/* functions below are dedicated to the mux setup and management */
684/*****************************************************************/
685
686/* Initializes the mux once it's attached. Only outgoing connections are
687 * supported. So the context is already initialized before installing the
688 * mux. <input> is always used as Input buffer and may contain data. It is the
689 * caller responsibility to not reuse it anymore. Returns < 0 on error.
690 */
691static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
692 struct buffer *input)
693{
694 struct fcgi_conn *fconn;
695 struct fcgi_strm *fstrm;
696 struct fcgi_app *app = get_px_fcgi_app(px);
697 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200698 void *conn_ctx = conn->ctx;
699
700 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200701
702 if (!app)
703 goto fail_conn;
704
705 fconn = pool_alloc(pool_head_fcgi_conn);
706 if (!fconn)
707 goto fail_conn;
708
709 fconn->shut_timeout = fconn->timeout = px->timeout.server;
710 if (tick_isset(px->timeout.serverfin))
711 fconn->shut_timeout = px->timeout.serverfin;
712
713 fconn->flags = FCGI_CF_NONE;
714
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500715 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200716 if (app->flags & FCGI_APP_FL_KEEP_CONN)
717 fconn->flags |= FCGI_CF_KEEP_CONN;
718 if (app->flags & FCGI_APP_FL_GET_VALUES)
719 fconn->flags |= FCGI_CF_GET_VALUES;
720 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
721 fconn->flags |= FCGI_CF_MPXS_CONNS;
722
723 fconn->proxy = px;
724 fconn->app = app;
725 fconn->task = NULL;
726 if (tick_isset(fconn->timeout)) {
727 t = task_new(tid_bit);
728 if (!t)
729 goto fail;
730
731 fconn->task = t;
732 t->process = fcgi_timeout_task;
733 t->context = fconn;
734 t->expire = tick_add(now_ms, fconn->timeout);
735 }
736
737 fconn->wait_event.tasklet = tasklet_new();
738 if (!fconn->wait_event.tasklet)
739 goto fail;
740 fconn->wait_event.tasklet->process = fcgi_io_cb;
741 fconn->wait_event.tasklet->context = fconn;
742 fconn->wait_event.events = 0;
743
744 /* Initialise the context. */
745 fconn->state = FCGI_CS_INIT;
746 fconn->conn = conn;
747 fconn->streams_limit = app->maxreqs;
748 fconn->max_id = -1;
749 fconn->nb_streams = 0;
750 fconn->nb_cs = 0;
751 fconn->nb_reserved = 0;
752 fconn->stream_cnt = 0;
753
754 fconn->dbuf = *input;
755 fconn->dsi = -1;
756
757 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
758 fconn->streams_by_id = EB_ROOT;
759 LIST_INIT(&fconn->send_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100760 MT_LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200761
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200762 conn->ctx = fconn;
763
Christopher Faulet99eff652019-08-11 23:11:30 +0200764 if (t)
765 task_queue(t);
766
767 /* FIXME: this is temporary, for outgoing connections we need to
768 * immediately allocate a stream until the code is modified so that the
769 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200770 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200771 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200772 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200773 if (!fstrm)
774 goto fail;
775
Christopher Faulet99eff652019-08-11 23:11:30 +0200776
777 /* Repare to read something */
778 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200779 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 return 0;
781
782 fail:
783 task_destroy(t);
784 if (fconn->wait_event.tasklet)
785 tasklet_free(fconn->wait_event.tasklet);
786 pool_free(pool_head_fcgi_conn, fconn);
787 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200788 conn->ctx = conn_ctx; // restore saved ctx
789 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200790 return -1;
791}
792
793/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
794 * -1 if no more is allocatable.
795 */
796static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
797{
798 int32_t id = (fconn->max_id + 1) | 1;
799
800 if ((id & 0x80000000U))
801 id = -1;
802 return id;
803}
804
805/* Returns the stream associated with id <id> or NULL if not found */
806static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
807{
808 struct eb32_node *node;
809
810 if (id == 0)
811 return (struct fcgi_strm *)fcgi_mgmt_stream;
812
813 if (id > fconn->max_id)
814 return (struct fcgi_strm *)fcgi_unknown_stream;
815
816 node = eb32_lookup(&fconn->streams_by_id, id);
817 if (!node)
818 return (struct fcgi_strm *)fcgi_unknown_stream;
819 return container_of(node, struct fcgi_strm, by_id);
820}
821
822
823/* Release function. This one should be called to free all resources allocated
824 * to the mux.
825 */
826static void fcgi_release(struct fcgi_conn *fconn)
827{
828 struct connection *conn = NULL;;
829
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200830 TRACE_POINT(FCGI_EV_FCONN_END);
831
Christopher Faulet99eff652019-08-11 23:11:30 +0200832 if (fconn) {
833 /* The connection must be attached to this mux to be released */
834 if (fconn->conn && fconn->conn->ctx == fconn)
835 conn = fconn->conn;
836
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200837 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
838
Willy Tarreau21046592020-02-26 10:39:36 +0100839 if (MT_LIST_ADDED(&fconn->buf_wait.list))
840 MT_LIST_DEL(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200841
842 fcgi_release_buf(fconn, &fconn->dbuf);
843 fcgi_release_mbuf(fconn);
844
845 if (fconn->task) {
846 fconn->task->context = NULL;
847 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
848 fconn->task = NULL;
849 }
850 if (fconn->wait_event.tasklet)
851 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200852 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200853 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
854 &fconn->wait_event);
855 }
856
857 if (conn) {
858 conn->mux = NULL;
859 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200860 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200861
862 conn_stop_tracking(conn);
863 conn_full_close(conn);
864 if (conn->destroy_cb)
865 conn->destroy_cb(conn);
866 conn_free(conn);
867 }
868}
869
870
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500871/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200872static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
873{
874 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
875 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
876 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
877 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
878 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
879 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
880 conn_xprt_read0_pending(fconn->conn))))
881 return 1;
882 return 0;
883}
884
885
886/********************************************************/
887/* functions below are for the FCGI protocol processing */
888/********************************************************/
889
Christopher Faulet99eff652019-08-11 23:11:30 +0200890/* Marks an error on the stream. */
891static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
892{
893 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200894 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
895 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200896 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200897 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
898 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200899 if (fstrm->cs)
900 cs_set_error(fstrm->cs);
901 }
902}
903
904/* Attempts to notify the data layer of recv availability */
905static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
906{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100907 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200908 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100909 tasklet_wakeup(fstrm->subs->tasklet);
910 fstrm->subs->events &= ~SUB_RETRY_RECV;
911 if (!fstrm->subs->events)
912 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200913 }
914}
915
916/* Attempts to notify the data layer of send availability */
917static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
918{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100919 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200920 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100921 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100922 tasklet_wakeup(fstrm->subs->tasklet);
923 fstrm->subs->events &= ~SUB_RETRY_SEND;
924 if (!fstrm->subs->events)
925 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200926 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100927 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
928 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
929 tasklet_wakeup(fstrm->shut_tl);
930 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200931}
932
933/* Alerts the data layer, trying to wake it up by all means, following
934 * this sequence :
935 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
936 * for recv
937 * - if its subscribed to send, then it's woken up for send
938 * - if it was subscribed to neither, its ->wake() callback is called
939 * It is safe to call this function with a closed stream which doesn't have a
940 * conn_stream anymore.
941 */
942static void fcgi_strm_alert(struct fcgi_strm *fstrm)
943{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200944 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100945 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100946 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200947 fcgi_strm_notify_recv(fstrm);
948 fcgi_strm_notify_send(fstrm);
949 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200950 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
951 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200952 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200953 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200954}
955
956/* Writes the 16-bit record size <len> at address <record> */
957static inline void fcgi_set_record_size(void *record, uint16_t len)
958{
959 uint8_t *out = (record + 4);
960
961 *out = (len >> 8);
962 *(out + 1) = (len & 0xff);
963}
964
965/* Writes the 16-bit stream id <id> at address <record> */
966static inline void fcgi_set_record_id(void *record, uint16_t id)
967{
968 uint8_t *out = (record + 2);
969
970 *out = (id >> 8);
971 *(out + 1) = (id & 0xff);
972}
973
974/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
975 * its connection if the stream was not yet closed. Please use this exclusively
976 * before closing a stream to ensure stream count is well maintained.
977 */
978static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
979{
980 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200981 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200982 fstrm->fconn->nb_streams--;
983 if (!fstrm->id)
984 fstrm->fconn->nb_reserved--;
985 if (fstrm->cs) {
986 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
987 fcgi_strm_notify_recv(fstrm);
988 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200989 fstrm->state = FCGI_SS_CLOSED;
990 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
991 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200992 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200993}
994
995/* Detaches a FCGI stream from its FCGI connection and releases it to the
996 * fcgi_strm pool.
997 */
998static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
999{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001000 struct connection *conn = fstrm->fconn->conn;
1001
1002 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1003
Christopher Faulet99eff652019-08-11 23:11:30 +02001004 fcgi_strm_close(fstrm);
1005 eb32_delete(&fstrm->by_id);
1006 if (b_size(&fstrm->rxbuf)) {
1007 b_free(&fstrm->rxbuf);
1008 offer_buffers(NULL, tasks_run_queue);
1009 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001010 if (fstrm->subs)
1011 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001012 /* There's no need to explicitly call unsubscribe here, the only
1013 * reference left would be in the fconn send_list/fctl_list, and if
1014 * we're in it, we're getting out anyway
1015 */
1016 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001017 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001018 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001019
1020 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001021}
1022
1023/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1024 * stream tree. In case of error, nothing is added and NULL is returned. The
1025 * causes of errors can be any failed memory allocation. The caller is
1026 * responsible for checking if the connection may support an extra stream prior
1027 * to calling this function.
1028 */
1029static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1030{
1031 struct fcgi_strm *fstrm;
1032
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001033 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1034
Christopher Faulet99eff652019-08-11 23:11:30 +02001035 fstrm = pool_alloc(pool_head_fcgi_strm);
1036 if (!fstrm)
1037 goto out;
1038
Willy Tarreau7aad7032020-01-16 17:20:57 +01001039 fstrm->shut_tl = tasklet_new();
1040 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001041 pool_free(pool_head_fcgi_strm, fstrm);
1042 goto out;
1043 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001044 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001045 fstrm->shut_tl->process = fcgi_deferred_shut;
1046 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001047 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001048 fstrm->fconn = fconn;
1049 fstrm->cs = NULL;
1050 fstrm->flags = FCGI_SF_NONE;
1051 fstrm->proto_status = 0;
1052 fstrm->state = FCGI_SS_IDLE;
1053 fstrm->rxbuf = BUF_NULL;
1054
1055 h1m_init_res(&fstrm->h1m);
1056 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1057 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1058
1059 fstrm->by_id.key = fstrm->id = id;
1060 if (id > 0)
1061 fconn->max_id = id;
1062 else
1063 fconn->nb_reserved++;
1064
1065 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1066 fconn->nb_streams++;
1067 fconn->stream_cnt++;
1068
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001069 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001070 return fstrm;
1071
1072 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001073 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 +02001074 return NULL;
1075}
1076
1077/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1078 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1079 * highest possible stream ID was reached.
1080 */
1081static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1082 struct session *sess)
1083{
1084 struct fcgi_strm *fstrm = NULL;
1085
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001086 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1087 if (fconn->nb_streams >= fconn->streams_limit) {
1088 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 +02001089 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001090 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001091
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001092 if (fcgi_streams_left(fconn) < 1) {
1093 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 +02001094 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001095 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001096
1097 /* Defer choosing the ID until we send the first message to create the stream */
1098 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001099 if (!fstrm) {
1100 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 +02001101 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001102 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001103
1104 fstrm->cs = cs;
1105 fstrm->sess = sess;
1106 cs->ctx = fstrm;
1107 fconn->nb_cs++;
1108
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001109 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001110 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001111
1112 out:
1113 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001114}
1115
1116/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1117 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1118 * automatically updated accordingly. If the stream is orphaned, it is
1119 * destroyed.
1120 */
1121static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1122{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001123 struct fcgi_conn *fconn = fstrm->fconn;
1124
1125 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1126
Christopher Faulet99eff652019-08-11 23:11:30 +02001127 if (!fstrm->cs) {
1128 /* this stream was already orphaned */
1129 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001130 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001131 return;
1132 }
1133
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001134 if (conn_xprt_read0_pending(fconn->conn)) {
1135 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001136 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001137 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1138 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001139 else if (fstrm->state == FCGI_SS_HLOC)
1140 fcgi_strm_close(fstrm);
1141 }
1142
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001143 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001144 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1145 if (fstrm->cs->flags & CS_FL_EOS)
1146 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001147
1148 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001149 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001150 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1151 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001152 }
1153
1154 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001155
1156 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001157}
1158
1159/* Wakes unassigned streams (ID == 0) attached to the connection. */
1160static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1161{
1162 struct eb32_node *node;
1163 struct fcgi_strm *fstrm;
1164
1165 node = eb32_lookup(&fconn->streams_by_id, 0);
1166 while (node) {
1167 fstrm = container_of(node, struct fcgi_strm, by_id);
1168 if (fstrm->id > 0)
1169 break;
1170 node = eb32_next(node);
1171 fcgi_strm_wake_one_stream(fstrm);
1172 }
1173}
1174
1175/* Wakes the streams attached to the connection, whose id is greater than <last>
1176 * or unassigned.
1177 */
1178static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1179{
1180 struct eb32_node *node;
1181 struct fcgi_strm *fstrm;
1182
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001183 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1184
Christopher Faulet99eff652019-08-11 23:11:30 +02001185 /* Wake all streams with ID > last */
1186 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1187 while (node) {
1188 fstrm = container_of(node, struct fcgi_strm, by_id);
1189 node = eb32_next(node);
1190 fcgi_strm_wake_one_stream(fstrm);
1191 }
1192 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001193
1194 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001195}
1196
1197static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1198 struct htx *htx, struct htx_sl *sl,
1199 struct fcgi_strm_params *params)
1200{
1201 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1202 struct ist p;
1203
1204 if (!sl)
1205 goto error;
1206
1207 if (!(params->mask & FCGI_SP_DOC_ROOT))
1208 params->docroot = fconn->app->docroot;
1209
1210 if (!(params->mask & FCGI_SP_REQ_METH)) {
1211 p = htx_sl_req_meth(sl);
1212 params->meth = ist2(b_tail(params->p), p.len);
1213 chunk_memcat(params->p, p.ptr, p.len);
1214 }
1215 if (!(params->mask & FCGI_SP_REQ_URI)) {
1216 p = htx_sl_req_uri(sl);
1217 params->uri = ist2(b_tail(params->p), p.len);
1218 chunk_memcat(params->p, p.ptr, p.len);
1219 }
1220 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1221 p = htx_sl_req_vsn(sl);
1222 params->vsn = ist2(b_tail(params->p), p.len);
1223 chunk_memcat(params->p, p.ptr, p.len);
1224 }
1225 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1226 char *end;
1227 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001228 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001229 port = get_host_port(cli_conn->dst);
1230 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1231 if (!end)
1232 goto error;
1233 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1234 params->p->data += params->srv_port.len;
1235 }
1236 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1237 /* If no Host header found, use the server address to fill
1238 * srv_name */
1239 if (!istlen(params->srv_name)) {
1240 char *ptr = NULL;
1241
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001242 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001243 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1244 ptr = b_tail(params->p);
1245 if (ptr) {
1246 params->srv_name = ist2(ptr, strlen(ptr));
1247 params->p->data += params->srv_name.len;
1248 }
1249 }
1250 }
1251 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1252 char *ptr = NULL;
1253
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001254 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001255 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1256 ptr = b_tail(params->p);
1257 if (ptr) {
1258 params->rem_addr = ist2(ptr, strlen(ptr));
1259 params->p->data += params->rem_addr.len;
1260 }
1261 }
1262 if (!(params->mask & FCGI_SP_REM_PORT)) {
1263 char *end;
1264 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001265 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001266 port = get_host_port(cli_conn->src);
1267 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1268 if (!end)
1269 goto error;
1270 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1271 params->p->data += params->rem_port.len;
1272 }
1273 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1274 struct htx_blk *blk;
1275 enum htx_blk_type type;
1276 char *end;
1277 size_t len = 0;
1278
1279 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1280 type = htx_get_blk_type(blk);
1281
1282 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1283 break;
1284 if (type == HTX_BLK_DATA)
1285 len += htx_get_blksz(blk);
1286 }
1287 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1288 if (!end)
1289 goto error;
1290 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1291 params->p->data += params->cont_len.len;
1292 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001293#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001294 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001295 if (cli_conn)
1296 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001297 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001298#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001299 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1300 /* one of scriptname, pathinfo or query_string is no set */
1301 struct ist path = http_get_path(params->uri);
1302 int len;
1303
1304 /* Decode the path. it must first be copied to keep the URI
1305 * untouched.
1306 */
1307 chunk_memcat(params->p, path.ptr, path.len);
1308 path.ptr = b_tail(params->p) - path.len;
1309 path.ptr[path.len] = '\0';
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001310 len = url_decode(path.ptr, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001311 if (len < 0)
1312 goto error;
1313 path.len = len;
1314
1315 /* No scrit_name set but no valid path ==> error */
1316 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1317 goto error;
1318
1319 /* Find limit between the path and the query-string */
1320 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1321
1322 /* If there is a query-string, Set it if not already set */
1323 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1324 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1325
1326 /* If the script_name is set, don't try to deduce the path_info
1327 * too. The opposite is not true.
1328 */
1329 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1330 params->mask |= FCGI_SP_PATH_INFO;
1331 goto end;
1332 }
1333
1334 /* script_name not set, preset it with the path for now */
1335 params->scriptname = ist2(path.ptr, len);
1336
1337 /* If there is no regex to match the pathinfo, just to the last
1338 * part and see if the index must be used.
1339 */
1340 if (!fconn->app->pathinfo_re)
1341 goto check_index;
1342
Christopher Faulet28cb3662020-02-14 14:47:37 +01001343 /* If some special characters are found in the decoded path (\n
1344 * or \0), the PATH_INFO regex cannot match. This is theorically
1345 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001346 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001347 * case.
1348 */
1349 if (istchr(path, '\n') || istchr(path, '\0'))
1350 goto error;
1351
Christopher Faulet99eff652019-08-11 23:11:30 +02001352 /* 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
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001358 /* We must have at least 1 capture for the script name,
1359 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001360 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001361 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001362 goto check_index;
1363
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001364 /* Finally we can set the script_name and the path_info. The
1365 * path_info is set if not already defined, and if it was
1366 * captured
1367 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001368 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001369 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1370 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001371
1372 check_index:
1373 len = params->scriptname.len;
1374 /* the script_name if finished by a '/' so we can add the index
1375 * part, if any.
1376 */
1377 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1378 struct ist sn = params->scriptname;
1379
1380 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1381 chunk_memcat(params->p, sn.ptr, sn.len);
1382 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1383 }
1384 }
1385
1386 end:
1387 return 1;
1388 error:
1389 return 0;
1390}
1391
1392static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1393 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1394{
1395 struct fcgi_param p;
1396
1397 if (params->mask & flag)
1398 return 1;
1399
1400 chunk_reset(&trash);
1401
1402 switch (flag) {
1403 case FCGI_SP_CGI_GATEWAY:
1404 p.n = ist("GATEWAY_INTERFACE");
1405 p.v = ist("CGI/1.1");
1406 goto encode;
1407 case FCGI_SP_DOC_ROOT:
1408 p.n = ist("DOCUMENT_ROOT");
1409 p.v = params->docroot;
1410 goto encode;
1411 case FCGI_SP_SCRIPT_NAME:
1412 p.n = ist("SCRIPT_NAME");
1413 p.v = params->scriptname;
1414 goto encode;
1415 case FCGI_SP_PATH_INFO:
1416 p.n = ist("PATH_INFO");
1417 p.v = params->pathinfo;
1418 goto encode;
1419 case FCGI_SP_REQ_URI:
1420 p.n = ist("REQUEST_URI");
1421 p.v = params->uri;
1422 goto encode;
1423 case FCGI_SP_REQ_METH:
1424 p.n = ist("REQUEST_METHOD");
1425 p.v = params->meth;
1426 goto encode;
1427 case FCGI_SP_REQ_QS:
1428 p.n = ist("QUERY_STRING");
1429 p.v = params->qs;
1430 goto encode;
1431 case FCGI_SP_SRV_NAME:
1432 p.n = ist("SERVER_NAME");
1433 p.v = params->srv_name;
1434 goto encode;
1435 case FCGI_SP_SRV_PORT:
1436 p.n = ist("SERVER_PORT");
1437 p.v = params->srv_port;
1438 goto encode;
1439 case FCGI_SP_SRV_PROTO:
1440 p.n = ist("SERVER_PROTOCOL");
1441 p.v = params->vsn;
1442 goto encode;
1443 case FCGI_SP_REM_ADDR:
1444 p.n = ist("REMOTE_ADDR");
1445 p.v = params->rem_addr;
1446 goto encode;
1447 case FCGI_SP_REM_PORT:
1448 p.n = ist("REMOTE_PORT");
1449 p.v = params->rem_port;
1450 goto encode;
1451 case FCGI_SP_SCRIPT_FILE:
1452 p.n = ist("SCRIPT_FILENAME");
1453 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1454 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1455 p.v = ist2(b_head(&trash), b_data(&trash));
1456 goto encode;
1457 case FCGI_SP_PATH_TRANS:
1458 if (!istlen(params->pathinfo))
1459 goto skip;
1460 p.n = ist("PATH_TRANSLATED");
1461 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1462 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1463 p.v = ist2(b_head(&trash), b_data(&trash));
1464 goto encode;
1465 case FCGI_SP_CONT_LEN:
1466 p.n = ist("CONTENT_LENGTH");
1467 p.v = params->cont_len;
1468 goto encode;
1469 case FCGI_SP_HTTPS:
1470 if (!params->https)
1471 goto skip;
1472 p.n = ist("HTTPS");
1473 p.v = ist("on");
1474 goto encode;
1475 default:
1476 goto skip;
1477 }
1478
1479 encode:
1480 if (!istlen(p.v))
1481 goto skip;
1482 if (!fcgi_encode_param(outbuf, &p))
1483 return 0;
1484 skip:
1485 params->mask |= flag;
1486 return 1;
1487}
1488
1489/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1490 * anything. It is highly unexpected, but if the record is larger than a buffer
1491 * and cannot be encoded in one time, an error is triggered and the connection is
1492 * closed. GET_VALUES record cannot be split.
1493 */
1494static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1495{
1496 struct buffer outbuf;
1497 struct buffer *mbuf;
1498 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1499 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001500 int ret = 0;
1501
1502 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001503
1504 mbuf = br_tail(fconn->mbuf);
1505 retry:
1506 if (!fcgi_get_buf(fconn, mbuf)) {
1507 fconn->flags |= FCGI_CF_MUX_MALLOC;
1508 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001509 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1510 ret = 0;
1511 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001512 }
1513
1514 while (1) {
1515 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1516 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1517 break;
1518 realign_again:
1519 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1520 }
1521
1522 if (outbuf.size < 8)
1523 goto full;
1524
1525 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1526 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1527 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1528 outbuf.data = 8;
1529
1530 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1531 * handled by HAProxy.
1532 */
1533 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1534 goto full;
1535
1536 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001537 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 +02001538 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1539 b_add(mbuf, outbuf.data);
1540 ret = 1;
1541
1542 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001543 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001544 return ret;
1545 full:
1546 /* Too large to be encoded. For GET_VALUES records, it is an error */
1547 if (!b_data(mbuf))
1548 goto fail;
1549
1550 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1551 goto retry;
1552 fconn->flags |= FCGI_CF_MUX_MFULL;
1553 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001554 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001555 ret = 0;
1556 goto end;
1557 fail:
1558 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001559 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1560 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1561 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001562}
1563
1564/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1565 * couldn't do anything. It is highly unexpected, but if the record is larger
1566 * than a buffer and cannot be decoded in one time, an error is triggered and
1567 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1568 */
1569static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1570{
1571 struct buffer inbuf;
1572 struct buffer *dbuf;
1573 size_t offset;
1574
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001575 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1576
Christopher Faulet99eff652019-08-11 23:11:30 +02001577 dbuf = &fconn->dbuf;
1578
1579 /* Record too large to be fully decoded */
1580 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1581 goto fail;
1582
1583 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001584 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1585 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001586 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001587 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001588
1589 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1590 /* Realign the dmux buffer if the record wraps. It is unexpected
1591 * at this stage because it should be the first record received
1592 * from the FCGI application.
1593 */
1594 b_slow_realign(dbuf, trash.area, 0);
1595 }
1596
1597 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1598
1599 for (offset = 0; offset < b_data(&inbuf); ) {
1600 struct fcgi_param p;
1601 size_t ret;
1602
1603 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1604 if (!ret) {
1605 /* name or value too large to be decoded at once */
1606 goto fail;
1607 }
1608 offset += ret;
1609
1610 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001611 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001612 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 +02001613 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001614 }
1615 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001616 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001617 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001618 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001619 }
1620 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1621 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001622 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 +02001623 }
1624 /*
1625 * Ignore all other params
1626 */
1627 }
1628
1629 /* Reset the number of concurrent streams supported if the FCGI
1630 * application does not support connection multiplexing
1631 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001632 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001633 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001634 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1635 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001636
1637 /* We must be sure to have read exactly the announced record length, no
1638 * more no less
1639 */
1640 if (offset != fconn->drl)
1641 goto fail;
1642
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001643 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 +02001644 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1645 fconn->drl = 0;
1646 fconn->drp = 0;
1647 fconn->state = FCGI_CS_RECORD_H;
1648 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001649 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1650 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001651 return 1;
1652 fail:
1653 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001654 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1655 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 +02001656 return 0;
1657}
1658
1659/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1660 * excluded, as the streams which already received the end-of-stream. It returns
1661 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1662 */
1663static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1664{
1665 struct eb32_node *node;
1666 struct fcgi_strm *fstrm;
1667
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001668 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1669
Christopher Faulet99eff652019-08-11 23:11:30 +02001670 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1671 while (node) {
1672 fstrm = container_of(node, struct fcgi_strm, by_id);
1673 node = eb32_next(node);
1674 if (fstrm->state != FCGI_SS_CLOSED &&
1675 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1676 !fcgi_strm_send_abort(fconn, fstrm))
1677 return 0;
1678 }
1679 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001680 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1681 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001682 return 1;
1683}
1684
1685/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1686 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1687 * space to proceed. It is small enough to be encoded in an empty buffer.
1688 */
1689static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1690{
1691 struct buffer outbuf;
1692 struct buffer *mbuf;
1693 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1694 int ret;
1695
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001696 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1697
Christopher Faulet99eff652019-08-11 23:11:30 +02001698 mbuf = br_tail(fconn->mbuf);
1699 retry:
1700 if (!fcgi_get_buf(fconn, mbuf)) {
1701 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001702 fstrm->flags |= FCGI_SF_BLK_MROOM;
1703 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1704 ret = 0;
1705 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001706 }
1707
1708 while (1) {
1709 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1710 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1711 break;
1712 realign_again:
1713 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1714 }
1715
1716 if (outbuf.size < 8)
1717 goto full;
1718
1719 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1720 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1721 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1722 fcgi_set_record_id(outbuf.area, fstrm->id);
1723 outbuf.data = 8;
1724
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001725 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1726 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001727 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001728 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001729 if (!fcgi_encode_begin_request(&outbuf, &rec))
1730 goto full;
1731
1732 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001733 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 +02001734 b_add(mbuf, outbuf.data);
1735 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1736 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001737 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001738 ret = 1;
1739
1740 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001741 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001742 return ret;
1743 full:
1744 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1745 goto retry;
1746 fconn->flags |= FCGI_CF_MUX_MFULL;
1747 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001748 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 +02001749 ret = 0;
1750 goto end;
1751}
1752
1753/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1754 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1755 * space to proceed. It is small enough to be encoded in an empty buffer.
1756 */
1757static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1758 enum fcgi_record_type rtype)
1759{
1760 struct buffer outbuf;
1761 struct buffer *mbuf;
1762 int ret;
1763
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001764 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001765 mbuf = br_tail(fconn->mbuf);
1766 retry:
1767 if (!fcgi_get_buf(fconn, mbuf)) {
1768 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001769 fstrm->flags |= FCGI_SF_BLK_MROOM;
1770 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1771 ret = 0;
1772 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001773 }
1774
1775 while (1) {
1776 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1777 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1778 break;
1779 realign_again:
1780 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1781 }
1782
1783 if (outbuf.size < 8)
1784 goto full;
1785
1786 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1787 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1788 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1789 outbuf.area[1] = rtype;
1790 fcgi_set_record_id(outbuf.area, fstrm->id);
1791 outbuf.data = 8;
1792
1793 /* commit the record */
1794 b_add(mbuf, outbuf.data);
1795 ret = 1;
1796
1797 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001798 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001799 return ret;
1800 full:
1801 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1802 goto retry;
1803 fconn->flags |= FCGI_CF_MUX_MFULL;
1804 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001805 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 +02001806 ret = 0;
1807 goto end;
1808}
1809
1810
1811/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1812 * marks the end of params.
1813 */
1814static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1815{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001816 int ret;
1817
1818 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1819 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1820 if (ret)
1821 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1822 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001823}
1824
1825/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1826 * marks the end of input. On success, all the request was successfully sent.
1827 */
1828static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1829{
1830 int ret;
1831
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001832 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001833 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001834 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001835 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001836 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1837 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1838 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1839 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001840 return ret;
1841}
1842
1843/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1844 * stops the request processing.
1845 */
1846static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1847{
1848 int ret;
1849
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001850 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001851 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001852 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001853 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001854 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1855 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1856 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1857 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001858 return ret;
1859}
1860
1861/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1862 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1863 * several records are sent. However, a K/V param cannot be split between 2
1864 * records.
1865 */
1866static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1867 struct htx *htx)
1868{
1869 struct buffer outbuf;
1870 struct buffer *mbuf;
1871 struct htx_blk *blk;
1872 struct htx_sl *sl = NULL;
1873 struct fcgi_strm_params params;
1874 size_t total = 0;
1875
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001876 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1877
Christopher Faulet99eff652019-08-11 23:11:30 +02001878 memset(&params, 0, sizeof(params));
1879 params.p = get_trash_chunk();
1880
1881 mbuf = br_tail(fconn->mbuf);
1882 retry:
1883 if (!fcgi_get_buf(fconn, mbuf)) {
1884 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001885 fstrm->flags |= FCGI_SF_BLK_MROOM;
1886 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1887 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001888 }
1889
1890 while (1) {
1891 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1892 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1893 break;
1894 realign_again:
1895 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1896 }
1897
1898 if (outbuf.size < 8)
1899 goto full;
1900
1901 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1902 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1903 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1904 fcgi_set_record_id(outbuf.area, fstrm->id);
1905 outbuf.data = 8;
1906
1907 blk = htx_get_head_blk(htx);
1908 while (blk) {
1909 enum htx_blk_type type;
1910 uint32_t size = htx_get_blksz(blk);
1911 struct fcgi_param p;
1912
1913 type = htx_get_blk_type(blk);
1914 switch (type) {
1915 case HTX_BLK_REQ_SL:
1916 sl = htx_get_blk_ptr(htx, blk);
1917 if (sl->info.req.meth == HTTP_METH_HEAD)
1918 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1919 if (sl->flags & HTX_SL_F_VER_11)
1920 fstrm->h1m.flags |= H1_MF_VER_11;
1921 break;
1922
1923 case HTX_BLK_HDR:
1924 p.n = htx_get_blk_name(htx, blk);
1925 p.v = htx_get_blk_value(htx, blk);
1926
1927 if (istmatch(p.n, ist(":fcgi-"))) {
1928 p.n.ptr += 6;
1929 p.n.len -= 6;
1930 if (isteq(p.n, ist("gateway_interface")))
1931 params.mask |= FCGI_SP_CGI_GATEWAY;
1932 else if (isteq(p.n, ist("document_root"))) {
1933 params.mask |= FCGI_SP_DOC_ROOT;
1934 params.docroot = p.v;
1935 }
1936 else if (isteq(p.n, ist("script_name"))) {
1937 params.mask |= FCGI_SP_SCRIPT_NAME;
1938 params.scriptname = p.v;
1939 }
1940 else if (isteq(p.n, ist("path_info"))) {
1941 params.mask |= FCGI_SP_PATH_INFO;
1942 params.pathinfo = p.v;
1943 }
1944 else if (isteq(p.n, ist("request_uri"))) {
1945 params.mask |= FCGI_SP_REQ_URI;
1946 params.uri = p.v;
1947 }
1948 else if (isteq(p.n, ist("request_meth")))
1949 params.mask |= FCGI_SP_REQ_METH;
1950 else if (isteq(p.n, ist("query_string")))
1951 params.mask |= FCGI_SP_REQ_QS;
1952 else if (isteq(p.n, ist("server_name")))
1953 params.mask |= FCGI_SP_SRV_NAME;
1954 else if (isteq(p.n, ist("server_port")))
1955 params.mask |= FCGI_SP_SRV_PORT;
1956 else if (isteq(p.n, ist("server_protocol")))
1957 params.mask |= FCGI_SP_SRV_PROTO;
1958 else if (isteq(p.n, ist("remote_addr")))
1959 params.mask |= FCGI_SP_REM_ADDR;
1960 else if (isteq(p.n, ist("remote_port")))
1961 params.mask |= FCGI_SP_REM_PORT;
1962 else if (isteq(p.n, ist("script_filename")))
1963 params.mask |= FCGI_SP_SCRIPT_FILE;
1964 else if (isteq(p.n, ist("path_translated")))
1965 params.mask |= FCGI_SP_PATH_TRANS;
1966 else if (isteq(p.n, ist("https")))
1967 params.mask |= FCGI_SP_HTTPS;
1968 }
1969 else if (isteq(p.n, ist("content-length"))) {
1970 p.n = ist("CONTENT_LENGTH");
1971 params.mask |= FCGI_SP_CONT_LEN;
1972 }
1973 else if (isteq(p.n, ist("content-type")))
1974 p.n = ist("CONTENT_TYPE");
1975 else {
1976 if (isteq(p.n, ist("host")))
1977 params.srv_name = p.v;
1978
Christopher Faulet67d58092019-10-02 10:51:38 +02001979 /* Skip header if same name is used to add the server name */
1980 if (fconn->proxy->server_id_hdr_name &&
1981 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1982 break;
1983
Christopher Faulet99eff652019-08-11 23:11:30 +02001984 memcpy(trash.area, "http_", 5);
1985 memcpy(trash.area+5, p.n.ptr, p.n.len);
1986 p.n = ist2(trash.area, p.n.len+5);
1987 }
1988
1989 if (!fcgi_encode_param(&outbuf, &p)) {
1990 if (b_space_wraps(mbuf))
1991 goto realign_again;
1992 if (outbuf.data == 8)
1993 goto full;
1994 goto done;
1995 }
1996 break;
1997
1998 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001999 if (fconn->proxy->server_id_hdr_name) {
2000 struct server *srv = objt_server(fconn->conn->target);
2001
2002 if (!srv)
2003 goto done;
2004
2005 memcpy(trash.area, "http_", 5);
2006 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2007 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2008 p.v = ist(srv->id);
2009
2010 if (!fcgi_encode_param(&outbuf, &p)) {
2011 if (b_space_wraps(mbuf))
2012 goto realign_again;
2013 if (outbuf.data == 8)
2014 goto full;
2015 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002016 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002017 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002018 goto done;
2019
2020 default:
2021 break;
2022 }
2023 total += size;
2024 blk = htx_remove_blk(htx, blk);
2025 }
2026
2027 done:
2028 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2029 goto error;
2030
2031 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2032 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2033 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2034 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2035 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2047 goto error;
2048
2049 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002050 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 +02002051 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2052 b_add(mbuf, outbuf.data);
2053
2054 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002055 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002056 return total;
2057 full:
2058 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2059 goto retry;
2060 fconn->flags |= FCGI_CF_MUX_MFULL;
2061 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002062 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 +02002063 if (total)
2064 goto error;
2065 goto end;
2066
2067 error:
2068 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002069 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002070 fcgi_strm_error(fstrm);
2071 goto end;
2072}
2073
2074/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2075 * anything. STDIN records contain the request body.
2076 */
2077static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2078 struct htx *htx, size_t count, struct buffer *buf)
2079{
2080 struct buffer outbuf;
2081 struct buffer *mbuf;
2082 struct htx_blk *blk;
2083 enum htx_blk_type type;
2084 uint32_t size;
2085 size_t total = 0;
2086
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002087 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002088 if (!count)
2089 goto end;
2090
2091 mbuf = br_tail(fconn->mbuf);
2092 retry:
2093 if (!fcgi_get_buf(fconn, mbuf)) {
2094 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002095 fstrm->flags |= FCGI_SF_BLK_MROOM;
2096 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2097 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002098 }
2099
2100 /* Perform some optimizations to reduce the number of buffer copies.
2101 * First, if the mux's buffer is empty and the htx area contains exactly
2102 * one data block of the same size as the requested count, and this
2103 * count fits within the record size, then it's possible to simply swap
2104 * the caller's buffer with the mux's output buffer and adjust offsets
2105 * and length to match the entire DATA HTX block in the middle. In this
2106 * case we perform a true zero-copy operation from end-to-end. This is
2107 * the situation that happens all the time with large files. Second, if
2108 * this is not possible, but the mux's output buffer is empty, we still
2109 * have an opportunity to avoid the copy to the intermediary buffer, by
2110 * making the intermediary buffer's area point to the output buffer's
2111 * area. In this case we want to skip the HTX header to make sure that
2112 * copies remain aligned and that this operation remains possible all
2113 * the time. This goes for headers, data blocks and any data extracted
2114 * from the HTX blocks.
2115 */
2116 blk = htx_get_head_blk(htx);
2117 if (!blk)
2118 goto end;
2119 type = htx_get_blk_type(blk);
2120 size = htx_get_blksz(blk);
2121 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2122 void *old_area = mbuf->area;
2123
2124 if (b_data(mbuf)) {
2125 /* Too bad there are data left there. We're willing to memcpy/memmove
2126 * up to 1/4 of the buffer, which means that it's OK to copy a large
2127 * record into a buffer containing few data if it needs to be realigned,
2128 * and that it's also OK to copy few data without realigning. Otherwise
2129 * we'll pretend the mbuf is full and wait for it to become empty.
2130 */
2131 if (size + 8 <= b_room(mbuf) &&
2132 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2133 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2134 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002135 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002136 }
2137
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002138 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 +02002139 /* map a FCGI record to the HTX block so that we can put the
2140 * record header there.
2141 */
2142 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2143 outbuf.area = b_head(mbuf);
2144
2145 /* prepend a FCGI record header just before the DATA block */
2146 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2147 fcgi_set_record_id(outbuf.area, fstrm->id);
2148 fcgi_set_record_size(outbuf.area, size);
2149
2150 /* and exchange with our old area */
2151 buf->area = old_area;
2152 buf->data = buf->head = 0;
2153 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002154
2155 htx = (struct htx *)buf->area;
2156 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002157 goto end;
2158 }
2159
2160 copy:
2161 while (1) {
2162 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2163 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2164 break;
2165 realign_again:
2166 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2167 }
2168
2169 if (outbuf.size < 8)
2170 goto full;
2171
2172 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2173 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2174 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2175 fcgi_set_record_id(outbuf.area, fstrm->id);
2176 outbuf.data = 8;
2177
2178 blk = htx_get_head_blk(htx);
2179 while (blk && count) {
2180 enum htx_blk_type type = htx_get_blk_type(blk);
2181 uint32_t size = htx_get_blksz(blk);
2182 struct ist v;
2183
2184 switch (type) {
2185 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002186 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 +02002187 v = htx_get_blk_value(htx, blk);
2188 if (v.len > count)
2189 v.len = count;
2190
2191 if (v.len > b_room(&outbuf)) {
2192 /* It doesn't fit at once. If it at least fits once split and
2193 * the amount of data to move is low, let's defragment the
2194 * buffer now.
2195 */
2196 if (b_space_wraps(mbuf) &&
2197 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2198 b_data(mbuf) <= MAX_DATA_REALIGN)
2199 goto realign_again;
2200 v.len = b_room(&outbuf);
2201 }
2202 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2203 if (outbuf.data == 8)
2204 goto full;
2205 goto done;
2206 }
2207 if (v.len != size) {
2208 total += v.len;
2209 count -= v.len;
2210 htx_cut_data_blk(htx, blk, v.len);
2211 goto done;
2212 }
2213 break;
2214
2215 case HTX_BLK_EOM:
2216 goto done;
2217
2218 default:
2219 break;
2220 }
2221 total += size;
2222 count -= size;
2223 blk = htx_remove_blk(htx, blk);
2224 }
2225
2226 done:
2227 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002228 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 +02002229 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2230 b_add(mbuf, outbuf.data);
2231
2232 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002234 return total;
2235 full:
2236 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2237 goto retry;
2238 fconn->flags |= FCGI_CF_MUX_MFULL;
2239 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002240 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 +02002241 goto end;
2242}
2243
2244/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2245 * anything. STDOUT records contain the entire response. All the content is
2246 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2247 */
2248static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2249{
2250 struct buffer *dbuf;
2251 size_t ret;
2252 size_t max;
2253
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002254 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2255
Christopher Faulet99eff652019-08-11 23:11:30 +02002256 dbuf = &fconn->dbuf;
2257
2258 /* Only padding remains */
2259 if (fconn->state == FCGI_CS_RECORD_P)
2260 goto end_transfer;
2261
2262 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2263 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2264 buf_room_for_htx_data(dbuf))
2265 goto fail; // incomplete record
2266
2267 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2268 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002269 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2270 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002271 }
2272
2273 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2274 max = buf_room_for_htx_data(&fstrm->rxbuf);
2275 if (!b_data(&fstrm->rxbuf))
2276 fstrm->rxbuf.head = sizeof(struct htx);
2277 if (max > fconn->drl)
2278 max = fconn->drl;
2279
2280 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2281 if (!ret)
2282 goto fail;
2283 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002284 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2285 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 +02002286
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002287 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002288 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002289 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2290 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002291
2292 if (fconn->drl)
2293 goto fail;
2294
2295 end_transfer:
2296 fconn->drl += fconn->drp;
2297 fconn->drp = 0;
2298 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2299 b_del(&fconn->dbuf, ret);
2300 fconn->drl -= ret;
2301 if (fconn->drl)
2302 goto fail;
2303
2304 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002305 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2306 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002307 return 1;
2308 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002309 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 +02002310 return 0;
2311}
2312
2313
2314/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2315 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002316 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002317 */
2318static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2319{
2320 int ret;
2321
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002322 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2323
Christopher Faulet99eff652019-08-11 23:11:30 +02002324 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002325 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002326 fconn->drl += fconn->drp;
2327 fconn->drp = 0;
2328 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2329 b_del(&fconn->dbuf, ret);
2330 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002331 if (fconn->drl) {
2332 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 +02002333 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002334 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002335 fconn->state = FCGI_CS_RECORD_H;
2336 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002337 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2338 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);
2339 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002340 return 1;
2341}
2342
2343/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2344 * anything.
2345 */
2346static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2347{
2348 struct buffer *dbuf;
2349 struct buffer tag;
2350 size_t ret;
2351
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002352 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002353 dbuf = &fconn->dbuf;
2354
2355 /* Only padding remains */
2356 if (fconn->state == FCGI_CS_RECORD_P)
2357 goto end_transfer;
2358
2359 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2360 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2361 buf_room_for_htx_data(dbuf))
2362 goto fail; // incomplete record
2363
2364 chunk_reset(&trash);
2365 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2366 if (!ret)
2367 goto fail;
2368 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002369 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 +02002370
2371 trash.area[ret] = '\n';
2372 trash.area[ret+1] = '\0';
2373 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002374 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002375
2376 if (fconn->drl)
2377 goto fail;
2378
2379 end_transfer:
2380 fconn->drl += fconn->drp;
2381 fconn->drp = 0;
2382 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2383 b_del(&fconn->dbuf, ret);
2384 fconn->drl -= ret;
2385 if (fconn->drl)
2386 goto fail;
2387 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002388 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2389 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002390 return 1;
2391 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 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 +02002393 return 0;
2394}
2395
2396/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2397 * anything. If the empty STDOUT record is not already received, this one marks
2398 * the end of the response. It is highly unexpected, but if the record is larger
2399 * than a buffer and cannot be decoded in one time, an error is triggered and
2400 * the connection is closed. END_REQUEST record cannot be split.
2401 */
2402static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2403{
2404 struct buffer inbuf;
2405 struct buffer *dbuf;
2406 struct fcgi_end_request endreq;
2407
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002408 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002409 dbuf = &fconn->dbuf;
2410
2411 /* Record too large to be fully decoded */
2412 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2413 goto fail;
2414
2415 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002416 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2417 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002418 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002419 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002420
2421 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2422 /* Realign the dmux buffer if the record wraps. It is unexpected
2423 * at this stage because it should be the first record received
2424 * from the FCGI application.
2425 */
2426 b_slow_realign(dbuf, trash.area, 0);
2427 }
2428
2429 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2430
2431 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2432 goto fail;
2433
2434 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002435 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2436 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 +02002437 fstrm->proto_status = endreq.errcode;
2438 fcgi_strm_close(fstrm);
2439
2440 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2441 fconn->drl = 0;
2442 fconn->drp = 0;
2443 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002444 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2445 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002446 return 1;
2447
2448 fail:
2449 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002450 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 +02002451 return 0;
2452}
2453
2454/* process Rx records to be demultiplexed */
2455static void fcgi_process_demux(struct fcgi_conn *fconn)
2456{
2457 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2458 struct fcgi_header hdr;
2459 int ret;
2460
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002461 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2462
Christopher Faulet99eff652019-08-11 23:11:30 +02002463 if (fconn->state == FCGI_CS_CLOSED)
2464 return;
2465
2466 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002467 if (fconn->state == FCGI_CS_INIT) {
2468 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2469 return;
2470 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002471 if (fconn->state == FCGI_CS_SETTINGS) {
2472 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002473 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002474 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2475 if (!ret)
2476 goto fail;
2477 b_del(&fconn->dbuf, ret);
2478
2479 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2480 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 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);
2482 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 +02002483 goto fail;
2484 }
2485 goto new_record;
2486 }
2487 }
2488
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002489 /* process as many incoming records as possible below */
2490 while (1) {
2491 if (!b_data(&fconn->dbuf)) {
2492 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2493 break;
2494 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002495
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002496 if (fconn->state == FCGI_CS_CLOSED) {
2497 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002498 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002499 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002500
2501 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002502 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002503 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2504 if (!ret)
2505 break;
2506 b_del(&fconn->dbuf, ret);
2507
2508 new_record:
2509 fconn->dsi = hdr.id;
2510 fconn->drt = hdr.type;
2511 fconn->drl = hdr.len;
2512 fconn->drp = hdr.padding;
2513 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002514 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 +02002515 }
2516
2517 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2518 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2519
2520 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2521 (b_data(&fstrm->rxbuf) ||
2522 conn_xprt_read0_pending(fconn->conn) ||
2523 fstrm->state == FCGI_SS_CLOSED ||
2524 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2525 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2526 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002527 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 +02002528 fstrm->cs->flags |= CS_FL_RCV_MORE;
2529 fcgi_strm_notify_recv(fstrm);
2530 }
2531 fstrm = tmp_fstrm;
2532
2533 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2534 /* ignore all record for closed streams */
2535 goto ignore_record;
2536 }
2537 if (fstrm->state == FCGI_SS_IDLE) {
2538 /* ignore all record for unknown streams */
2539 goto ignore_record;
2540 }
2541
2542 switch (fconn->drt) {
2543 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002544 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 +02002545 ret = fcgi_conn_handle_values_result(fconn);
2546 break;
2547
2548 case FCGI_STDOUT:
2549 if (fstrm->flags & FCGI_SF_ES_RCVD)
2550 goto ignore_record;
2551
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002552 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002553 if (fconn->drl)
2554 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2555 else
2556 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2557 break;
2558
2559 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002560 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002561 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2562 break;
2563
2564 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002565 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 +02002566 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2567 break;
2568
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002569 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002570 default:
2571 ignore_record:
2572 /* drop records that we ignore. They may be
2573 * larger than the buffer so we drain all of
2574 * their contents until we reach the end.
2575 */
2576 fconn->state = FCGI_CS_RECORD_P;
2577 fconn->drl += fconn->drp;
2578 fconn->drp = 0;
2579 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002580 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2581 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002582 b_del(&fconn->dbuf, ret);
2583 fconn->drl -= ret;
2584 ret = (fconn->drl == 0);
2585 }
2586
2587 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 if (ret <= 0) {
2589 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002590 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002591 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002592
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002593 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002594 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002595 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2596 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002597 }
2598
2599 fail:
2600 /* we can go here on missing data, blocked response or error */
2601 if (fstrm && fstrm->cs &&
2602 (b_data(&fstrm->rxbuf) ||
2603 conn_xprt_read0_pending(fconn->conn) ||
2604 fstrm->state == FCGI_SS_CLOSED ||
2605 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2606 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2607 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002608 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 +02002609 fstrm->cs->flags |= CS_FL_RCV_MORE;
2610 fcgi_strm_notify_recv(fstrm);
2611 }
2612
2613 fcgi_conn_restart_reading(fconn, 0);
2614}
2615
2616/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2617 * the end.
2618 */
2619static int fcgi_process_mux(struct fcgi_conn *fconn)
2620{
2621 struct fcgi_strm *fstrm, *fstrm_back;
2622
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002623 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2624
Christopher Faulet99eff652019-08-11 23:11:30 +02002625 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2626 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2627 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2628 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002629 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 +02002630 fcgi_wake_unassigned_streams(fconn);
2631 goto mux;
2632 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002633 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002634 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2635 goto fail;
2636 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 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 +02002638 }
2639 /* need to wait for the other side */
2640 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002641 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002642 }
2643
2644 mux:
2645 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2646 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2647 break;
2648
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002649 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002650 continue;
2651
Willy Tarreau7aad7032020-01-16 17:20:57 +01002652 /* If the sender changed his mind and unsubscribed, let's just
2653 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002654 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002655 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2656 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002657 LIST_DEL_INIT(&fstrm->send_list);
2658 continue;
2659 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002660
2661 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002662 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2663 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002664 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002665 tasklet_wakeup(fstrm->subs->tasklet);
2666 fstrm->subs->events &= ~SUB_RETRY_SEND;
2667 if (!fstrm->subs->events)
2668 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002669 } else {
2670 /* it's the shut request that was queued */
2671 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2672 tasklet_wakeup(fstrm->shut_tl);
2673 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002674 }
2675
2676 fail:
2677 if (fconn->state == FCGI_CS_CLOSED) {
2678 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2679 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002680 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2681 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002682 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002683 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002684 }
2685 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002686
2687 done:
2688 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002689 return 1;
2690}
2691
2692
2693/* Attempt to read data, and subscribe if none available.
2694 * The function returns 1 if data has been received, otherwise zero.
2695 */
2696static int fcgi_recv(struct fcgi_conn *fconn)
2697{
2698 struct connection *conn = fconn->conn;
2699 struct buffer *buf;
2700 int max;
2701 size_t ret;
2702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002703 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2704
2705 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2706 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002707 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002708 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002709
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002710 if (!fcgi_recv_allowed(fconn)) {
2711 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002712 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002713 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002714
2715 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2716 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002717 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002718 fconn->flags |= FCGI_CF_DEM_DALLOC;
2719 return 0;
2720 }
2721
2722 b_realign_if_empty(buf);
2723 if (!b_data(buf)) {
2724 /* try to pre-align the buffer like the
2725 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002726 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002727 * HTX block to alias it upon recv. We cannot use the
2728 * head because rcv_buf() will realign the buffer if
2729 * it's empty. Thus we cheat and pretend we already
2730 * have a few bytes there.
2731 */
2732 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2733 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2734 }
2735 else
2736 max = buf_room_for_htx_data(buf);
2737
2738 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2739
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002740 if (max && !ret && fcgi_recv_allowed(fconn)) {
2741 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002742 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002743 }
2744 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002745 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002746
2747 if (!b_data(buf)) {
2748 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002750 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2751 }
2752
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002753 if (ret == max) {
2754 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002755 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002756 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002757
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002758 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002759 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2760}
2761
2762
2763/* Try to send data if possible.
2764 * The function returns 1 if data have been sent, otherwise zero.
2765 */
2766static int fcgi_send(struct fcgi_conn *fconn)
2767{
2768 struct connection *conn = fconn->conn;
2769 int done;
2770 int sent = 0;
2771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002772 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2773
2774 if (conn->flags & CO_FL_ERROR) {
2775 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002776 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002778
2779
Willy Tarreau911db9b2020-01-23 16:27:54 +01002780 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002781 /* a handshake was requested */
2782 goto schedule;
2783 }
2784
2785 /* This loop is quite simple : it tries to fill as much as it can from
2786 * pending streams into the existing buffer until it's reportedly full
2787 * or the end of send requests is reached. Then it tries to send this
2788 * buffer's contents out, marks it not full if at least one byte could
2789 * be sent, and tries again.
2790 *
2791 * The snd_buf() function normally takes a "flags" argument which may
2792 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2793 * data immediately comes and CO_SFL_STREAMER to indicate that the
2794 * connection is streaming lots of data (used to increase TLS record
2795 * size at the expense of latency). The former can be sent any time
2796 * there's a buffer full flag, as it indicates at least one stream
2797 * attempted to send and failed so there are pending data. An
2798 * alternative would be to set it as long as there's an active stream
2799 * but that would be problematic for ACKs until we have an absolute
2800 * guarantee that all waiters have at least one byte to send. The
2801 * latter should possibly not be set for now.
2802 */
2803
2804 done = 0;
2805 while (!done) {
2806 unsigned int flags = 0;
2807 unsigned int released = 0;
2808 struct buffer *buf;
2809
2810 /* fill as much as we can into the current buffer */
2811 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2812 done = fcgi_process_mux(fconn);
2813
2814 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2815 done = 1; // we won't go further without extra buffers
2816
2817 if (conn->flags & CO_FL_ERROR)
2818 break;
2819
2820 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2821 flags |= CO_SFL_MSG_MORE;
2822
2823 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2824 if (b_data(buf)) {
2825 int ret;
2826
2827 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2828 if (!ret) {
2829 done = 1;
2830 break;
2831 }
2832 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002833 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002834 b_del(buf, ret);
2835 if (b_data(buf)) {
2836 done = 1;
2837 break;
2838 }
2839 }
2840 b_free(buf);
2841 released++;
2842 }
2843
2844 if (released)
2845 offer_buffers(NULL, tasks_run_queue);
2846
2847 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002848 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2849 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002850 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2851 }
2852
2853 if (conn->flags & CO_FL_SOCK_WR_SH) {
2854 /* output closed, nothing to send, clear the buffer to release it */
2855 b_reset(br_tail(fconn->mbuf));
2856 }
2857 /* We're not full anymore, so we can wake any task that are waiting
2858 * for us.
2859 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002860 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002861 struct fcgi_strm *fstrm;
2862
2863 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2864 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2865 break;
2866
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002867 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002868 continue;
2869
Willy Tarreau7aad7032020-01-16 17:20:57 +01002870 /* If the sender changed his mind and unsubscribed, let's just
2871 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002872 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002873 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2874 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002875 LIST_DEL_INIT(&fstrm->send_list);
2876 continue;
2877 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002878
2879 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002880 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002881 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002882 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002883 tasklet_wakeup(fstrm->subs->tasklet);
2884 fstrm->subs->events &= ~SUB_RETRY_SEND;
2885 if (!fstrm->subs->events)
2886 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002887 } else {
2888 /* it's the shut request that was queued */
2889 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2890 tasklet_wakeup(fstrm->shut_tl);
2891 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002892 }
2893 }
2894 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002895 if (!br_data(fconn->mbuf)) {
2896 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002897 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002898 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002899schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002900 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2901 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002902 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002903 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002904
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002905 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002906 return sent;
2907}
2908
2909/* this is the tasklet referenced in fconn->wait_event.tasklet */
2910static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2911{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002912 struct connection *conn;
2913 struct fcgi_conn *fconn;
2914 struct tasklet *tl = (struct tasklet *)t;
2915 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002916 int ret = 0;
2917
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002918
2919 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
2920 if (tl->context == NULL) {
2921 /* The connection has been taken over by another thread,
2922 * we're no longer responsible for it, so just free the
2923 * tasklet, and do nothing.
2924 */
2925 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
2926 tasklet_free(tl);
2927 return NULL;
2928
2929 }
2930 fconn = ctx;
2931 conn = fconn->conn;
2932
2933 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2934
2935 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2936 if (conn_in_list)
2937 MT_LIST_DEL(&conn->list);
2938
2939 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002940
Christopher Faulet99eff652019-08-11 23:11:30 +02002941 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2942 ret = fcgi_send(fconn);
2943 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2944 ret |= fcgi_recv(fconn);
2945 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002946 ret = fcgi_process(fconn);
2947
2948 /* If we were in an idle list, we want to add it back into it,
2949 * unless fcgi_process() returned -1, which mean it has destroyed
2950 * the connection (testing !ret is enough, if fcgi_process() wasn't
2951 * called then ret will be 0 anyway.
2952 */
2953 if (!ret && conn_in_list) {
2954 struct server *srv = objt_server(conn->target);
2955
2956 if (conn_in_list == CO_FL_SAFE_LIST)
2957 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
2958 else
2959 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
2960 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002961 return NULL;
2962}
2963
2964/* callback called on any event by the connection handler.
2965 * It applies changes and returns zero, or < 0 if it wants immediate
2966 * destruction of the connection (which normally doesn not happen in FCGI).
2967 */
2968static int fcgi_process(struct fcgi_conn *fconn)
2969{
2970 struct connection *conn = fconn->conn;
2971
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002972 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2973
Christopher Faulet99eff652019-08-11 23:11:30 +02002974 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2975 fcgi_process_demux(fconn);
2976
2977 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2978 b_reset(&fconn->dbuf);
2979
2980 if (buf_room_for_htx_data(&fconn->dbuf))
2981 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2982 }
2983 fcgi_send(fconn);
2984
2985 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2986 /* frontend is stopping, reload likely in progress, let's try
2987 * to announce a graceful shutdown if not yet done. We don't
2988 * care if it fails, it will be tried again later.
2989 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002990 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 +02002991 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2992 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2993 fcgi_conn_send_aborts(fconn);
2994 }
2995 }
2996
2997 /*
2998 * If we received early data, and the handshake is done, wake
2999 * any stream that was waiting for it.
3000 */
3001 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003002 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003003 struct eb32_node *node;
3004 struct fcgi_strm *fstrm;
3005
3006 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3007 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3008
3009 while (node) {
3010 fstrm = container_of(node, struct fcgi_strm, by_id);
3011 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3012 fcgi_strm_notify_recv(fstrm);
3013 node = eb32_next(node);
3014 }
3015 }
3016
3017 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
3018 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3019 eb_is_empty(&fconn->streams_by_id)) {
3020 fcgi_wake_some_streams(fconn, 0);
3021
3022 if (eb_is_empty(&fconn->streams_by_id)) {
3023 /* no more stream, kill the connection now */
3024 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003025 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003026 return -1;
3027 }
3028 }
3029
3030 if (!b_data(&fconn->dbuf))
3031 fcgi_release_buf(fconn, &fconn->dbuf);
3032
3033 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3034 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3035 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3036 fcgi_release_mbuf(fconn);
3037
3038 if (fconn->task) {
3039 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3040 task_queue(fconn->task);
3041 }
3042
3043 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003044 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003045 return 0;
3046}
3047
3048
3049/* wake-up function called by the connection layer (mux_ops.wake) */
3050static int fcgi_wake(struct connection *conn)
3051{
3052 struct fcgi_conn *fconn = conn->ctx;
3053
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003054 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003055 return (fcgi_process(fconn));
3056}
3057
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003058
3059static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3060{
3061 int ret = 0;
3062 switch (mux_ctl) {
3063 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003064 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003065 ret |= MUX_STATUS_READY;
3066 return ret;
3067 default:
3068 return -1;
3069 }
3070}
3071
Christopher Faulet99eff652019-08-11 23:11:30 +02003072/* Connection timeout management. The principle is that if there's no receipt
3073 * nor sending for a certain amount of time, the connection is closed. If the
3074 * MUX buffer still has lying data or is not allocatable, the connection is
3075 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003076 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003077 */
3078static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3079{
3080 struct fcgi_conn *fconn = context;
3081 int expired = tick_is_expired(t->expire, now_ms);
3082
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003083 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3084
3085 if (!expired && fconn) {
3086 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003087 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003088 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003089
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003090 /* We're about to destroy the connection, so make sure nobody attempts
3091 * to steal it from us.
3092 */
3093 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3094
3095 if (fconn && fconn->conn->flags & CO_FL_LIST_MASK)
3096 MT_LIST_DEL(&fconn->conn->list);
3097
3098 /* Somebody already stole the connection from us, so we should not
3099 * free it, we just have to free the task.
3100 */
3101 if (!t->context)
3102 fconn = NULL;
3103
3104 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3105
Christopher Faulet99eff652019-08-11 23:11:30 +02003106 task_destroy(t);
3107
3108 if (!fconn) {
3109 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003110 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003111 return NULL;
3112 }
3113
3114 fconn->task = NULL;
3115 fconn->state = FCGI_CS_CLOSED;
3116 fcgi_wake_some_streams(fconn, 0);
3117
3118 if (br_data(fconn->mbuf)) {
3119 /* don't even try to send aborts, the buffer is stuck */
3120 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3121 goto end;
3122 }
3123
3124 /* try to send but no need to insist */
3125 if (!fcgi_conn_send_aborts(fconn))
3126 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3127
3128 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3129 conn_xprt_ready(fconn->conn)) {
3130 unsigned int released = 0;
3131 struct buffer *buf;
3132
3133 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3134 if (b_data(buf)) {
3135 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3136 buf, b_data(buf), 0);
3137 if (!ret)
3138 break;
3139 b_del(buf, ret);
3140 if (b_data(buf))
3141 break;
3142 b_free(buf);
3143 released++;
3144 }
3145 }
3146
3147 if (released)
3148 offer_buffers(NULL, tasks_run_queue);
3149 }
3150
3151 end:
3152 /* either we can release everything now or it will be done later once
3153 * the last stream closes.
3154 */
3155 if (eb_is_empty(&fconn->streams_by_id))
3156 fcgi_release(fconn);
3157
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003158 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003159 return NULL;
3160}
3161
3162
3163/*******************************************/
3164/* functions below are used by the streams */
3165/*******************************************/
3166
3167/* Append the description of what is present in error snapshot <es> into <out>.
3168 * The description must be small enough to always fit in a buffer. The output
3169 * buffer may be the trash so the trash must not be used inside this function.
3170 */
3171static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3172{
3173 chunk_appendf(out,
3174 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3175 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3176 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3177 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3178 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3179 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3180}
3181/*
3182 * Capture a bad response and archive it in the proxy's structure. By default
3183 * it tries to report the error position as h1m->err_pos. However if this one is
3184 * not set, it will then report h1m->next, which is the last known parsing
3185 * point. The function is able to deal with wrapping buffers. It always displays
3186 * buffers as a contiguous area starting at buf->p. The direction is determined
3187 * thanks to the h1m's flags.
3188 */
3189static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3190 struct h1m *h1m, struct buffer *buf)
3191{
3192 struct session *sess = fstrm->sess;
3193 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003194 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003195 union error_snapshot_ctx ctx;
3196
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003197 if (fstrm->cs && fstrm->cs->data) {
3198 if (sess == NULL)
3199 sess = si_strm(fstrm->cs->data)->sess;
3200 if (!(h1m->flags & H1_MF_RESP))
3201 other_end = si_strm(fstrm->cs->data)->be;
3202 else
3203 other_end = sess->fe;
3204 } else
3205 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003206 /* http-specific part now */
3207 ctx.h1.state = h1m->state;
3208 ctx.h1.c_flags = fconn->flags;
3209 ctx.h1.s_flags = fstrm->flags;
3210 ctx.h1.m_flags = h1m->flags;
3211 ctx.h1.m_clen = h1m->curr_len;
3212 ctx.h1.m_blen = h1m->body_len;
3213
3214 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3215 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3216 &ctx, fcgi_show_error_snapshot);
3217}
3218
3219static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3220 struct buffer *buf, size_t *ofs, size_t max)
3221{
3222 int ret;
3223
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003224 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003225 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3226 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003227 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 +02003228 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003229 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 +02003230 fcgi_strm_error(fstrm);
3231 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3232 }
3233 goto end;
3234 }
3235
3236 *ofs += ret;
3237 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003238 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003239 return ret;
3240
3241}
3242
Christopher Fauletaf542632019-10-01 21:52:49 +02003243static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003244 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3245{
3246 int ret;
3247
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003248 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003249 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003250 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003251 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 +02003252 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003253 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 +02003254 fcgi_strm_error(fstrm);
3255 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3256 }
3257 goto end;
3258 }
3259 *ofs += ret;
3260 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003261 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003262 return ret;
3263}
3264
3265static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3266 struct buffer *buf, size_t *ofs, size_t max)
3267{
3268 int ret;
3269
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003270 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003271 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003272 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003273 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 +02003274 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003275 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 +02003276 fcgi_strm_error(fstrm);
3277 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3278 }
3279 goto end;
3280 }
3281 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003282 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003283 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003284 return ret;
3285}
3286
3287static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003288 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003289{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003290 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003291
Willy Tarreaubf5b4912020-04-23 17:24:59 +02003292 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003293 ret = h1_parse_msg_eom(h1m, htx, max);
3294 if (!ret) {
3295 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3296 if (htx->flags & HTX_FL_PARSING_ERROR) {
3297 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3298 fcgi_strm_error(fstrm);
3299 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3300 }
3301 goto end;
3302 }
3303 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3304 end:
3305 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3306 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003307}
3308
3309static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3310{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003311 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003312 struct htx *htx;
3313 struct h1m *h1m = &fstrm->h1m;
3314 size_t ret, data, total = 0;
3315
3316 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003317 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3318
Christopher Faulet99eff652019-08-11 23:11:30 +02003319 data = htx->data;
3320 if (fstrm->state == FCGI_SS_ERROR)
3321 goto end;
3322
3323 do {
3324 size_t used = htx_used_space(htx);
3325
3326 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003327 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003328 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3329 if (!ret)
3330 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003331
3332 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3333
Christopher Faulet99eff652019-08-11 23:11:30 +02003334 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3335 struct htx_blk *blk = htx_get_head_blk(htx);
3336 struct htx_sl *sl;
3337
3338 if (!blk)
3339 break;
3340 sl = htx_get_blk_ptr(htx, blk);
3341 sl->flags |= HTX_SL_F_XFER_LEN;
3342 htx->extra = 0;
3343 }
3344 }
3345 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003346 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003347 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003348 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003349 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003350
3351 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 +02003352 }
3353 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003354 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3355 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3356 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003357 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003358
Christopher Faulet76014fd2019-12-10 11:47:22 +01003359 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 +02003360 }
3361 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003362 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3363 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3364 break;
3365
3366 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3367 }
3368
Christopher Faulet99eff652019-08-11 23:11:30 +02003369 if (b_data(&fstrm->rxbuf) > total) {
3370 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003371 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003372 fcgi_strm_error(fstrm);
3373 }
3374 break;
3375 }
3376 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003377 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003378 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003379
Christopher Faulet99eff652019-08-11 23:11:30 +02003380 if (fstrm->state != FCGI_SS_ERROR &&
3381 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003382 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003383 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3384 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3385 h1m->state = H1_MSG_DONE;
3386 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 +02003387 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003388 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003389 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003390
3391 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003392 }
3393 else {
3394 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003395 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003396 fcgi_strm_error(fstrm);
3397 break;
3398 }
3399
3400 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003401 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003402
3403 if (fstrm->state == FCGI_SS_ERROR) {
3404 b_reset(&fstrm->rxbuf);
3405 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003406 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003407 return 0;
3408 }
3409
3410 b_del(&fstrm->rxbuf, total);
3411
3412 end:
3413 htx_to_buf(htx, buf);
3414 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003415 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003416 return ret;
3417}
3418
3419/*
3420 * Attach a new stream to a connection
3421 * (Used for outgoing connections)
3422 */
3423static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3424{
3425 struct conn_stream *cs;
3426 struct fcgi_strm *fstrm;
3427 struct fcgi_conn *fconn = conn->ctx;
3428
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003429 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003430 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003431 if (!cs) {
3432 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003433 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003434 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003435 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3436 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003437 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003438 cs_free(cs);
3439 return NULL;
3440 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003441 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003442 return cs;
3443}
3444
3445/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3446 * We have to scan because we may have some orphan streams. It might be
3447 * beneficial to scan backwards from the end to reduce the likeliness to find
3448 * orphans.
3449 */
3450static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3451{
3452 struct fcgi_conn *fconn = conn->ctx;
3453 struct fcgi_strm *fstrm;
3454 struct eb32_node *node;
3455
3456 node = eb32_first(&fconn->streams_by_id);
3457 while (node) {
3458 fstrm = container_of(node, struct fcgi_strm, by_id);
3459 if (fstrm->cs)
3460 return fstrm->cs;
3461 node = eb32_next(node);
3462 }
3463 return NULL;
3464}
3465
3466/*
3467 * Destroy the mux and the associated connection, if it is no longer used
3468 */
3469static void fcgi_destroy(void *ctx)
3470{
3471 struct fcgi_conn *fconn = ctx;
3472
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003473 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003474 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3475 fcgi_release(fconn);
3476}
3477
3478/*
3479 * Detach the stream from the connection and possibly release the connection.
3480 */
3481static void fcgi_detach(struct conn_stream *cs)
3482{
3483 struct fcgi_strm *fstrm = cs->ctx;
3484 struct fcgi_conn *fconn;
3485 struct session *sess;
3486
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003487 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3488
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003490 if (!fstrm) {
3491 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003492 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003493 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003494
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003495 /* there's no txbuf so we're certain no to be able to send anything */
3496 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003497
3498 sess = fstrm->sess;
3499 fconn = fstrm->fconn;
3500 fstrm->cs = NULL;
3501 fconn->nb_cs--;
3502
3503 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3504 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3505 fconn->streams_limit = 1;
3506 }
3507 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3508 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3509 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3510 fconn->state = FCGI_CS_CLOSED;
3511 }
3512
3513 /* this stream may be blocked waiting for some data to leave, so orphan
3514 * it in this case.
3515 */
3516 if (!(cs->conn->flags & CO_FL_ERROR) &&
3517 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003518 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003519 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003520 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003521 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003522 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003523
3524 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3525 /* unblock the connection if it was blocked on this stream. */
3526 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3527 fcgi_conn_restart_reading(fconn, 1);
3528 }
3529
3530 fcgi_strm_destroy(fstrm);
3531
3532 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3533 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003534 /* Never ever allow to reuse a connection from a non-reuse backend */
3535 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3536 fconn->conn->flags |= CO_FL_PRIVATE;
3537 if (!fconn->conn->owner && (fconn->conn->flags & CO_FL_PRIVATE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003538 fconn->conn->owner = sess;
3539 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3540 fconn->conn->owner = NULL;
3541 if (eb_is_empty(&fconn->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003542 /* let's kill the connection right away */
3543 fconn->conn->mux->destroy(fconn);
3544 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003545 }
3546 }
3547 }
3548 if (eb_is_empty(&fconn->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003549 if (sess && fconn->conn->owner == sess &&
3550 session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003551 /* The connection is destroyed, let's leave */
3552 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003553 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003554 }
Olivier Houchard2444aa52020-01-20 13:56:01 +01003555 if (!(fconn->conn->flags & CO_FL_PRIVATE)) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003556 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003557 /* The server doesn't want it, let's kill the connection right away */
3558 fconn->conn->mux->destroy(fconn);
3559 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3560 return;
3561 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003562 /* At this point, the connection has been added to the
3563 * server idle list, so another thread may already have
3564 * hijacked it, so we can't do anything with it.
3565 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003566 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3567 return;
3568 }
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003569 } else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003570 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003571 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003572 }
3573
Christopher Faulet99eff652019-08-11 23:11:30 +02003574 }
3575
3576 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003577 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003578 */
3579 if (fcgi_conn_is_dead(fconn)) {
3580 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003581 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003582 fcgi_release(fconn);
3583 }
3584 else if (fconn->task) {
3585 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3586 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003587 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003588 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003589 else
3590 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003591}
3592
3593
3594/* Performs a synchronous or asynchronous shutr(). */
3595static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3596{
3597 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003598
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003599 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3600
Christopher Faulet99eff652019-08-11 23:11:30 +02003601 if (fstrm->state == FCGI_SS_CLOSED)
3602 goto done;
3603
3604 /* a connstream may require us to immediately kill the whole connection
3605 * for example because of a "tcp-request content reject" rule that is
3606 * normally used to limit abuse.
3607 */
3608 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003609 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3610 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003611 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003612 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003613 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003614 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003615 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3616 !fcgi_strm_send_abort(fconn, fstrm))
3617 goto add_to_list;
3618 }
3619
3620 fcgi_strm_close(fstrm);
3621
3622 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3623 tasklet_wakeup(fconn->wait_event.tasklet);
3624 done:
3625 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003626 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 return;
3628
3629 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003630 /* Let the handler know we want to shutr, and add ourselves to the
3631 * send list if not yet done. fcgi_deferred_shut() will be
3632 * automatically called via the shut_tl tasklet when there's room
3633 * again.
3634 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003636 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3638 }
3639 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003640 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003641 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003642 return;
3643}
3644
3645/* Performs a synchronous or asynchronous shutw(). */
3646static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3647{
3648 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003649
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003650 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3651
Christopher Faulet99eff652019-08-11 23:11:30 +02003652 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3653 goto done;
3654
3655 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3656 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3657 !fcgi_strm_send_abort(fconn, fstrm))
3658 goto add_to_list;
3659
3660 if (fstrm->state == FCGI_SS_HREM)
3661 fcgi_strm_close(fstrm);
3662 else
3663 fstrm->state = FCGI_SS_HLOC;
3664 } else {
3665 /* a connstream may require us to immediately kill the whole connection
3666 * for example because of a "tcp-request content reject" rule that is
3667 * normally used to limit abuse.
3668 */
3669 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003670 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3671 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003672 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003673 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003674
3675 fcgi_strm_close(fstrm);
3676 }
3677
3678 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3679 tasklet_wakeup(fconn->wait_event.tasklet);
3680 done:
3681 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003682 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003683 return;
3684
3685 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003686 /* Let the handler know we want to shutr, and add ourselves to the
3687 * send list if not yet done. fcgi_deferred_shut() will be
3688 * automatically called via the shut_tl tasklet when there's room
3689 * again.
3690 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003691 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003692 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3694 }
3695 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003696 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003697 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003698 return;
3699}
3700
Willy Tarreau7aad7032020-01-16 17:20:57 +01003701/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003702 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003703 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003704 */
3705static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3706{
3707 struct fcgi_strm *fstrm = ctx;
3708 struct fcgi_conn *fconn = fstrm->fconn;
3709
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003710 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3711
Willy Tarreau7aad7032020-01-16 17:20:57 +01003712 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3713 /* some data processing remains to be done first */
3714 goto end;
3715 }
3716
Christopher Faulet99eff652019-08-11 23:11:30 +02003717 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3718 fcgi_do_shutw(fstrm);
3719
3720 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3721 fcgi_do_shutr(fstrm);
3722
3723 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3724 /* We're done trying to send, remove ourself from the send_list */
3725 LIST_DEL_INIT(&fstrm->send_list);
3726
3727 if (!fstrm->cs) {
3728 fcgi_strm_destroy(fstrm);
3729 if (fcgi_conn_is_dead(fconn))
3730 fcgi_release(fconn);
3731 }
3732 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003733 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003734 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003735 return NULL;
3736}
3737
3738/* shutr() called by the conn_stream (mux_ops.shutr) */
3739static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3740{
3741 struct fcgi_strm *fstrm = cs->ctx;
3742
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003743 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003744 if (cs->flags & CS_FL_KILL_CONN)
3745 fstrm->flags |= FCGI_SF_KILL_CONN;
3746
3747 if (!mode)
3748 return;
3749
3750 fcgi_do_shutr(fstrm);
3751}
3752
3753/* shutw() called by the conn_stream (mux_ops.shutw) */
3754static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3755{
3756 struct fcgi_strm *fstrm = cs->ctx;
3757
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003758 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003759 if (cs->flags & CS_FL_KILL_CONN)
3760 fstrm->flags |= FCGI_SF_KILL_CONN;
3761
3762 fcgi_do_shutw(fstrm);
3763}
3764
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003765/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3766 * event subscriber <es> is not allowed to change from a previous call as long
3767 * as at least one event is still subscribed. The <event_type> must only be a
3768 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003769 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003770static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003771{
Christopher Faulet99eff652019-08-11 23:11:30 +02003772 struct fcgi_strm *fstrm = cs->ctx;
3773 struct fcgi_conn *fconn = fstrm->fconn;
3774
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003775 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003776 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003777
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003778 es->events |= event_type;
3779 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003780
3781 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003782 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003783
Christopher Faulet99eff652019-08-11 23:11:30 +02003784 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003785 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003786 if (!LIST_ADDED(&fstrm->send_list))
3787 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003789 return 0;
3790}
3791
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003792/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3793 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3794 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003795 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003796static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003797{
Christopher Faulet99eff652019-08-11 23:11:30 +02003798 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003799 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003800
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003801 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003802 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003803
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003804 es->events &= ~event_type;
3805 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003806 fstrm->subs = NULL;
3807
3808 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003809 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003810
Christopher Faulet99eff652019-08-11 23:11:30 +02003811 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003812 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003813 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003814 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3815 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003816 }
3817 return 0;
3818}
3819
3820/* Called from the upper layer, to receive data */
3821static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3822{
3823 struct fcgi_strm *fstrm = cs->ctx;
3824 struct fcgi_conn *fconn = fstrm->fconn;
3825 size_t ret = 0;
3826
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003827 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3828
Christopher Faulet99eff652019-08-11 23:11:30 +02003829 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3830 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003831 else
3832 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003833
Christopher Faulet76014fd2019-12-10 11:47:22 +01003834 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 +02003835 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3836 else {
3837 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003838 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003839 cs->flags |= CS_FL_EOI;
3840 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3841 cs->flags |= CS_FL_EOS;
3842 }
3843 if (conn_xprt_read0_pending(fconn->conn))
3844 cs->flags |= CS_FL_EOS;
3845 if (cs->flags & CS_FL_ERR_PENDING)
3846 cs->flags |= CS_FL_ERROR;
3847 fcgi_release_buf(fconn, &fstrm->rxbuf);
3848 }
3849
3850 if (ret && fconn->dsi == fstrm->id) {
3851 /* demux is blocking on this stream's buffer */
3852 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3853 fcgi_conn_restart_reading(fconn, 1);
3854 }
3855
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003856 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003857 return ret;
3858}
3859
3860
Christopher Faulet99eff652019-08-11 23:11:30 +02003861/* Called from the upper layer, to send data from buffer <buf> for no more than
3862 * <count> bytes. Returns the number of bytes effectively sent. Some status
3863 * flags may be updated on the conn_stream.
3864 */
3865static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3866{
3867 struct fcgi_strm *fstrm = cs->ctx;
3868 struct fcgi_conn *fconn = fstrm->fconn;
3869 size_t total = 0;
3870 size_t ret;
3871 struct htx *htx = NULL;
3872 struct htx_sl *sl;
3873 struct htx_blk *blk;
3874 uint32_t bsize;
3875
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003876 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3877
Christopher Faulet99eff652019-08-11 23:11:30 +02003878 /* If we were not just woken because we wanted to send but couldn't,
3879 * and there's somebody else that is waiting to send, do nothing,
3880 * we will subscribe later and be put at the end of the list
3881 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003882 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003883 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 +02003884 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003885 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003886 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003887
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003888 if (fconn->state < FCGI_CS_RECORD_H) {
3889 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003890 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003891 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003892
3893 htx = htxbuf(buf);
3894 if (fstrm->id == 0) {
3895 int32_t id = fcgi_conn_get_next_sid(fconn);
3896
3897 if (id < 0) {
3898 fcgi_strm_close(fstrm);
3899 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003900 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 +02003901 return 0;
3902 }
3903
3904 eb32_delete(&fstrm->by_id);
3905 fstrm->by_id.key = fstrm->id = id;
3906 fconn->max_id = id;
3907 fconn->nb_reserved--;
3908 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3909
3910
3911 /* Check if length of the body is known or if the message is
3912 * full. Otherwise, the request is invalid.
3913 */
3914 sl = http_get_stline(htx);
3915 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3916 htx->flags |= HTX_FL_PARSING_ERROR;
3917 fcgi_strm_error(fstrm);
3918 goto done;
3919 }
3920 }
3921
3922 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003923 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 +02003924 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3925 goto done;
3926 }
3927
3928 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3929 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3930
3931 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3932 count && !htx_is_empty(htx)) {
3933 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003934 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003935 bsize = htx_get_blksz(blk);
3936
3937 switch (htx_get_blk_type(blk)) {
3938 case HTX_BLK_REQ_SL:
3939 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003940 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 +02003941 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3942 if (!ret) {
3943 goto done;
3944 }
3945 total += ret;
3946 count -= ret;
3947 break;
3948
3949 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003950 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 +02003951 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3952 if (!ret)
3953 goto done;
3954 goto remove_blk;
3955
3956 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003957 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 +02003958 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3959 if (ret > 0) {
3960 htx = htx_from_buf(buf);
3961 total += ret;
3962 count -= ret;
3963 if (ret < bsize)
3964 goto done;
3965 }
3966 break;
3967
3968 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003969 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 +02003970 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3971 if (!ret)
3972 goto done;
3973 goto remove_blk;
3974
3975 default:
3976 remove_blk:
3977 htx_remove_blk(htx, blk);
3978 total += bsize;
3979 count -= bsize;
3980 break;
3981 }
3982 }
3983
3984 done:
3985 if (fstrm->state >= FCGI_SS_HLOC) {
3986 /* trim any possibly pending data after we close (extra CR-LF,
3987 * unprocessed trailers, abnormal extra data, ...)
3988 */
3989 total += count;
3990 count = 0;
3991 }
3992
3993 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003994 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 +02003995 cs_set_error(cs);
3996 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3997 fcgi_strm_close(fstrm);
3998 }
3999
4000 if (htx)
4001 htx_to_buf(htx, buf);
4002
Christopher Faulet99eff652019-08-11 23:11:30 +02004003 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004004 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4005 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 +02004006 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004007 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004008
4009 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004010 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4011 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004012 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004013
4014 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004015 return total;
4016}
4017
4018/* for debugging with CLI's "show fd" command */
4019static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
4020{
4021 struct fcgi_conn *fconn = conn->ctx;
4022 struct fcgi_strm *fstrm = NULL;
4023 struct eb32_node *node;
4024 int send_cnt = 0;
4025 int tree_cnt = 0;
4026 int orph_cnt = 0;
4027 struct buffer *hmbuf, *tmbuf;
4028
4029 if (!fconn)
4030 return;
4031
4032 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4033 send_cnt++;
4034
4035 fstrm = NULL;
4036 node = eb32_first(&fconn->streams_by_id);
4037 while (node) {
4038 fstrm = container_of(node, struct fcgi_strm, by_id);
4039 tree_cnt++;
4040 if (!fstrm->cs)
4041 orph_cnt++;
4042 node = eb32_next(node);
4043 }
4044
4045 hmbuf = br_head(fconn->mbuf);
4046 tmbuf = br_tail(fconn->mbuf);
4047 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4048 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4049 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4050 fconn->state, fconn->max_id, fconn->flags,
4051 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4052 fconn->wait_event.events, fconn->dsi,
4053 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4054 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4055 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4056 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4057 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4058 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4059 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4060
4061 if (fstrm) {
4062 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4063 fstrm, fstrm->id, fstrm->flags,
4064 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4065 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4066 fstrm->cs);
4067 if (fstrm->cs)
4068 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4069 fstrm->cs->flags, fstrm->cs->data);
4070 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004071}
4072
4073/* Migrate the the connection to the current thread.
4074 * Return 0 if successful, non-zero otherwise.
4075 * Expected to be called with the old thread lock held.
4076 */
4077static int fcgi_takeover(struct connection *conn)
4078{
4079 struct fcgi_conn *fcgi = conn->ctx;
4080
4081 if (fd_takeover(conn->handle.fd, conn) != 0)
4082 return -1;
4083 if (fcgi->wait_event.events)
4084 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4085 fcgi->wait_event.events, &fcgi->wait_event);
4086 /* To let the tasklet know it should free itself, and do nothing else,
4087 * set its context to NULL;
4088 */
4089 fcgi->wait_event.tasklet->context = NULL;
4090 tasklet_wakeup(fcgi->wait_event.tasklet);
4091 if (fcgi->task) {
4092 fcgi->task->context = NULL;
4093 /* Wake the task, to let it free itself */
4094 task_wakeup(fcgi->task, TASK_WOKEN_OTHER);
4095
4096 fcgi->task = task_new(tid_bit);
4097 if (!fcgi->task) {
4098 fcgi_release(fcgi);
4099 return -1;
4100 }
4101 fcgi->task->process = fcgi_timeout_task;
4102 fcgi->task->context = fcgi;
4103 }
4104 fcgi->wait_event.tasklet = tasklet_new();
4105 if (!fcgi->wait_event.tasklet) {
4106 fcgi_release(fcgi);
4107 return -1;
4108 }
4109 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4110 fcgi->wait_event.tasklet->context = fcgi;
4111 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4112 SUB_RETRY_RECV, &fcgi->wait_event);
4113
4114 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004115}
4116
4117/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004118/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004119/****************************************/
4120
4121/* The mux operations */
4122static const struct mux_ops mux_fcgi_ops = {
4123 .init = fcgi_init,
4124 .wake = fcgi_wake,
4125 .attach = fcgi_attach,
4126 .get_first_cs = fcgi_get_first_cs,
4127 .detach = fcgi_detach,
4128 .destroy = fcgi_destroy,
4129 .avail_streams = fcgi_avail_streams,
4130 .used_streams = fcgi_used_streams,
4131 .rcv_buf = fcgi_rcv_buf,
4132 .snd_buf = fcgi_snd_buf,
4133 .subscribe = fcgi_subscribe,
4134 .unsubscribe = fcgi_unsubscribe,
4135 .shutr = fcgi_shutr,
4136 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004137 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004138 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004139 .takeover = fcgi_takeover,
Christopher Faulet99eff652019-08-11 23:11:30 +02004140 .flags = MX_FL_HTX,
4141 .name = "FCGI",
4142};
4143
4144
4145/* this mux registers FCGI proto */
4146static struct mux_proto_list mux_proto_fcgi =
4147{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4148
4149INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4150
4151/*
4152 * Local variables:
4153 * c-indent-level: 8
4154 * c-basic-offset: 8
4155 * End:
4156 */