blob: bfe44017ef9b1f252742b8015acc52c16f3bc06e [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
604 if (likely(!LIST_ADDED(&fconn->buf_wait.list)) &&
605 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
606 fconn->buf_wait.target = fconn;
607 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
608 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
609 LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
610 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet99eff652019-08-11 23:11:30 +0200611 }
612 return buf;
613}
614
615static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
616{
617 if (bptr->size) {
618 b_free(bptr);
619 offer_buffers(NULL, tasks_run_queue);
620 }
621}
622
623static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
624{
625 struct buffer *buf;
626 unsigned int count = 0;
627
628 while (b_size(buf = br_head_pick(fconn->mbuf))) {
629 b_free(buf);
630 count++;
631 }
632 if (count)
633 offer_buffers(NULL, tasks_run_queue);
634}
635
636/* Returns the number of allocatable outgoing streams for the connection taking
637 * the number reserved streams into account.
638 */
639static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
640{
641 int ret;
642
643 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
644 if (ret < 0)
645 ret = 0;
646 return ret;
647}
648
649/* Returns the number of streams in use on a connection to figure if it's
650 * idle or not. We check nb_cs and not nb_streams as the caller will want
651 * to know if it was the last one after a detach().
652 */
653static int fcgi_used_streams(struct connection *conn)
654{
655 struct fcgi_conn *fconn = conn->ctx;
656
657 return fconn->nb_cs;
658}
659
660/* Returns the number of concurrent streams available on the connection */
661static int fcgi_avail_streams(struct connection *conn)
662{
663 struct server *srv = objt_server(conn->target);
664 struct fcgi_conn *fconn = conn->ctx;
665 int ret1, ret2;
666
667 /* Don't open new stream if the connection is closed */
668 if (fconn->state == FCGI_CS_CLOSED)
669 return 0;
670
671 /* May be negative if this setting has changed */
672 ret1 = (fconn->streams_limit - fconn->nb_streams);
673
674 /* we must also consider the limit imposed by stream IDs */
675 ret2 = fcgi_streams_left(fconn);
676 ret1 = MIN(ret1, ret2);
677 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
678 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
679 ret1 = MIN(ret1, ret2);
680 }
681 return ret1;
682}
683
684/*****************************************************************/
685/* functions below are dedicated to the mux setup and management */
686/*****************************************************************/
687
688/* Initializes the mux once it's attached. Only outgoing connections are
689 * supported. So the context is already initialized before installing the
690 * mux. <input> is always used as Input buffer and may contain data. It is the
691 * caller responsibility to not reuse it anymore. Returns < 0 on error.
692 */
693static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
694 struct buffer *input)
695{
696 struct fcgi_conn *fconn;
697 struct fcgi_strm *fstrm;
698 struct fcgi_app *app = get_px_fcgi_app(px);
699 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200700 void *conn_ctx = conn->ctx;
701
702 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200703
704 if (!app)
705 goto fail_conn;
706
707 fconn = pool_alloc(pool_head_fcgi_conn);
708 if (!fconn)
709 goto fail_conn;
710
711 fconn->shut_timeout = fconn->timeout = px->timeout.server;
712 if (tick_isset(px->timeout.serverfin))
713 fconn->shut_timeout = px->timeout.serverfin;
714
715 fconn->flags = FCGI_CF_NONE;
716
717 /* Retrieve usefull info from the FCGI app */
718 if (app->flags & FCGI_APP_FL_KEEP_CONN)
719 fconn->flags |= FCGI_CF_KEEP_CONN;
720 if (app->flags & FCGI_APP_FL_GET_VALUES)
721 fconn->flags |= FCGI_CF_GET_VALUES;
722 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
723 fconn->flags |= FCGI_CF_MPXS_CONNS;
724
725 fconn->proxy = px;
726 fconn->app = app;
727 fconn->task = NULL;
728 if (tick_isset(fconn->timeout)) {
729 t = task_new(tid_bit);
730 if (!t)
731 goto fail;
732
733 fconn->task = t;
734 t->process = fcgi_timeout_task;
735 t->context = fconn;
736 t->expire = tick_add(now_ms, fconn->timeout);
737 }
738
739 fconn->wait_event.tasklet = tasklet_new();
740 if (!fconn->wait_event.tasklet)
741 goto fail;
742 fconn->wait_event.tasklet->process = fcgi_io_cb;
743 fconn->wait_event.tasklet->context = fconn;
744 fconn->wait_event.events = 0;
745
746 /* Initialise the context. */
747 fconn->state = FCGI_CS_INIT;
748 fconn->conn = conn;
749 fconn->streams_limit = app->maxreqs;
750 fconn->max_id = -1;
751 fconn->nb_streams = 0;
752 fconn->nb_cs = 0;
753 fconn->nb_reserved = 0;
754 fconn->stream_cnt = 0;
755
756 fconn->dbuf = *input;
757 fconn->dsi = -1;
758
759 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
760 fconn->streams_by_id = EB_ROOT;
761 LIST_INIT(&fconn->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200762 LIST_INIT(&fconn->buf_wait.list);
763
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200764 conn->ctx = fconn;
765
Christopher Faulet99eff652019-08-11 23:11:30 +0200766 if (t)
767 task_queue(t);
768
769 /* FIXME: this is temporary, for outgoing connections we need to
770 * immediately allocate a stream until the code is modified so that the
771 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200772 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200773 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200774 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200775 if (!fstrm)
776 goto fail;
777
Christopher Faulet99eff652019-08-11 23:11:30 +0200778
779 /* Repare to read something */
780 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200781 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200782 return 0;
783
784 fail:
785 task_destroy(t);
786 if (fconn->wait_event.tasklet)
787 tasklet_free(fconn->wait_event.tasklet);
788 pool_free(pool_head_fcgi_conn, fconn);
789 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200790 conn->ctx = conn_ctx; // restore saved ctx
791 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200792 return -1;
793}
794
795/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
796 * -1 if no more is allocatable.
797 */
798static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
799{
800 int32_t id = (fconn->max_id + 1) | 1;
801
802 if ((id & 0x80000000U))
803 id = -1;
804 return id;
805}
806
807/* Returns the stream associated with id <id> or NULL if not found */
808static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
809{
810 struct eb32_node *node;
811
812 if (id == 0)
813 return (struct fcgi_strm *)fcgi_mgmt_stream;
814
815 if (id > fconn->max_id)
816 return (struct fcgi_strm *)fcgi_unknown_stream;
817
818 node = eb32_lookup(&fconn->streams_by_id, id);
819 if (!node)
820 return (struct fcgi_strm *)fcgi_unknown_stream;
821 return container_of(node, struct fcgi_strm, by_id);
822}
823
824
825/* Release function. This one should be called to free all resources allocated
826 * to the mux.
827 */
828static void fcgi_release(struct fcgi_conn *fconn)
829{
830 struct connection *conn = NULL;;
831
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200832 TRACE_POINT(FCGI_EV_FCONN_END);
833
Christopher Faulet99eff652019-08-11 23:11:30 +0200834 if (fconn) {
835 /* The connection must be attached to this mux to be released */
836 if (fconn->conn && fconn->conn->ctx == fconn)
837 conn = fconn->conn;
838
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200839 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
840
Christopher Faulet99eff652019-08-11 23:11:30 +0200841 if (LIST_ADDED(&fconn->buf_wait.list)) {
842 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
843 LIST_DEL(&fconn->buf_wait.list);
844 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
845 }
846
847 fcgi_release_buf(fconn, &fconn->dbuf);
848 fcgi_release_mbuf(fconn);
849
850 if (fconn->task) {
851 fconn->task->context = NULL;
852 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
853 fconn->task = NULL;
854 }
855 if (fconn->wait_event.tasklet)
856 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200857 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200858 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
859 &fconn->wait_event);
860 }
861
862 if (conn) {
863 conn->mux = NULL;
864 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200865 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200866
867 conn_stop_tracking(conn);
868 conn_full_close(conn);
869 if (conn->destroy_cb)
870 conn->destroy_cb(conn);
871 conn_free(conn);
872 }
873}
874
875
876/* Retruns true if the FCGI connection must be release */
877static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
878{
879 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
880 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
881 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
882 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
883 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
884 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
885 conn_xprt_read0_pending(fconn->conn))))
886 return 1;
887 return 0;
888}
889
890
891/********************************************************/
892/* functions below are for the FCGI protocol processing */
893/********************************************************/
894
Christopher Faulet99eff652019-08-11 23:11:30 +0200895/* Marks an error on the stream. */
896static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
897{
898 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200899 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
900 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200901 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200902 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
903 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200904 if (fstrm->cs)
905 cs_set_error(fstrm->cs);
906 }
907}
908
909/* Attempts to notify the data layer of recv availability */
910static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
911{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100912 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200913 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100914 tasklet_wakeup(fstrm->subs->tasklet);
915 fstrm->subs->events &= ~SUB_RETRY_RECV;
916 if (!fstrm->subs->events)
917 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200918 }
919}
920
921/* Attempts to notify the data layer of send availability */
922static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
923{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100924 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200925 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100926 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100927 tasklet_wakeup(fstrm->subs->tasklet);
928 fstrm->subs->events &= ~SUB_RETRY_SEND;
929 if (!fstrm->subs->events)
930 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200931 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100932 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
933 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
934 tasklet_wakeup(fstrm->shut_tl);
935 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200936}
937
938/* Alerts the data layer, trying to wake it up by all means, following
939 * this sequence :
940 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
941 * for recv
942 * - if its subscribed to send, then it's woken up for send
943 * - if it was subscribed to neither, its ->wake() callback is called
944 * It is safe to call this function with a closed stream which doesn't have a
945 * conn_stream anymore.
946 */
947static void fcgi_strm_alert(struct fcgi_strm *fstrm)
948{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200949 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100950 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100951 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200952 fcgi_strm_notify_recv(fstrm);
953 fcgi_strm_notify_send(fstrm);
954 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200955 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
956 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200957 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200958 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200959}
960
961/* Writes the 16-bit record size <len> at address <record> */
962static inline void fcgi_set_record_size(void *record, uint16_t len)
963{
964 uint8_t *out = (record + 4);
965
966 *out = (len >> 8);
967 *(out + 1) = (len & 0xff);
968}
969
970/* Writes the 16-bit stream id <id> at address <record> */
971static inline void fcgi_set_record_id(void *record, uint16_t id)
972{
973 uint8_t *out = (record + 2);
974
975 *out = (id >> 8);
976 *(out + 1) = (id & 0xff);
977}
978
979/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
980 * its connection if the stream was not yet closed. Please use this exclusively
981 * before closing a stream to ensure stream count is well maintained.
982 */
983static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
984{
985 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200986 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200987 fstrm->fconn->nb_streams--;
988 if (!fstrm->id)
989 fstrm->fconn->nb_reserved--;
990 if (fstrm->cs) {
991 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
992 fcgi_strm_notify_recv(fstrm);
993 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200994 fstrm->state = FCGI_SS_CLOSED;
995 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
996 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200997 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200998}
999
1000/* Detaches a FCGI stream from its FCGI connection and releases it to the
1001 * fcgi_strm pool.
1002 */
1003static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1004{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001005 struct connection *conn = fstrm->fconn->conn;
1006
1007 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1008
Christopher Faulet99eff652019-08-11 23:11:30 +02001009 fcgi_strm_close(fstrm);
1010 eb32_delete(&fstrm->by_id);
1011 if (b_size(&fstrm->rxbuf)) {
1012 b_free(&fstrm->rxbuf);
1013 offer_buffers(NULL, tasks_run_queue);
1014 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001015 if (fstrm->subs)
1016 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001017 /* There's no need to explicitly call unsubscribe here, the only
1018 * reference left would be in the fconn send_list/fctl_list, and if
1019 * we're in it, we're getting out anyway
1020 */
1021 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001022 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001023 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001024
1025 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001026}
1027
1028/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1029 * stream tree. In case of error, nothing is added and NULL is returned. The
1030 * causes of errors can be any failed memory allocation. The caller is
1031 * responsible for checking if the connection may support an extra stream prior
1032 * to calling this function.
1033 */
1034static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1035{
1036 struct fcgi_strm *fstrm;
1037
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001038 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1039
Christopher Faulet99eff652019-08-11 23:11:30 +02001040 fstrm = pool_alloc(pool_head_fcgi_strm);
1041 if (!fstrm)
1042 goto out;
1043
Willy Tarreau7aad7032020-01-16 17:20:57 +01001044 fstrm->shut_tl = tasklet_new();
1045 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001046 pool_free(pool_head_fcgi_strm, fstrm);
1047 goto out;
1048 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001049 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001050 fstrm->shut_tl->process = fcgi_deferred_shut;
1051 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001052 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001053 fstrm->fconn = fconn;
1054 fstrm->cs = NULL;
1055 fstrm->flags = FCGI_SF_NONE;
1056 fstrm->proto_status = 0;
1057 fstrm->state = FCGI_SS_IDLE;
1058 fstrm->rxbuf = BUF_NULL;
1059
1060 h1m_init_res(&fstrm->h1m);
1061 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1062 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1063
1064 fstrm->by_id.key = fstrm->id = id;
1065 if (id > 0)
1066 fconn->max_id = id;
1067 else
1068 fconn->nb_reserved++;
1069
1070 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1071 fconn->nb_streams++;
1072 fconn->stream_cnt++;
1073
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001074 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001075 return fstrm;
1076
1077 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001078 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 +02001079 return NULL;
1080}
1081
1082/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1083 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1084 * highest possible stream ID was reached.
1085 */
1086static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1087 struct session *sess)
1088{
1089 struct fcgi_strm *fstrm = NULL;
1090
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001091 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1092 if (fconn->nb_streams >= fconn->streams_limit) {
1093 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 +02001094 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001095 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001096
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001097 if (fcgi_streams_left(fconn) < 1) {
1098 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 +02001099 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001100 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001101
1102 /* Defer choosing the ID until we send the first message to create the stream */
1103 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001104 if (!fstrm) {
1105 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 +02001106 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001107 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001108
1109 fstrm->cs = cs;
1110 fstrm->sess = sess;
1111 cs->ctx = fstrm;
1112 fconn->nb_cs++;
1113
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001114 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001115 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001116
1117 out:
1118 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001119}
1120
1121/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1122 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1123 * automatically updated accordingly. If the stream is orphaned, it is
1124 * destroyed.
1125 */
1126static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1127{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001128 struct fcgi_conn *fconn = fstrm->fconn;
1129
1130 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1131
Christopher Faulet99eff652019-08-11 23:11:30 +02001132 if (!fstrm->cs) {
1133 /* this stream was already orphaned */
1134 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001135 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001136 return;
1137 }
1138
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001139 if (conn_xprt_read0_pending(fconn->conn)) {
1140 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001141 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001142 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1143 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001144 else if (fstrm->state == FCGI_SS_HLOC)
1145 fcgi_strm_close(fstrm);
1146 }
1147
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001148 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001149 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1150 if (fstrm->cs->flags & CS_FL_EOS)
1151 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001152
1153 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001155 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1156 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001157 }
1158
1159 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160
1161 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001162}
1163
1164/* Wakes unassigned streams (ID == 0) attached to the connection. */
1165static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1166{
1167 struct eb32_node *node;
1168 struct fcgi_strm *fstrm;
1169
1170 node = eb32_lookup(&fconn->streams_by_id, 0);
1171 while (node) {
1172 fstrm = container_of(node, struct fcgi_strm, by_id);
1173 if (fstrm->id > 0)
1174 break;
1175 node = eb32_next(node);
1176 fcgi_strm_wake_one_stream(fstrm);
1177 }
1178}
1179
1180/* Wakes the streams attached to the connection, whose id is greater than <last>
1181 * or unassigned.
1182 */
1183static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1184{
1185 struct eb32_node *node;
1186 struct fcgi_strm *fstrm;
1187
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001188 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1189
Christopher Faulet99eff652019-08-11 23:11:30 +02001190 /* Wake all streams with ID > last */
1191 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1192 while (node) {
1193 fstrm = container_of(node, struct fcgi_strm, by_id);
1194 node = eb32_next(node);
1195 fcgi_strm_wake_one_stream(fstrm);
1196 }
1197 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001198
1199 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001200}
1201
1202static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1203 struct htx *htx, struct htx_sl *sl,
1204 struct fcgi_strm_params *params)
1205{
1206 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1207 struct ist p;
1208
1209 if (!sl)
1210 goto error;
1211
1212 if (!(params->mask & FCGI_SP_DOC_ROOT))
1213 params->docroot = fconn->app->docroot;
1214
1215 if (!(params->mask & FCGI_SP_REQ_METH)) {
1216 p = htx_sl_req_meth(sl);
1217 params->meth = ist2(b_tail(params->p), p.len);
1218 chunk_memcat(params->p, p.ptr, p.len);
1219 }
1220 if (!(params->mask & FCGI_SP_REQ_URI)) {
1221 p = htx_sl_req_uri(sl);
1222 params->uri = ist2(b_tail(params->p), p.len);
1223 chunk_memcat(params->p, p.ptr, p.len);
1224 }
1225 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1226 p = htx_sl_req_vsn(sl);
1227 params->vsn = ist2(b_tail(params->p), p.len);
1228 chunk_memcat(params->p, p.ptr, p.len);
1229 }
1230 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1231 char *end;
1232 int port = 0;
1233 if (conn_get_dst(cli_conn))
1234 port = get_host_port(cli_conn->dst);
1235 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1236 if (!end)
1237 goto error;
1238 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1239 params->p->data += params->srv_port.len;
1240 }
1241 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1242 /* If no Host header found, use the server address to fill
1243 * srv_name */
1244 if (!istlen(params->srv_name)) {
1245 char *ptr = NULL;
1246
1247 if (conn_get_dst(cli_conn))
1248 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1249 ptr = b_tail(params->p);
1250 if (ptr) {
1251 params->srv_name = ist2(ptr, strlen(ptr));
1252 params->p->data += params->srv_name.len;
1253 }
1254 }
1255 }
1256 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1257 char *ptr = NULL;
1258
1259 if (conn_get_src(cli_conn))
1260 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1261 ptr = b_tail(params->p);
1262 if (ptr) {
1263 params->rem_addr = ist2(ptr, strlen(ptr));
1264 params->p->data += params->rem_addr.len;
1265 }
1266 }
1267 if (!(params->mask & FCGI_SP_REM_PORT)) {
1268 char *end;
1269 int port = 0;
1270 if (conn_get_src(cli_conn))
1271 port = get_host_port(cli_conn->src);
1272 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1273 if (!end)
1274 goto error;
1275 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1276 params->p->data += params->rem_port.len;
1277 }
1278 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1279 struct htx_blk *blk;
1280 enum htx_blk_type type;
1281 char *end;
1282 size_t len = 0;
1283
1284 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1285 type = htx_get_blk_type(blk);
1286
1287 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1288 break;
1289 if (type == HTX_BLK_DATA)
1290 len += htx_get_blksz(blk);
1291 }
1292 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1293 if (!end)
1294 goto error;
1295 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1296 params->p->data += params->cont_len.len;
1297 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001298#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001299 if (!(params->mask & FCGI_SP_HTTPS)) {
1300 params->https = ssl_sock_is_ssl(cli_conn);
1301 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001302#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001303 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1304 /* one of scriptname, pathinfo or query_string is no set */
1305 struct ist path = http_get_path(params->uri);
1306 int len;
1307
1308 /* Decode the path. it must first be copied to keep the URI
1309 * untouched.
1310 */
1311 chunk_memcat(params->p, path.ptr, path.len);
1312 path.ptr = b_tail(params->p) - path.len;
1313 path.ptr[path.len] = '\0';
1314 len = url_decode(path.ptr);
1315 if (len < 0)
1316 goto error;
1317 path.len = len;
1318
1319 /* No scrit_name set but no valid path ==> error */
1320 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1321 goto error;
1322
1323 /* Find limit between the path and the query-string */
1324 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1325
1326 /* If there is a query-string, Set it if not already set */
1327 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1328 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1329
1330 /* If the script_name is set, don't try to deduce the path_info
1331 * too. The opposite is not true.
1332 */
1333 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1334 params->mask |= FCGI_SP_PATH_INFO;
1335 goto end;
1336 }
1337
1338 /* script_name not set, preset it with the path for now */
1339 params->scriptname = ist2(path.ptr, len);
1340
1341 /* If there is no regex to match the pathinfo, just to the last
1342 * part and see if the index must be used.
1343 */
1344 if (!fconn->app->pathinfo_re)
1345 goto check_index;
1346
Christopher Faulet28cb3662020-02-14 14:47:37 +01001347 /* If some special characters are found in the decoded path (\n
1348 * or \0), the PATH_INFO regex cannot match. This is theorically
1349 * valid, but probably unexpected, to have such characters. So,
1350 * to avoid any suprises, an error is triggered in this
1351 * case.
1352 */
1353 if (istchr(path, '\n') || istchr(path, '\0'))
1354 goto error;
1355
Christopher Faulet99eff652019-08-11 23:11:30 +02001356 /* The regex does not match, just to the last part and see if
1357 * the index must be used.
1358 */
1359 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1360 goto check_index;
1361
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001362 /* We must have at least 1 capture for the script name,
1363 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001364 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001365 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001366 goto check_index;
1367
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001368 /* Finally we can set the script_name and the path_info. The
1369 * path_info is set if not already defined, and if it was
1370 * captured
1371 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001372 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001373 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1374 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001375
1376 check_index:
1377 len = params->scriptname.len;
1378 /* the script_name if finished by a '/' so we can add the index
1379 * part, if any.
1380 */
1381 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1382 struct ist sn = params->scriptname;
1383
1384 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1385 chunk_memcat(params->p, sn.ptr, sn.len);
1386 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1387 }
1388 }
1389
1390 end:
1391 return 1;
1392 error:
1393 return 0;
1394}
1395
1396static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1397 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1398{
1399 struct fcgi_param p;
1400
1401 if (params->mask & flag)
1402 return 1;
1403
1404 chunk_reset(&trash);
1405
1406 switch (flag) {
1407 case FCGI_SP_CGI_GATEWAY:
1408 p.n = ist("GATEWAY_INTERFACE");
1409 p.v = ist("CGI/1.1");
1410 goto encode;
1411 case FCGI_SP_DOC_ROOT:
1412 p.n = ist("DOCUMENT_ROOT");
1413 p.v = params->docroot;
1414 goto encode;
1415 case FCGI_SP_SCRIPT_NAME:
1416 p.n = ist("SCRIPT_NAME");
1417 p.v = params->scriptname;
1418 goto encode;
1419 case FCGI_SP_PATH_INFO:
1420 p.n = ist("PATH_INFO");
1421 p.v = params->pathinfo;
1422 goto encode;
1423 case FCGI_SP_REQ_URI:
1424 p.n = ist("REQUEST_URI");
1425 p.v = params->uri;
1426 goto encode;
1427 case FCGI_SP_REQ_METH:
1428 p.n = ist("REQUEST_METHOD");
1429 p.v = params->meth;
1430 goto encode;
1431 case FCGI_SP_REQ_QS:
1432 p.n = ist("QUERY_STRING");
1433 p.v = params->qs;
1434 goto encode;
1435 case FCGI_SP_SRV_NAME:
1436 p.n = ist("SERVER_NAME");
1437 p.v = params->srv_name;
1438 goto encode;
1439 case FCGI_SP_SRV_PORT:
1440 p.n = ist("SERVER_PORT");
1441 p.v = params->srv_port;
1442 goto encode;
1443 case FCGI_SP_SRV_PROTO:
1444 p.n = ist("SERVER_PROTOCOL");
1445 p.v = params->vsn;
1446 goto encode;
1447 case FCGI_SP_REM_ADDR:
1448 p.n = ist("REMOTE_ADDR");
1449 p.v = params->rem_addr;
1450 goto encode;
1451 case FCGI_SP_REM_PORT:
1452 p.n = ist("REMOTE_PORT");
1453 p.v = params->rem_port;
1454 goto encode;
1455 case FCGI_SP_SCRIPT_FILE:
1456 p.n = ist("SCRIPT_FILENAME");
1457 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1458 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1459 p.v = ist2(b_head(&trash), b_data(&trash));
1460 goto encode;
1461 case FCGI_SP_PATH_TRANS:
1462 if (!istlen(params->pathinfo))
1463 goto skip;
1464 p.n = ist("PATH_TRANSLATED");
1465 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1466 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1467 p.v = ist2(b_head(&trash), b_data(&trash));
1468 goto encode;
1469 case FCGI_SP_CONT_LEN:
1470 p.n = ist("CONTENT_LENGTH");
1471 p.v = params->cont_len;
1472 goto encode;
1473 case FCGI_SP_HTTPS:
1474 if (!params->https)
1475 goto skip;
1476 p.n = ist("HTTPS");
1477 p.v = ist("on");
1478 goto encode;
1479 default:
1480 goto skip;
1481 }
1482
1483 encode:
1484 if (!istlen(p.v))
1485 goto skip;
1486 if (!fcgi_encode_param(outbuf, &p))
1487 return 0;
1488 skip:
1489 params->mask |= flag;
1490 return 1;
1491}
1492
1493/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1494 * anything. It is highly unexpected, but if the record is larger than a buffer
1495 * and cannot be encoded in one time, an error is triggered and the connection is
1496 * closed. GET_VALUES record cannot be split.
1497 */
1498static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1499{
1500 struct buffer outbuf;
1501 struct buffer *mbuf;
1502 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1503 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001504 int ret = 0;
1505
1506 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001507
1508 mbuf = br_tail(fconn->mbuf);
1509 retry:
1510 if (!fcgi_get_buf(fconn, mbuf)) {
1511 fconn->flags |= FCGI_CF_MUX_MALLOC;
1512 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001513 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1514 ret = 0;
1515 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001516 }
1517
1518 while (1) {
1519 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1520 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1521 break;
1522 realign_again:
1523 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1524 }
1525
1526 if (outbuf.size < 8)
1527 goto full;
1528
1529 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1530 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1531 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1532 outbuf.data = 8;
1533
1534 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1535 * handled by HAProxy.
1536 */
1537 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1538 goto full;
1539
1540 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001541 TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn,,, (size_t[]){outbuf.data-8});
Christopher Faulet99eff652019-08-11 23:11:30 +02001542 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1543 b_add(mbuf, outbuf.data);
1544 ret = 1;
1545
1546 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001547 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001548 return ret;
1549 full:
1550 /* Too large to be encoded. For GET_VALUES records, it is an error */
1551 if (!b_data(mbuf))
1552 goto fail;
1553
1554 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1555 goto retry;
1556 fconn->flags |= FCGI_CF_MUX_MFULL;
1557 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001558 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001559 ret = 0;
1560 goto end;
1561 fail:
1562 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001563 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1564 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1565 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001566}
1567
1568/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1569 * couldn't do anything. It is highly unexpected, but if the record is larger
1570 * than a buffer and cannot be decoded in one time, an error is triggered and
1571 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1572 */
1573static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1574{
1575 struct buffer inbuf;
1576 struct buffer *dbuf;
1577 size_t offset;
1578
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001579 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1580
Christopher Faulet99eff652019-08-11 23:11:30 +02001581 dbuf = &fconn->dbuf;
1582
1583 /* Record too large to be fully decoded */
1584 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1585 goto fail;
1586
1587 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001588 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1589 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001590 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001591 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001592
1593 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1594 /* Realign the dmux buffer if the record wraps. It is unexpected
1595 * at this stage because it should be the first record received
1596 * from the FCGI application.
1597 */
1598 b_slow_realign(dbuf, trash.area, 0);
1599 }
1600
1601 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1602
1603 for (offset = 0; offset < b_data(&inbuf); ) {
1604 struct fcgi_param p;
1605 size_t ret;
1606
1607 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1608 if (!ret) {
1609 /* name or value too large to be decoded at once */
1610 goto fail;
1611 }
1612 offset += ret;
1613
1614 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001615 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001616 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001617 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001618 }
1619 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001620 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001621 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001622 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001623 }
1624 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1625 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001626 TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->streams_limit});
Christopher Faulet99eff652019-08-11 23:11:30 +02001627 }
1628 /*
1629 * Ignore all other params
1630 */
1631 }
1632
1633 /* Reset the number of concurrent streams supported if the FCGI
1634 * application does not support connection multiplexing
1635 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001636 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001637 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001638 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1639 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001640
1641 /* We must be sure to have read exactly the announced record length, no
1642 * more no less
1643 */
1644 if (offset != fconn->drl)
1645 goto fail;
1646
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001647 TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02001648 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1649 fconn->drl = 0;
1650 fconn->drp = 0;
1651 fconn->state = FCGI_CS_RECORD_H;
1652 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001653 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1654 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001655 return 1;
1656 fail:
1657 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001658 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1659 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001660 return 0;
1661}
1662
1663/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1664 * excluded, as the streams which already received the end-of-stream. It returns
1665 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1666 */
1667static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1668{
1669 struct eb32_node *node;
1670 struct fcgi_strm *fstrm;
1671
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001672 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1673
Christopher Faulet99eff652019-08-11 23:11:30 +02001674 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1675 while (node) {
1676 fstrm = container_of(node, struct fcgi_strm, by_id);
1677 node = eb32_next(node);
1678 if (fstrm->state != FCGI_SS_CLOSED &&
1679 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1680 !fcgi_strm_send_abort(fconn, fstrm))
1681 return 0;
1682 }
1683 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001684 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1685 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001686 return 1;
1687}
1688
1689/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1690 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1691 * space to proceed. It is small enough to be encoded in an empty buffer.
1692 */
1693static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1694{
1695 struct buffer outbuf;
1696 struct buffer *mbuf;
1697 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1698 int ret;
1699
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001700 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1701
Christopher Faulet99eff652019-08-11 23:11:30 +02001702 mbuf = br_tail(fconn->mbuf);
1703 retry:
1704 if (!fcgi_get_buf(fconn, mbuf)) {
1705 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001706 fstrm->flags |= FCGI_SF_BLK_MROOM;
1707 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1708 ret = 0;
1709 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001710 }
1711
1712 while (1) {
1713 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1714 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1715 break;
1716 realign_again:
1717 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1718 }
1719
1720 if (outbuf.size < 8)
1721 goto full;
1722
1723 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1724 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1725 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1726 fcgi_set_record_id(outbuf.area, fstrm->id);
1727 outbuf.data = 8;
1728
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001729 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1730 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001731 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001732 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 if (!fcgi_encode_begin_request(&outbuf, &rec))
1734 goto full;
1735
1736 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001737 TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001738 b_add(mbuf, outbuf.data);
1739 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1740 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001741 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001742 ret = 1;
1743
1744 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001745 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 return ret;
1747 full:
1748 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1749 goto retry;
1750 fconn->flags |= FCGI_CF_MUX_MFULL;
1751 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001752 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001753 ret = 0;
1754 goto end;
1755}
1756
1757/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1758 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1759 * space to proceed. It is small enough to be encoded in an empty buffer.
1760 */
1761static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1762 enum fcgi_record_type rtype)
1763{
1764 struct buffer outbuf;
1765 struct buffer *mbuf;
1766 int ret;
1767
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001768 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001769 mbuf = br_tail(fconn->mbuf);
1770 retry:
1771 if (!fcgi_get_buf(fconn, mbuf)) {
1772 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001773 fstrm->flags |= FCGI_SF_BLK_MROOM;
1774 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1775 ret = 0;
1776 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001777 }
1778
1779 while (1) {
1780 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1781 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1782 break;
1783 realign_again:
1784 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1785 }
1786
1787 if (outbuf.size < 8)
1788 goto full;
1789
1790 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1791 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1792 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1793 outbuf.area[1] = rtype;
1794 fcgi_set_record_id(outbuf.area, fstrm->id);
1795 outbuf.data = 8;
1796
1797 /* commit the record */
1798 b_add(mbuf, outbuf.data);
1799 ret = 1;
1800
1801 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001802 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001803 return ret;
1804 full:
1805 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1806 goto retry;
1807 fconn->flags |= FCGI_CF_MUX_MFULL;
1808 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001809 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001810 ret = 0;
1811 goto end;
1812}
1813
1814
1815/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1816 * marks the end of params.
1817 */
1818static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1819{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001820 int ret;
1821
1822 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1823 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1824 if (ret)
1825 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1826 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001827}
1828
1829/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1830 * marks the end of input. On success, all the request was successfully sent.
1831 */
1832static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1833{
1834 int ret;
1835
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001836 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001837 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001838 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001839 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001840 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1841 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1842 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1843 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001844 return ret;
1845}
1846
1847/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1848 * stops the request processing.
1849 */
1850static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1851{
1852 int ret;
1853
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001854 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001855 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001856 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001858 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1859 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1860 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1861 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001862 return ret;
1863}
1864
1865/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1866 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1867 * several records are sent. However, a K/V param cannot be split between 2
1868 * records.
1869 */
1870static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1871 struct htx *htx)
1872{
1873 struct buffer outbuf;
1874 struct buffer *mbuf;
1875 struct htx_blk *blk;
1876 struct htx_sl *sl = NULL;
1877 struct fcgi_strm_params params;
1878 size_t total = 0;
1879
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001880 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1881
Christopher Faulet99eff652019-08-11 23:11:30 +02001882 memset(&params, 0, sizeof(params));
1883 params.p = get_trash_chunk();
1884
1885 mbuf = br_tail(fconn->mbuf);
1886 retry:
1887 if (!fcgi_get_buf(fconn, mbuf)) {
1888 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001889 fstrm->flags |= FCGI_SF_BLK_MROOM;
1890 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1891 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001892 }
1893
1894 while (1) {
1895 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1896 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1897 break;
1898 realign_again:
1899 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1900 }
1901
1902 if (outbuf.size < 8)
1903 goto full;
1904
1905 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1906 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1907 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1908 fcgi_set_record_id(outbuf.area, fstrm->id);
1909 outbuf.data = 8;
1910
1911 blk = htx_get_head_blk(htx);
1912 while (blk) {
1913 enum htx_blk_type type;
1914 uint32_t size = htx_get_blksz(blk);
1915 struct fcgi_param p;
1916
1917 type = htx_get_blk_type(blk);
1918 switch (type) {
1919 case HTX_BLK_REQ_SL:
1920 sl = htx_get_blk_ptr(htx, blk);
1921 if (sl->info.req.meth == HTTP_METH_HEAD)
1922 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1923 if (sl->flags & HTX_SL_F_VER_11)
1924 fstrm->h1m.flags |= H1_MF_VER_11;
1925 break;
1926
1927 case HTX_BLK_HDR:
1928 p.n = htx_get_blk_name(htx, blk);
1929 p.v = htx_get_blk_value(htx, blk);
1930
1931 if (istmatch(p.n, ist(":fcgi-"))) {
1932 p.n.ptr += 6;
1933 p.n.len -= 6;
1934 if (isteq(p.n, ist("gateway_interface")))
1935 params.mask |= FCGI_SP_CGI_GATEWAY;
1936 else if (isteq(p.n, ist("document_root"))) {
1937 params.mask |= FCGI_SP_DOC_ROOT;
1938 params.docroot = p.v;
1939 }
1940 else if (isteq(p.n, ist("script_name"))) {
1941 params.mask |= FCGI_SP_SCRIPT_NAME;
1942 params.scriptname = p.v;
1943 }
1944 else if (isteq(p.n, ist("path_info"))) {
1945 params.mask |= FCGI_SP_PATH_INFO;
1946 params.pathinfo = p.v;
1947 }
1948 else if (isteq(p.n, ist("request_uri"))) {
1949 params.mask |= FCGI_SP_REQ_URI;
1950 params.uri = p.v;
1951 }
1952 else if (isteq(p.n, ist("request_meth")))
1953 params.mask |= FCGI_SP_REQ_METH;
1954 else if (isteq(p.n, ist("query_string")))
1955 params.mask |= FCGI_SP_REQ_QS;
1956 else if (isteq(p.n, ist("server_name")))
1957 params.mask |= FCGI_SP_SRV_NAME;
1958 else if (isteq(p.n, ist("server_port")))
1959 params.mask |= FCGI_SP_SRV_PORT;
1960 else if (isteq(p.n, ist("server_protocol")))
1961 params.mask |= FCGI_SP_SRV_PROTO;
1962 else if (isteq(p.n, ist("remote_addr")))
1963 params.mask |= FCGI_SP_REM_ADDR;
1964 else if (isteq(p.n, ist("remote_port")))
1965 params.mask |= FCGI_SP_REM_PORT;
1966 else if (isteq(p.n, ist("script_filename")))
1967 params.mask |= FCGI_SP_SCRIPT_FILE;
1968 else if (isteq(p.n, ist("path_translated")))
1969 params.mask |= FCGI_SP_PATH_TRANS;
1970 else if (isteq(p.n, ist("https")))
1971 params.mask |= FCGI_SP_HTTPS;
1972 }
1973 else if (isteq(p.n, ist("content-length"))) {
1974 p.n = ist("CONTENT_LENGTH");
1975 params.mask |= FCGI_SP_CONT_LEN;
1976 }
1977 else if (isteq(p.n, ist("content-type")))
1978 p.n = ist("CONTENT_TYPE");
1979 else {
1980 if (isteq(p.n, ist("host")))
1981 params.srv_name = p.v;
1982
Christopher Faulet67d58092019-10-02 10:51:38 +02001983 /* Skip header if same name is used to add the server name */
1984 if (fconn->proxy->server_id_hdr_name &&
1985 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1986 break;
1987
Christopher Faulet99eff652019-08-11 23:11:30 +02001988 memcpy(trash.area, "http_", 5);
1989 memcpy(trash.area+5, p.n.ptr, p.n.len);
1990 p.n = ist2(trash.area, p.n.len+5);
1991 }
1992
1993 if (!fcgi_encode_param(&outbuf, &p)) {
1994 if (b_space_wraps(mbuf))
1995 goto realign_again;
1996 if (outbuf.data == 8)
1997 goto full;
1998 goto done;
1999 }
2000 break;
2001
2002 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002003 if (fconn->proxy->server_id_hdr_name) {
2004 struct server *srv = objt_server(fconn->conn->target);
2005
2006 if (!srv)
2007 goto done;
2008
2009 memcpy(trash.area, "http_", 5);
2010 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2011 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2012 p.v = ist(srv->id);
2013
2014 if (!fcgi_encode_param(&outbuf, &p)) {
2015 if (b_space_wraps(mbuf))
2016 goto realign_again;
2017 if (outbuf.data == 8)
2018 goto full;
2019 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002020 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002022 goto done;
2023
2024 default:
2025 break;
2026 }
2027 total += size;
2028 blk = htx_remove_blk(htx, blk);
2029 }
2030
2031 done:
2032 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2033 goto error;
2034
2035 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2047 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2048 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2049 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2051 goto error;
2052
2053 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002054 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002055 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2056 b_add(mbuf, outbuf.data);
2057
2058 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002059 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002060 return total;
2061 full:
2062 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2063 goto retry;
2064 fconn->flags |= FCGI_CF_MUX_MFULL;
2065 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002066 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002067 if (total)
2068 goto error;
2069 goto end;
2070
2071 error:
2072 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002073 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002074 fcgi_strm_error(fstrm);
2075 goto end;
2076}
2077
2078/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2079 * anything. STDIN records contain the request body.
2080 */
2081static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2082 struct htx *htx, size_t count, struct buffer *buf)
2083{
2084 struct buffer outbuf;
2085 struct buffer *mbuf;
2086 struct htx_blk *blk;
2087 enum htx_blk_type type;
2088 uint32_t size;
2089 size_t total = 0;
2090
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002091 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002092 if (!count)
2093 goto end;
2094
2095 mbuf = br_tail(fconn->mbuf);
2096 retry:
2097 if (!fcgi_get_buf(fconn, mbuf)) {
2098 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002099 fstrm->flags |= FCGI_SF_BLK_MROOM;
2100 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2101 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002102 }
2103
2104 /* Perform some optimizations to reduce the number of buffer copies.
2105 * First, if the mux's buffer is empty and the htx area contains exactly
2106 * one data block of the same size as the requested count, and this
2107 * count fits within the record size, then it's possible to simply swap
2108 * the caller's buffer with the mux's output buffer and adjust offsets
2109 * and length to match the entire DATA HTX block in the middle. In this
2110 * case we perform a true zero-copy operation from end-to-end. This is
2111 * the situation that happens all the time with large files. Second, if
2112 * this is not possible, but the mux's output buffer is empty, we still
2113 * have an opportunity to avoid the copy to the intermediary buffer, by
2114 * making the intermediary buffer's area point to the output buffer's
2115 * area. In this case we want to skip the HTX header to make sure that
2116 * copies remain aligned and that this operation remains possible all
2117 * the time. This goes for headers, data blocks and any data extracted
2118 * from the HTX blocks.
2119 */
2120 blk = htx_get_head_blk(htx);
2121 if (!blk)
2122 goto end;
2123 type = htx_get_blk_type(blk);
2124 size = htx_get_blksz(blk);
2125 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2126 void *old_area = mbuf->area;
2127
2128 if (b_data(mbuf)) {
2129 /* Too bad there are data left there. We're willing to memcpy/memmove
2130 * up to 1/4 of the buffer, which means that it's OK to copy a large
2131 * record into a buffer containing few data if it needs to be realigned,
2132 * and that it's also OK to copy few data without realigning. Otherwise
2133 * we'll pretend the mbuf is full and wait for it to become empty.
2134 */
2135 if (size + 8 <= b_room(mbuf) &&
2136 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2137 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2138 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002139 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002140 }
2141
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002142 TRACE_PROTO("sending stding data (zero-copy)", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002143 /* map a FCGI record to the HTX block so that we can put the
2144 * record header there.
2145 */
2146 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2147 outbuf.area = b_head(mbuf);
2148
2149 /* prepend a FCGI record header just before the DATA block */
2150 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2151 fcgi_set_record_id(outbuf.area, fstrm->id);
2152 fcgi_set_record_size(outbuf.area, size);
2153
2154 /* and exchange with our old area */
2155 buf->area = old_area;
2156 buf->data = buf->head = 0;
2157 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002158
2159 htx = (struct htx *)buf->area;
2160 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002161 goto end;
2162 }
2163
2164 copy:
2165 while (1) {
2166 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2167 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2168 break;
2169 realign_again:
2170 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2171 }
2172
2173 if (outbuf.size < 8)
2174 goto full;
2175
2176 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2177 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2178 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2179 fcgi_set_record_id(outbuf.area, fstrm->id);
2180 outbuf.data = 8;
2181
2182 blk = htx_get_head_blk(htx);
2183 while (blk && count) {
2184 enum htx_blk_type type = htx_get_blk_type(blk);
2185 uint32_t size = htx_get_blksz(blk);
2186 struct ist v;
2187
2188 switch (type) {
2189 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002190 TRACE_PROTO("sending stding data", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){size});
Christopher Faulet99eff652019-08-11 23:11:30 +02002191 v = htx_get_blk_value(htx, blk);
2192 if (v.len > count)
2193 v.len = count;
2194
2195 if (v.len > b_room(&outbuf)) {
2196 /* It doesn't fit at once. If it at least fits once split and
2197 * the amount of data to move is low, let's defragment the
2198 * buffer now.
2199 */
2200 if (b_space_wraps(mbuf) &&
2201 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2202 b_data(mbuf) <= MAX_DATA_REALIGN)
2203 goto realign_again;
2204 v.len = b_room(&outbuf);
2205 }
2206 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2207 if (outbuf.data == 8)
2208 goto full;
2209 goto done;
2210 }
2211 if (v.len != size) {
2212 total += v.len;
2213 count -= v.len;
2214 htx_cut_data_blk(htx, blk, v.len);
2215 goto done;
2216 }
2217 break;
2218
2219 case HTX_BLK_EOM:
2220 goto done;
2221
2222 default:
2223 break;
2224 }
2225 total += size;
2226 count -= size;
2227 blk = htx_remove_blk(htx, blk);
2228 }
2229
2230 done:
2231 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002232 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002233 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2234 b_add(mbuf, outbuf.data);
2235
2236 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002237 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002238 return total;
2239 full:
2240 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2241 goto retry;
2242 fconn->flags |= FCGI_CF_MUX_MFULL;
2243 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002244 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002245 goto end;
2246}
2247
2248/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2249 * anything. STDOUT records contain the entire response. All the content is
2250 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2251 */
2252static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2253{
2254 struct buffer *dbuf;
2255 size_t ret;
2256 size_t max;
2257
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002258 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2259
Christopher Faulet99eff652019-08-11 23:11:30 +02002260 dbuf = &fconn->dbuf;
2261
2262 /* Only padding remains */
2263 if (fconn->state == FCGI_CS_RECORD_P)
2264 goto end_transfer;
2265
2266 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2267 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2268 buf_room_for_htx_data(dbuf))
2269 goto fail; // incomplete record
2270
2271 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2272 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002273 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2274 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002275 }
2276
2277 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2278 max = buf_room_for_htx_data(&fstrm->rxbuf);
2279 if (!b_data(&fstrm->rxbuf))
2280 fstrm->rxbuf.head = sizeof(struct htx);
2281 if (max > fconn->drl)
2282 max = fconn->drl;
2283
2284 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2285 if (!ret)
2286 goto fail;
2287 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002288 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2289 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002290
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002291 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002292 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002293 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2294 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002295
2296 if (fconn->drl)
2297 goto fail;
2298
2299 end_transfer:
2300 fconn->drl += fconn->drp;
2301 fconn->drp = 0;
2302 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2303 b_del(&fconn->dbuf, ret);
2304 fconn->drl -= ret;
2305 if (fconn->drl)
2306 goto fail;
2307
2308 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002309 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2310 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002311 return 1;
2312 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002313 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002314 return 0;
2315}
2316
2317
2318/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2319 * anything. It only skip the padding in fact, there is no payload for such
2320 * records. It makrs the end of the response.
2321 */
2322static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2323{
2324 int ret;
2325
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002326 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2327
Christopher Faulet99eff652019-08-11 23:11:30 +02002328 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002329 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002330 fconn->drl += fconn->drp;
2331 fconn->drp = 0;
2332 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2333 b_del(&fconn->dbuf, ret);
2334 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002335 if (fconn->drl) {
2336 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002337 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002338 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002339 fconn->state = FCGI_CS_RECORD_H;
2340 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002341 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2342 TRACE_STATE("stdout data fully send, switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2343 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002344 return 1;
2345}
2346
2347/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2348 * anything.
2349 */
2350static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2351{
2352 struct buffer *dbuf;
2353 struct buffer tag;
2354 size_t ret;
2355
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002356 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002357 dbuf = &fconn->dbuf;
2358
2359 /* Only padding remains */
2360 if (fconn->state == FCGI_CS_RECORD_P)
2361 goto end_transfer;
2362
2363 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2364 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2365 buf_room_for_htx_data(dbuf))
2366 goto fail; // incomplete record
2367
2368 chunk_reset(&trash);
2369 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2370 if (!ret)
2371 goto fail;
2372 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002373 TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002374
2375 trash.area[ret] = '\n';
2376 trash.area[ret+1] = '\0';
2377 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002378 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002379
2380 if (fconn->drl)
2381 goto fail;
2382
2383 end_transfer:
2384 fconn->drl += fconn->drp;
2385 fconn->drp = 0;
2386 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2387 b_del(&fconn->dbuf, ret);
2388 fconn->drl -= ret;
2389 if (fconn->drl)
2390 goto fail;
2391 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002392 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2393 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002394 return 1;
2395 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002397 return 0;
2398}
2399
2400/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2401 * anything. If the empty STDOUT record is not already received, this one marks
2402 * the end of the response. It is highly unexpected, but if the record is larger
2403 * than a buffer and cannot be decoded in one time, an error is triggered and
2404 * the connection is closed. END_REQUEST record cannot be split.
2405 */
2406static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2407{
2408 struct buffer inbuf;
2409 struct buffer *dbuf;
2410 struct fcgi_end_request endreq;
2411
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002413 dbuf = &fconn->dbuf;
2414
2415 /* Record too large to be fully decoded */
2416 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2417 goto fail;
2418
2419 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2421 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002422 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002423 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002424
2425 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2426 /* Realign the dmux buffer if the record wraps. It is unexpected
2427 * at this stage because it should be the first record received
2428 * from the FCGI application.
2429 */
2430 b_slow_realign(dbuf, trash.area, 0);
2431 }
2432
2433 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2434
2435 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2436 goto fail;
2437
2438 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002439 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2440 TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm,, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02002441 fstrm->proto_status = endreq.errcode;
2442 fcgi_strm_close(fstrm);
2443
2444 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2445 fconn->drl = 0;
2446 fconn->drp = 0;
2447 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002448 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2449 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002450 return 1;
2451
2452 fail:
2453 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002454 TRACE_DEVEL("leaving on error", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_FSTRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002455 return 0;
2456}
2457
2458/* process Rx records to be demultiplexed */
2459static void fcgi_process_demux(struct fcgi_conn *fconn)
2460{
2461 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2462 struct fcgi_header hdr;
2463 int ret;
2464
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002465 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2466
Christopher Faulet99eff652019-08-11 23:11:30 +02002467 if (fconn->state == FCGI_CS_CLOSED)
2468 return;
2469
2470 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002471 if (fconn->state == FCGI_CS_INIT) {
2472 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2473 return;
2474 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002475 if (fconn->state == FCGI_CS_SETTINGS) {
2476 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002477 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002478 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2479 if (!ret)
2480 goto fail;
2481 b_del(&fconn->dbuf, ret);
2482
2483 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2484 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002485 TRACE_PROTO("unexpected record type or flags", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
2486 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002487 goto fail;
2488 }
2489 goto new_record;
2490 }
2491 }
2492
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002493 /* process as many incoming records as possible below */
2494 while (1) {
2495 if (!b_data(&fconn->dbuf)) {
2496 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2497 break;
2498 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002499
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002500 if (fconn->state == FCGI_CS_CLOSED) {
2501 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002502 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002503 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002504
2505 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002506 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002507 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2508 if (!ret)
2509 break;
2510 b_del(&fconn->dbuf, ret);
2511
2512 new_record:
2513 fconn->dsi = hdr.id;
2514 fconn->drt = hdr.type;
2515 fconn->drl = hdr.len;
2516 fconn->drp = hdr.padding;
2517 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002518 TRACE_STATE("FCGI record header rcvd, switching to RECORD_D", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002519 }
2520
2521 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2522 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2523
2524 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2525 (b_data(&fstrm->rxbuf) ||
2526 conn_xprt_read0_pending(fconn->conn) ||
2527 fstrm->state == FCGI_SS_CLOSED ||
2528 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2529 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2530 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002531 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002532 fstrm->cs->flags |= CS_FL_RCV_MORE;
2533 fcgi_strm_notify_recv(fstrm);
2534 }
2535 fstrm = tmp_fstrm;
2536
2537 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2538 /* ignore all record for closed streams */
2539 goto ignore_record;
2540 }
2541 if (fstrm->state == FCGI_SS_IDLE) {
2542 /* ignore all record for unknown streams */
2543 goto ignore_record;
2544 }
2545
2546 switch (fconn->drt) {
2547 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002548 TRACE_PROTO("receiving FCGI GET_VALUES_RESULT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002549 ret = fcgi_conn_handle_values_result(fconn);
2550 break;
2551
2552 case FCGI_STDOUT:
2553 if (fstrm->flags & FCGI_SF_ES_RCVD)
2554 goto ignore_record;
2555
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002556 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002557 if (fconn->drl)
2558 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2559 else
2560 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2561 break;
2562
2563 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002564 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002565 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2566 break;
2567
2568 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002569 TRACE_PROTO("receiving FCGI END_REQUEST record", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002570 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2571 break;
2572
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002573 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002574 default:
2575 ignore_record:
2576 /* drop records that we ignore. They may be
2577 * larger than the buffer so we drain all of
2578 * their contents until we reach the end.
2579 */
2580 fconn->state = FCGI_CS_RECORD_P;
2581 fconn->drl += fconn->drp;
2582 fconn->drp = 0;
2583 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002584 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2585 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002586 b_del(&fconn->dbuf, ret);
2587 fconn->drl -= ret;
2588 ret = (fconn->drl == 0);
2589 }
2590
2591 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002592 if (ret <= 0) {
2593 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002594 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002595 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002596
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002597 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002598 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002599 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2600 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002601 }
2602
2603 fail:
2604 /* we can go here on missing data, blocked response or error */
2605 if (fstrm && fstrm->cs &&
2606 (b_data(&fstrm->rxbuf) ||
2607 conn_xprt_read0_pending(fconn->conn) ||
2608 fstrm->state == FCGI_SS_CLOSED ||
2609 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2610 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2611 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002612 TRACE_DEVEL("notifying stream before switching SID", FCGI_EV_RX_RECORD|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002613 fstrm->cs->flags |= CS_FL_RCV_MORE;
2614 fcgi_strm_notify_recv(fstrm);
2615 }
2616
2617 fcgi_conn_restart_reading(fconn, 0);
2618}
2619
2620/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2621 * the end.
2622 */
2623static int fcgi_process_mux(struct fcgi_conn *fconn)
2624{
2625 struct fcgi_strm *fstrm, *fstrm_back;
2626
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002627 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2628
Christopher Faulet99eff652019-08-11 23:11:30 +02002629 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2630 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2631 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2632 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002633 TRACE_STATE("switching to RECORD_H", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002634 fcgi_wake_unassigned_streams(fconn);
2635 goto mux;
2636 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002638 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2639 goto fail;
2640 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002641 TRACE_STATE("switching to SETTINGS", FCGI_EV_TX_RECORD|FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002642 }
2643 /* need to wait for the other side */
2644 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002645 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002646 }
2647
2648 mux:
2649 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2650 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2651 break;
2652
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002653 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002654 continue;
2655
Willy Tarreau7aad7032020-01-16 17:20:57 +01002656 /* If the sender changed his mind and unsubscribed, let's just
2657 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002658 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002659 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2660 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002661 LIST_DEL_INIT(&fstrm->send_list);
2662 continue;
2663 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002664
2665 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002666 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2667 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002668 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002669 tasklet_wakeup(fstrm->subs->tasklet);
2670 fstrm->subs->events &= ~SUB_RETRY_SEND;
2671 if (!fstrm->subs->events)
2672 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002673 } else {
2674 /* it's the shut request that was queued */
2675 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2676 tasklet_wakeup(fstrm->shut_tl);
2677 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002678 }
2679
2680 fail:
2681 if (fconn->state == FCGI_CS_CLOSED) {
2682 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2683 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002684 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2685 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002686 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002687 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002688 }
2689 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002690
2691 done:
2692 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002693 return 1;
2694}
2695
2696
2697/* Attempt to read data, and subscribe if none available.
2698 * The function returns 1 if data has been received, otherwise zero.
2699 */
2700static int fcgi_recv(struct fcgi_conn *fconn)
2701{
2702 struct connection *conn = fconn->conn;
2703 struct buffer *buf;
2704 int max;
2705 size_t ret;
2706
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002707 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2708
2709 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2710 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002711 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002712 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002713
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002714 if (!fcgi_recv_allowed(fconn)) {
2715 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002716 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002717 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002718
2719 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2720 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002721 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002722 fconn->flags |= FCGI_CF_DEM_DALLOC;
2723 return 0;
2724 }
2725
2726 b_realign_if_empty(buf);
2727 if (!b_data(buf)) {
2728 /* try to pre-align the buffer like the
2729 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002730 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002731 * HTX block to alias it upon recv. We cannot use the
2732 * head because rcv_buf() will realign the buffer if
2733 * it's empty. Thus we cheat and pretend we already
2734 * have a few bytes there.
2735 */
2736 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2737 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2738 }
2739 else
2740 max = buf_room_for_htx_data(buf);
2741
2742 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2743
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002744 if (max && !ret && fcgi_recv_allowed(fconn)) {
2745 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002746 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002747 }
2748 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002749 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002750
2751 if (!b_data(buf)) {
2752 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002753 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002754 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2755 }
2756
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002757 if (ret == max) {
2758 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002759 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002760 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002761
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002762 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002763 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2764}
2765
2766
2767/* Try to send data if possible.
2768 * The function returns 1 if data have been sent, otherwise zero.
2769 */
2770static int fcgi_send(struct fcgi_conn *fconn)
2771{
2772 struct connection *conn = fconn->conn;
2773 int done;
2774 int sent = 0;
2775
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002776 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2777
2778 if (conn->flags & CO_FL_ERROR) {
2779 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002780 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002781 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002782
2783
Willy Tarreau911db9b2020-01-23 16:27:54 +01002784 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002785 /* a handshake was requested */
2786 goto schedule;
2787 }
2788
2789 /* This loop is quite simple : it tries to fill as much as it can from
2790 * pending streams into the existing buffer until it's reportedly full
2791 * or the end of send requests is reached. Then it tries to send this
2792 * buffer's contents out, marks it not full if at least one byte could
2793 * be sent, and tries again.
2794 *
2795 * The snd_buf() function normally takes a "flags" argument which may
2796 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2797 * data immediately comes and CO_SFL_STREAMER to indicate that the
2798 * connection is streaming lots of data (used to increase TLS record
2799 * size at the expense of latency). The former can be sent any time
2800 * there's a buffer full flag, as it indicates at least one stream
2801 * attempted to send and failed so there are pending data. An
2802 * alternative would be to set it as long as there's an active stream
2803 * but that would be problematic for ACKs until we have an absolute
2804 * guarantee that all waiters have at least one byte to send. The
2805 * latter should possibly not be set for now.
2806 */
2807
2808 done = 0;
2809 while (!done) {
2810 unsigned int flags = 0;
2811 unsigned int released = 0;
2812 struct buffer *buf;
2813
2814 /* fill as much as we can into the current buffer */
2815 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2816 done = fcgi_process_mux(fconn);
2817
2818 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2819 done = 1; // we won't go further without extra buffers
2820
2821 if (conn->flags & CO_FL_ERROR)
2822 break;
2823
2824 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2825 flags |= CO_SFL_MSG_MORE;
2826
2827 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2828 if (b_data(buf)) {
2829 int ret;
2830
2831 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2832 if (!ret) {
2833 done = 1;
2834 break;
2835 }
2836 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002837 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002838 b_del(buf, ret);
2839 if (b_data(buf)) {
2840 done = 1;
2841 break;
2842 }
2843 }
2844 b_free(buf);
2845 released++;
2846 }
2847
2848 if (released)
2849 offer_buffers(NULL, tasks_run_queue);
2850
2851 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002852 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2853 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002854 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2855 }
2856
2857 if (conn->flags & CO_FL_SOCK_WR_SH) {
2858 /* output closed, nothing to send, clear the buffer to release it */
2859 b_reset(br_tail(fconn->mbuf));
2860 }
2861 /* We're not full anymore, so we can wake any task that are waiting
2862 * for us.
2863 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002864 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002865 struct fcgi_strm *fstrm;
2866
2867 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2868 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2869 break;
2870
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002871 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002872 continue;
2873
Willy Tarreau7aad7032020-01-16 17:20:57 +01002874 /* If the sender changed his mind and unsubscribed, let's just
2875 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002876 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002877 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2878 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002879 LIST_DEL_INIT(&fstrm->send_list);
2880 continue;
2881 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002882
2883 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002884 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002885 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002886 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002887 tasklet_wakeup(fstrm->subs->tasklet);
2888 fstrm->subs->events &= ~SUB_RETRY_SEND;
2889 if (!fstrm->subs->events)
2890 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002891 } else {
2892 /* it's the shut request that was queued */
2893 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2894 tasklet_wakeup(fstrm->shut_tl);
2895 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002896 }
2897 }
2898 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002899 if (!br_data(fconn->mbuf)) {
2900 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002901 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002902 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002903schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002904 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2905 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002906 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002907 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002908
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002909 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002910 return sent;
2911}
2912
2913/* this is the tasklet referenced in fconn->wait_event.tasklet */
2914static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2915{
2916 struct fcgi_conn *fconn = ctx;
2917 int ret = 0;
2918
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002919 TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn);
2920
Christopher Faulet99eff652019-08-11 23:11:30 +02002921 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2922 ret = fcgi_send(fconn);
2923 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2924 ret |= fcgi_recv(fconn);
2925 if (ret || b_data(&fconn->dbuf))
2926 fcgi_process(fconn);
2927 return NULL;
2928}
2929
2930/* callback called on any event by the connection handler.
2931 * It applies changes and returns zero, or < 0 if it wants immediate
2932 * destruction of the connection (which normally doesn not happen in FCGI).
2933 */
2934static int fcgi_process(struct fcgi_conn *fconn)
2935{
2936 struct connection *conn = fconn->conn;
2937
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002938 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2939
Christopher Faulet99eff652019-08-11 23:11:30 +02002940 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2941 fcgi_process_demux(fconn);
2942
2943 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2944 b_reset(&fconn->dbuf);
2945
2946 if (buf_room_for_htx_data(&fconn->dbuf))
2947 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2948 }
2949 fcgi_send(fconn);
2950
2951 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2952 /* frontend is stopping, reload likely in progress, let's try
2953 * to announce a graceful shutdown if not yet done. We don't
2954 * care if it fails, it will be tried again later.
2955 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002956 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 +02002957 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2958 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2959 fcgi_conn_send_aborts(fconn);
2960 }
2961 }
2962
2963 /*
2964 * If we received early data, and the handshake is done, wake
2965 * any stream that was waiting for it.
2966 */
2967 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01002968 (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 +02002969 struct eb32_node *node;
2970 struct fcgi_strm *fstrm;
2971
2972 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
2973 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
2974
2975 while (node) {
2976 fstrm = container_of(node, struct fcgi_strm, by_id);
2977 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
2978 fcgi_strm_notify_recv(fstrm);
2979 node = eb32_next(node);
2980 }
2981 }
2982
2983 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
2984 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2985 eb_is_empty(&fconn->streams_by_id)) {
2986 fcgi_wake_some_streams(fconn, 0);
2987
2988 if (eb_is_empty(&fconn->streams_by_id)) {
2989 /* no more stream, kill the connection now */
2990 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002991 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002992 return -1;
2993 }
2994 }
2995
2996 if (!b_data(&fconn->dbuf))
2997 fcgi_release_buf(fconn, &fconn->dbuf);
2998
2999 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3000 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3001 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3002 fcgi_release_mbuf(fconn);
3003
3004 if (fconn->task) {
3005 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3006 task_queue(fconn->task);
3007 }
3008
3009 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003010 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003011 return 0;
3012}
3013
3014
3015/* wake-up function called by the connection layer (mux_ops.wake) */
3016static int fcgi_wake(struct connection *conn)
3017{
3018 struct fcgi_conn *fconn = conn->ctx;
3019
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003020 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003021 return (fcgi_process(fconn));
3022}
3023
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003024
3025static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3026{
3027 int ret = 0;
3028 switch (mux_ctl) {
3029 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003030 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003031 ret |= MUX_STATUS_READY;
3032 return ret;
3033 default:
3034 return -1;
3035 }
3036}
3037
Christopher Faulet99eff652019-08-11 23:11:30 +02003038/* Connection timeout management. The principle is that if there's no receipt
3039 * nor sending for a certain amount of time, the connection is closed. If the
3040 * MUX buffer still has lying data or is not allocatable, the connection is
3041 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003042 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003043 */
3044static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3045{
3046 struct fcgi_conn *fconn = context;
3047 int expired = tick_is_expired(t->expire, now_ms);
3048
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003049 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3050
3051 if (!expired && fconn) {
3052 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003053 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003054 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003055
3056 task_destroy(t);
3057
3058 if (!fconn) {
3059 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003060 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003061 return NULL;
3062 }
3063
3064 fconn->task = NULL;
3065 fconn->state = FCGI_CS_CLOSED;
3066 fcgi_wake_some_streams(fconn, 0);
3067
3068 if (br_data(fconn->mbuf)) {
3069 /* don't even try to send aborts, the buffer is stuck */
3070 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3071 goto end;
3072 }
3073
3074 /* try to send but no need to insist */
3075 if (!fcgi_conn_send_aborts(fconn))
3076 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3077
3078 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3079 conn_xprt_ready(fconn->conn)) {
3080 unsigned int released = 0;
3081 struct buffer *buf;
3082
3083 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3084 if (b_data(buf)) {
3085 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3086 buf, b_data(buf), 0);
3087 if (!ret)
3088 break;
3089 b_del(buf, ret);
3090 if (b_data(buf))
3091 break;
3092 b_free(buf);
3093 released++;
3094 }
3095 }
3096
3097 if (released)
3098 offer_buffers(NULL, tasks_run_queue);
3099 }
3100
3101 end:
3102 /* either we can release everything now or it will be done later once
3103 * the last stream closes.
3104 */
3105 if (eb_is_empty(&fconn->streams_by_id))
3106 fcgi_release(fconn);
3107
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003108 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003109 return NULL;
3110}
3111
3112
3113/*******************************************/
3114/* functions below are used by the streams */
3115/*******************************************/
3116
3117/* Append the description of what is present in error snapshot <es> into <out>.
3118 * The description must be small enough to always fit in a buffer. The output
3119 * buffer may be the trash so the trash must not be used inside this function.
3120 */
3121static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3122{
3123 chunk_appendf(out,
3124 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3125 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3126 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3127 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3128 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3129 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3130}
3131/*
3132 * Capture a bad response and archive it in the proxy's structure. By default
3133 * it tries to report the error position as h1m->err_pos. However if this one is
3134 * not set, it will then report h1m->next, which is the last known parsing
3135 * point. The function is able to deal with wrapping buffers. It always displays
3136 * buffers as a contiguous area starting at buf->p. The direction is determined
3137 * thanks to the h1m's flags.
3138 */
3139static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3140 struct h1m *h1m, struct buffer *buf)
3141{
3142 struct session *sess = fstrm->sess;
3143 struct proxy *proxy = fconn->proxy;
3144 struct proxy *other_end = sess->fe;
3145 union error_snapshot_ctx ctx;
3146
3147 /* http-specific part now */
3148 ctx.h1.state = h1m->state;
3149 ctx.h1.c_flags = fconn->flags;
3150 ctx.h1.s_flags = fstrm->flags;
3151 ctx.h1.m_flags = h1m->flags;
3152 ctx.h1.m_clen = h1m->curr_len;
3153 ctx.h1.m_blen = h1m->body_len;
3154
3155 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3156 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3157 &ctx, fcgi_show_error_snapshot);
3158}
3159
3160static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3161 struct buffer *buf, size_t *ofs, size_t max)
3162{
3163 int ret;
3164
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003165 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003166 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3167 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003168 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 +02003169 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003170 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 +02003171 fcgi_strm_error(fstrm);
3172 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3173 }
3174 goto end;
3175 }
3176
3177 *ofs += ret;
3178 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003179 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003180 return ret;
3181
3182}
3183
Christopher Fauletaf542632019-10-01 21:52:49 +02003184static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003185 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3186{
3187 int ret;
3188
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003189 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003190 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003191 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003192 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 +02003193 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003194 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 +02003195 fcgi_strm_error(fstrm);
3196 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3197 }
3198 goto end;
3199 }
3200 *ofs += ret;
3201 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003202 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003203 return ret;
3204}
3205
3206static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3207 struct buffer *buf, size_t *ofs, size_t max)
3208{
3209 int ret;
3210
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003211 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003212 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003213 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003214 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 +02003215 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003216 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 +02003217 fcgi_strm_error(fstrm);
3218 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3219 }
3220 goto end;
3221 }
3222 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003223 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003224 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003225 return ret;
3226}
3227
3228static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003229 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003230{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003231 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003232
Christopher Faulet76014fd2019-12-10 11:47:22 +01003233 TRACE_ENTER(FCGI_EV_RSP_DATA||FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
3234 ret = h1_parse_msg_eom(h1m, htx, max);
3235 if (!ret) {
3236 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3237 if (htx->flags & HTX_FL_PARSING_ERROR) {
3238 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3239 fcgi_strm_error(fstrm);
3240 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3241 }
3242 goto end;
3243 }
3244 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3245 end:
3246 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3247 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003248}
3249
3250static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3251{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003252 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003253 struct htx *htx;
3254 struct h1m *h1m = &fstrm->h1m;
3255 size_t ret, data, total = 0;
3256
3257 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003258 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3259
Christopher Faulet99eff652019-08-11 23:11:30 +02003260 data = htx->data;
3261 if (fstrm->state == FCGI_SS_ERROR)
3262 goto end;
3263
3264 do {
3265 size_t used = htx_used_space(htx);
3266
3267 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003268 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003269 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3270 if (!ret)
3271 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003272
3273 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3274
Christopher Faulet99eff652019-08-11 23:11:30 +02003275 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3276 struct htx_blk *blk = htx_get_head_blk(htx);
3277 struct htx_sl *sl;
3278
3279 if (!blk)
3280 break;
3281 sl = htx_get_blk_ptr(htx, blk);
3282 sl->flags |= HTX_SL_F_XFER_LEN;
3283 htx->extra = 0;
3284 }
3285 }
3286 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003287 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003288 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003289 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003290 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003291
3292 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 +02003293 }
3294 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003295 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3296 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3297 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003298 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003299
Christopher Faulet76014fd2019-12-10 11:47:22 +01003300 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 +02003301 }
3302 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003303 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3304 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3305 break;
3306
3307 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3308 }
3309
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 if (b_data(&fstrm->rxbuf) > total) {
3311 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003312 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003313 fcgi_strm_error(fstrm);
3314 }
3315 break;
3316 }
3317 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003318 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003319 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003320
Christopher Faulet99eff652019-08-11 23:11:30 +02003321 if (fstrm->state != FCGI_SS_ERROR &&
3322 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003323 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003324 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3325 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3326 h1m->state = H1_MSG_DONE;
3327 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 +02003328 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003329 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003330 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003331
3332 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003333 }
3334 else {
3335 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003336 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003337 fcgi_strm_error(fstrm);
3338 break;
3339 }
3340
3341 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003342 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003343
3344 if (fstrm->state == FCGI_SS_ERROR) {
3345 b_reset(&fstrm->rxbuf);
3346 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003347 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003348 return 0;
3349 }
3350
3351 b_del(&fstrm->rxbuf, total);
3352
3353 end:
3354 htx_to_buf(htx, buf);
3355 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003356 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003357 return ret;
3358}
3359
3360/*
3361 * Attach a new stream to a connection
3362 * (Used for outgoing connections)
3363 */
3364static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3365{
3366 struct conn_stream *cs;
3367 struct fcgi_strm *fstrm;
3368 struct fcgi_conn *fconn = conn->ctx;
3369
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003370 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003371 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003372 if (!cs) {
3373 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003374 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003375 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003376 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3377 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003378 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003379 cs_free(cs);
3380 return NULL;
3381 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003382 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003383 return cs;
3384}
3385
3386/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3387 * We have to scan because we may have some orphan streams. It might be
3388 * beneficial to scan backwards from the end to reduce the likeliness to find
3389 * orphans.
3390 */
3391static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3392{
3393 struct fcgi_conn *fconn = conn->ctx;
3394 struct fcgi_strm *fstrm;
3395 struct eb32_node *node;
3396
3397 node = eb32_first(&fconn->streams_by_id);
3398 while (node) {
3399 fstrm = container_of(node, struct fcgi_strm, by_id);
3400 if (fstrm->cs)
3401 return fstrm->cs;
3402 node = eb32_next(node);
3403 }
3404 return NULL;
3405}
3406
3407/*
3408 * Destroy the mux and the associated connection, if it is no longer used
3409 */
3410static void fcgi_destroy(void *ctx)
3411{
3412 struct fcgi_conn *fconn = ctx;
3413
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003414 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003415 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3416 fcgi_release(fconn);
3417}
3418
3419/*
3420 * Detach the stream from the connection and possibly release the connection.
3421 */
3422static void fcgi_detach(struct conn_stream *cs)
3423{
3424 struct fcgi_strm *fstrm = cs->ctx;
3425 struct fcgi_conn *fconn;
3426 struct session *sess;
3427
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003428 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3429
Christopher Faulet99eff652019-08-11 23:11:30 +02003430 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003431 if (!fstrm) {
3432 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003433 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003434 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003435
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003436 /* there's no txbuf so we're certain no to be able to send anything */
3437 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003438
3439 sess = fstrm->sess;
3440 fconn = fstrm->fconn;
3441 fstrm->cs = NULL;
3442 fconn->nb_cs--;
3443
3444 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3445 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3446 fconn->streams_limit = 1;
3447 }
3448 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3449 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3450 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3451 fconn->state = FCGI_CS_CLOSED;
3452 }
3453
3454 /* this stream may be blocked waiting for some data to leave, so orphan
3455 * it in this case.
3456 */
3457 if (!(cs->conn->flags & CO_FL_ERROR) &&
3458 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003459 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003460 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003461 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003462 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003463 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003464
3465 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3466 /* unblock the connection if it was blocked on this stream. */
3467 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3468 fcgi_conn_restart_reading(fconn, 1);
3469 }
3470
3471 fcgi_strm_destroy(fstrm);
3472
3473 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3474 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
3475 if (!fconn->conn->owner) {
3476 fconn->conn->owner = sess;
3477 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3478 fconn->conn->owner = NULL;
3479 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003480 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003481 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard12ffab02020-02-14 13:23:45 +01003482 fconn->conn->mux->destroy(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003483 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3484 }
3485 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003486 return;
3487 }
3488 }
3489 }
3490 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003491 int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn);
3492 if (ret == -1) {
3493 /* The connection is destroyed, let's leave */
3494 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003495 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003496 }
3497 else if (ret == 1) {
3498 /* The connection was added to the server idle list, just stop */
3499 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3500 return;
3501 }
3502 TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003503 }
3504 /* Never ever allow to reuse a connection from a non-reuse backend */
3505 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3506 fconn->conn->flags |= CO_FL_PRIVATE;
3507 if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) {
3508 struct server *srv = objt_server(fconn->conn->target);
3509
3510 if (srv) {
3511 if (fconn->conn->flags & CO_FL_PRIVATE)
3512 LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list);
3513 else
3514 LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list);
3515 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003516 TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003517 }
3518 }
3519
3520 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003521 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003522 */
3523 if (fcgi_conn_is_dead(fconn)) {
3524 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003525 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003526 fcgi_release(fconn);
3527 }
3528 else if (fconn->task) {
3529 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3530 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003531 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003532 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003533 else
3534 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003535}
3536
3537
3538/* Performs a synchronous or asynchronous shutr(). */
3539static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3540{
3541 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003542
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003543 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3544
Christopher Faulet99eff652019-08-11 23:11:30 +02003545 if (fstrm->state == FCGI_SS_CLOSED)
3546 goto done;
3547
3548 /* a connstream may require us to immediately kill the whole connection
3549 * for example because of a "tcp-request content reject" rule that is
3550 * normally used to limit abuse.
3551 */
3552 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003553 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3554 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003555 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003556 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003557 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003558 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003559 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3560 !fcgi_strm_send_abort(fconn, fstrm))
3561 goto add_to_list;
3562 }
3563
3564 fcgi_strm_close(fstrm);
3565
3566 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3567 tasklet_wakeup(fconn->wait_event.tasklet);
3568 done:
3569 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003570 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003571 return;
3572
3573 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003574 /* Let the handler know we want to shutr, and add ourselves to the
3575 * send list if not yet done. fcgi_deferred_shut() will be
3576 * automatically called via the shut_tl tasklet when there's room
3577 * again.
3578 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003579 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003580 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003581 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3582 }
3583 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003584 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003585 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003586 return;
3587}
3588
3589/* Performs a synchronous or asynchronous shutw(). */
3590static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3591{
3592 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003593
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003594 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3595
Christopher Faulet99eff652019-08-11 23:11:30 +02003596 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3597 goto done;
3598
3599 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3600 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3601 !fcgi_strm_send_abort(fconn, fstrm))
3602 goto add_to_list;
3603
3604 if (fstrm->state == FCGI_SS_HREM)
3605 fcgi_strm_close(fstrm);
3606 else
3607 fstrm->state = FCGI_SS_HLOC;
3608 } else {
3609 /* a connstream may require us to immediately kill the whole connection
3610 * for example because of a "tcp-request content reject" rule that is
3611 * normally used to limit abuse.
3612 */
3613 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003614 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3615 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003616 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003617 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003618
3619 fcgi_strm_close(fstrm);
3620 }
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_SHUTW;
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_SHUTW;
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
Willy Tarreau7aad7032020-01-16 17:20:57 +01003645/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003646 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003647 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003648 */
3649static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3650{
3651 struct fcgi_strm *fstrm = ctx;
3652 struct fcgi_conn *fconn = fstrm->fconn;
3653
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003654 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3655
Willy Tarreau7aad7032020-01-16 17:20:57 +01003656 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3657 /* some data processing remains to be done first */
3658 goto end;
3659 }
3660
Christopher Faulet99eff652019-08-11 23:11:30 +02003661 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3662 fcgi_do_shutw(fstrm);
3663
3664 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3665 fcgi_do_shutr(fstrm);
3666
3667 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3668 /* We're done trying to send, remove ourself from the send_list */
3669 LIST_DEL_INIT(&fstrm->send_list);
3670
3671 if (!fstrm->cs) {
3672 fcgi_strm_destroy(fstrm);
3673 if (fcgi_conn_is_dead(fconn))
3674 fcgi_release(fconn);
3675 }
3676 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003677 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003678 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003679 return NULL;
3680}
3681
3682/* shutr() called by the conn_stream (mux_ops.shutr) */
3683static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3684{
3685 struct fcgi_strm *fstrm = cs->ctx;
3686
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003687 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003688 if (cs->flags & CS_FL_KILL_CONN)
3689 fstrm->flags |= FCGI_SF_KILL_CONN;
3690
3691 if (!mode)
3692 return;
3693
3694 fcgi_do_shutr(fstrm);
3695}
3696
3697/* shutw() called by the conn_stream (mux_ops.shutw) */
3698static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3699{
3700 struct fcgi_strm *fstrm = cs->ctx;
3701
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003702 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003703 if (cs->flags & CS_FL_KILL_CONN)
3704 fstrm->flags |= FCGI_SF_KILL_CONN;
3705
3706 fcgi_do_shutw(fstrm);
3707}
3708
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003709/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3710 * event subscriber <es> is not allowed to change from a previous call as long
3711 * as at least one event is still subscribed. The <event_type> must only be a
3712 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003713 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003714static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003715{
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 struct fcgi_strm *fstrm = cs->ctx;
3717 struct fcgi_conn *fconn = fstrm->fconn;
3718
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003719 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
3720 BUG_ON(fstrm->subs && fstrm->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003721 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003722
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003723 es->events |= event_type;
3724 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003725
3726 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003727 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003728
Christopher Faulet99eff652019-08-11 23:11:30 +02003729 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003730 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003731 if (!LIST_ADDED(&fstrm->send_list))
3732 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003733 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003734 return 0;
3735}
3736
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003737/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3738 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3739 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003740 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003741static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003742{
Christopher Faulet99eff652019-08-11 23:11:30 +02003743 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003744 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003745
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003746 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003747 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003748
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003749 es->events &= ~event_type;
3750 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003751 fstrm->subs = NULL;
3752
3753 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003754 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003755
Christopher Faulet99eff652019-08-11 23:11:30 +02003756 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003757 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003758 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003759 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3760 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003761 }
3762 return 0;
3763}
3764
3765/* Called from the upper layer, to receive data */
3766static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3767{
3768 struct fcgi_strm *fstrm = cs->ctx;
3769 struct fcgi_conn *fconn = fstrm->fconn;
3770 size_t ret = 0;
3771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003772 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3773
Christopher Faulet99eff652019-08-11 23:11:30 +02003774 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3775 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003776 else
3777 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003778
Christopher Faulet76014fd2019-12-10 11:47:22 +01003779 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 +02003780 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3781 else {
3782 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003783 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003784 cs->flags |= CS_FL_EOI;
3785 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3786 cs->flags |= CS_FL_EOS;
3787 }
3788 if (conn_xprt_read0_pending(fconn->conn))
3789 cs->flags |= CS_FL_EOS;
3790 if (cs->flags & CS_FL_ERR_PENDING)
3791 cs->flags |= CS_FL_ERROR;
3792 fcgi_release_buf(fconn, &fstrm->rxbuf);
3793 }
3794
3795 if (ret && fconn->dsi == fstrm->id) {
3796 /* demux is blocking on this stream's buffer */
3797 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3798 fcgi_conn_restart_reading(fconn, 1);
3799 }
3800
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003801 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003802 return ret;
3803}
3804
3805
Christopher Faulet99eff652019-08-11 23:11:30 +02003806/* Called from the upper layer, to send data from buffer <buf> for no more than
3807 * <count> bytes. Returns the number of bytes effectively sent. Some status
3808 * flags may be updated on the conn_stream.
3809 */
3810static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3811{
3812 struct fcgi_strm *fstrm = cs->ctx;
3813 struct fcgi_conn *fconn = fstrm->fconn;
3814 size_t total = 0;
3815 size_t ret;
3816 struct htx *htx = NULL;
3817 struct htx_sl *sl;
3818 struct htx_blk *blk;
3819 uint32_t bsize;
3820
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003821 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3822
Christopher Faulet99eff652019-08-11 23:11:30 +02003823 /* If we were not just woken because we wanted to send but couldn't,
3824 * and there's somebody else that is waiting to send, do nothing,
3825 * we will subscribe later and be put at the end of the list
3826 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003827 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003828 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 +02003829 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003830 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003831 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003832
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003833 if (fconn->state < FCGI_CS_RECORD_H) {
3834 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003835 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003836 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003837
3838 htx = htxbuf(buf);
3839 if (fstrm->id == 0) {
3840 int32_t id = fcgi_conn_get_next_sid(fconn);
3841
3842 if (id < 0) {
3843 fcgi_strm_close(fstrm);
3844 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003845 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 +02003846 return 0;
3847 }
3848
3849 eb32_delete(&fstrm->by_id);
3850 fstrm->by_id.key = fstrm->id = id;
3851 fconn->max_id = id;
3852 fconn->nb_reserved--;
3853 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3854
3855
3856 /* Check if length of the body is known or if the message is
3857 * full. Otherwise, the request is invalid.
3858 */
3859 sl = http_get_stline(htx);
3860 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3861 htx->flags |= HTX_FL_PARSING_ERROR;
3862 fcgi_strm_error(fstrm);
3863 goto done;
3864 }
3865 }
3866
3867 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003868 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 +02003869 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3870 goto done;
3871 }
3872
3873 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3874 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3875
3876 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3877 count && !htx_is_empty(htx)) {
3878 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003879 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003880 bsize = htx_get_blksz(blk);
3881
3882 switch (htx_get_blk_type(blk)) {
3883 case HTX_BLK_REQ_SL:
3884 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003885 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 +02003886 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3887 if (!ret) {
3888 goto done;
3889 }
3890 total += ret;
3891 count -= ret;
3892 break;
3893
3894 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003895 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 +02003896 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3897 if (!ret)
3898 goto done;
3899 goto remove_blk;
3900
3901 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003902 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 +02003903 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3904 if (ret > 0) {
3905 htx = htx_from_buf(buf);
3906 total += ret;
3907 count -= ret;
3908 if (ret < bsize)
3909 goto done;
3910 }
3911 break;
3912
3913 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003914 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 +02003915 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3916 if (!ret)
3917 goto done;
3918 goto remove_blk;
3919
3920 default:
3921 remove_blk:
3922 htx_remove_blk(htx, blk);
3923 total += bsize;
3924 count -= bsize;
3925 break;
3926 }
3927 }
3928
3929 done:
3930 if (fstrm->state >= FCGI_SS_HLOC) {
3931 /* trim any possibly pending data after we close (extra CR-LF,
3932 * unprocessed trailers, abnormal extra data, ...)
3933 */
3934 total += count;
3935 count = 0;
3936 }
3937
3938 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003939 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 +02003940 cs_set_error(cs);
3941 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3942 fcgi_strm_close(fstrm);
3943 }
3944
3945 if (htx)
3946 htx_to_buf(htx, buf);
3947
Christopher Faulet99eff652019-08-11 23:11:30 +02003948 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003949 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
3950 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 +02003951 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003952 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003953
3954 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01003955 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3956 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003957 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003958
3959 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02003960 return total;
3961}
3962
3963/* for debugging with CLI's "show fd" command */
3964static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
3965{
3966 struct fcgi_conn *fconn = conn->ctx;
3967 struct fcgi_strm *fstrm = NULL;
3968 struct eb32_node *node;
3969 int send_cnt = 0;
3970 int tree_cnt = 0;
3971 int orph_cnt = 0;
3972 struct buffer *hmbuf, *tmbuf;
3973
3974 if (!fconn)
3975 return;
3976
3977 list_for_each_entry(fstrm, &fconn->send_list, send_list)
3978 send_cnt++;
3979
3980 fstrm = NULL;
3981 node = eb32_first(&fconn->streams_by_id);
3982 while (node) {
3983 fstrm = container_of(node, struct fcgi_strm, by_id);
3984 tree_cnt++;
3985 if (!fstrm->cs)
3986 orph_cnt++;
3987 node = eb32_next(node);
3988 }
3989
3990 hmbuf = br_head(fconn->mbuf);
3991 tmbuf = br_tail(fconn->mbuf);
3992 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
3993 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
3994 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
3995 fconn->state, fconn->max_id, fconn->flags,
3996 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
3997 fconn->wait_event.events, fconn->dsi,
3998 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
3999 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4000 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4001 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4002 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4003 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4004 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4005
4006 if (fstrm) {
4007 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4008 fstrm, fstrm->id, fstrm->flags,
4009 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4010 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4011 fstrm->cs);
4012 if (fstrm->cs)
4013 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4014 fstrm->cs->flags, fstrm->cs->data);
4015 }
4016}
4017
4018/****************************************/
4019/* MUX initialization and instanciation */
4020/****************************************/
4021
4022/* The mux operations */
4023static const struct mux_ops mux_fcgi_ops = {
4024 .init = fcgi_init,
4025 .wake = fcgi_wake,
4026 .attach = fcgi_attach,
4027 .get_first_cs = fcgi_get_first_cs,
4028 .detach = fcgi_detach,
4029 .destroy = fcgi_destroy,
4030 .avail_streams = fcgi_avail_streams,
4031 .used_streams = fcgi_used_streams,
4032 .rcv_buf = fcgi_rcv_buf,
4033 .snd_buf = fcgi_snd_buf,
4034 .subscribe = fcgi_subscribe,
4035 .unsubscribe = fcgi_unsubscribe,
4036 .shutr = fcgi_shutr,
4037 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004038 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004039 .show_fd = fcgi_show_fd,
4040 .flags = MX_FL_HTX,
4041 .name = "FCGI",
4042};
4043
4044
4045/* this mux registers FCGI proto */
4046static struct mux_proto_list mux_proto_fcgi =
4047{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4048
4049INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4050
4051/*
4052 * Local variables:
4053 * c-indent-level: 8
4054 * c-basic-offset: 8
4055 * End:
4056 */