blob: 09d35751212a8a3bff488edbf92094efef1d6ffb [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
1348 /* The regex does not match, just to the last part and see if
1349 * the index must be used.
1350 */
1351 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1352 goto check_index;
1353
1354 /* We must have at least 2 captures, otherwise we do nothing and
1355 * jump to the last part. Only first 2 ones will be considered
1356 */
1357 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1 ||
1358 pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1)
1359 goto check_index;
1360
1361 /* Finally we can set the script_name and the path_info */
1362 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
1363 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
1364
1365 check_index:
1366 len = params->scriptname.len;
1367 /* the script_name if finished by a '/' so we can add the index
1368 * part, if any.
1369 */
1370 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1371 struct ist sn = params->scriptname;
1372
1373 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1374 chunk_memcat(params->p, sn.ptr, sn.len);
1375 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1376 }
1377 }
1378
1379 end:
1380 return 1;
1381 error:
1382 return 0;
1383}
1384
1385static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1386 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1387{
1388 struct fcgi_param p;
1389
1390 if (params->mask & flag)
1391 return 1;
1392
1393 chunk_reset(&trash);
1394
1395 switch (flag) {
1396 case FCGI_SP_CGI_GATEWAY:
1397 p.n = ist("GATEWAY_INTERFACE");
1398 p.v = ist("CGI/1.1");
1399 goto encode;
1400 case FCGI_SP_DOC_ROOT:
1401 p.n = ist("DOCUMENT_ROOT");
1402 p.v = params->docroot;
1403 goto encode;
1404 case FCGI_SP_SCRIPT_NAME:
1405 p.n = ist("SCRIPT_NAME");
1406 p.v = params->scriptname;
1407 goto encode;
1408 case FCGI_SP_PATH_INFO:
1409 p.n = ist("PATH_INFO");
1410 p.v = params->pathinfo;
1411 goto encode;
1412 case FCGI_SP_REQ_URI:
1413 p.n = ist("REQUEST_URI");
1414 p.v = params->uri;
1415 goto encode;
1416 case FCGI_SP_REQ_METH:
1417 p.n = ist("REQUEST_METHOD");
1418 p.v = params->meth;
1419 goto encode;
1420 case FCGI_SP_REQ_QS:
1421 p.n = ist("QUERY_STRING");
1422 p.v = params->qs;
1423 goto encode;
1424 case FCGI_SP_SRV_NAME:
1425 p.n = ist("SERVER_NAME");
1426 p.v = params->srv_name;
1427 goto encode;
1428 case FCGI_SP_SRV_PORT:
1429 p.n = ist("SERVER_PORT");
1430 p.v = params->srv_port;
1431 goto encode;
1432 case FCGI_SP_SRV_PROTO:
1433 p.n = ist("SERVER_PROTOCOL");
1434 p.v = params->vsn;
1435 goto encode;
1436 case FCGI_SP_REM_ADDR:
1437 p.n = ist("REMOTE_ADDR");
1438 p.v = params->rem_addr;
1439 goto encode;
1440 case FCGI_SP_REM_PORT:
1441 p.n = ist("REMOTE_PORT");
1442 p.v = params->rem_port;
1443 goto encode;
1444 case FCGI_SP_SCRIPT_FILE:
1445 p.n = ist("SCRIPT_FILENAME");
1446 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1447 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1448 p.v = ist2(b_head(&trash), b_data(&trash));
1449 goto encode;
1450 case FCGI_SP_PATH_TRANS:
1451 if (!istlen(params->pathinfo))
1452 goto skip;
1453 p.n = ist("PATH_TRANSLATED");
1454 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1455 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1456 p.v = ist2(b_head(&trash), b_data(&trash));
1457 goto encode;
1458 case FCGI_SP_CONT_LEN:
1459 p.n = ist("CONTENT_LENGTH");
1460 p.v = params->cont_len;
1461 goto encode;
1462 case FCGI_SP_HTTPS:
1463 if (!params->https)
1464 goto skip;
1465 p.n = ist("HTTPS");
1466 p.v = ist("on");
1467 goto encode;
1468 default:
1469 goto skip;
1470 }
1471
1472 encode:
1473 if (!istlen(p.v))
1474 goto skip;
1475 if (!fcgi_encode_param(outbuf, &p))
1476 return 0;
1477 skip:
1478 params->mask |= flag;
1479 return 1;
1480}
1481
1482/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1483 * anything. It is highly unexpected, but if the record is larger than a buffer
1484 * and cannot be encoded in one time, an error is triggered and the connection is
1485 * closed. GET_VALUES record cannot be split.
1486 */
1487static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1488{
1489 struct buffer outbuf;
1490 struct buffer *mbuf;
1491 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1492 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001493 int ret = 0;
1494
1495 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001496
1497 mbuf = br_tail(fconn->mbuf);
1498 retry:
1499 if (!fcgi_get_buf(fconn, mbuf)) {
1500 fconn->flags |= FCGI_CF_MUX_MALLOC;
1501 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001502 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1503 ret = 0;
1504 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001505 }
1506
1507 while (1) {
1508 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1509 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1510 break;
1511 realign_again:
1512 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1513 }
1514
1515 if (outbuf.size < 8)
1516 goto full;
1517
1518 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1519 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1520 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1521 outbuf.data = 8;
1522
1523 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1524 * handled by HAProxy.
1525 */
1526 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1527 goto full;
1528
1529 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001530 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 +02001531 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1532 b_add(mbuf, outbuf.data);
1533 ret = 1;
1534
1535 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001536 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001537 return ret;
1538 full:
1539 /* Too large to be encoded. For GET_VALUES records, it is an error */
1540 if (!b_data(mbuf))
1541 goto fail;
1542
1543 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1544 goto retry;
1545 fconn->flags |= FCGI_CF_MUX_MFULL;
1546 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001547 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001548 ret = 0;
1549 goto end;
1550 fail:
1551 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001552 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1553 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1554 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001555}
1556
1557/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1558 * couldn't do anything. It is highly unexpected, but if the record is larger
1559 * than a buffer and cannot be decoded in one time, an error is triggered and
1560 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1561 */
1562static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1563{
1564 struct buffer inbuf;
1565 struct buffer *dbuf;
1566 size_t offset;
1567
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001568 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1569
Christopher Faulet99eff652019-08-11 23:11:30 +02001570 dbuf = &fconn->dbuf;
1571
1572 /* Record too large to be fully decoded */
1573 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1574 goto fail;
1575
1576 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001577 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1578 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001579 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001580 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001581
1582 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1583 /* Realign the dmux buffer if the record wraps. It is unexpected
1584 * at this stage because it should be the first record received
1585 * from the FCGI application.
1586 */
1587 b_slow_realign(dbuf, trash.area, 0);
1588 }
1589
1590 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1591
1592 for (offset = 0; offset < b_data(&inbuf); ) {
1593 struct fcgi_param p;
1594 size_t ret;
1595
1596 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1597 if (!ret) {
1598 /* name or value too large to be decoded at once */
1599 goto fail;
1600 }
1601 offset += ret;
1602
1603 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001604 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001605 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 +02001606 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001607 }
1608 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001609 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001610 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001611 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001612 }
1613 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1614 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001615 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 +02001616 }
1617 /*
1618 * Ignore all other params
1619 */
1620 }
1621
1622 /* Reset the number of concurrent streams supported if the FCGI
1623 * application does not support connection multiplexing
1624 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001625 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001626 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001627 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1628 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001629
1630 /* We must be sure to have read exactly the announced record length, no
1631 * more no less
1632 */
1633 if (offset != fconn->drl)
1634 goto fail;
1635
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001636 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 +02001637 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1638 fconn->drl = 0;
1639 fconn->drp = 0;
1640 fconn->state = FCGI_CS_RECORD_H;
1641 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001642 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1643 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001644 return 1;
1645 fail:
1646 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001647 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1648 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 +02001649 return 0;
1650}
1651
1652/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1653 * excluded, as the streams which already received the end-of-stream. It returns
1654 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1655 */
1656static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1657{
1658 struct eb32_node *node;
1659 struct fcgi_strm *fstrm;
1660
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001661 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1662
Christopher Faulet99eff652019-08-11 23:11:30 +02001663 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1664 while (node) {
1665 fstrm = container_of(node, struct fcgi_strm, by_id);
1666 node = eb32_next(node);
1667 if (fstrm->state != FCGI_SS_CLOSED &&
1668 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1669 !fcgi_strm_send_abort(fconn, fstrm))
1670 return 0;
1671 }
1672 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001673 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1674 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001675 return 1;
1676}
1677
1678/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1679 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1680 * space to proceed. It is small enough to be encoded in an empty buffer.
1681 */
1682static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1683{
1684 struct buffer outbuf;
1685 struct buffer *mbuf;
1686 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1687 int ret;
1688
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001689 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1690
Christopher Faulet99eff652019-08-11 23:11:30 +02001691 mbuf = br_tail(fconn->mbuf);
1692 retry:
1693 if (!fcgi_get_buf(fconn, mbuf)) {
1694 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001695 fstrm->flags |= FCGI_SF_BLK_MROOM;
1696 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1697 ret = 0;
1698 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001699 }
1700
1701 while (1) {
1702 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1703 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1704 break;
1705 realign_again:
1706 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1707 }
1708
1709 if (outbuf.size < 8)
1710 goto full;
1711
1712 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1713 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1714 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1715 fcgi_set_record_id(outbuf.area, fstrm->id);
1716 outbuf.data = 8;
1717
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001718 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1719 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001720 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001721 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001722 if (!fcgi_encode_begin_request(&outbuf, &rec))
1723 goto full;
1724
1725 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001726 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 +02001727 b_add(mbuf, outbuf.data);
1728 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1729 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001730 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001731 ret = 1;
1732
1733 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001734 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001735 return ret;
1736 full:
1737 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1738 goto retry;
1739 fconn->flags |= FCGI_CF_MUX_MFULL;
1740 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001741 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 +02001742 ret = 0;
1743 goto end;
1744}
1745
1746/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1747 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1748 * space to proceed. It is small enough to be encoded in an empty buffer.
1749 */
1750static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1751 enum fcgi_record_type rtype)
1752{
1753 struct buffer outbuf;
1754 struct buffer *mbuf;
1755 int ret;
1756
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001757 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001758 mbuf = br_tail(fconn->mbuf);
1759 retry:
1760 if (!fcgi_get_buf(fconn, mbuf)) {
1761 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001762 fstrm->flags |= FCGI_SF_BLK_MROOM;
1763 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1764 ret = 0;
1765 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001766 }
1767
1768 while (1) {
1769 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1770 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1771 break;
1772 realign_again:
1773 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1774 }
1775
1776 if (outbuf.size < 8)
1777 goto full;
1778
1779 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1780 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1781 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1782 outbuf.area[1] = rtype;
1783 fcgi_set_record_id(outbuf.area, fstrm->id);
1784 outbuf.data = 8;
1785
1786 /* commit the record */
1787 b_add(mbuf, outbuf.data);
1788 ret = 1;
1789
1790 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001791 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001792 return ret;
1793 full:
1794 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1795 goto retry;
1796 fconn->flags |= FCGI_CF_MUX_MFULL;
1797 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001798 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 +02001799 ret = 0;
1800 goto end;
1801}
1802
1803
1804/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1805 * marks the end of params.
1806 */
1807static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1808{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001809 int ret;
1810
1811 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1812 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1813 if (ret)
1814 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1815 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001816}
1817
1818/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1819 * marks the end of input. On success, all the request was successfully sent.
1820 */
1821static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1822{
1823 int ret;
1824
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001825 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001826 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001827 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001828 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001829 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1830 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1831 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1832 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001833 return ret;
1834}
1835
1836/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1837 * stops the request processing.
1838 */
1839static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1840{
1841 int ret;
1842
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001843 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001844 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001845 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001846 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001847 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1848 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1849 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1850 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001851 return ret;
1852}
1853
1854/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1855 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1856 * several records are sent. However, a K/V param cannot be split between 2
1857 * records.
1858 */
1859static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1860 struct htx *htx)
1861{
1862 struct buffer outbuf;
1863 struct buffer *mbuf;
1864 struct htx_blk *blk;
1865 struct htx_sl *sl = NULL;
1866 struct fcgi_strm_params params;
1867 size_t total = 0;
1868
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001869 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1870
Christopher Faulet99eff652019-08-11 23:11:30 +02001871 memset(&params, 0, sizeof(params));
1872 params.p = get_trash_chunk();
1873
1874 mbuf = br_tail(fconn->mbuf);
1875 retry:
1876 if (!fcgi_get_buf(fconn, mbuf)) {
1877 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001878 fstrm->flags |= FCGI_SF_BLK_MROOM;
1879 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1880 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001881 }
1882
1883 while (1) {
1884 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1885 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1886 break;
1887 realign_again:
1888 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1889 }
1890
1891 if (outbuf.size < 8)
1892 goto full;
1893
1894 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1895 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1896 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1897 fcgi_set_record_id(outbuf.area, fstrm->id);
1898 outbuf.data = 8;
1899
1900 blk = htx_get_head_blk(htx);
1901 while (blk) {
1902 enum htx_blk_type type;
1903 uint32_t size = htx_get_blksz(blk);
1904 struct fcgi_param p;
1905
1906 type = htx_get_blk_type(blk);
1907 switch (type) {
1908 case HTX_BLK_REQ_SL:
1909 sl = htx_get_blk_ptr(htx, blk);
1910 if (sl->info.req.meth == HTTP_METH_HEAD)
1911 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1912 if (sl->flags & HTX_SL_F_VER_11)
1913 fstrm->h1m.flags |= H1_MF_VER_11;
1914 break;
1915
1916 case HTX_BLK_HDR:
1917 p.n = htx_get_blk_name(htx, blk);
1918 p.v = htx_get_blk_value(htx, blk);
1919
1920 if (istmatch(p.n, ist(":fcgi-"))) {
1921 p.n.ptr += 6;
1922 p.n.len -= 6;
1923 if (isteq(p.n, ist("gateway_interface")))
1924 params.mask |= FCGI_SP_CGI_GATEWAY;
1925 else if (isteq(p.n, ist("document_root"))) {
1926 params.mask |= FCGI_SP_DOC_ROOT;
1927 params.docroot = p.v;
1928 }
1929 else if (isteq(p.n, ist("script_name"))) {
1930 params.mask |= FCGI_SP_SCRIPT_NAME;
1931 params.scriptname = p.v;
1932 }
1933 else if (isteq(p.n, ist("path_info"))) {
1934 params.mask |= FCGI_SP_PATH_INFO;
1935 params.pathinfo = p.v;
1936 }
1937 else if (isteq(p.n, ist("request_uri"))) {
1938 params.mask |= FCGI_SP_REQ_URI;
1939 params.uri = p.v;
1940 }
1941 else if (isteq(p.n, ist("request_meth")))
1942 params.mask |= FCGI_SP_REQ_METH;
1943 else if (isteq(p.n, ist("query_string")))
1944 params.mask |= FCGI_SP_REQ_QS;
1945 else if (isteq(p.n, ist("server_name")))
1946 params.mask |= FCGI_SP_SRV_NAME;
1947 else if (isteq(p.n, ist("server_port")))
1948 params.mask |= FCGI_SP_SRV_PORT;
1949 else if (isteq(p.n, ist("server_protocol")))
1950 params.mask |= FCGI_SP_SRV_PROTO;
1951 else if (isteq(p.n, ist("remote_addr")))
1952 params.mask |= FCGI_SP_REM_ADDR;
1953 else if (isteq(p.n, ist("remote_port")))
1954 params.mask |= FCGI_SP_REM_PORT;
1955 else if (isteq(p.n, ist("script_filename")))
1956 params.mask |= FCGI_SP_SCRIPT_FILE;
1957 else if (isteq(p.n, ist("path_translated")))
1958 params.mask |= FCGI_SP_PATH_TRANS;
1959 else if (isteq(p.n, ist("https")))
1960 params.mask |= FCGI_SP_HTTPS;
1961 }
1962 else if (isteq(p.n, ist("content-length"))) {
1963 p.n = ist("CONTENT_LENGTH");
1964 params.mask |= FCGI_SP_CONT_LEN;
1965 }
1966 else if (isteq(p.n, ist("content-type")))
1967 p.n = ist("CONTENT_TYPE");
1968 else {
1969 if (isteq(p.n, ist("host")))
1970 params.srv_name = p.v;
1971
Christopher Faulet67d58092019-10-02 10:51:38 +02001972 /* Skip header if same name is used to add the server name */
1973 if (fconn->proxy->server_id_hdr_name &&
1974 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1975 break;
1976
Christopher Faulet99eff652019-08-11 23:11:30 +02001977 memcpy(trash.area, "http_", 5);
1978 memcpy(trash.area+5, p.n.ptr, p.n.len);
1979 p.n = ist2(trash.area, p.n.len+5);
1980 }
1981
1982 if (!fcgi_encode_param(&outbuf, &p)) {
1983 if (b_space_wraps(mbuf))
1984 goto realign_again;
1985 if (outbuf.data == 8)
1986 goto full;
1987 goto done;
1988 }
1989 break;
1990
1991 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001992 if (fconn->proxy->server_id_hdr_name) {
1993 struct server *srv = objt_server(fconn->conn->target);
1994
1995 if (!srv)
1996 goto done;
1997
1998 memcpy(trash.area, "http_", 5);
1999 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2000 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2001 p.v = ist(srv->id);
2002
2003 if (!fcgi_encode_param(&outbuf, &p)) {
2004 if (b_space_wraps(mbuf))
2005 goto realign_again;
2006 if (outbuf.data == 8)
2007 goto full;
2008 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002009 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002010 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002011 goto done;
2012
2013 default:
2014 break;
2015 }
2016 total += size;
2017 blk = htx_remove_blk(htx, blk);
2018 }
2019
2020 done:
2021 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2022 goto error;
2023
2024 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2025 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2026 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2027 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2028 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2029 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2030 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2031 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2032 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2033 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2034 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2035 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2040 goto error;
2041
2042 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002043 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 +02002044 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2045 b_add(mbuf, outbuf.data);
2046
2047 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002048 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002049 return total;
2050 full:
2051 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2052 goto retry;
2053 fconn->flags |= FCGI_CF_MUX_MFULL;
2054 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002055 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 +02002056 if (total)
2057 goto error;
2058 goto end;
2059
2060 error:
2061 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002062 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002063 fcgi_strm_error(fstrm);
2064 goto end;
2065}
2066
2067/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2068 * anything. STDIN records contain the request body.
2069 */
2070static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2071 struct htx *htx, size_t count, struct buffer *buf)
2072{
2073 struct buffer outbuf;
2074 struct buffer *mbuf;
2075 struct htx_blk *blk;
2076 enum htx_blk_type type;
2077 uint32_t size;
2078 size_t total = 0;
2079
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002080 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002081 if (!count)
2082 goto end;
2083
2084 mbuf = br_tail(fconn->mbuf);
2085 retry:
2086 if (!fcgi_get_buf(fconn, mbuf)) {
2087 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002088 fstrm->flags |= FCGI_SF_BLK_MROOM;
2089 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2090 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002091 }
2092
2093 /* Perform some optimizations to reduce the number of buffer copies.
2094 * First, if the mux's buffer is empty and the htx area contains exactly
2095 * one data block of the same size as the requested count, and this
2096 * count fits within the record size, then it's possible to simply swap
2097 * the caller's buffer with the mux's output buffer and adjust offsets
2098 * and length to match the entire DATA HTX block in the middle. In this
2099 * case we perform a true zero-copy operation from end-to-end. This is
2100 * the situation that happens all the time with large files. Second, if
2101 * this is not possible, but the mux's output buffer is empty, we still
2102 * have an opportunity to avoid the copy to the intermediary buffer, by
2103 * making the intermediary buffer's area point to the output buffer's
2104 * area. In this case we want to skip the HTX header to make sure that
2105 * copies remain aligned and that this operation remains possible all
2106 * the time. This goes for headers, data blocks and any data extracted
2107 * from the HTX blocks.
2108 */
2109 blk = htx_get_head_blk(htx);
2110 if (!blk)
2111 goto end;
2112 type = htx_get_blk_type(blk);
2113 size = htx_get_blksz(blk);
2114 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2115 void *old_area = mbuf->area;
2116
2117 if (b_data(mbuf)) {
2118 /* Too bad there are data left there. We're willing to memcpy/memmove
2119 * up to 1/4 of the buffer, which means that it's OK to copy a large
2120 * record into a buffer containing few data if it needs to be realigned,
2121 * and that it's also OK to copy few data without realigning. Otherwise
2122 * we'll pretend the mbuf is full and wait for it to become empty.
2123 */
2124 if (size + 8 <= b_room(mbuf) &&
2125 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2126 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2127 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002128 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002129 }
2130
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002131 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 +02002132 /* map a FCGI record to the HTX block so that we can put the
2133 * record header there.
2134 */
2135 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2136 outbuf.area = b_head(mbuf);
2137
2138 /* prepend a FCGI record header just before the DATA block */
2139 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2140 fcgi_set_record_id(outbuf.area, fstrm->id);
2141 fcgi_set_record_size(outbuf.area, size);
2142
2143 /* and exchange with our old area */
2144 buf->area = old_area;
2145 buf->data = buf->head = 0;
2146 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002147
2148 htx = (struct htx *)buf->area;
2149 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002150 goto end;
2151 }
2152
2153 copy:
2154 while (1) {
2155 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2156 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2157 break;
2158 realign_again:
2159 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2160 }
2161
2162 if (outbuf.size < 8)
2163 goto full;
2164
2165 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2166 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2167 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2168 fcgi_set_record_id(outbuf.area, fstrm->id);
2169 outbuf.data = 8;
2170
2171 blk = htx_get_head_blk(htx);
2172 while (blk && count) {
2173 enum htx_blk_type type = htx_get_blk_type(blk);
2174 uint32_t size = htx_get_blksz(blk);
2175 struct ist v;
2176
2177 switch (type) {
2178 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002179 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 +02002180 v = htx_get_blk_value(htx, blk);
2181 if (v.len > count)
2182 v.len = count;
2183
2184 if (v.len > b_room(&outbuf)) {
2185 /* It doesn't fit at once. If it at least fits once split and
2186 * the amount of data to move is low, let's defragment the
2187 * buffer now.
2188 */
2189 if (b_space_wraps(mbuf) &&
2190 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2191 b_data(mbuf) <= MAX_DATA_REALIGN)
2192 goto realign_again;
2193 v.len = b_room(&outbuf);
2194 }
2195 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2196 if (outbuf.data == 8)
2197 goto full;
2198 goto done;
2199 }
2200 if (v.len != size) {
2201 total += v.len;
2202 count -= v.len;
2203 htx_cut_data_blk(htx, blk, v.len);
2204 goto done;
2205 }
2206 break;
2207
2208 case HTX_BLK_EOM:
2209 goto done;
2210
2211 default:
2212 break;
2213 }
2214 total += size;
2215 count -= size;
2216 blk = htx_remove_blk(htx, blk);
2217 }
2218
2219 done:
2220 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002221 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 +02002222 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2223 b_add(mbuf, outbuf.data);
2224
2225 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002226 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002227 return total;
2228 full:
2229 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2230 goto retry;
2231 fconn->flags |= FCGI_CF_MUX_MFULL;
2232 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002233 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 +02002234 goto end;
2235}
2236
2237/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2238 * anything. STDOUT records contain the entire response. All the content is
2239 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2240 */
2241static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2242{
2243 struct buffer *dbuf;
2244 size_t ret;
2245 size_t max;
2246
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002247 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2248
Christopher Faulet99eff652019-08-11 23:11:30 +02002249 dbuf = &fconn->dbuf;
2250
2251 /* Only padding remains */
2252 if (fconn->state == FCGI_CS_RECORD_P)
2253 goto end_transfer;
2254
2255 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2256 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2257 buf_room_for_htx_data(dbuf))
2258 goto fail; // incomplete record
2259
2260 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2261 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002262 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2263 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002264 }
2265
2266 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2267 max = buf_room_for_htx_data(&fstrm->rxbuf);
2268 if (!b_data(&fstrm->rxbuf))
2269 fstrm->rxbuf.head = sizeof(struct htx);
2270 if (max > fconn->drl)
2271 max = fconn->drl;
2272
2273 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2274 if (!ret)
2275 goto fail;
2276 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002277 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2278 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 +02002279
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002280 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002281 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002282 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2283 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002284
2285 if (fconn->drl)
2286 goto fail;
2287
2288 end_transfer:
2289 fconn->drl += fconn->drp;
2290 fconn->drp = 0;
2291 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2292 b_del(&fconn->dbuf, ret);
2293 fconn->drl -= ret;
2294 if (fconn->drl)
2295 goto fail;
2296
2297 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002298 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2299 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002300 return 1;
2301 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002302 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 +02002303 return 0;
2304}
2305
2306
2307/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2308 * anything. It only skip the padding in fact, there is no payload for such
2309 * records. It makrs the end of the response.
2310 */
2311static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2312{
2313 int ret;
2314
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002315 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2316
Christopher Faulet99eff652019-08-11 23:11:30 +02002317 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002318 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002319 fconn->drl += fconn->drp;
2320 fconn->drp = 0;
2321 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2322 b_del(&fconn->dbuf, ret);
2323 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002324 if (fconn->drl) {
2325 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 +02002326 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002327 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002328 fconn->state = FCGI_CS_RECORD_H;
2329 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002330 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2331 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);
2332 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002333 return 1;
2334}
2335
2336/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2337 * anything.
2338 */
2339static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2340{
2341 struct buffer *dbuf;
2342 struct buffer tag;
2343 size_t ret;
2344
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002345 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002346 dbuf = &fconn->dbuf;
2347
2348 /* Only padding remains */
2349 if (fconn->state == FCGI_CS_RECORD_P)
2350 goto end_transfer;
2351
2352 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2353 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2354 buf_room_for_htx_data(dbuf))
2355 goto fail; // incomplete record
2356
2357 chunk_reset(&trash);
2358 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2359 if (!ret)
2360 goto fail;
2361 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002362 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 +02002363
2364 trash.area[ret] = '\n';
2365 trash.area[ret+1] = '\0';
2366 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002367 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002368
2369 if (fconn->drl)
2370 goto fail;
2371
2372 end_transfer:
2373 fconn->drl += fconn->drp;
2374 fconn->drp = 0;
2375 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2376 b_del(&fconn->dbuf, ret);
2377 fconn->drl -= ret;
2378 if (fconn->drl)
2379 goto fail;
2380 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002381 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2382 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002383 return 1;
2384 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002385 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 +02002386 return 0;
2387}
2388
2389/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2390 * anything. If the empty STDOUT record is not already received, this one marks
2391 * the end of the response. It is highly unexpected, but if the record is larger
2392 * than a buffer and cannot be decoded in one time, an error is triggered and
2393 * the connection is closed. END_REQUEST record cannot be split.
2394 */
2395static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2396{
2397 struct buffer inbuf;
2398 struct buffer *dbuf;
2399 struct fcgi_end_request endreq;
2400
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002401 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002402 dbuf = &fconn->dbuf;
2403
2404 /* Record too large to be fully decoded */
2405 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2406 goto fail;
2407
2408 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002409 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2410 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002411 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002412 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002413
2414 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2415 /* Realign the dmux buffer if the record wraps. It is unexpected
2416 * at this stage because it should be the first record received
2417 * from the FCGI application.
2418 */
2419 b_slow_realign(dbuf, trash.area, 0);
2420 }
2421
2422 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2423
2424 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2425 goto fail;
2426
2427 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002428 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2429 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 +02002430 fstrm->proto_status = endreq.errcode;
2431 fcgi_strm_close(fstrm);
2432
2433 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2434 fconn->drl = 0;
2435 fconn->drp = 0;
2436 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002437 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2438 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002439 return 1;
2440
2441 fail:
2442 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002443 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 +02002444 return 0;
2445}
2446
2447/* process Rx records to be demultiplexed */
2448static void fcgi_process_demux(struct fcgi_conn *fconn)
2449{
2450 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2451 struct fcgi_header hdr;
2452 int ret;
2453
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002454 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2455
Christopher Faulet99eff652019-08-11 23:11:30 +02002456 if (fconn->state == FCGI_CS_CLOSED)
2457 return;
2458
2459 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002460 if (fconn->state == FCGI_CS_INIT) {
2461 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2462 return;
2463 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002464 if (fconn->state == FCGI_CS_SETTINGS) {
2465 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002466 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002467 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2468 if (!ret)
2469 goto fail;
2470 b_del(&fconn->dbuf, ret);
2471
2472 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2473 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002474 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);
2475 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 +02002476 goto fail;
2477 }
2478 goto new_record;
2479 }
2480 }
2481
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002482 /* process as many incoming records as possible below */
2483 while (1) {
2484 if (!b_data(&fconn->dbuf)) {
2485 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2486 break;
2487 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002488
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002489 if (fconn->state == FCGI_CS_CLOSED) {
2490 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002491 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002492 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002493
2494 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002495 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002496 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2497 if (!ret)
2498 break;
2499 b_del(&fconn->dbuf, ret);
2500
2501 new_record:
2502 fconn->dsi = hdr.id;
2503 fconn->drt = hdr.type;
2504 fconn->drl = hdr.len;
2505 fconn->drp = hdr.padding;
2506 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002507 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 +02002508 }
2509
2510 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2511 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2512
2513 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2514 (b_data(&fstrm->rxbuf) ||
2515 conn_xprt_read0_pending(fconn->conn) ||
2516 fstrm->state == FCGI_SS_CLOSED ||
2517 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2518 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2519 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002520 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 +02002521 fstrm->cs->flags |= CS_FL_RCV_MORE;
2522 fcgi_strm_notify_recv(fstrm);
2523 }
2524 fstrm = tmp_fstrm;
2525
2526 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2527 /* ignore all record for closed streams */
2528 goto ignore_record;
2529 }
2530 if (fstrm->state == FCGI_SS_IDLE) {
2531 /* ignore all record for unknown streams */
2532 goto ignore_record;
2533 }
2534
2535 switch (fconn->drt) {
2536 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002537 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 +02002538 ret = fcgi_conn_handle_values_result(fconn);
2539 break;
2540
2541 case FCGI_STDOUT:
2542 if (fstrm->flags & FCGI_SF_ES_RCVD)
2543 goto ignore_record;
2544
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002545 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002546 if (fconn->drl)
2547 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2548 else
2549 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2550 break;
2551
2552 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002553 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002554 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2555 break;
2556
2557 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002558 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 +02002559 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2560 break;
2561
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002562 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002563 default:
2564 ignore_record:
2565 /* drop records that we ignore. They may be
2566 * larger than the buffer so we drain all of
2567 * their contents until we reach the end.
2568 */
2569 fconn->state = FCGI_CS_RECORD_P;
2570 fconn->drl += fconn->drp;
2571 fconn->drp = 0;
2572 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002573 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2574 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002575 b_del(&fconn->dbuf, ret);
2576 fconn->drl -= ret;
2577 ret = (fconn->drl == 0);
2578 }
2579
2580 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002581 if (ret <= 0) {
2582 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002583 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002584 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002585
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002586 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002587 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2589 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002590 }
2591
2592 fail:
2593 /* we can go here on missing data, blocked response or error */
2594 if (fstrm && fstrm->cs &&
2595 (b_data(&fstrm->rxbuf) ||
2596 conn_xprt_read0_pending(fconn->conn) ||
2597 fstrm->state == FCGI_SS_CLOSED ||
2598 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2599 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2600 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 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 +02002602 fstrm->cs->flags |= CS_FL_RCV_MORE;
2603 fcgi_strm_notify_recv(fstrm);
2604 }
2605
2606 fcgi_conn_restart_reading(fconn, 0);
2607}
2608
2609/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2610 * the end.
2611 */
2612static int fcgi_process_mux(struct fcgi_conn *fconn)
2613{
2614 struct fcgi_strm *fstrm, *fstrm_back;
2615
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002616 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2617
Christopher Faulet99eff652019-08-11 23:11:30 +02002618 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2619 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2620 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2621 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002622 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 +02002623 fcgi_wake_unassigned_streams(fconn);
2624 goto mux;
2625 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002626 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002627 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2628 goto fail;
2629 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002630 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 +02002631 }
2632 /* need to wait for the other side */
2633 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002635 }
2636
2637 mux:
2638 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2639 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2640 break;
2641
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002642 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002643 continue;
2644
Willy Tarreau7aad7032020-01-16 17:20:57 +01002645 /* If the sender changed his mind and unsubscribed, let's just
2646 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002647 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002648 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2649 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002650 LIST_DEL_INIT(&fstrm->send_list);
2651 continue;
2652 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002653
2654 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002655 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2656 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002657 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002658 tasklet_wakeup(fstrm->subs->tasklet);
2659 fstrm->subs->events &= ~SUB_RETRY_SEND;
2660 if (!fstrm->subs->events)
2661 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002662 } else {
2663 /* it's the shut request that was queued */
2664 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2665 tasklet_wakeup(fstrm->shut_tl);
2666 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002667 }
2668
2669 fail:
2670 if (fconn->state == FCGI_CS_CLOSED) {
2671 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2672 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002673 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2674 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002675 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002676 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002677 }
2678 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002679
2680 done:
2681 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002682 return 1;
2683}
2684
2685
2686/* Attempt to read data, and subscribe if none available.
2687 * The function returns 1 if data has been received, otherwise zero.
2688 */
2689static int fcgi_recv(struct fcgi_conn *fconn)
2690{
2691 struct connection *conn = fconn->conn;
2692 struct buffer *buf;
2693 int max;
2694 size_t ret;
2695
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002696 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2697
2698 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2699 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002700 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002701 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002702
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002703 if (!fcgi_recv_allowed(fconn)) {
2704 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002705 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002706 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002707
2708 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2709 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002710 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002711 fconn->flags |= FCGI_CF_DEM_DALLOC;
2712 return 0;
2713 }
2714
2715 b_realign_if_empty(buf);
2716 if (!b_data(buf)) {
2717 /* try to pre-align the buffer like the
2718 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002719 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002720 * HTX block to alias it upon recv. We cannot use the
2721 * head because rcv_buf() will realign the buffer if
2722 * it's empty. Thus we cheat and pretend we already
2723 * have a few bytes there.
2724 */
2725 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2726 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2727 }
2728 else
2729 max = buf_room_for_htx_data(buf);
2730
2731 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2732
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002733 if (max && !ret && fcgi_recv_allowed(fconn)) {
2734 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002735 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002736 }
2737 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002738 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002739
2740 if (!b_data(buf)) {
2741 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002742 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2744 }
2745
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002746 if (ret == max) {
2747 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002748 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002749 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002750
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002751 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002752 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2753}
2754
2755
2756/* Try to send data if possible.
2757 * The function returns 1 if data have been sent, otherwise zero.
2758 */
2759static int fcgi_send(struct fcgi_conn *fconn)
2760{
2761 struct connection *conn = fconn->conn;
2762 int done;
2763 int sent = 0;
2764
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002765 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2766
2767 if (conn->flags & CO_FL_ERROR) {
2768 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002769 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002770 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002771
2772
2773 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2774 /* a handshake was requested */
2775 goto schedule;
2776 }
2777
2778 /* This loop is quite simple : it tries to fill as much as it can from
2779 * pending streams into the existing buffer until it's reportedly full
2780 * or the end of send requests is reached. Then it tries to send this
2781 * buffer's contents out, marks it not full if at least one byte could
2782 * be sent, and tries again.
2783 *
2784 * The snd_buf() function normally takes a "flags" argument which may
2785 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2786 * data immediately comes and CO_SFL_STREAMER to indicate that the
2787 * connection is streaming lots of data (used to increase TLS record
2788 * size at the expense of latency). The former can be sent any time
2789 * there's a buffer full flag, as it indicates at least one stream
2790 * attempted to send and failed so there are pending data. An
2791 * alternative would be to set it as long as there's an active stream
2792 * but that would be problematic for ACKs until we have an absolute
2793 * guarantee that all waiters have at least one byte to send. The
2794 * latter should possibly not be set for now.
2795 */
2796
2797 done = 0;
2798 while (!done) {
2799 unsigned int flags = 0;
2800 unsigned int released = 0;
2801 struct buffer *buf;
2802
2803 /* fill as much as we can into the current buffer */
2804 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2805 done = fcgi_process_mux(fconn);
2806
2807 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2808 done = 1; // we won't go further without extra buffers
2809
2810 if (conn->flags & CO_FL_ERROR)
2811 break;
2812
2813 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2814 flags |= CO_SFL_MSG_MORE;
2815
2816 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2817 if (b_data(buf)) {
2818 int ret;
2819
2820 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2821 if (!ret) {
2822 done = 1;
2823 break;
2824 }
2825 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002826 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002827 b_del(buf, ret);
2828 if (b_data(buf)) {
2829 done = 1;
2830 break;
2831 }
2832 }
2833 b_free(buf);
2834 released++;
2835 }
2836
2837 if (released)
2838 offer_buffers(NULL, tasks_run_queue);
2839
2840 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002841 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2842 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002843 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2844 }
2845
2846 if (conn->flags & CO_FL_SOCK_WR_SH) {
2847 /* output closed, nothing to send, clear the buffer to release it */
2848 b_reset(br_tail(fconn->mbuf));
2849 }
2850 /* We're not full anymore, so we can wake any task that are waiting
2851 * for us.
2852 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002853 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002854 struct fcgi_strm *fstrm;
2855
2856 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2857 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2858 break;
2859
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002860 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002861 continue;
2862
Willy Tarreau7aad7032020-01-16 17:20:57 +01002863 /* If the sender changed his mind and unsubscribed, let's just
2864 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002865 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002866 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2867 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002868 LIST_DEL_INIT(&fstrm->send_list);
2869 continue;
2870 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002871
2872 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002873 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002874 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002875 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002876 tasklet_wakeup(fstrm->subs->tasklet);
2877 fstrm->subs->events &= ~SUB_RETRY_SEND;
2878 if (!fstrm->subs->events)
2879 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002880 } else {
2881 /* it's the shut request that was queued */
2882 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2883 tasklet_wakeup(fstrm->shut_tl);
2884 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002885 }
2886 }
2887 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002888 if (!br_data(fconn->mbuf)) {
2889 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002890 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002891 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002892schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002893 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2894 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002895 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002896 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002897
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002898 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002899 return sent;
2900}
2901
2902/* this is the tasklet referenced in fconn->wait_event.tasklet */
2903static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2904{
2905 struct fcgi_conn *fconn = ctx;
2906 int ret = 0;
2907
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002908 TRACE_POINT(FCGI_EV_FCONN_WAKE, fconn->conn);
2909
Christopher Faulet99eff652019-08-11 23:11:30 +02002910 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2911 ret = fcgi_send(fconn);
2912 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2913 ret |= fcgi_recv(fconn);
2914 if (ret || b_data(&fconn->dbuf))
2915 fcgi_process(fconn);
2916 return NULL;
2917}
2918
2919/* callback called on any event by the connection handler.
2920 * It applies changes and returns zero, or < 0 if it wants immediate
2921 * destruction of the connection (which normally doesn not happen in FCGI).
2922 */
2923static int fcgi_process(struct fcgi_conn *fconn)
2924{
2925 struct connection *conn = fconn->conn;
2926
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002927 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2928
Christopher Faulet99eff652019-08-11 23:11:30 +02002929 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2930 fcgi_process_demux(fconn);
2931
2932 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2933 b_reset(&fconn->dbuf);
2934
2935 if (buf_room_for_htx_data(&fconn->dbuf))
2936 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2937 }
2938 fcgi_send(fconn);
2939
2940 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2941 /* frontend is stopping, reload likely in progress, let's try
2942 * to announce a graceful shutdown if not yet done. We don't
2943 * care if it fails, it will be tried again later.
2944 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002945 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 +02002946 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2947 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2948 fcgi_conn_send_aborts(fconn);
2949 }
2950 }
2951
2952 /*
2953 * If we received early data, and the handshake is done, wake
2954 * any stream that was waiting for it.
2955 */
2956 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
2957 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2958 struct eb32_node *node;
2959 struct fcgi_strm *fstrm;
2960
2961 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
2962 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
2963
2964 while (node) {
2965 fstrm = container_of(node, struct fcgi_strm, by_id);
2966 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
2967 fcgi_strm_notify_recv(fstrm);
2968 node = eb32_next(node);
2969 }
2970 }
2971
2972 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
2973 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2974 eb_is_empty(&fconn->streams_by_id)) {
2975 fcgi_wake_some_streams(fconn, 0);
2976
2977 if (eb_is_empty(&fconn->streams_by_id)) {
2978 /* no more stream, kill the connection now */
2979 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002980 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02002981 return -1;
2982 }
2983 }
2984
2985 if (!b_data(&fconn->dbuf))
2986 fcgi_release_buf(fconn, &fconn->dbuf);
2987
2988 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2989 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
2990 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
2991 fcgi_release_mbuf(fconn);
2992
2993 if (fconn->task) {
2994 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
2995 task_queue(fconn->task);
2996 }
2997
2998 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002999 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003000 return 0;
3001}
3002
3003
3004/* wake-up function called by the connection layer (mux_ops.wake) */
3005static int fcgi_wake(struct connection *conn)
3006{
3007 struct fcgi_conn *fconn = conn->ctx;
3008
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003009 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003010 return (fcgi_process(fconn));
3011}
3012
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003013
3014static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3015{
3016 int ret = 0;
3017 switch (mux_ctl) {
3018 case MUX_STATUS:
3019 if (conn->flags & CO_FL_CONNECTED)
3020 ret |= MUX_STATUS_READY;
3021 return ret;
3022 default:
3023 return -1;
3024 }
3025}
3026
Christopher Faulet99eff652019-08-11 23:11:30 +02003027/* Connection timeout management. The principle is that if there's no receipt
3028 * nor sending for a certain amount of time, the connection is closed. If the
3029 * MUX buffer still has lying data or is not allocatable, the connection is
3030 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003031 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003032 */
3033static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3034{
3035 struct fcgi_conn *fconn = context;
3036 int expired = tick_is_expired(t->expire, now_ms);
3037
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003038 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3039
3040 if (!expired && fconn) {
3041 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003042 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003043 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003044
3045 task_destroy(t);
3046
3047 if (!fconn) {
3048 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003049 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003050 return NULL;
3051 }
3052
3053 fconn->task = NULL;
3054 fconn->state = FCGI_CS_CLOSED;
3055 fcgi_wake_some_streams(fconn, 0);
3056
3057 if (br_data(fconn->mbuf)) {
3058 /* don't even try to send aborts, the buffer is stuck */
3059 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3060 goto end;
3061 }
3062
3063 /* try to send but no need to insist */
3064 if (!fcgi_conn_send_aborts(fconn))
3065 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3066
3067 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3068 conn_xprt_ready(fconn->conn)) {
3069 unsigned int released = 0;
3070 struct buffer *buf;
3071
3072 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3073 if (b_data(buf)) {
3074 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3075 buf, b_data(buf), 0);
3076 if (!ret)
3077 break;
3078 b_del(buf, ret);
3079 if (b_data(buf))
3080 break;
3081 b_free(buf);
3082 released++;
3083 }
3084 }
3085
3086 if (released)
3087 offer_buffers(NULL, tasks_run_queue);
3088 }
3089
3090 end:
3091 /* either we can release everything now or it will be done later once
3092 * the last stream closes.
3093 */
3094 if (eb_is_empty(&fconn->streams_by_id))
3095 fcgi_release(fconn);
3096
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003097 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003098 return NULL;
3099}
3100
3101
3102/*******************************************/
3103/* functions below are used by the streams */
3104/*******************************************/
3105
3106/* Append the description of what is present in error snapshot <es> into <out>.
3107 * The description must be small enough to always fit in a buffer. The output
3108 * buffer may be the trash so the trash must not be used inside this function.
3109 */
3110static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3111{
3112 chunk_appendf(out,
3113 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3114 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3115 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3116 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3117 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3118 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3119}
3120/*
3121 * Capture a bad response and archive it in the proxy's structure. By default
3122 * it tries to report the error position as h1m->err_pos. However if this one is
3123 * not set, it will then report h1m->next, which is the last known parsing
3124 * point. The function is able to deal with wrapping buffers. It always displays
3125 * buffers as a contiguous area starting at buf->p. The direction is determined
3126 * thanks to the h1m's flags.
3127 */
3128static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3129 struct h1m *h1m, struct buffer *buf)
3130{
3131 struct session *sess = fstrm->sess;
3132 struct proxy *proxy = fconn->proxy;
3133 struct proxy *other_end = sess->fe;
3134 union error_snapshot_ctx ctx;
3135
3136 /* http-specific part now */
3137 ctx.h1.state = h1m->state;
3138 ctx.h1.c_flags = fconn->flags;
3139 ctx.h1.s_flags = fstrm->flags;
3140 ctx.h1.m_flags = h1m->flags;
3141 ctx.h1.m_clen = h1m->curr_len;
3142 ctx.h1.m_blen = h1m->body_len;
3143
3144 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3145 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3146 &ctx, fcgi_show_error_snapshot);
3147}
3148
3149static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3150 struct buffer *buf, size_t *ofs, size_t max)
3151{
3152 int ret;
3153
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003154 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003155 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3156 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003157 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 +02003158 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003159 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 +02003160 fcgi_strm_error(fstrm);
3161 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3162 }
3163 goto end;
3164 }
3165
3166 *ofs += ret;
3167 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003168 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003169 return ret;
3170
3171}
3172
Christopher Fauletaf542632019-10-01 21:52:49 +02003173static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003174 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3175{
3176 int ret;
3177
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003178 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003179 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003180 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003181 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 +02003182 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003183 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 +02003184 fcgi_strm_error(fstrm);
3185 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3186 }
3187 goto end;
3188 }
3189 *ofs += ret;
3190 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003191 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003192 return ret;
3193}
3194
3195static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3196 struct buffer *buf, size_t *ofs, size_t max)
3197{
3198 int ret;
3199
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003200 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003201 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003202 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003203 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 +02003204 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003205 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 +02003206 fcgi_strm_error(fstrm);
3207 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3208 }
3209 goto end;
3210 }
3211 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003212 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003213 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003214 return ret;
3215}
3216
3217static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003218 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003219{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003220 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003221
Christopher Faulet76014fd2019-12-10 11:47:22 +01003222 TRACE_ENTER(FCGI_EV_RSP_DATA||FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
3223 ret = h1_parse_msg_eom(h1m, htx, max);
3224 if (!ret) {
3225 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3226 if (htx->flags & HTX_FL_PARSING_ERROR) {
3227 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3228 fcgi_strm_error(fstrm);
3229 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3230 }
3231 goto end;
3232 }
3233 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3234 end:
3235 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3236 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003237}
3238
3239static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3240{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003241 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003242 struct htx *htx;
3243 struct h1m *h1m = &fstrm->h1m;
3244 size_t ret, data, total = 0;
3245
3246 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003247 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3248
Christopher Faulet99eff652019-08-11 23:11:30 +02003249 data = htx->data;
3250 if (fstrm->state == FCGI_SS_ERROR)
3251 goto end;
3252
3253 do {
3254 size_t used = htx_used_space(htx);
3255
3256 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003257 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003258 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3259 if (!ret)
3260 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003261
3262 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3263
Christopher Faulet99eff652019-08-11 23:11:30 +02003264 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3265 struct htx_blk *blk = htx_get_head_blk(htx);
3266 struct htx_sl *sl;
3267
3268 if (!blk)
3269 break;
3270 sl = htx_get_blk_ptr(htx, blk);
3271 sl->flags |= HTX_SL_F_XFER_LEN;
3272 htx->extra = 0;
3273 }
3274 }
3275 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003276 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003277 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003278 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003279 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003280
3281 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 +02003282 }
3283 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003284 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3285 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3286 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003287 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003288
Christopher Faulet76014fd2019-12-10 11:47:22 +01003289 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 +02003290 }
3291 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003292 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3293 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3294 break;
3295
3296 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3297 }
3298
Christopher Faulet99eff652019-08-11 23:11:30 +02003299 if (b_data(&fstrm->rxbuf) > total) {
3300 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003301 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003302 fcgi_strm_error(fstrm);
3303 }
3304 break;
3305 }
3306 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003307 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003308 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003309
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 if (fstrm->state != FCGI_SS_ERROR &&
3311 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003312 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003313 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3314 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3315 h1m->state = H1_MSG_DONE;
3316 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 +02003317 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003318 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003319 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003320
3321 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003322 }
3323 else {
3324 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003325 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003326 fcgi_strm_error(fstrm);
3327 break;
3328 }
3329
3330 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003331 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003332
3333 if (fstrm->state == FCGI_SS_ERROR) {
3334 b_reset(&fstrm->rxbuf);
3335 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003336 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003337 return 0;
3338 }
3339
3340 b_del(&fstrm->rxbuf, total);
3341
3342 end:
3343 htx_to_buf(htx, buf);
3344 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003345 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003346 return ret;
3347}
3348
3349/*
3350 * Attach a new stream to a connection
3351 * (Used for outgoing connections)
3352 */
3353static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3354{
3355 struct conn_stream *cs;
3356 struct fcgi_strm *fstrm;
3357 struct fcgi_conn *fconn = conn->ctx;
3358
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003359 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003360 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003361 if (!cs) {
3362 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003363 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003364 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003365 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3366 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003367 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003368 cs_free(cs);
3369 return NULL;
3370 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003371 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003372 return cs;
3373}
3374
3375/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3376 * We have to scan because we may have some orphan streams. It might be
3377 * beneficial to scan backwards from the end to reduce the likeliness to find
3378 * orphans.
3379 */
3380static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3381{
3382 struct fcgi_conn *fconn = conn->ctx;
3383 struct fcgi_strm *fstrm;
3384 struct eb32_node *node;
3385
3386 node = eb32_first(&fconn->streams_by_id);
3387 while (node) {
3388 fstrm = container_of(node, struct fcgi_strm, by_id);
3389 if (fstrm->cs)
3390 return fstrm->cs;
3391 node = eb32_next(node);
3392 }
3393 return NULL;
3394}
3395
3396/*
3397 * Destroy the mux and the associated connection, if it is no longer used
3398 */
3399static void fcgi_destroy(void *ctx)
3400{
3401 struct fcgi_conn *fconn = ctx;
3402
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003403 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003404 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3405 fcgi_release(fconn);
3406}
3407
3408/*
3409 * Detach the stream from the connection and possibly release the connection.
3410 */
3411static void fcgi_detach(struct conn_stream *cs)
3412{
3413 struct fcgi_strm *fstrm = cs->ctx;
3414 struct fcgi_conn *fconn;
3415 struct session *sess;
3416
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003417 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3418
Christopher Faulet99eff652019-08-11 23:11:30 +02003419 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003420 if (!fstrm) {
3421 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003423 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003424
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003425 /* there's no txbuf so we're certain no to be able to send anything */
3426 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003427
3428 sess = fstrm->sess;
3429 fconn = fstrm->fconn;
3430 fstrm->cs = NULL;
3431 fconn->nb_cs--;
3432
3433 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3434 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3435 fconn->streams_limit = 1;
3436 }
3437 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3438 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3439 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3440 fconn->state = FCGI_CS_CLOSED;
3441 }
3442
3443 /* this stream may be blocked waiting for some data to leave, so orphan
3444 * it in this case.
3445 */
3446 if (!(cs->conn->flags & CO_FL_ERROR) &&
3447 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003448 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003449 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003450 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003451 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003452 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003453
3454 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3455 /* unblock the connection if it was blocked on this stream. */
3456 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3457 fcgi_conn_restart_reading(fconn, 1);
3458 }
3459
3460 fcgi_strm_destroy(fstrm);
3461
3462 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
3463 !(fconn->flags & FCGI_CF_KEEP_CONN)) {
3464 if (!fconn->conn->owner) {
3465 fconn->conn->owner = sess;
3466 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3467 fconn->conn->owner = NULL;
3468 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003469 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003470 /* The server doesn't want it, let's kill the connection right away */
3471 fconn->conn->mux->destroy(fconn->conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003472 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3473 }
3474 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003475 return;
3476 }
3477 }
3478 }
3479 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003480 int ret = session_check_idle_conn(fconn->conn->owner, fconn->conn);
3481 if (ret == -1) {
3482 /* The connection is destroyed, let's leave */
3483 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003484 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003485 }
3486 else if (ret == 1) {
3487 /* The connection was added to the server idle list, just stop */
3488 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3489 return;
3490 }
3491 TRACE_DEVEL("connection in idle session list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003492 }
3493 /* Never ever allow to reuse a connection from a non-reuse backend */
3494 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3495 fconn->conn->flags |= CO_FL_PRIVATE;
3496 if (!LIST_ADDED(&fconn->conn->list) && fconn->nb_streams < fconn->streams_limit) {
3497 struct server *srv = objt_server(fconn->conn->target);
3498
3499 if (srv) {
3500 if (fconn->conn->flags & CO_FL_PRIVATE)
3501 LIST_ADD(&srv->priv_conns[tid], &fconn->conn->list);
3502 else
3503 LIST_ADD(&srv->idle_conns[tid], &fconn->conn->list);
3504 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003505 TRACE_DEVEL("connection in idle server list", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003506 }
3507 }
3508
3509 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003510 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003511 */
3512 if (fcgi_conn_is_dead(fconn)) {
3513 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003514 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003515 fcgi_release(fconn);
3516 }
3517 else if (fconn->task) {
3518 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3519 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003520 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003521 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003522 else
3523 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003524}
3525
3526
3527/* Performs a synchronous or asynchronous shutr(). */
3528static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3529{
3530 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003531
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003532 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3533
Christopher Faulet99eff652019-08-11 23:11:30 +02003534 if (fstrm->state == FCGI_SS_CLOSED)
3535 goto done;
3536
3537 /* a connstream may require us to immediately kill the whole connection
3538 * for example because of a "tcp-request content reject" rule that is
3539 * normally used to limit abuse.
3540 */
3541 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003542 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3543 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003544 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003545 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003546 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003547 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003548 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3549 !fcgi_strm_send_abort(fconn, fstrm))
3550 goto add_to_list;
3551 }
3552
3553 fcgi_strm_close(fstrm);
3554
3555 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3556 tasklet_wakeup(fconn->wait_event.tasklet);
3557 done:
3558 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003559 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003560 return;
3561
3562 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003563 /* Let the handler know we want to shutr, and add ourselves to the
3564 * send list if not yet done. fcgi_deferred_shut() will be
3565 * automatically called via the shut_tl tasklet when there's room
3566 * again.
3567 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003568 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003569 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003570 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3571 }
3572 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003573 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003574 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003575 return;
3576}
3577
3578/* Performs a synchronous or asynchronous shutw(). */
3579static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3580{
3581 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003582
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003583 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3584
Christopher Faulet99eff652019-08-11 23:11:30 +02003585 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3586 goto done;
3587
3588 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3589 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3590 !fcgi_strm_send_abort(fconn, fstrm))
3591 goto add_to_list;
3592
3593 if (fstrm->state == FCGI_SS_HREM)
3594 fcgi_strm_close(fstrm);
3595 else
3596 fstrm->state = FCGI_SS_HLOC;
3597 } else {
3598 /* a connstream may require us to immediately kill the whole connection
3599 * for example because of a "tcp-request content reject" rule that is
3600 * normally used to limit abuse.
3601 */
3602 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003603 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3604 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003605 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003606 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003607
3608 fcgi_strm_close(fstrm);
3609 }
3610
3611 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3612 tasklet_wakeup(fconn->wait_event.tasklet);
3613 done:
3614 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003615 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003616 return;
3617
3618 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003619 /* Let the handler know we want to shutr, and add ourselves to the
3620 * send list if not yet done. fcgi_deferred_shut() will be
3621 * automatically called via the shut_tl tasklet when there's room
3622 * again.
3623 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003624 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003625 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003626 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3627 }
3628 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003629 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003630 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003631 return;
3632}
3633
Willy Tarreau7aad7032020-01-16 17:20:57 +01003634/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003635 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003636 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 */
3638static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3639{
3640 struct fcgi_strm *fstrm = ctx;
3641 struct fcgi_conn *fconn = fstrm->fconn;
3642
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003643 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3644
Willy Tarreau7aad7032020-01-16 17:20:57 +01003645 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3646 /* some data processing remains to be done first */
3647 goto end;
3648 }
3649
Christopher Faulet99eff652019-08-11 23:11:30 +02003650 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3651 fcgi_do_shutw(fstrm);
3652
3653 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3654 fcgi_do_shutr(fstrm);
3655
3656 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3657 /* We're done trying to send, remove ourself from the send_list */
3658 LIST_DEL_INIT(&fstrm->send_list);
3659
3660 if (!fstrm->cs) {
3661 fcgi_strm_destroy(fstrm);
3662 if (fcgi_conn_is_dead(fconn))
3663 fcgi_release(fconn);
3664 }
3665 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003666 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003667 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003668 return NULL;
3669}
3670
3671/* shutr() called by the conn_stream (mux_ops.shutr) */
3672static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3673{
3674 struct fcgi_strm *fstrm = cs->ctx;
3675
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003676 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003677 if (cs->flags & CS_FL_KILL_CONN)
3678 fstrm->flags |= FCGI_SF_KILL_CONN;
3679
3680 if (!mode)
3681 return;
3682
3683 fcgi_do_shutr(fstrm);
3684}
3685
3686/* shutw() called by the conn_stream (mux_ops.shutw) */
3687static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3688{
3689 struct fcgi_strm *fstrm = cs->ctx;
3690
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003691 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003692 if (cs->flags & CS_FL_KILL_CONN)
3693 fstrm->flags |= FCGI_SF_KILL_CONN;
3694
3695 fcgi_do_shutw(fstrm);
3696}
3697
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003698/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3699 * event subscriber <es> is not allowed to change from a previous call as long
3700 * as at least one event is still subscribed. The <event_type> must only be a
3701 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003702 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003703static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003704{
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 struct fcgi_strm *fstrm = cs->ctx;
3706 struct fcgi_conn *fconn = fstrm->fconn;
3707
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003708 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
3709 BUG_ON(fstrm->subs && fstrm->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003710 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003711
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003712 es->events |= event_type;
3713 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003714
3715 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003716 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003717
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003719 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003720 if (!LIST_ADDED(&fstrm->send_list))
3721 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003722 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003723 return 0;
3724}
3725
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003726/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3727 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3728 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003729 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003730static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003731{
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003733 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003734
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003735 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003736 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003737
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003738 es->events &= ~event_type;
3739 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003740 fstrm->subs = NULL;
3741
3742 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003743 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003744
Christopher Faulet99eff652019-08-11 23:11:30 +02003745 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003746 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003747 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003748 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3749 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003750 }
3751 return 0;
3752}
3753
3754/* Called from the upper layer, to receive data */
3755static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3756{
3757 struct fcgi_strm *fstrm = cs->ctx;
3758 struct fcgi_conn *fconn = fstrm->fconn;
3759 size_t ret = 0;
3760
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003761 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3762
Christopher Faulet99eff652019-08-11 23:11:30 +02003763 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3764 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003765 else
3766 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003767
Christopher Faulet76014fd2019-12-10 11:47:22 +01003768 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 +02003769 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3770 else {
3771 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003772 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003773 cs->flags |= CS_FL_EOI;
3774 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3775 cs->flags |= CS_FL_EOS;
3776 }
3777 if (conn_xprt_read0_pending(fconn->conn))
3778 cs->flags |= CS_FL_EOS;
3779 if (cs->flags & CS_FL_ERR_PENDING)
3780 cs->flags |= CS_FL_ERROR;
3781 fcgi_release_buf(fconn, &fstrm->rxbuf);
3782 }
3783
3784 if (ret && fconn->dsi == fstrm->id) {
3785 /* demux is blocking on this stream's buffer */
3786 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3787 fcgi_conn_restart_reading(fconn, 1);
3788 }
3789
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003790 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003791 return ret;
3792}
3793
3794
Christopher Faulet99eff652019-08-11 23:11:30 +02003795/* Called from the upper layer, to send data from buffer <buf> for no more than
3796 * <count> bytes. Returns the number of bytes effectively sent. Some status
3797 * flags may be updated on the conn_stream.
3798 */
3799static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3800{
3801 struct fcgi_strm *fstrm = cs->ctx;
3802 struct fcgi_conn *fconn = fstrm->fconn;
3803 size_t total = 0;
3804 size_t ret;
3805 struct htx *htx = NULL;
3806 struct htx_sl *sl;
3807 struct htx_blk *blk;
3808 uint32_t bsize;
3809
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003810 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3811
Christopher Faulet99eff652019-08-11 23:11:30 +02003812 /* If we were not just woken because we wanted to send but couldn't,
3813 * and there's somebody else that is waiting to send, do nothing,
3814 * we will subscribe later and be put at the end of the list
3815 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003816 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003817 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 +02003818 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003819 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003820 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003821
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003822 if (fconn->state < FCGI_CS_RECORD_H) {
3823 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003824 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003825 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003826
3827 htx = htxbuf(buf);
3828 if (fstrm->id == 0) {
3829 int32_t id = fcgi_conn_get_next_sid(fconn);
3830
3831 if (id < 0) {
3832 fcgi_strm_close(fstrm);
3833 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003834 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 +02003835 return 0;
3836 }
3837
3838 eb32_delete(&fstrm->by_id);
3839 fstrm->by_id.key = fstrm->id = id;
3840 fconn->max_id = id;
3841 fconn->nb_reserved--;
3842 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3843
3844
3845 /* Check if length of the body is known or if the message is
3846 * full. Otherwise, the request is invalid.
3847 */
3848 sl = http_get_stline(htx);
3849 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3850 htx->flags |= HTX_FL_PARSING_ERROR;
3851 fcgi_strm_error(fstrm);
3852 goto done;
3853 }
3854 }
3855
3856 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003857 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 +02003858 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3859 goto done;
3860 }
3861
3862 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3863 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3864
3865 while (fstrm->state < FCGI_SS_HLOC && !(fconn->flags & FCGI_SF_BLK_ANY) &&
3866 count && !htx_is_empty(htx)) {
3867 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003868 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003869 bsize = htx_get_blksz(blk);
3870
3871 switch (htx_get_blk_type(blk)) {
3872 case HTX_BLK_REQ_SL:
3873 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003874 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 +02003875 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3876 if (!ret) {
3877 goto done;
3878 }
3879 total += ret;
3880 count -= ret;
3881 break;
3882
3883 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003884 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 +02003885 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3886 if (!ret)
3887 goto done;
3888 goto remove_blk;
3889
3890 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003891 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3893 if (ret > 0) {
3894 htx = htx_from_buf(buf);
3895 total += ret;
3896 count -= ret;
3897 if (ret < bsize)
3898 goto done;
3899 }
3900 break;
3901
3902 case HTX_BLK_EOM:
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_empty_stdin(fconn, fstrm);
3905 if (!ret)
3906 goto done;
3907 goto remove_blk;
3908
3909 default:
3910 remove_blk:
3911 htx_remove_blk(htx, blk);
3912 total += bsize;
3913 count -= bsize;
3914 break;
3915 }
3916 }
3917
3918 done:
3919 if (fstrm->state >= FCGI_SS_HLOC) {
3920 /* trim any possibly pending data after we close (extra CR-LF,
3921 * unprocessed trailers, abnormal extra data, ...)
3922 */
3923 total += count;
3924 count = 0;
3925 }
3926
3927 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003928 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 +02003929 cs_set_error(cs);
3930 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3931 fcgi_strm_close(fstrm);
3932 }
3933
3934 if (htx)
3935 htx_to_buf(htx, buf);
3936
Christopher Faulet99eff652019-08-11 23:11:30 +02003937 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003938 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
3939 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 +02003940 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003941 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003942
3943 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01003944 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3945 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003946 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003947
3948 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02003949 return total;
3950}
3951
3952/* for debugging with CLI's "show fd" command */
3953static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
3954{
3955 struct fcgi_conn *fconn = conn->ctx;
3956 struct fcgi_strm *fstrm = NULL;
3957 struct eb32_node *node;
3958 int send_cnt = 0;
3959 int tree_cnt = 0;
3960 int orph_cnt = 0;
3961 struct buffer *hmbuf, *tmbuf;
3962
3963 if (!fconn)
3964 return;
3965
3966 list_for_each_entry(fstrm, &fconn->send_list, send_list)
3967 send_cnt++;
3968
3969 fstrm = NULL;
3970 node = eb32_first(&fconn->streams_by_id);
3971 while (node) {
3972 fstrm = container_of(node, struct fcgi_strm, by_id);
3973 tree_cnt++;
3974 if (!fstrm->cs)
3975 orph_cnt++;
3976 node = eb32_next(node);
3977 }
3978
3979 hmbuf = br_head(fconn->mbuf);
3980 tmbuf = br_tail(fconn->mbuf);
3981 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
3982 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
3983 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
3984 fconn->state, fconn->max_id, fconn->flags,
3985 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
3986 fconn->wait_event.events, fconn->dsi,
3987 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
3988 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
3989 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
3990 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
3991 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
3992 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
3993 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
3994
3995 if (fstrm) {
3996 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
3997 fstrm, fstrm->id, fstrm->flags,
3998 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
3999 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4000 fstrm->cs);
4001 if (fstrm->cs)
4002 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4003 fstrm->cs->flags, fstrm->cs->data);
4004 }
4005}
4006
4007/****************************************/
4008/* MUX initialization and instanciation */
4009/****************************************/
4010
4011/* The mux operations */
4012static const struct mux_ops mux_fcgi_ops = {
4013 .init = fcgi_init,
4014 .wake = fcgi_wake,
4015 .attach = fcgi_attach,
4016 .get_first_cs = fcgi_get_first_cs,
4017 .detach = fcgi_detach,
4018 .destroy = fcgi_destroy,
4019 .avail_streams = fcgi_avail_streams,
4020 .used_streams = fcgi_used_streams,
4021 .rcv_buf = fcgi_rcv_buf,
4022 .snd_buf = fcgi_snd_buf,
4023 .subscribe = fcgi_subscribe,
4024 .unsubscribe = fcgi_unsubscribe,
4025 .shutr = fcgi_shutr,
4026 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004027 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004028 .show_fd = fcgi_show_fd,
4029 .flags = MX_FL_HTX,
4030 .name = "FCGI",
4031};
4032
4033
4034/* this mux registers FCGI proto */
4035static struct mux_proto_list mux_proto_fcgi =
4036{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4037
4038INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4039
4040/*
4041 * Local variables:
4042 * c-indent-level: 8
4043 * c-basic-offset: 8
4044 * End:
4045 */