blob: 27f5edce0c30171d57875705ac2b3363733a8e3e [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);
611 __conn_xprt_stop_recv(fconn->conn);
612 }
613 return buf;
614}
615
616static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
617{
618 if (bptr->size) {
619 b_free(bptr);
620 offer_buffers(NULL, tasks_run_queue);
621 }
622}
623
624static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
625{
626 struct buffer *buf;
627 unsigned int count = 0;
628
629 while (b_size(buf = br_head_pick(fconn->mbuf))) {
630 b_free(buf);
631 count++;
632 }
633 if (count)
634 offer_buffers(NULL, tasks_run_queue);
635}
636
637/* Returns the number of allocatable outgoing streams for the connection taking
638 * the number reserved streams into account.
639 */
640static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
641{
642 int ret;
643
644 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
645 if (ret < 0)
646 ret = 0;
647 return ret;
648}
649
650/* Returns the number of streams in use on a connection to figure if it's
651 * idle or not. We check nb_cs and not nb_streams as the caller will want
652 * to know if it was the last one after a detach().
653 */
654static int fcgi_used_streams(struct connection *conn)
655{
656 struct fcgi_conn *fconn = conn->ctx;
657
658 return fconn->nb_cs;
659}
660
661/* Returns the number of concurrent streams available on the connection */
662static int fcgi_avail_streams(struct connection *conn)
663{
664 struct server *srv = objt_server(conn->target);
665 struct fcgi_conn *fconn = conn->ctx;
666 int ret1, ret2;
667
668 /* Don't open new stream if the connection is closed */
669 if (fconn->state == FCGI_CS_CLOSED)
670 return 0;
671
672 /* May be negative if this setting has changed */
673 ret1 = (fconn->streams_limit - fconn->nb_streams);
674
675 /* we must also consider the limit imposed by stream IDs */
676 ret2 = fcgi_streams_left(fconn);
677 ret1 = MIN(ret1, ret2);
678 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
679 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
680 ret1 = MIN(ret1, ret2);
681 }
682 return ret1;
683}
684
685/*****************************************************************/
686/* functions below are dedicated to the mux setup and management */
687/*****************************************************************/
688
689/* Initializes the mux once it's attached. Only outgoing connections are
690 * supported. So the context is already initialized before installing the
691 * mux. <input> is always used as Input buffer and may contain data. It is the
692 * caller responsibility to not reuse it anymore. Returns < 0 on error.
693 */
694static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
695 struct buffer *input)
696{
697 struct fcgi_conn *fconn;
698 struct fcgi_strm *fstrm;
699 struct fcgi_app *app = get_px_fcgi_app(px);
700 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200701 void *conn_ctx = conn->ctx;
702
703 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200704
705 if (!app)
706 goto fail_conn;
707
708 fconn = pool_alloc(pool_head_fcgi_conn);
709 if (!fconn)
710 goto fail_conn;
711
712 fconn->shut_timeout = fconn->timeout = px->timeout.server;
713 if (tick_isset(px->timeout.serverfin))
714 fconn->shut_timeout = px->timeout.serverfin;
715
716 fconn->flags = FCGI_CF_NONE;
717
718 /* Retrieve usefull info from the FCGI app */
719 if (app->flags & FCGI_APP_FL_KEEP_CONN)
720 fconn->flags |= FCGI_CF_KEEP_CONN;
721 if (app->flags & FCGI_APP_FL_GET_VALUES)
722 fconn->flags |= FCGI_CF_GET_VALUES;
723 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
724 fconn->flags |= FCGI_CF_MPXS_CONNS;
725
726 fconn->proxy = px;
727 fconn->app = app;
728 fconn->task = NULL;
729 if (tick_isset(fconn->timeout)) {
730 t = task_new(tid_bit);
731 if (!t)
732 goto fail;
733
734 fconn->task = t;
735 t->process = fcgi_timeout_task;
736 t->context = fconn;
737 t->expire = tick_add(now_ms, fconn->timeout);
738 }
739
740 fconn->wait_event.tasklet = tasklet_new();
741 if (!fconn->wait_event.tasklet)
742 goto fail;
743 fconn->wait_event.tasklet->process = fcgi_io_cb;
744 fconn->wait_event.tasklet->context = fconn;
745 fconn->wait_event.events = 0;
746
747 /* Initialise the context. */
748 fconn->state = FCGI_CS_INIT;
749 fconn->conn = conn;
750 fconn->streams_limit = app->maxreqs;
751 fconn->max_id = -1;
752 fconn->nb_streams = 0;
753 fconn->nb_cs = 0;
754 fconn->nb_reserved = 0;
755 fconn->stream_cnt = 0;
756
757 fconn->dbuf = *input;
758 fconn->dsi = -1;
759
760 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
761 fconn->streams_by_id = EB_ROOT;
762 LIST_INIT(&fconn->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200763 LIST_INIT(&fconn->buf_wait.list);
764
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200765 conn->ctx = fconn;
766
Christopher Faulet99eff652019-08-11 23:11:30 +0200767 if (t)
768 task_queue(t);
769
770 /* FIXME: this is temporary, for outgoing connections we need to
771 * immediately allocate a stream until the code is modified so that the
772 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200773 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200774 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200775 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200776 if (!fstrm)
777 goto fail;
778
Christopher Faulet99eff652019-08-11 23:11:30 +0200779
780 /* Repare to read something */
781 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200782 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200783 return 0;
784
785 fail:
786 task_destroy(t);
787 if (fconn->wait_event.tasklet)
788 tasklet_free(fconn->wait_event.tasklet);
789 pool_free(pool_head_fcgi_conn, fconn);
790 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200791 conn->ctx = conn_ctx; // restore saved ctx
792 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200793 return -1;
794}
795
796/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
797 * -1 if no more is allocatable.
798 */
799static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
800{
801 int32_t id = (fconn->max_id + 1) | 1;
802
803 if ((id & 0x80000000U))
804 id = -1;
805 return id;
806}
807
808/* Returns the stream associated with id <id> or NULL if not found */
809static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
810{
811 struct eb32_node *node;
812
813 if (id == 0)
814 return (struct fcgi_strm *)fcgi_mgmt_stream;
815
816 if (id > fconn->max_id)
817 return (struct fcgi_strm *)fcgi_unknown_stream;
818
819 node = eb32_lookup(&fconn->streams_by_id, id);
820 if (!node)
821 return (struct fcgi_strm *)fcgi_unknown_stream;
822 return container_of(node, struct fcgi_strm, by_id);
823}
824
825
826/* Release function. This one should be called to free all resources allocated
827 * to the mux.
828 */
829static void fcgi_release(struct fcgi_conn *fconn)
830{
831 struct connection *conn = NULL;;
832
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200833 TRACE_POINT(FCGI_EV_FCONN_END);
834
Christopher Faulet99eff652019-08-11 23:11:30 +0200835 if (fconn) {
836 /* The connection must be attached to this mux to be released */
837 if (fconn->conn && fconn->conn->ctx == fconn)
838 conn = fconn->conn;
839
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200840 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
841
Christopher Faulet99eff652019-08-11 23:11:30 +0200842 if (LIST_ADDED(&fconn->buf_wait.list)) {
843 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
844 LIST_DEL(&fconn->buf_wait.list);
845 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
846 }
847
848 fcgi_release_buf(fconn, &fconn->dbuf);
849 fcgi_release_mbuf(fconn);
850
851 if (fconn->task) {
852 fconn->task->context = NULL;
853 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
854 fconn->task = NULL;
855 }
856 if (fconn->wait_event.tasklet)
857 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200858 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200859 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
860 &fconn->wait_event);
861 }
862
863 if (conn) {
864 conn->mux = NULL;
865 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200866 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200867
868 conn_stop_tracking(conn);
869 conn_full_close(conn);
870 if (conn->destroy_cb)
871 conn->destroy_cb(conn);
872 conn_free(conn);
873 }
874}
875
876
877/* Retruns true if the FCGI connection must be release */
878static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
879{
880 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
881 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
882 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
883 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
884 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
885 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
886 conn_xprt_read0_pending(fconn->conn))))
887 return 1;
888 return 0;
889}
890
891
892/********************************************************/
893/* functions below are for the FCGI protocol processing */
894/********************************************************/
895
Christopher Faulet99eff652019-08-11 23:11:30 +0200896/* Marks an error on the stream. */
897static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
898{
899 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200900 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
901 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200902 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200903 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
904 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200905 if (fstrm->cs)
906 cs_set_error(fstrm->cs);
907 }
908}
909
910/* Attempts to notify the data layer of recv availability */
911static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
912{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100913 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200914 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100915 tasklet_wakeup(fstrm->subs->tasklet);
916 fstrm->subs->events &= ~SUB_RETRY_RECV;
917 if (!fstrm->subs->events)
918 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200919 }
920}
921
922/* Attempts to notify the data layer of send availability */
923static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
924{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100925 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200926 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100927 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100928 tasklet_wakeup(fstrm->subs->tasklet);
929 fstrm->subs->events &= ~SUB_RETRY_SEND;
930 if (!fstrm->subs->events)
931 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200932 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100933 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
934 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
935 tasklet_wakeup(fstrm->shut_tl);
936 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200937}
938
939/* Alerts the data layer, trying to wake it up by all means, following
940 * this sequence :
941 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
942 * for recv
943 * - if its subscribed to send, then it's woken up for send
944 * - if it was subscribed to neither, its ->wake() callback is called
945 * It is safe to call this function with a closed stream which doesn't have a
946 * conn_stream anymore.
947 */
948static void fcgi_strm_alert(struct fcgi_strm *fstrm)
949{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200950 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100951 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100952 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200953 fcgi_strm_notify_recv(fstrm);
954 fcgi_strm_notify_send(fstrm);
955 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200956 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
957 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200958 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200959 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200960}
961
962/* Writes the 16-bit record size <len> at address <record> */
963static inline void fcgi_set_record_size(void *record, uint16_t len)
964{
965 uint8_t *out = (record + 4);
966
967 *out = (len >> 8);
968 *(out + 1) = (len & 0xff);
969}
970
971/* Writes the 16-bit stream id <id> at address <record> */
972static inline void fcgi_set_record_id(void *record, uint16_t id)
973{
974 uint8_t *out = (record + 2);
975
976 *out = (id >> 8);
977 *(out + 1) = (id & 0xff);
978}
979
980/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
981 * its connection if the stream was not yet closed. Please use this exclusively
982 * before closing a stream to ensure stream count is well maintained.
983 */
984static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
985{
986 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200987 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200988 fstrm->fconn->nb_streams--;
989 if (!fstrm->id)
990 fstrm->fconn->nb_reserved--;
991 if (fstrm->cs) {
992 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
993 fcgi_strm_notify_recv(fstrm);
994 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200995 fstrm->state = FCGI_SS_CLOSED;
996 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
997 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200998 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200999}
1000
1001/* Detaches a FCGI stream from its FCGI connection and releases it to the
1002 * fcgi_strm pool.
1003 */
1004static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1005{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001006 struct connection *conn = fstrm->fconn->conn;
1007
1008 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1009
Christopher Faulet99eff652019-08-11 23:11:30 +02001010 fcgi_strm_close(fstrm);
1011 eb32_delete(&fstrm->by_id);
1012 if (b_size(&fstrm->rxbuf)) {
1013 b_free(&fstrm->rxbuf);
1014 offer_buffers(NULL, tasks_run_queue);
1015 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001016 if (fstrm->subs)
1017 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001018 /* There's no need to explicitly call unsubscribe here, the only
1019 * reference left would be in the fconn send_list/fctl_list, and if
1020 * we're in it, we're getting out anyway
1021 */
1022 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001023 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001024 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001025
1026 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001027}
1028
1029/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1030 * stream tree. In case of error, nothing is added and NULL is returned. The
1031 * causes of errors can be any failed memory allocation. The caller is
1032 * responsible for checking if the connection may support an extra stream prior
1033 * to calling this function.
1034 */
1035static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1036{
1037 struct fcgi_strm *fstrm;
1038
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001039 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1040
Christopher Faulet99eff652019-08-11 23:11:30 +02001041 fstrm = pool_alloc(pool_head_fcgi_strm);
1042 if (!fstrm)
1043 goto out;
1044
Willy Tarreau7aad7032020-01-16 17:20:57 +01001045 fstrm->shut_tl = tasklet_new();
1046 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001047 pool_free(pool_head_fcgi_strm, fstrm);
1048 goto out;
1049 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001050 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001051 fstrm->shut_tl->process = fcgi_deferred_shut;
1052 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001053 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001054 fstrm->fconn = fconn;
1055 fstrm->cs = NULL;
1056 fstrm->flags = FCGI_SF_NONE;
1057 fstrm->proto_status = 0;
1058 fstrm->state = FCGI_SS_IDLE;
1059 fstrm->rxbuf = BUF_NULL;
1060
1061 h1m_init_res(&fstrm->h1m);
1062 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1063 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1064
1065 fstrm->by_id.key = fstrm->id = id;
1066 if (id > 0)
1067 fconn->max_id = id;
1068 else
1069 fconn->nb_reserved++;
1070
1071 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1072 fconn->nb_streams++;
1073 fconn->stream_cnt++;
1074
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001075 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001076 return fstrm;
1077
1078 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001079 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 +02001080 return NULL;
1081}
1082
1083/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1084 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1085 * highest possible stream ID was reached.
1086 */
1087static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1088 struct session *sess)
1089{
1090 struct fcgi_strm *fstrm = NULL;
1091
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001092 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1093 if (fconn->nb_streams >= fconn->streams_limit) {
1094 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 +02001095 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001096 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001097
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001098 if (fcgi_streams_left(fconn) < 1) {
1099 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 +02001100 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001101 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001102
1103 /* Defer choosing the ID until we send the first message to create the stream */
1104 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001105 if (!fstrm) {
1106 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 +02001107 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001108 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001109
1110 fstrm->cs = cs;
1111 fstrm->sess = sess;
1112 cs->ctx = fstrm;
1113 fconn->nb_cs++;
1114
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001116 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001117
1118 out:
1119 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001120}
1121
1122/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1123 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1124 * automatically updated accordingly. If the stream is orphaned, it is
1125 * destroyed.
1126 */
1127static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1128{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001129 struct fcgi_conn *fconn = fstrm->fconn;
1130
1131 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1132
Christopher Faulet99eff652019-08-11 23:11:30 +02001133 if (!fstrm->cs) {
1134 /* this stream was already orphaned */
1135 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001136 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001137 return;
1138 }
1139
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001140 if (conn_xprt_read0_pending(fconn->conn)) {
1141 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001142 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001143 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1144 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001145 else if (fstrm->state == FCGI_SS_HLOC)
1146 fcgi_strm_close(fstrm);
1147 }
1148
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001150 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1151 if (fstrm->cs->flags & CS_FL_EOS)
1152 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001153
1154 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001155 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001156 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1157 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001158 }
1159
1160 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001161
1162 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001163}
1164
1165/* Wakes unassigned streams (ID == 0) attached to the connection. */
1166static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1167{
1168 struct eb32_node *node;
1169 struct fcgi_strm *fstrm;
1170
1171 node = eb32_lookup(&fconn->streams_by_id, 0);
1172 while (node) {
1173 fstrm = container_of(node, struct fcgi_strm, by_id);
1174 if (fstrm->id > 0)
1175 break;
1176 node = eb32_next(node);
1177 fcgi_strm_wake_one_stream(fstrm);
1178 }
1179}
1180
1181/* Wakes the streams attached to the connection, whose id is greater than <last>
1182 * or unassigned.
1183 */
1184static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1185{
1186 struct eb32_node *node;
1187 struct fcgi_strm *fstrm;
1188
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001189 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1190
Christopher Faulet99eff652019-08-11 23:11:30 +02001191 /* Wake all streams with ID > last */
1192 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1193 while (node) {
1194 fstrm = container_of(node, struct fcgi_strm, by_id);
1195 node = eb32_next(node);
1196 fcgi_strm_wake_one_stream(fstrm);
1197 }
1198 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001199
1200 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001201}
1202
1203static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1204 struct htx *htx, struct htx_sl *sl,
1205 struct fcgi_strm_params *params)
1206{
1207 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1208 struct ist p;
1209
1210 if (!sl)
1211 goto error;
1212
1213 if (!(params->mask & FCGI_SP_DOC_ROOT))
1214 params->docroot = fconn->app->docroot;
1215
1216 if (!(params->mask & FCGI_SP_REQ_METH)) {
1217 p = htx_sl_req_meth(sl);
1218 params->meth = ist2(b_tail(params->p), p.len);
1219 chunk_memcat(params->p, p.ptr, p.len);
1220 }
1221 if (!(params->mask & FCGI_SP_REQ_URI)) {
1222 p = htx_sl_req_uri(sl);
1223 params->uri = ist2(b_tail(params->p), p.len);
1224 chunk_memcat(params->p, p.ptr, p.len);
1225 }
1226 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1227 p = htx_sl_req_vsn(sl);
1228 params->vsn = ist2(b_tail(params->p), p.len);
1229 chunk_memcat(params->p, p.ptr, p.len);
1230 }
1231 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1232 char *end;
1233 int port = 0;
1234 if (conn_get_dst(cli_conn))
1235 port = get_host_port(cli_conn->dst);
1236 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1237 if (!end)
1238 goto error;
1239 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1240 params->p->data += params->srv_port.len;
1241 }
1242 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1243 /* If no Host header found, use the server address to fill
1244 * srv_name */
1245 if (!istlen(params->srv_name)) {
1246 char *ptr = NULL;
1247
1248 if (conn_get_dst(cli_conn))
1249 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1250 ptr = b_tail(params->p);
1251 if (ptr) {
1252 params->srv_name = ist2(ptr, strlen(ptr));
1253 params->p->data += params->srv_name.len;
1254 }
1255 }
1256 }
1257 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1258 char *ptr = NULL;
1259
1260 if (conn_get_src(cli_conn))
1261 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1262 ptr = b_tail(params->p);
1263 if (ptr) {
1264 params->rem_addr = ist2(ptr, strlen(ptr));
1265 params->p->data += params->rem_addr.len;
1266 }
1267 }
1268 if (!(params->mask & FCGI_SP_REM_PORT)) {
1269 char *end;
1270 int port = 0;
1271 if (conn_get_src(cli_conn))
1272 port = get_host_port(cli_conn->src);
1273 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1274 if (!end)
1275 goto error;
1276 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1277 params->p->data += params->rem_port.len;
1278 }
1279 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1280 struct htx_blk *blk;
1281 enum htx_blk_type type;
1282 char *end;
1283 size_t len = 0;
1284
1285 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1286 type = htx_get_blk_type(blk);
1287
1288 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1289 break;
1290 if (type == HTX_BLK_DATA)
1291 len += htx_get_blksz(blk);
1292 }
1293 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1294 if (!end)
1295 goto error;
1296 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1297 params->p->data += params->cont_len.len;
1298 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001299#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001300 if (!(params->mask & FCGI_SP_HTTPS)) {
1301 params->https = ssl_sock_is_ssl(cli_conn);
1302 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001303#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001304 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1305 /* one of scriptname, pathinfo or query_string is no set */
1306 struct ist path = http_get_path(params->uri);
1307 int len;
1308
1309 /* Decode the path. it must first be copied to keep the URI
1310 * untouched.
1311 */
1312 chunk_memcat(params->p, path.ptr, path.len);
1313 path.ptr = b_tail(params->p) - path.len;
1314 path.ptr[path.len] = '\0';
1315 len = url_decode(path.ptr);
1316 if (len < 0)
1317 goto error;
1318 path.len = len;
1319
1320 /* No scrit_name set but no valid path ==> error */
1321 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1322 goto error;
1323
1324 /* Find limit between the path and the query-string */
1325 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1326
1327 /* If there is a query-string, Set it if not already set */
1328 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1329 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1330
1331 /* If the script_name is set, don't try to deduce the path_info
1332 * too. The opposite is not true.
1333 */
1334 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1335 params->mask |= FCGI_SP_PATH_INFO;
1336 goto end;
1337 }
1338
1339 /* script_name not set, preset it with the path for now */
1340 params->scriptname = ist2(path.ptr, len);
1341
1342 /* If there is no regex to match the pathinfo, just to the last
1343 * part and see if the index must be used.
1344 */
1345 if (!fconn->app->pathinfo_re)
1346 goto check_index;
1347
Christopher Faulet28cb3662020-02-14 14:47:37 +01001348 /* If some special characters are found in the decoded path (\n
1349 * or \0), the PATH_INFO regex cannot match. This is theorically
1350 * valid, but probably unexpected, to have such characters. So,
1351 * to avoid any suprises, an error is triggered in this
1352 * case.
1353 */
1354 if (istchr(path, '\n') || istchr(path, '\0'))
1355 goto error;
1356
Christopher Faulet99eff652019-08-11 23:11:30 +02001357 /* The regex does not match, just to the last part and see if
1358 * the index must be used.
1359 */
1360 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1361 goto check_index;
1362
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001363 /* We must have at least 1 capture for the script name,
1364 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001365 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001366 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001367 goto check_index;
1368
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001369 /* Finally we can set the script_name and the path_info. The
1370 * path_info is set if not already defined, and if it was
1371 * captured
1372 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001373 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001374 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1375 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001376
1377 check_index:
1378 len = params->scriptname.len;
1379 /* the script_name if finished by a '/' so we can add the index
1380 * part, if any.
1381 */
1382 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1383 struct ist sn = params->scriptname;
1384
1385 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1386 chunk_memcat(params->p, sn.ptr, sn.len);
1387 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1388 }
1389 }
1390
1391 end:
1392 return 1;
1393 error:
1394 return 0;
1395}
1396
1397static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1398 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1399{
1400 struct fcgi_param p;
1401
1402 if (params->mask & flag)
1403 return 1;
1404
1405 chunk_reset(&trash);
1406
1407 switch (flag) {
1408 case FCGI_SP_CGI_GATEWAY:
1409 p.n = ist("GATEWAY_INTERFACE");
1410 p.v = ist("CGI/1.1");
1411 goto encode;
1412 case FCGI_SP_DOC_ROOT:
1413 p.n = ist("DOCUMENT_ROOT");
1414 p.v = params->docroot;
1415 goto encode;
1416 case FCGI_SP_SCRIPT_NAME:
1417 p.n = ist("SCRIPT_NAME");
1418 p.v = params->scriptname;
1419 goto encode;
1420 case FCGI_SP_PATH_INFO:
1421 p.n = ist("PATH_INFO");
1422 p.v = params->pathinfo;
1423 goto encode;
1424 case FCGI_SP_REQ_URI:
1425 p.n = ist("REQUEST_URI");
1426 p.v = params->uri;
1427 goto encode;
1428 case FCGI_SP_REQ_METH:
1429 p.n = ist("REQUEST_METHOD");
1430 p.v = params->meth;
1431 goto encode;
1432 case FCGI_SP_REQ_QS:
1433 p.n = ist("QUERY_STRING");
1434 p.v = params->qs;
1435 goto encode;
1436 case FCGI_SP_SRV_NAME:
1437 p.n = ist("SERVER_NAME");
1438 p.v = params->srv_name;
1439 goto encode;
1440 case FCGI_SP_SRV_PORT:
1441 p.n = ist("SERVER_PORT");
1442 p.v = params->srv_port;
1443 goto encode;
1444 case FCGI_SP_SRV_PROTO:
1445 p.n = ist("SERVER_PROTOCOL");
1446 p.v = params->vsn;
1447 goto encode;
1448 case FCGI_SP_REM_ADDR:
1449 p.n = ist("REMOTE_ADDR");
1450 p.v = params->rem_addr;
1451 goto encode;
1452 case FCGI_SP_REM_PORT:
1453 p.n = ist("REMOTE_PORT");
1454 p.v = params->rem_port;
1455 goto encode;
1456 case FCGI_SP_SCRIPT_FILE:
1457 p.n = ist("SCRIPT_FILENAME");
1458 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1459 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1460 p.v = ist2(b_head(&trash), b_data(&trash));
1461 goto encode;
1462 case FCGI_SP_PATH_TRANS:
1463 if (!istlen(params->pathinfo))
1464 goto skip;
1465 p.n = ist("PATH_TRANSLATED");
1466 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1467 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1468 p.v = ist2(b_head(&trash), b_data(&trash));
1469 goto encode;
1470 case FCGI_SP_CONT_LEN:
1471 p.n = ist("CONTENT_LENGTH");
1472 p.v = params->cont_len;
1473 goto encode;
1474 case FCGI_SP_HTTPS:
1475 if (!params->https)
1476 goto skip;
1477 p.n = ist("HTTPS");
1478 p.v = ist("on");
1479 goto encode;
1480 default:
1481 goto skip;
1482 }
1483
1484 encode:
1485 if (!istlen(p.v))
1486 goto skip;
1487 if (!fcgi_encode_param(outbuf, &p))
1488 return 0;
1489 skip:
1490 params->mask |= flag;
1491 return 1;
1492}
1493
1494/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1495 * anything. It is highly unexpected, but if the record is larger than a buffer
1496 * and cannot be encoded in one time, an error is triggered and the connection is
1497 * closed. GET_VALUES record cannot be split.
1498 */
1499static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1500{
1501 struct buffer outbuf;
1502 struct buffer *mbuf;
1503 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1504 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001505 int ret = 0;
1506
1507 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001508
1509 mbuf = br_tail(fconn->mbuf);
1510 retry:
1511 if (!fcgi_get_buf(fconn, mbuf)) {
1512 fconn->flags |= FCGI_CF_MUX_MALLOC;
1513 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001514 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1515 ret = 0;
1516 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001517 }
1518
1519 while (1) {
1520 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1521 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1522 break;
1523 realign_again:
1524 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1525 }
1526
1527 if (outbuf.size < 8)
1528 goto full;
1529
1530 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1531 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1532 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1533 outbuf.data = 8;
1534
1535 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1536 * handled by HAProxy.
1537 */
1538 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1539 goto full;
1540
1541 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001542 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 +02001543 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1544 b_add(mbuf, outbuf.data);
1545 ret = 1;
1546
1547 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001548 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001549 return ret;
1550 full:
1551 /* Too large to be encoded. For GET_VALUES records, it is an error */
1552 if (!b_data(mbuf))
1553 goto fail;
1554
1555 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1556 goto retry;
1557 fconn->flags |= FCGI_CF_MUX_MFULL;
1558 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001559 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001560 ret = 0;
1561 goto end;
1562 fail:
1563 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001564 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1565 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1566 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001567}
1568
1569/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1570 * couldn't do anything. It is highly unexpected, but if the record is larger
1571 * than a buffer and cannot be decoded in one time, an error is triggered and
1572 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1573 */
1574static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1575{
1576 struct buffer inbuf;
1577 struct buffer *dbuf;
1578 size_t offset;
1579
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001580 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1581
Christopher Faulet99eff652019-08-11 23:11:30 +02001582 dbuf = &fconn->dbuf;
1583
1584 /* Record too large to be fully decoded */
1585 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1586 goto fail;
1587
1588 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001589 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1590 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001591 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001592 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001593
1594 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1595 /* Realign the dmux buffer if the record wraps. It is unexpected
1596 * at this stage because it should be the first record received
1597 * from the FCGI application.
1598 */
1599 b_slow_realign(dbuf, trash.area, 0);
1600 }
1601
1602 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1603
1604 for (offset = 0; offset < b_data(&inbuf); ) {
1605 struct fcgi_param p;
1606 size_t ret;
1607
1608 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1609 if (!ret) {
1610 /* name or value too large to be decoded at once */
1611 goto fail;
1612 }
1613 offset += ret;
1614
1615 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001616 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001617 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 +02001618 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001619 }
1620 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001621 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 +02001622 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001623 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001624 }
1625 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1626 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001627 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 +02001628 }
1629 /*
1630 * Ignore all other params
1631 */
1632 }
1633
1634 /* Reset the number of concurrent streams supported if the FCGI
1635 * application does not support connection multiplexing
1636 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001637 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001638 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001639 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1640 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001641
1642 /* We must be sure to have read exactly the announced record length, no
1643 * more no less
1644 */
1645 if (offset != fconn->drl)
1646 goto fail;
1647
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001648 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 +02001649 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1650 fconn->drl = 0;
1651 fconn->drp = 0;
1652 fconn->state = FCGI_CS_RECORD_H;
1653 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001654 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1655 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001656 return 1;
1657 fail:
1658 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001659 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1660 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 +02001661 return 0;
1662}
1663
1664/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1665 * excluded, as the streams which already received the end-of-stream. It returns
1666 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1667 */
1668static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1669{
1670 struct eb32_node *node;
1671 struct fcgi_strm *fstrm;
1672
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001673 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1674
Christopher Faulet99eff652019-08-11 23:11:30 +02001675 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1676 while (node) {
1677 fstrm = container_of(node, struct fcgi_strm, by_id);
1678 node = eb32_next(node);
1679 if (fstrm->state != FCGI_SS_CLOSED &&
1680 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1681 !fcgi_strm_send_abort(fconn, fstrm))
1682 return 0;
1683 }
1684 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001685 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1686 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001687 return 1;
1688}
1689
1690/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1691 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1692 * space to proceed. It is small enough to be encoded in an empty buffer.
1693 */
1694static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1695{
1696 struct buffer outbuf;
1697 struct buffer *mbuf;
1698 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1699 int ret;
1700
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001701 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1702
Christopher Faulet99eff652019-08-11 23:11:30 +02001703 mbuf = br_tail(fconn->mbuf);
1704 retry:
1705 if (!fcgi_get_buf(fconn, mbuf)) {
1706 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001707 fstrm->flags |= FCGI_SF_BLK_MROOM;
1708 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1709 ret = 0;
1710 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001711 }
1712
1713 while (1) {
1714 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1715 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1716 break;
1717 realign_again:
1718 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1719 }
1720
1721 if (outbuf.size < 8)
1722 goto full;
1723
1724 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1725 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1726 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1727 fcgi_set_record_id(outbuf.area, fstrm->id);
1728 outbuf.data = 8;
1729
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001730 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1731 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001732 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001733 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001734 if (!fcgi_encode_begin_request(&outbuf, &rec))
1735 goto full;
1736
1737 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001738 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 +02001739 b_add(mbuf, outbuf.data);
1740 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1741 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001742 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001743 ret = 1;
1744
1745 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001746 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001747 return ret;
1748 full:
1749 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1750 goto retry;
1751 fconn->flags |= FCGI_CF_MUX_MFULL;
1752 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001753 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 +02001754 ret = 0;
1755 goto end;
1756}
1757
1758/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1759 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1760 * space to proceed. It is small enough to be encoded in an empty buffer.
1761 */
1762static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1763 enum fcgi_record_type rtype)
1764{
1765 struct buffer outbuf;
1766 struct buffer *mbuf;
1767 int ret;
1768
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001769 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001770 mbuf = br_tail(fconn->mbuf);
1771 retry:
1772 if (!fcgi_get_buf(fconn, mbuf)) {
1773 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001774 fstrm->flags |= FCGI_SF_BLK_MROOM;
1775 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1776 ret = 0;
1777 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001778 }
1779
1780 while (1) {
1781 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1782 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1783 break;
1784 realign_again:
1785 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1786 }
1787
1788 if (outbuf.size < 8)
1789 goto full;
1790
1791 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1792 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1793 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1794 outbuf.area[1] = rtype;
1795 fcgi_set_record_id(outbuf.area, fstrm->id);
1796 outbuf.data = 8;
1797
1798 /* commit the record */
1799 b_add(mbuf, outbuf.data);
1800 ret = 1;
1801
1802 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001803 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001804 return ret;
1805 full:
1806 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1807 goto retry;
1808 fconn->flags |= FCGI_CF_MUX_MFULL;
1809 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001810 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 +02001811 ret = 0;
1812 goto end;
1813}
1814
1815
1816/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1817 * marks the end of params.
1818 */
1819static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1820{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001821 int ret;
1822
1823 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1824 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1825 if (ret)
1826 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1827 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001828}
1829
1830/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1831 * marks the end of input. On success, all the request was successfully sent.
1832 */
1833static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1834{
1835 int ret;
1836
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001837 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001838 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001839 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001840 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001841 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1842 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1843 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1844 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001845 return ret;
1846}
1847
1848/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1849 * stops the request processing.
1850 */
1851static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1852{
1853 int ret;
1854
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001855 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001856 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001857 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001858 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001859 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1860 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1861 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1862 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001863 return ret;
1864}
1865
1866/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1867 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1868 * several records are sent. However, a K/V param cannot be split between 2
1869 * records.
1870 */
1871static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1872 struct htx *htx)
1873{
1874 struct buffer outbuf;
1875 struct buffer *mbuf;
1876 struct htx_blk *blk;
1877 struct htx_sl *sl = NULL;
1878 struct fcgi_strm_params params;
1879 size_t total = 0;
1880
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001881 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1882
Christopher Faulet99eff652019-08-11 23:11:30 +02001883 memset(&params, 0, sizeof(params));
1884 params.p = get_trash_chunk();
1885
1886 mbuf = br_tail(fconn->mbuf);
1887 retry:
1888 if (!fcgi_get_buf(fconn, mbuf)) {
1889 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001890 fstrm->flags |= FCGI_SF_BLK_MROOM;
1891 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1892 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001893 }
1894
1895 while (1) {
1896 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1897 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1898 break;
1899 realign_again:
1900 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1901 }
1902
1903 if (outbuf.size < 8)
1904 goto full;
1905
1906 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1907 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1908 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1909 fcgi_set_record_id(outbuf.area, fstrm->id);
1910 outbuf.data = 8;
1911
1912 blk = htx_get_head_blk(htx);
1913 while (blk) {
1914 enum htx_blk_type type;
1915 uint32_t size = htx_get_blksz(blk);
1916 struct fcgi_param p;
1917
1918 type = htx_get_blk_type(blk);
1919 switch (type) {
1920 case HTX_BLK_REQ_SL:
1921 sl = htx_get_blk_ptr(htx, blk);
1922 if (sl->info.req.meth == HTTP_METH_HEAD)
1923 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1924 if (sl->flags & HTX_SL_F_VER_11)
1925 fstrm->h1m.flags |= H1_MF_VER_11;
1926 break;
1927
1928 case HTX_BLK_HDR:
1929 p.n = htx_get_blk_name(htx, blk);
1930 p.v = htx_get_blk_value(htx, blk);
1931
1932 if (istmatch(p.n, ist(":fcgi-"))) {
1933 p.n.ptr += 6;
1934 p.n.len -= 6;
1935 if (isteq(p.n, ist("gateway_interface")))
1936 params.mask |= FCGI_SP_CGI_GATEWAY;
1937 else if (isteq(p.n, ist("document_root"))) {
1938 params.mask |= FCGI_SP_DOC_ROOT;
1939 params.docroot = p.v;
1940 }
1941 else if (isteq(p.n, ist("script_name"))) {
1942 params.mask |= FCGI_SP_SCRIPT_NAME;
1943 params.scriptname = p.v;
1944 }
1945 else if (isteq(p.n, ist("path_info"))) {
1946 params.mask |= FCGI_SP_PATH_INFO;
1947 params.pathinfo = p.v;
1948 }
1949 else if (isteq(p.n, ist("request_uri"))) {
1950 params.mask |= FCGI_SP_REQ_URI;
1951 params.uri = p.v;
1952 }
1953 else if (isteq(p.n, ist("request_meth")))
1954 params.mask |= FCGI_SP_REQ_METH;
1955 else if (isteq(p.n, ist("query_string")))
1956 params.mask |= FCGI_SP_REQ_QS;
1957 else if (isteq(p.n, ist("server_name")))
1958 params.mask |= FCGI_SP_SRV_NAME;
1959 else if (isteq(p.n, ist("server_port")))
1960 params.mask |= FCGI_SP_SRV_PORT;
1961 else if (isteq(p.n, ist("server_protocol")))
1962 params.mask |= FCGI_SP_SRV_PROTO;
1963 else if (isteq(p.n, ist("remote_addr")))
1964 params.mask |= FCGI_SP_REM_ADDR;
1965 else if (isteq(p.n, ist("remote_port")))
1966 params.mask |= FCGI_SP_REM_PORT;
1967 else if (isteq(p.n, ist("script_filename")))
1968 params.mask |= FCGI_SP_SCRIPT_FILE;
1969 else if (isteq(p.n, ist("path_translated")))
1970 params.mask |= FCGI_SP_PATH_TRANS;
1971 else if (isteq(p.n, ist("https")))
1972 params.mask |= FCGI_SP_HTTPS;
1973 }
1974 else if (isteq(p.n, ist("content-length"))) {
1975 p.n = ist("CONTENT_LENGTH");
1976 params.mask |= FCGI_SP_CONT_LEN;
1977 }
1978 else if (isteq(p.n, ist("content-type")))
1979 p.n = ist("CONTENT_TYPE");
1980 else {
1981 if (isteq(p.n, ist("host")))
1982 params.srv_name = p.v;
1983
Christopher Faulet67d58092019-10-02 10:51:38 +02001984 /* Skip header if same name is used to add the server name */
1985 if (fconn->proxy->server_id_hdr_name &&
1986 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1987 break;
1988
Christopher Faulet99eff652019-08-11 23:11:30 +02001989 memcpy(trash.area, "http_", 5);
1990 memcpy(trash.area+5, p.n.ptr, p.n.len);
1991 p.n = ist2(trash.area, p.n.len+5);
1992 }
1993
1994 if (!fcgi_encode_param(&outbuf, &p)) {
1995 if (b_space_wraps(mbuf))
1996 goto realign_again;
1997 if (outbuf.data == 8)
1998 goto full;
1999 goto done;
2000 }
2001 break;
2002
2003 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002004 if (fconn->proxy->server_id_hdr_name) {
2005 struct server *srv = objt_server(fconn->conn->target);
2006
2007 if (!srv)
2008 goto done;
2009
2010 memcpy(trash.area, "http_", 5);
2011 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2012 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2013 p.v = ist(srv->id);
2014
2015 if (!fcgi_encode_param(&outbuf, &p)) {
2016 if (b_space_wraps(mbuf))
2017 goto realign_again;
2018 if (outbuf.data == 8)
2019 goto full;
2020 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002021 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002022 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002023 goto done;
2024
2025 default:
2026 break;
2027 }
2028 total += size;
2029 blk = htx_remove_blk(htx, blk);
2030 }
2031
2032 done:
2033 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2034 goto error;
2035
2036 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2047 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2048 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2049 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2051 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2052 goto error;
2053
2054 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002055 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 +02002056 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2057 b_add(mbuf, outbuf.data);
2058
2059 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002060 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002061 return total;
2062 full:
2063 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2064 goto retry;
2065 fconn->flags |= FCGI_CF_MUX_MFULL;
2066 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002067 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 +02002068 if (total)
2069 goto error;
2070 goto end;
2071
2072 error:
2073 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002074 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002075 fcgi_strm_error(fstrm);
2076 goto end;
2077}
2078
2079/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2080 * anything. STDIN records contain the request body.
2081 */
2082static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2083 struct htx *htx, size_t count, struct buffer *buf)
2084{
2085 struct buffer outbuf;
2086 struct buffer *mbuf;
2087 struct htx_blk *blk;
2088 enum htx_blk_type type;
2089 uint32_t size;
2090 size_t total = 0;
2091
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002092 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002093 if (!count)
2094 goto end;
2095
2096 mbuf = br_tail(fconn->mbuf);
2097 retry:
2098 if (!fcgi_get_buf(fconn, mbuf)) {
2099 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002100 fstrm->flags |= FCGI_SF_BLK_MROOM;
2101 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2102 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002103 }
2104
2105 /* Perform some optimizations to reduce the number of buffer copies.
2106 * First, if the mux's buffer is empty and the htx area contains exactly
2107 * one data block of the same size as the requested count, and this
2108 * count fits within the record size, then it's possible to simply swap
2109 * the caller's buffer with the mux's output buffer and adjust offsets
2110 * and length to match the entire DATA HTX block in the middle. In this
2111 * case we perform a true zero-copy operation from end-to-end. This is
2112 * the situation that happens all the time with large files. Second, if
2113 * this is not possible, but the mux's output buffer is empty, we still
2114 * have an opportunity to avoid the copy to the intermediary buffer, by
2115 * making the intermediary buffer's area point to the output buffer's
2116 * area. In this case we want to skip the HTX header to make sure that
2117 * copies remain aligned and that this operation remains possible all
2118 * the time. This goes for headers, data blocks and any data extracted
2119 * from the HTX blocks.
2120 */
2121 blk = htx_get_head_blk(htx);
2122 if (!blk)
2123 goto end;
2124 type = htx_get_blk_type(blk);
2125 size = htx_get_blksz(blk);
2126 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2127 void *old_area = mbuf->area;
2128
2129 if (b_data(mbuf)) {
2130 /* Too bad there are data left there. We're willing to memcpy/memmove
2131 * up to 1/4 of the buffer, which means that it's OK to copy a large
2132 * record into a buffer containing few data if it needs to be realigned,
2133 * and that it's also OK to copy few data without realigning. Otherwise
2134 * we'll pretend the mbuf is full and wait for it to become empty.
2135 */
2136 if (size + 8 <= b_room(mbuf) &&
2137 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2138 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2139 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002140 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002141 }
2142
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002143 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 +02002144 /* map a FCGI record to the HTX block so that we can put the
2145 * record header there.
2146 */
2147 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2148 outbuf.area = b_head(mbuf);
2149
2150 /* prepend a FCGI record header just before the DATA block */
2151 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2152 fcgi_set_record_id(outbuf.area, fstrm->id);
2153 fcgi_set_record_size(outbuf.area, size);
2154
2155 /* and exchange with our old area */
2156 buf->area = old_area;
2157 buf->data = buf->head = 0;
2158 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002159
2160 htx = (struct htx *)buf->area;
2161 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002162 goto end;
2163 }
2164
2165 copy:
2166 while (1) {
2167 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2168 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2169 break;
2170 realign_again:
2171 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2172 }
2173
2174 if (outbuf.size < 8)
2175 goto full;
2176
2177 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2178 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2179 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2180 fcgi_set_record_id(outbuf.area, fstrm->id);
2181 outbuf.data = 8;
2182
2183 blk = htx_get_head_blk(htx);
2184 while (blk && count) {
2185 enum htx_blk_type type = htx_get_blk_type(blk);
2186 uint32_t size = htx_get_blksz(blk);
2187 struct ist v;
2188
2189 switch (type) {
2190 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002191 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 +02002192 v = htx_get_blk_value(htx, blk);
2193 if (v.len > count)
2194 v.len = count;
2195
2196 if (v.len > b_room(&outbuf)) {
2197 /* It doesn't fit at once. If it at least fits once split and
2198 * the amount of data to move is low, let's defragment the
2199 * buffer now.
2200 */
2201 if (b_space_wraps(mbuf) &&
2202 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2203 b_data(mbuf) <= MAX_DATA_REALIGN)
2204 goto realign_again;
2205 v.len = b_room(&outbuf);
2206 }
2207 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2208 if (outbuf.data == 8)
2209 goto full;
2210 goto done;
2211 }
2212 if (v.len != size) {
2213 total += v.len;
2214 count -= v.len;
2215 htx_cut_data_blk(htx, blk, v.len);
2216 goto done;
2217 }
2218 break;
2219
2220 case HTX_BLK_EOM:
2221 goto done;
2222
2223 default:
2224 break;
2225 }
2226 total += size;
2227 count -= size;
2228 blk = htx_remove_blk(htx, blk);
2229 }
2230
2231 done:
2232 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 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 +02002234 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2235 b_add(mbuf, outbuf.data);
2236
2237 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002238 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002239 return total;
2240 full:
2241 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2242 goto retry;
2243 fconn->flags |= FCGI_CF_MUX_MFULL;
2244 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002245 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 +02002246 goto end;
2247}
2248
2249/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2250 * anything. STDOUT records contain the entire response. All the content is
2251 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2252 */
2253static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2254{
2255 struct buffer *dbuf;
2256 size_t ret;
2257 size_t max;
2258
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002259 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2260
Christopher Faulet99eff652019-08-11 23:11:30 +02002261 dbuf = &fconn->dbuf;
2262
2263 /* Only padding remains */
2264 if (fconn->state == FCGI_CS_RECORD_P)
2265 goto end_transfer;
2266
2267 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2268 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2269 buf_room_for_htx_data(dbuf))
2270 goto fail; // incomplete record
2271
2272 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2273 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002274 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2275 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002276 }
2277
2278 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2279 max = buf_room_for_htx_data(&fstrm->rxbuf);
2280 if (!b_data(&fstrm->rxbuf))
2281 fstrm->rxbuf.head = sizeof(struct htx);
2282 if (max > fconn->drl)
2283 max = fconn->drl;
2284
2285 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2286 if (!ret)
2287 goto fail;
2288 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002289 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2290 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 +02002291
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002292 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002293 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002294 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2295 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002296
2297 if (fconn->drl)
2298 goto fail;
2299
2300 end_transfer:
2301 fconn->drl += fconn->drp;
2302 fconn->drp = 0;
2303 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2304 b_del(&fconn->dbuf, ret);
2305 fconn->drl -= ret;
2306 if (fconn->drl)
2307 goto fail;
2308
2309 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002310 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2311 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002312 return 1;
2313 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002314 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 +02002315 return 0;
2316}
2317
2318
2319/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2320 * anything. It only skip the padding in fact, there is no payload for such
2321 * records. It makrs the end of the response.
2322 */
2323static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2324{
2325 int ret;
2326
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002327 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2328
Christopher Faulet99eff652019-08-11 23:11:30 +02002329 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002331 fconn->drl += fconn->drp;
2332 fconn->drp = 0;
2333 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2334 b_del(&fconn->dbuf, ret);
2335 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002336 if (fconn->drl) {
2337 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 +02002338 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002339 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002340 fconn->state = FCGI_CS_RECORD_H;
2341 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002342 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2343 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);
2344 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002345 return 1;
2346}
2347
2348/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2349 * anything.
2350 */
2351static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2352{
2353 struct buffer *dbuf;
2354 struct buffer tag;
2355 size_t ret;
2356
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002357 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002358 dbuf = &fconn->dbuf;
2359
2360 /* Only padding remains */
2361 if (fconn->state == FCGI_CS_RECORD_P)
2362 goto end_transfer;
2363
2364 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2365 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2366 buf_room_for_htx_data(dbuf))
2367 goto fail; // incomplete record
2368
2369 chunk_reset(&trash);
2370 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2371 if (!ret)
2372 goto fail;
2373 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002374 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 +02002375
2376 trash.area[ret] = '\n';
2377 trash.area[ret+1] = '\0';
2378 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002379 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002380
2381 if (fconn->drl)
2382 goto fail;
2383
2384 end_transfer:
2385 fconn->drl += fconn->drp;
2386 fconn->drp = 0;
2387 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2388 b_del(&fconn->dbuf, ret);
2389 fconn->drl -= ret;
2390 if (fconn->drl)
2391 goto fail;
2392 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002393 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2394 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002395 return 1;
2396 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002397 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 +02002398 return 0;
2399}
2400
2401/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2402 * anything. If the empty STDOUT record is not already received, this one marks
2403 * the end of the response. It is highly unexpected, but if the record is larger
2404 * than a buffer and cannot be decoded in one time, an error is triggered and
2405 * the connection is closed. END_REQUEST record cannot be split.
2406 */
2407static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2408{
2409 struct buffer inbuf;
2410 struct buffer *dbuf;
2411 struct fcgi_end_request endreq;
2412
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002413 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002414 dbuf = &fconn->dbuf;
2415
2416 /* Record too large to be fully decoded */
2417 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2418 goto fail;
2419
2420 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002421 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2422 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002423 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002424 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002425
2426 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2427 /* Realign the dmux buffer if the record wraps. It is unexpected
2428 * at this stage because it should be the first record received
2429 * from the FCGI application.
2430 */
2431 b_slow_realign(dbuf, trash.area, 0);
2432 }
2433
2434 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2435
2436 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2437 goto fail;
2438
2439 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002440 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2441 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 +02002442 fstrm->proto_status = endreq.errcode;
2443 fcgi_strm_close(fstrm);
2444
2445 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2446 fconn->drl = 0;
2447 fconn->drp = 0;
2448 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002449 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2450 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002451 return 1;
2452
2453 fail:
2454 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002455 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 +02002456 return 0;
2457}
2458
2459/* process Rx records to be demultiplexed */
2460static void fcgi_process_demux(struct fcgi_conn *fconn)
2461{
2462 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2463 struct fcgi_header hdr;
2464 int ret;
2465
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002466 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2467
Christopher Faulet99eff652019-08-11 23:11:30 +02002468 if (fconn->state == FCGI_CS_CLOSED)
2469 return;
2470
2471 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002472 if (fconn->state == FCGI_CS_INIT) {
2473 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2474 return;
2475 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002476 if (fconn->state == FCGI_CS_SETTINGS) {
2477 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002478 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002479 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2480 if (!ret)
2481 goto fail;
2482 b_del(&fconn->dbuf, ret);
2483
2484 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2485 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002486 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);
2487 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 +02002488 goto fail;
2489 }
2490 goto new_record;
2491 }
2492 }
2493
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002494 /* process as many incoming records as possible below */
2495 while (1) {
2496 if (!b_data(&fconn->dbuf)) {
2497 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2498 break;
2499 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002500
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002501 if (fconn->state == FCGI_CS_CLOSED) {
2502 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002503 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002504 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002505
2506 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002507 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002508 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2509 if (!ret)
2510 break;
2511 b_del(&fconn->dbuf, ret);
2512
2513 new_record:
2514 fconn->dsi = hdr.id;
2515 fconn->drt = hdr.type;
2516 fconn->drl = hdr.len;
2517 fconn->drp = hdr.padding;
2518 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002519 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 +02002520 }
2521
2522 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2523 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2524
2525 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2526 (b_data(&fstrm->rxbuf) ||
2527 conn_xprt_read0_pending(fconn->conn) ||
2528 fstrm->state == FCGI_SS_CLOSED ||
2529 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2530 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2531 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002532 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 +02002533 fstrm->cs->flags |= CS_FL_RCV_MORE;
2534 fcgi_strm_notify_recv(fstrm);
2535 }
2536 fstrm = tmp_fstrm;
2537
2538 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2539 /* ignore all record for closed streams */
2540 goto ignore_record;
2541 }
2542 if (fstrm->state == FCGI_SS_IDLE) {
2543 /* ignore all record for unknown streams */
2544 goto ignore_record;
2545 }
2546
2547 switch (fconn->drt) {
2548 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002549 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 +02002550 ret = fcgi_conn_handle_values_result(fconn);
2551 break;
2552
2553 case FCGI_STDOUT:
2554 if (fstrm->flags & FCGI_SF_ES_RCVD)
2555 goto ignore_record;
2556
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002557 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002558 if (fconn->drl)
2559 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2560 else
2561 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2562 break;
2563
2564 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002565 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002566 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2567 break;
2568
2569 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002570 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 +02002571 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2572 break;
2573
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002574 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002575 default:
2576 ignore_record:
2577 /* drop records that we ignore. They may be
2578 * larger than the buffer so we drain all of
2579 * their contents until we reach the end.
2580 */
2581 fconn->state = FCGI_CS_RECORD_P;
2582 fconn->drl += fconn->drp;
2583 fconn->drp = 0;
2584 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002585 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2586 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002587 b_del(&fconn->dbuf, ret);
2588 fconn->drl -= ret;
2589 ret = (fconn->drl == 0);
2590 }
2591
2592 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002593 if (ret <= 0) {
2594 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002595 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002597
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002598 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002599 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002600 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2601 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002602 }
2603
2604 fail:
2605 /* we can go here on missing data, blocked response or error */
2606 if (fstrm && fstrm->cs &&
2607 (b_data(&fstrm->rxbuf) ||
2608 conn_xprt_read0_pending(fconn->conn) ||
2609 fstrm->state == FCGI_SS_CLOSED ||
2610 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2611 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2612 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002613 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 +02002614 fstrm->cs->flags |= CS_FL_RCV_MORE;
2615 fcgi_strm_notify_recv(fstrm);
2616 }
2617
2618 fcgi_conn_restart_reading(fconn, 0);
2619}
2620
2621/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2622 * the end.
2623 */
2624static int fcgi_process_mux(struct fcgi_conn *fconn)
2625{
2626 struct fcgi_strm *fstrm, *fstrm_back;
2627
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002628 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2629
Christopher Faulet99eff652019-08-11 23:11:30 +02002630 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2631 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2632 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2633 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 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 +02002635 fcgi_wake_unassigned_streams(fconn);
2636 goto mux;
2637 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002639 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2640 goto fail;
2641 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002642 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 +02002643 }
2644 /* need to wait for the other side */
2645 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002646 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002647 }
2648
2649 mux:
2650 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2651 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2652 break;
2653
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002654 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002655 continue;
2656
Willy Tarreau7aad7032020-01-16 17:20:57 +01002657 /* If the sender changed his mind and unsubscribed, let's just
2658 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002659 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002660 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2661 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002662 LIST_DEL_INIT(&fstrm->send_list);
2663 continue;
2664 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002665
2666 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002667 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2668 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002669 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002670 tasklet_wakeup(fstrm->subs->tasklet);
2671 fstrm->subs->events &= ~SUB_RETRY_SEND;
2672 if (!fstrm->subs->events)
2673 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002674 } else {
2675 /* it's the shut request that was queued */
2676 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2677 tasklet_wakeup(fstrm->shut_tl);
2678 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002679 }
2680
2681 fail:
2682 if (fconn->state == FCGI_CS_CLOSED) {
2683 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2684 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002685 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2686 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002687 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002688 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002689 }
2690 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002691
2692 done:
2693 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002694 return 1;
2695}
2696
2697
2698/* Attempt to read data, and subscribe if none available.
2699 * The function returns 1 if data has been received, otherwise zero.
2700 */
2701static int fcgi_recv(struct fcgi_conn *fconn)
2702{
2703 struct connection *conn = fconn->conn;
2704 struct buffer *buf;
2705 int max;
2706 size_t ret;
2707
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002708 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2709
2710 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2711 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002712 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002713 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002714
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002715 if (!fcgi_recv_allowed(fconn)) {
2716 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002717 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002718 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002719
2720 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2721 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002722 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002723 fconn->flags |= FCGI_CF_DEM_DALLOC;
2724 return 0;
2725 }
2726
2727 b_realign_if_empty(buf);
2728 if (!b_data(buf)) {
2729 /* try to pre-align the buffer like the
2730 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002731 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002732 * HTX block to alias it upon recv. We cannot use the
2733 * head because rcv_buf() will realign the buffer if
2734 * it's empty. Thus we cheat and pretend we already
2735 * have a few bytes there.
2736 */
2737 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2738 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2739 }
2740 else
2741 max = buf_room_for_htx_data(buf);
2742
2743 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2744
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002745 if (max && !ret && fcgi_recv_allowed(fconn)) {
2746 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002747 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002748 }
2749 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002750 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002751
2752 if (!b_data(buf)) {
2753 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002754 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002755 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2756 }
2757
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002758 if (ret == max) {
2759 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002760 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002761 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002762
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002763 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002764 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2765}
2766
2767
2768/* Try to send data if possible.
2769 * The function returns 1 if data have been sent, otherwise zero.
2770 */
2771static int fcgi_send(struct fcgi_conn *fconn)
2772{
2773 struct connection *conn = fconn->conn;
2774 int done;
2775 int sent = 0;
2776
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2778
2779 if (conn->flags & CO_FL_ERROR) {
2780 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002781 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002782 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002783
2784
Willy Tarreau911db9b2020-01-23 16:27:54 +01002785 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002786 /* a handshake was requested */
2787 goto schedule;
2788 }
2789
2790 /* This loop is quite simple : it tries to fill as much as it can from
2791 * pending streams into the existing buffer until it's reportedly full
2792 * or the end of send requests is reached. Then it tries to send this
2793 * buffer's contents out, marks it not full if at least one byte could
2794 * be sent, and tries again.
2795 *
2796 * The snd_buf() function normally takes a "flags" argument which may
2797 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2798 * data immediately comes and CO_SFL_STREAMER to indicate that the
2799 * connection is streaming lots of data (used to increase TLS record
2800 * size at the expense of latency). The former can be sent any time
2801 * there's a buffer full flag, as it indicates at least one stream
2802 * attempted to send and failed so there are pending data. An
2803 * alternative would be to set it as long as there's an active stream
2804 * but that would be problematic for ACKs until we have an absolute
2805 * guarantee that all waiters have at least one byte to send. The
2806 * latter should possibly not be set for now.
2807 */
2808
2809 done = 0;
2810 while (!done) {
2811 unsigned int flags = 0;
2812 unsigned int released = 0;
2813 struct buffer *buf;
2814
2815 /* fill as much as we can into the current buffer */
2816 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2817 done = fcgi_process_mux(fconn);
2818
2819 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2820 done = 1; // we won't go further without extra buffers
2821
2822 if (conn->flags & CO_FL_ERROR)
2823 break;
2824
2825 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2826 flags |= CO_SFL_MSG_MORE;
2827
2828 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2829 if (b_data(buf)) {
2830 int ret;
2831
2832 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2833 if (!ret) {
2834 done = 1;
2835 break;
2836 }
2837 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002838 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002839 b_del(buf, ret);
2840 if (b_data(buf)) {
2841 done = 1;
2842 break;
2843 }
2844 }
2845 b_free(buf);
2846 released++;
2847 }
2848
2849 if (released)
2850 offer_buffers(NULL, tasks_run_queue);
2851
2852 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002853 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2854 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002855 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2856 }
2857
2858 if (conn->flags & CO_FL_SOCK_WR_SH) {
2859 /* output closed, nothing to send, clear the buffer to release it */
2860 b_reset(br_tail(fconn->mbuf));
2861 }
2862 /* We're not full anymore, so we can wake any task that are waiting
2863 * for us.
2864 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002865 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002866 struct fcgi_strm *fstrm;
2867
2868 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2869 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2870 break;
2871
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002872 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002873 continue;
2874
Willy Tarreau7aad7032020-01-16 17:20:57 +01002875 /* If the sender changed his mind and unsubscribed, let's just
2876 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002877 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002878 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2879 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002880 LIST_DEL_INIT(&fstrm->send_list);
2881 continue;
2882 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002883
2884 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002885 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002886 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002887 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002888 tasklet_wakeup(fstrm->subs->tasklet);
2889 fstrm->subs->events &= ~SUB_RETRY_SEND;
2890 if (!fstrm->subs->events)
2891 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002892 } else {
2893 /* it's the shut request that was queued */
2894 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2895 tasklet_wakeup(fstrm->shut_tl);
2896 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002897 }
2898 }
2899 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002900 if (!br_data(fconn->mbuf)) {
2901 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002902 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002903 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002904schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002905 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2906 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002907 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002908 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002909
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002910 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002911 return sent;
2912}
2913
2914/* this is the tasklet referenced in fconn->wait_event.tasklet */
2915static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2916{
2917 struct fcgi_conn *fconn = ctx;
2918 int ret = 0;
2919
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002920 TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn);
2921
Christopher Faulet99eff652019-08-11 23:11:30 +02002922 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2923 ret = fcgi_send(fconn);
2924 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2925 ret |= fcgi_recv(fconn);
2926 if (ret || b_data(&fconn->dbuf))
2927 fcgi_process(fconn);
2928 return NULL;
2929}
2930
2931/* callback called on any event by the connection handler.
2932 * It applies changes and returns zero, or < 0 if it wants immediate
2933 * destruction of the connection (which normally doesn not happen in FCGI).
2934 */
2935static int fcgi_process(struct fcgi_conn *fconn)
2936{
2937 struct connection *conn = fconn->conn;
2938
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002939 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2940
Christopher Faulet99eff652019-08-11 23:11:30 +02002941 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2942 fcgi_process_demux(fconn);
2943
2944 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2945 b_reset(&fconn->dbuf);
2946
2947 if (buf_room_for_htx_data(&fconn->dbuf))
2948 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2949 }
2950 fcgi_send(fconn);
2951
2952 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2953 /* frontend is stopping, reload likely in progress, let's try
2954 * to announce a graceful shutdown if not yet done. We don't
2955 * care if it fails, it will be tried again later.
2956 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002957 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 +02002958 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2959 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2960 fcgi_conn_send_aborts(fconn);
2961 }
2962 }
2963
2964 /*
2965 * If we received early data, and the handshake is done, wake
2966 * any stream that was waiting for it.
2967 */
2968 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01002969 (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 +02002970 struct eb32_node *node;
2971 struct fcgi_strm *fstrm;
2972
2973 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
2974 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
2975
2976 while (node) {
2977 fstrm = container_of(node, struct fcgi_strm, by_id);
2978 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
2979 fcgi_strm_notify_recv(fstrm);
2980 node = eb32_next(node);
2981 }
2982 }
2983
2984 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
2985 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2986 eb_is_empty(&fconn->streams_by_id)) {
2987 fcgi_wake_some_streams(fconn, 0);
2988
2989 if (eb_is_empty(&fconn->streams_by_id)) {
2990 /* no more stream, kill the connection now */
2991 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002992 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002993 return -1;
2994 }
2995 }
2996
2997 if (!b_data(&fconn->dbuf))
2998 fcgi_release_buf(fconn, &fconn->dbuf);
2999
3000 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3001 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3002 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3003 fcgi_release_mbuf(fconn);
3004
3005 if (fconn->task) {
3006 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3007 task_queue(fconn->task);
3008 }
3009
3010 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003011 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003012 return 0;
3013}
3014
3015
3016/* wake-up function called by the connection layer (mux_ops.wake) */
3017static int fcgi_wake(struct connection *conn)
3018{
3019 struct fcgi_conn *fconn = conn->ctx;
3020
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003021 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003022 return (fcgi_process(fconn));
3023}
3024
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003025
3026static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3027{
3028 int ret = 0;
3029 switch (mux_ctl) {
3030 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003031 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003032 ret |= MUX_STATUS_READY;
3033 return ret;
3034 default:
3035 return -1;
3036 }
3037}
3038
Christopher Faulet99eff652019-08-11 23:11:30 +02003039/* Connection timeout management. The principle is that if there's no receipt
3040 * nor sending for a certain amount of time, the connection is closed. If the
3041 * MUX buffer still has lying data or is not allocatable, the connection is
3042 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003043 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003044 */
3045static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3046{
3047 struct fcgi_conn *fconn = context;
3048 int expired = tick_is_expired(t->expire, now_ms);
3049
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003050 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3051
3052 if (!expired && fconn) {
3053 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003054 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003055 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003056
3057 task_destroy(t);
3058
3059 if (!fconn) {
3060 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003061 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003062 return NULL;
3063 }
3064
3065 fconn->task = NULL;
3066 fconn->state = FCGI_CS_CLOSED;
3067 fcgi_wake_some_streams(fconn, 0);
3068
3069 if (br_data(fconn->mbuf)) {
3070 /* don't even try to send aborts, the buffer is stuck */
3071 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3072 goto end;
3073 }
3074
3075 /* try to send but no need to insist */
3076 if (!fcgi_conn_send_aborts(fconn))
3077 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3078
3079 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3080 conn_xprt_ready(fconn->conn)) {
3081 unsigned int released = 0;
3082 struct buffer *buf;
3083
3084 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3085 if (b_data(buf)) {
3086 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3087 buf, b_data(buf), 0);
3088 if (!ret)
3089 break;
3090 b_del(buf, ret);
3091 if (b_data(buf))
3092 break;
3093 b_free(buf);
3094 released++;
3095 }
3096 }
3097
3098 if (released)
3099 offer_buffers(NULL, tasks_run_queue);
3100 }
3101
3102 end:
3103 /* either we can release everything now or it will be done later once
3104 * the last stream closes.
3105 */
3106 if (eb_is_empty(&fconn->streams_by_id))
3107 fcgi_release(fconn);
3108
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003109 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003110 return NULL;
3111}
3112
3113
3114/*******************************************/
3115/* functions below are used by the streams */
3116/*******************************************/
3117
3118/* Append the description of what is present in error snapshot <es> into <out>.
3119 * The description must be small enough to always fit in a buffer. The output
3120 * buffer may be the trash so the trash must not be used inside this function.
3121 */
3122static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3123{
3124 chunk_appendf(out,
3125 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3126 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3127 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3128 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3129 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3130 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3131}
3132/*
3133 * Capture a bad response and archive it in the proxy's structure. By default
3134 * it tries to report the error position as h1m->err_pos. However if this one is
3135 * not set, it will then report h1m->next, which is the last known parsing
3136 * point. The function is able to deal with wrapping buffers. It always displays
3137 * buffers as a contiguous area starting at buf->p. The direction is determined
3138 * thanks to the h1m's flags.
3139 */
3140static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3141 struct h1m *h1m, struct buffer *buf)
3142{
3143 struct session *sess = fstrm->sess;
3144 struct proxy *proxy = fconn->proxy;
3145 struct proxy *other_end = sess->fe;
3146 union error_snapshot_ctx ctx;
3147
3148 /* http-specific part now */
3149 ctx.h1.state = h1m->state;
3150 ctx.h1.c_flags = fconn->flags;
3151 ctx.h1.s_flags = fstrm->flags;
3152 ctx.h1.m_flags = h1m->flags;
3153 ctx.h1.m_clen = h1m->curr_len;
3154 ctx.h1.m_blen = h1m->body_len;
3155
3156 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3157 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3158 &ctx, fcgi_show_error_snapshot);
3159}
3160
3161static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3162 struct buffer *buf, size_t *ofs, size_t max)
3163{
3164 int ret;
3165
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003166 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003167 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3168 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003169 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 +02003170 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003171 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 +02003172 fcgi_strm_error(fstrm);
3173 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3174 }
3175 goto end;
3176 }
3177
3178 *ofs += ret;
3179 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003180 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003181 return ret;
3182
3183}
3184
Christopher Fauletaf542632019-10-01 21:52:49 +02003185static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003186 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3187{
3188 int ret;
3189
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003190 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003191 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003192 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003193 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 +02003194 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003195 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 +02003196 fcgi_strm_error(fstrm);
3197 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3198 }
3199 goto end;
3200 }
3201 *ofs += ret;
3202 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003203 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003204 return ret;
3205}
3206
3207static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3208 struct buffer *buf, size_t *ofs, size_t max)
3209{
3210 int ret;
3211
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003212 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003213 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003214 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003215 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 +02003216 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003217 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 +02003218 fcgi_strm_error(fstrm);
3219 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3220 }
3221 goto end;
3222 }
3223 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003224 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003225 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003226 return ret;
3227}
3228
3229static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003230 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003231{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003232 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003233
Christopher Faulet76014fd2019-12-10 11:47:22 +01003234 TRACE_ENTER(FCGI_EV_RSP_DATA||FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
3235 ret = h1_parse_msg_eom(h1m, htx, max);
3236 if (!ret) {
3237 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3238 if (htx->flags & HTX_FL_PARSING_ERROR) {
3239 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3240 fcgi_strm_error(fstrm);
3241 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3242 }
3243 goto end;
3244 }
3245 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3246 end:
3247 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3248 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003249}
3250
3251static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3252{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003253 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003254 struct htx *htx;
3255 struct h1m *h1m = &fstrm->h1m;
3256 size_t ret, data, total = 0;
3257
3258 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003259 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3260
Christopher Faulet99eff652019-08-11 23:11:30 +02003261 data = htx->data;
3262 if (fstrm->state == FCGI_SS_ERROR)
3263 goto end;
3264
3265 do {
3266 size_t used = htx_used_space(htx);
3267
3268 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003269 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003270 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3271 if (!ret)
3272 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003273
3274 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3275
Christopher Faulet99eff652019-08-11 23:11:30 +02003276 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3277 struct htx_blk *blk = htx_get_head_blk(htx);
3278 struct htx_sl *sl;
3279
3280 if (!blk)
3281 break;
3282 sl = htx_get_blk_ptr(htx, blk);
3283 sl->flags |= HTX_SL_F_XFER_LEN;
3284 htx->extra = 0;
3285 }
3286 }
3287 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003288 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003289 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003290 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003291 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003292
3293 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 +02003294 }
3295 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003296 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3297 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3298 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003299 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003300
Christopher Faulet76014fd2019-12-10 11:47:22 +01003301 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 +02003302 }
3303 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003304 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3305 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3306 break;
3307
3308 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3309 }
3310
Christopher Faulet99eff652019-08-11 23:11:30 +02003311 if (b_data(&fstrm->rxbuf) > total) {
3312 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003313 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003314 fcgi_strm_error(fstrm);
3315 }
3316 break;
3317 }
3318 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003319 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003320 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003321
Christopher Faulet99eff652019-08-11 23:11:30 +02003322 if (fstrm->state != FCGI_SS_ERROR &&
3323 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003324 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003325 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3326 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3327 h1m->state = H1_MSG_DONE;
3328 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 +02003329 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003330 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003331 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003332
3333 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003334 }
3335 else {
3336 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003337 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003338 fcgi_strm_error(fstrm);
3339 break;
3340 }
3341
3342 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003343 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003344
3345 if (fstrm->state == FCGI_SS_ERROR) {
3346 b_reset(&fstrm->rxbuf);
3347 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003348 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003349 return 0;
3350 }
3351
3352 b_del(&fstrm->rxbuf, total);
3353
3354 end:
3355 htx_to_buf(htx, buf);
3356 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003357 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003358 return ret;
3359}
3360
3361/*
3362 * Attach a new stream to a connection
3363 * (Used for outgoing connections)
3364 */
3365static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3366{
3367 struct conn_stream *cs;
3368 struct fcgi_strm *fstrm;
3369 struct fcgi_conn *fconn = conn->ctx;
3370
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003371 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003372 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003373 if (!cs) {
3374 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003375 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003376 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003377 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3378 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003379 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003380 cs_free(cs);
3381 return NULL;
3382 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003383 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003384 return cs;
3385}
3386
3387/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3388 * We have to scan because we may have some orphan streams. It might be
3389 * beneficial to scan backwards from the end to reduce the likeliness to find
3390 * orphans.
3391 */
3392static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3393{
3394 struct fcgi_conn *fconn = conn->ctx;
3395 struct fcgi_strm *fstrm;
3396 struct eb32_node *node;
3397
3398 node = eb32_first(&fconn->streams_by_id);
3399 while (node) {
3400 fstrm = container_of(node, struct fcgi_strm, by_id);
3401 if (fstrm->cs)
3402 return fstrm->cs;
3403 node = eb32_next(node);
3404 }
3405 return NULL;
3406}
3407
3408/*
3409 * Destroy the mux and the associated connection, if it is no longer used
3410 */
3411static void fcgi_destroy(void *ctx)
3412{
3413 struct fcgi_conn *fconn = ctx;
3414
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003415 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003416 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3417 fcgi_release(fconn);
3418}
3419
3420/*
3421 * Detach the stream from the connection and possibly release the connection.
3422 */
3423static void fcgi_detach(struct conn_stream *cs)
3424{
3425 struct fcgi_strm *fstrm = cs->ctx;
3426 struct fcgi_conn *fconn;
3427 struct session *sess;
3428
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003429 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3430
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003432 if (!fstrm) {
3433 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003434 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003436
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003437 /* there's no txbuf so we're certain no to be able to send anything */
3438 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003439
3440 sess = fstrm->sess;
3441 fconn = fstrm->fconn;
3442 fstrm->cs = NULL;
3443 fconn->nb_cs--;
3444
3445 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3446 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3447 fconn->streams_limit = 1;
3448 }
3449 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3450 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3451 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3452 fconn->state = FCGI_CS_CLOSED;
3453 }
3454
3455 /* this stream may be blocked waiting for some data to leave, so orphan
3456 * it in this case.
3457 */
3458 if (!(cs->conn->flags & CO_FL_ERROR) &&
3459 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003460 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003461 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003462 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003463 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003464 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003465
3466 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3467 /* unblock the connection if it was blocked on this stream. */
3468 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3469 fcgi_conn_restart_reading(fconn, 1);
3470 }
3471
3472 fcgi_strm_destroy(fstrm);
3473
3474 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3475 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
3476 if (!fconn->conn->owner) {
3477 fconn->conn->owner = sess;
3478 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3479 fconn->conn->owner = NULL;
3480 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003481 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003482 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard12ffab02020-02-14 13:23:45 +01003483 fconn->conn->mux->destroy(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003484 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3485 }
3486 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003487 return;
3488 }
3489 }
3490 }
3491 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003492 int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn);
3493 if (ret == -1) {
3494 /* The connection is destroyed, let's leave */
3495 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003496 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003497 }
3498 else if (ret == 1) {
3499 /* The connection was added to the server idle list, just stop */
3500 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3501 return;
3502 }
3503 TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003504 }
3505 /* Never ever allow to reuse a connection from a non-reuse backend */
3506 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3507 fconn->conn->flags |= CO_FL_PRIVATE;
3508 if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) {
3509 struct server *srv = objt_server(fconn->conn->target);
3510
3511 if (srv) {
3512 if (fconn->conn->flags & CO_FL_PRIVATE)
3513 LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list);
3514 else
3515 LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list);
3516 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003517 TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003518 }
3519 }
3520
3521 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003522 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003523 */
3524 if (fcgi_conn_is_dead(fconn)) {
3525 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003526 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003527 fcgi_release(fconn);
3528 }
3529 else if (fconn->task) {
3530 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3531 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003532 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003533 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003534 else
3535 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003536}
3537
3538
3539/* Performs a synchronous or asynchronous shutr(). */
3540static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3541{
3542 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003543
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003544 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3545
Christopher Faulet99eff652019-08-11 23:11:30 +02003546 if (fstrm->state == FCGI_SS_CLOSED)
3547 goto done;
3548
3549 /* a connstream may require us to immediately kill the whole connection
3550 * for example because of a "tcp-request content reject" rule that is
3551 * normally used to limit abuse.
3552 */
3553 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003554 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3555 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003556 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003557 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003558 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003559 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003560 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3561 !fcgi_strm_send_abort(fconn, fstrm))
3562 goto add_to_list;
3563 }
3564
3565 fcgi_strm_close(fstrm);
3566
3567 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3568 tasklet_wakeup(fconn->wait_event.tasklet);
3569 done:
3570 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003571 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003572 return;
3573
3574 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003575 /* Let the handler know we want to shutr, and add ourselves to the
3576 * send list if not yet done. fcgi_deferred_shut() will be
3577 * automatically called via the shut_tl tasklet when there's room
3578 * again.
3579 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003580 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003581 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003582 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3583 }
3584 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003585 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003586 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003587 return;
3588}
3589
3590/* Performs a synchronous or asynchronous shutw(). */
3591static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3592{
3593 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003594
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003595 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3596
Christopher Faulet99eff652019-08-11 23:11:30 +02003597 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3598 goto done;
3599
3600 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3601 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3602 !fcgi_strm_send_abort(fconn, fstrm))
3603 goto add_to_list;
3604
3605 if (fstrm->state == FCGI_SS_HREM)
3606 fcgi_strm_close(fstrm);
3607 else
3608 fstrm->state = FCGI_SS_HLOC;
3609 } else {
3610 /* a connstream may require us to immediately kill the whole connection
3611 * for example because of a "tcp-request content reject" rule that is
3612 * normally used to limit abuse.
3613 */
3614 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003615 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3616 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003617 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003618 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003619
3620 fcgi_strm_close(fstrm);
3621 }
3622
3623 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3624 tasklet_wakeup(fconn->wait_event.tasklet);
3625 done:
3626 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003627 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003628 return;
3629
3630 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003631 /* Let the handler know we want to shutr, and add ourselves to the
3632 * send list if not yet done. fcgi_deferred_shut() will be
3633 * automatically called via the shut_tl tasklet when there's room
3634 * again.
3635 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003636 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003638 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3639 }
3640 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003641 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003642 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003643 return;
3644}
3645
Willy Tarreau7aad7032020-01-16 17:20:57 +01003646/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003647 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003648 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003649 */
3650static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3651{
3652 struct fcgi_strm *fstrm = ctx;
3653 struct fcgi_conn *fconn = fstrm->fconn;
3654
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003655 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3656
Willy Tarreau7aad7032020-01-16 17:20:57 +01003657 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3658 /* some data processing remains to be done first */
3659 goto end;
3660 }
3661
Christopher Faulet99eff652019-08-11 23:11:30 +02003662 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3663 fcgi_do_shutw(fstrm);
3664
3665 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3666 fcgi_do_shutr(fstrm);
3667
3668 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3669 /* We're done trying to send, remove ourself from the send_list */
3670 LIST_DEL_INIT(&fstrm->send_list);
3671
3672 if (!fstrm->cs) {
3673 fcgi_strm_destroy(fstrm);
3674 if (fcgi_conn_is_dead(fconn))
3675 fcgi_release(fconn);
3676 }
3677 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003678 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003679 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003680 return NULL;
3681}
3682
3683/* shutr() called by the conn_stream (mux_ops.shutr) */
3684static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3685{
3686 struct fcgi_strm *fstrm = cs->ctx;
3687
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003688 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003689 if (cs->flags & CS_FL_KILL_CONN)
3690 fstrm->flags |= FCGI_SF_KILL_CONN;
3691
3692 if (!mode)
3693 return;
3694
3695 fcgi_do_shutr(fstrm);
3696}
3697
3698/* shutw() called by the conn_stream (mux_ops.shutw) */
3699static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3700{
3701 struct fcgi_strm *fstrm = cs->ctx;
3702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003703 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003704 if (cs->flags & CS_FL_KILL_CONN)
3705 fstrm->flags |= FCGI_SF_KILL_CONN;
3706
3707 fcgi_do_shutw(fstrm);
3708}
3709
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003710/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3711 * event subscriber <es> is not allowed to change from a previous call as long
3712 * as at least one event is still subscribed. The <event_type> must only be a
3713 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003714 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003715static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003716{
Christopher Faulet99eff652019-08-11 23:11:30 +02003717 struct fcgi_strm *fstrm = cs->ctx;
3718 struct fcgi_conn *fconn = fstrm->fconn;
3719
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003720 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
3721 BUG_ON(fstrm->subs && fstrm->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003722 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003723
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003724 es->events |= event_type;
3725 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003726
3727 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003728 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003729
Christopher Faulet99eff652019-08-11 23:11:30 +02003730 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003731 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 if (!LIST_ADDED(&fstrm->send_list))
3733 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003734 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003735 return 0;
3736}
3737
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003738/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3739 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3740 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003741 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003742static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003743{
Christopher Faulet99eff652019-08-11 23:11:30 +02003744 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003745 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003746
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003747 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003748 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003749
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003750 es->events &= ~event_type;
3751 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003752 fstrm->subs = NULL;
3753
3754 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003755 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003756
Christopher Faulet99eff652019-08-11 23:11:30 +02003757 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003758 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003759 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003760 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3761 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003762 }
3763 return 0;
3764}
3765
3766/* Called from the upper layer, to receive data */
3767static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3768{
3769 struct fcgi_strm *fstrm = cs->ctx;
3770 struct fcgi_conn *fconn = fstrm->fconn;
3771 size_t ret = 0;
3772
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003773 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3774
Christopher Faulet99eff652019-08-11 23:11:30 +02003775 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3776 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003777 else
3778 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003779
Christopher Faulet76014fd2019-12-10 11:47:22 +01003780 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 +02003781 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3782 else {
3783 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003784 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003785 cs->flags |= CS_FL_EOI;
3786 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3787 cs->flags |= CS_FL_EOS;
3788 }
3789 if (conn_xprt_read0_pending(fconn->conn))
3790 cs->flags |= CS_FL_EOS;
3791 if (cs->flags & CS_FL_ERR_PENDING)
3792 cs->flags |= CS_FL_ERROR;
3793 fcgi_release_buf(fconn, &fstrm->rxbuf);
3794 }
3795
3796 if (ret && fconn->dsi == fstrm->id) {
3797 /* demux is blocking on this stream's buffer */
3798 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3799 fcgi_conn_restart_reading(fconn, 1);
3800 }
3801
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003802 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003803 return ret;
3804}
3805
3806
Christopher Faulet99eff652019-08-11 23:11:30 +02003807/* Called from the upper layer, to send data from buffer <buf> for no more than
3808 * <count> bytes. Returns the number of bytes effectively sent. Some status
3809 * flags may be updated on the conn_stream.
3810 */
3811static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3812{
3813 struct fcgi_strm *fstrm = cs->ctx;
3814 struct fcgi_conn *fconn = fstrm->fconn;
3815 size_t total = 0;
3816 size_t ret;
3817 struct htx *htx = NULL;
3818 struct htx_sl *sl;
3819 struct htx_blk *blk;
3820 uint32_t bsize;
3821
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003822 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3823
Christopher Faulet99eff652019-08-11 23:11:30 +02003824 /* If we were not just woken because we wanted to send but couldn't,
3825 * and there's somebody else that is waiting to send, do nothing,
3826 * we will subscribe later and be put at the end of the list
3827 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003828 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003829 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 +02003830 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003831 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003832 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003833
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003834 if (fconn->state < FCGI_CS_RECORD_H) {
3835 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003836 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003837 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003838
3839 htx = htxbuf(buf);
3840 if (fstrm->id == 0) {
3841 int32_t id = fcgi_conn_get_next_sid(fconn);
3842
3843 if (id < 0) {
3844 fcgi_strm_close(fstrm);
3845 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003846 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 +02003847 return 0;
3848 }
3849
3850 eb32_delete(&fstrm->by_id);
3851 fstrm->by_id.key = fstrm->id = id;
3852 fconn->max_id = id;
3853 fconn->nb_reserved--;
3854 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3855
3856
3857 /* Check if length of the body is known or if the message is
3858 * full. Otherwise, the request is invalid.
3859 */
3860 sl = http_get_stline(htx);
3861 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3862 htx->flags |= HTX_FL_PARSING_ERROR;
3863 fcgi_strm_error(fstrm);
3864 goto done;
3865 }
3866 }
3867
3868 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003869 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 +02003870 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3871 goto done;
3872 }
3873
3874 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3875 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3876
3877 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3878 count && !htx_is_empty(htx)) {
3879 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003880 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003881 bsize = htx_get_blksz(blk);
3882
3883 switch (htx_get_blk_type(blk)) {
3884 case HTX_BLK_REQ_SL:
3885 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003886 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 +02003887 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3888 if (!ret) {
3889 goto done;
3890 }
3891 total += ret;
3892 count -= ret;
3893 break;
3894
3895 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003896 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 +02003897 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3898 if (!ret)
3899 goto done;
3900 goto remove_blk;
3901
3902 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003903 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 +02003904 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3905 if (ret > 0) {
3906 htx = htx_from_buf(buf);
3907 total += ret;
3908 count -= ret;
3909 if (ret < bsize)
3910 goto done;
3911 }
3912 break;
3913
3914 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003915 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 +02003916 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3917 if (!ret)
3918 goto done;
3919 goto remove_blk;
3920
3921 default:
3922 remove_blk:
3923 htx_remove_blk(htx, blk);
3924 total += bsize;
3925 count -= bsize;
3926 break;
3927 }
3928 }
3929
3930 done:
3931 if (fstrm->state >= FCGI_SS_HLOC) {
3932 /* trim any possibly pending data after we close (extra CR-LF,
3933 * unprocessed trailers, abnormal extra data, ...)
3934 */
3935 total += count;
3936 count = 0;
3937 }
3938
3939 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003940 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 +02003941 cs_set_error(cs);
3942 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3943 fcgi_strm_close(fstrm);
3944 }
3945
3946 if (htx)
3947 htx_to_buf(htx, buf);
3948
Christopher Faulet99eff652019-08-11 23:11:30 +02003949 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003950 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
3951 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 +02003952 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003953 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003954
3955 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01003956 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3957 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003958 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003959
3960 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02003961 return total;
3962}
3963
3964/* for debugging with CLI's "show fd" command */
3965static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
3966{
3967 struct fcgi_conn *fconn = conn->ctx;
3968 struct fcgi_strm *fstrm = NULL;
3969 struct eb32_node *node;
3970 int send_cnt = 0;
3971 int tree_cnt = 0;
3972 int orph_cnt = 0;
3973 struct buffer *hmbuf, *tmbuf;
3974
3975 if (!fconn)
3976 return;
3977
3978 list_for_each_entry(fstrm, &fconn->send_list, send_list)
3979 send_cnt++;
3980
3981 fstrm = NULL;
3982 node = eb32_first(&fconn->streams_by_id);
3983 while (node) {
3984 fstrm = container_of(node, struct fcgi_strm, by_id);
3985 tree_cnt++;
3986 if (!fstrm->cs)
3987 orph_cnt++;
3988 node = eb32_next(node);
3989 }
3990
3991 hmbuf = br_head(fconn->mbuf);
3992 tmbuf = br_tail(fconn->mbuf);
3993 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
3994 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
3995 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
3996 fconn->state, fconn->max_id, fconn->flags,
3997 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
3998 fconn->wait_event.events, fconn->dsi,
3999 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4000 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4001 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4002 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4003 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4004 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4005 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4006
4007 if (fstrm) {
4008 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4009 fstrm, fstrm->id, fstrm->flags,
4010 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4011 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4012 fstrm->cs);
4013 if (fstrm->cs)
4014 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4015 fstrm->cs->flags, fstrm->cs->data);
4016 }
4017}
4018
4019/****************************************/
4020/* MUX initialization and instanciation */
4021/****************************************/
4022
4023/* The mux operations */
4024static const struct mux_ops mux_fcgi_ops = {
4025 .init = fcgi_init,
4026 .wake = fcgi_wake,
4027 .attach = fcgi_attach,
4028 .get_first_cs = fcgi_get_first_cs,
4029 .detach = fcgi_detach,
4030 .destroy = fcgi_destroy,
4031 .avail_streams = fcgi_avail_streams,
4032 .used_streams = fcgi_used_streams,
4033 .rcv_buf = fcgi_rcv_buf,
4034 .snd_buf = fcgi_snd_buf,
4035 .subscribe = fcgi_subscribe,
4036 .unsubscribe = fcgi_unsubscribe,
4037 .shutr = fcgi_shutr,
4038 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004039 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004040 .show_fd = fcgi_show_fd,
4041 .flags = MX_FL_HTX,
4042 .name = "FCGI",
4043};
4044
4045
4046/* this mux registers FCGI proto */
4047static struct mux_proto_list mux_proto_fcgi =
4048{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4049
4050INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4051
4052/*
4053 * Local variables:
4054 * c-indent-level: 8
4055 * c-basic-offset: 8
4056 * End:
4057 */