blob: 87c84fb1adc554dc6f7c5f6ec379213affa3e4f3 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020014#include <common/cfgparse.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020015#include <common/fcgi.h>
16#include <common/h1.h>
17#include <common/htx.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020018#include <import/ist.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020019#include <haproxy/list.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020020#include <common/net_helper.h>
21
22#include <types/proxy.h>
23#include <types/session.h>
24
25#include <proto/connection.h>
26#include <proto/fcgi-app.h>
27#include <proto/h1_htx.h>
28#include <proto/http_htx.h>
29#include <proto/log.h>
30#include <proto/session.h>
31#include <proto/ssl_sock.h>
32#include <proto/stream.h>
33#include <proto/stream_interface.h>
Christopher Faulet5c0f8592019-10-04 15:21:17 +020034#include <proto/trace.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020035
36/* FCGI Connection flags (32 bits) */
37#define FCGI_CF_NONE 0x00000000
38
39/* Flags indicating why writing to the mux is blockes */
40#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
41#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
42#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
43
44/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
49#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
50#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
51#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
52#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
53#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
54#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
55#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
56
57/* Other flags */
58#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
59#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
60#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
61#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
62#define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */
63#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
64
65/* FCGI connection state (fcgi_conn->state) */
66enum fcgi_conn_st {
67 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
68 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
69 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
70 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
71 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
72 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
73 FCGI_CS_ENTRIES
74} __attribute__((packed));
75
76/* 32 buffers: one for the ring's root, rest for the mbuf itself */
77#define FCGI_C_MBUF_CNT 32
78
79/* FCGI connection descriptor */
80struct fcgi_conn {
81 struct connection *conn;
82
83 enum fcgi_conn_st state; /* FCGI connection state */
84 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
85 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
86 uint32_t flags; /* Connection flags: FCGI_CF_* */
87
88 int16_t dsi; /* dmux stream ID (<0 = idle ) */
89 uint16_t drl; /* demux record length (if dsi >= 0) */
90 uint8_t drt; /* demux record type (if dsi >= 0) */
91 uint8_t drp; /* demux record padding (if dsi >= 0) */
92
93 struct buffer dbuf; /* demux buffer */
94 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
95
96 int timeout; /* idle timeout duration in ticks */
97 int shut_timeout; /* idle timeout duration in ticks after shutdown */
98 unsigned int nb_streams; /* number of streams in the tree */
99 unsigned int nb_cs; /* number of attached conn_streams */
100 unsigned int nb_reserved; /* number of reserved streams */
101 unsigned int stream_cnt; /* total number of streams seen */
102
103 struct proxy *proxy; /* the proxy this connection was created for */
104 struct fcgi_app *app; /* FCGI application used by this mux */
105 struct task *task; /* timeout management task */
106 struct eb_root streams_by_id; /* all active streams by their ID */
107
108 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200109
110 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
111 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
112};
113
114
115/* FCGI stream state, in fcgi_strm->state */
116enum fcgi_strm_st {
117 FCGI_SS_IDLE = 0,
118 FCGI_SS_OPEN,
119 FCGI_SS_HREM, // half-closed(remote)
120 FCGI_SS_HLOC, // half-closed(local)
121 FCGI_SS_ERROR,
122 FCGI_SS_CLOSED,
123 FCGI_SS_ENTRIES
124} __attribute__((packed));
125
126
127/* FCGI stream flags (32 bits) */
128#define FCGI_SF_NONE 0x00000000
129#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
130#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
131#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
132
133/* Stream flags indicating the reason the stream is blocked */
134#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
135#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
136#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
137
138#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
139#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100140#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200141
142#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
143#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
144#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
145
146/* Other flags */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100147#define FCGI_SF_H1_PARSING_DONE 0x00010000
Christopher Faulet99eff652019-08-11 23:11:30 +0200148
149/* FCGI stream descriptor */
150struct fcgi_strm {
151 struct conn_stream *cs;
152 struct session *sess;
153 struct fcgi_conn *fconn;
154
155 int32_t id; /* stream ID */
156
157 uint32_t flags; /* Connection flags: FCGI_SF_* */
158 enum fcgi_strm_st state; /* FCGI stream state */
159 int proto_status; /* FCGI_PS_* */
160
161 struct h1m h1m; /* response parser state for H1 */
162
163 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
164
165 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100166 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200167 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100168 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 +0200169};
170
171/* Flags representing all default FCGI parameters */
172#define FCGI_SP_CGI_GATEWAY 0x00000001
173#define FCGI_SP_DOC_ROOT 0x00000002
174#define FCGI_SP_SCRIPT_NAME 0x00000004
175#define FCGI_SP_PATH_INFO 0x00000008
176#define FCGI_SP_REQ_URI 0x00000010
177#define FCGI_SP_REQ_METH 0x00000020
178#define FCGI_SP_REQ_QS 0x00000040
179#define FCGI_SP_SRV_PORT 0x00000080
180#define FCGI_SP_SRV_PROTO 0x00000100
181#define FCGI_SP_SRV_NAME 0x00000200
182#define FCGI_SP_REM_ADDR 0x00000400
183#define FCGI_SP_REM_PORT 0x00000800
184#define FCGI_SP_SCRIPT_FILE 0x00001000
185#define FCGI_SP_PATH_TRANS 0x00002000
186#define FCGI_SP_CONT_LEN 0x00004000
187#define FCGI_SP_HTTPS 0x00008000
188#define FCGI_SP_MASK 0x0000FFFF
189#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
190
191/* FCGI parameters used when PARAMS record is sent */
192struct fcgi_strm_params {
193 uint32_t mask;
194 struct ist docroot;
195 struct ist scriptname;
196 struct ist pathinfo;
197 struct ist meth;
198 struct ist uri;
199 struct ist vsn;
200 struct ist qs;
201 struct ist srv_name;
202 struct ist srv_port;
203 struct ist rem_addr;
204 struct ist rem_port;
205 struct ist cont_len;
206 int https;
207 struct buffer *p;
208};
209
210/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
211#define MAX_DATA_REALIGN 1024
212
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200213/* trace source and events */
214static void fcgi_trace(enum trace_level level, uint64_t mask,
215 const struct trace_source *src,
216 const struct ist where, const struct ist func,
217 const void *a1, const void *a2, const void *a3, const void *a4);
218
219/* The event representation is split like this :
220 * fconn - internal FCGI connection
221 * fstrm - internal FCGI stream
222 * strm - application layer
223 * rx - data receipt
224 * tx - data transmission
225 * rsp - response parsing
226 */
227static const struct trace_event fcgi_trace_events[] = {
228#define FCGI_EV_FCONN_NEW (1ULL << 0)
229 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
230#define FCGI_EV_FCONN_RECV (1ULL << 1)
231 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
232#define FCGI_EV_FCONN_SEND (1ULL << 2)
233 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
234#define FCGI_EV_FCONN_BLK (1ULL << 3)
235 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
236#define FCGI_EV_FCONN_WAKE (1ULL << 4)
237 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
238#define FCGI_EV_FCONN_END (1ULL << 5)
239 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
240#define FCGI_EV_FCONN_ERR (1ULL << 6)
241 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
242
243#define FCGI_EV_RX_FHDR (1ULL << 7)
244 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
245#define FCGI_EV_RX_RECORD (1ULL << 8)
246 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
247#define FCGI_EV_RX_EOI (1ULL << 9)
248 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
249#define FCGI_EV_RX_GETVAL (1ULL << 10)
250 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
251#define FCGI_EV_RX_STDOUT (1ULL << 11)
252 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
253#define FCGI_EV_RX_STDERR (1ULL << 12)
254 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
255#define FCGI_EV_RX_ENDREQ (1ULL << 13)
256 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
257
258#define FCGI_EV_TX_RECORD (1ULL << 14)
259 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
260#define FCGI_EV_TX_EOI (1ULL << 15)
261 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
262#define FCGI_EV_TX_BEGREQ (1ULL << 16)
263 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
264#define FCGI_EV_TX_GETVAL (1ULL << 17)
265 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
266#define FCGI_EV_TX_PARAMS (1ULL << 18)
267 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
268#define FCGI_EV_TX_STDIN (1ULL << 19)
269 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
270#define FCGI_EV_TX_ABORT (1ULL << 20)
271 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
272
273#define FCGI_EV_RSP_DATA (1ULL << 21)
274 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
275#define FCGI_EV_RSP_EOM (1ULL << 22)
276 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
277#define FCGI_EV_RSP_HDRS (1ULL << 23)
278 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
279#define FCGI_EV_RSP_BODY (1ULL << 24)
280 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
281#define FCGI_EV_RSP_TLRS (1ULL << 25)
282 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
283
284#define FCGI_EV_FSTRM_NEW (1ULL << 26)
285 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
286#define FCGI_EV_FSTRM_BLK (1ULL << 27)
287 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
288#define FCGI_EV_FSTRM_END (1ULL << 28)
289 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
290#define FCGI_EV_FSTRM_ERR (1ULL << 29)
291 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
292
293#define FCGI_EV_STRM_NEW (1ULL << 30)
294 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
295#define FCGI_EV_STRM_RECV (1ULL << 31)
296 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
297#define FCGI_EV_STRM_SEND (1ULL << 32)
298 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
299#define FCGI_EV_STRM_FULL (1ULL << 33)
300 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
301#define FCGI_EV_STRM_WAKE (1ULL << 34)
302 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
303#define FCGI_EV_STRM_SHUT (1ULL << 35)
304 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
305#define FCGI_EV_STRM_END (1ULL << 36)
306 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
307#define FCGI_EV_STRM_ERR (1ULL << 37)
308 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
309
310 { }
311};
312
313static const struct name_desc fcgi_trace_lockon_args[4] = {
314 /* arg1 */ { /* already used by the connection */ },
315 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
316 /* arg3 */ { },
317 /* arg4 */ { }
318};
319
320
321static const struct name_desc fcgi_trace_decoding[] = {
322#define FCGI_VERB_CLEAN 1
323 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
324#define FCGI_VERB_MINIMAL 2
325 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
326#define FCGI_VERB_SIMPLE 3
327 { .name="simple", .desc="add request/response status line or htx info when available" },
328#define FCGI_VERB_ADVANCED 4
329 { .name="advanced", .desc="add header fields or record decoding when available" },
330#define FCGI_VERB_COMPLETE 5
331 { .name="complete", .desc="add full data dump when available" },
332 { /* end */ }
333};
334
335static struct trace_source trace_fcgi = {
336 .name = IST("fcgi"),
337 .desc = "FastCGI multiplexer",
338 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
339 .default_cb = fcgi_trace,
340 .known_events = fcgi_trace_events,
341 .lockon_args = fcgi_trace_lockon_args,
342 .decoding = fcgi_trace_decoding,
343 .report_events = ~0, // report everything by default
344};
345
346#define TRACE_SOURCE &trace_fcgi
347INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
348
Christopher Faulet99eff652019-08-11 23:11:30 +0200349/* FCGI connection and stream pools */
350DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
351DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
352
353static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
354static int fcgi_process(struct fcgi_conn *fconn);
355static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
356static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
357static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
358static 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 +0200359static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
360static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200361static void fcgi_strm_alert(struct fcgi_strm *fstrm);
362static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
363
364/* a dmumy management stream */
365static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
366 .cs = NULL,
367 .fconn = NULL,
368 .state = FCGI_SS_CLOSED,
369 .flags = FCGI_SF_NONE,
370 .id = 0,
371};
372
373/* and a dummy idle stream for use with any unknown stream */
374static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
375 .cs = NULL,
376 .fconn = NULL,
377 .state = FCGI_SS_IDLE,
378 .flags = FCGI_SF_NONE,
379 .id = 0,
380};
381
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200382/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
383static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
384{
385 switch (st) {
386 case FCGI_CS_INIT : return "INI";
387 case FCGI_CS_SETTINGS : return "STG";
388 case FCGI_CS_RECORD_H : return "RDH";
389 case FCGI_CS_RECORD_D : return "RDD";
390 case FCGI_CS_RECORD_P : return "RDP";
391 case FCGI_CS_CLOSED : return "CLO";
392 default : return "???";
393 }
394}
395
396/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
397static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
398{
399 switch (st) {
400 case FCGI_SS_IDLE : return "IDL";
401 case FCGI_SS_OPEN : return "OPN";
402 case FCGI_SS_HREM : return "RCL";
403 case FCGI_SS_HLOC : return "HCL";
404 case FCGI_SS_ERROR : return "ERR";
405 case FCGI_SS_CLOSED : return "CLO";
406 default : return "???";
407 }
408}
409
410
411/* the FCGI traces always expect that arg1, if non-null, is of type connection
412 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
413 * and that arg3, if non-null, is a htx for rx/tx headers.
414 */
415static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
416 const struct ist where, const struct ist func,
417 const void *a1, const void *a2, const void *a3, const void *a4)
418{
419 const struct connection *conn = a1;
420 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
421 const struct fcgi_strm *fstrm = a2;
422 const struct htx *htx = a3;
423 const size_t *val = a4;
424
425 if (!fconn)
426 fconn = (fstrm ? fstrm->fconn : NULL);
427
428 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
429 return;
430
431 /* Display the response state if fstrm is defined */
432 if (fstrm)
433 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
434
435 if (src->verbosity == FCGI_VERB_CLEAN)
436 return;
437
438 /* Display the value to the 4th argument (level > STATE) */
439 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100440 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200441
442 /* Display status-line if possible (verbosity > MINIMAL) */
443 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
444 const struct htx_blk *blk = htx_get_head_blk(htx);
445 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
446 enum htx_blk_type type = htx_get_blk_type(blk);
447
448 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
449 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
450 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
451 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
452 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
453 }
454
455 /* Display fconn info and, if defined, fstrm info */
456 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
457 if (fstrm)
458 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
459
460 if (!fstrm || fstrm->id <= 0)
461 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
462 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
463 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
464
465 if (src->verbosity == FCGI_VERB_MINIMAL)
466 return;
467
468 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
469 if (src->level > TRACE_LEVEL_USER) {
470 if (src->verbosity == FCGI_VERB_COMPLETE ||
471 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
472 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
473 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
474 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
475 if (src->verbosity == FCGI_VERB_COMPLETE ||
476 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
477 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
478 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
479
480 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
481 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
482 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
483 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
484 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
485 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
486 }
487
488 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
489 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
490 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
491 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
492 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
493 }
494
495 /* Display htx info if defined (level > USER) */
496 if (src->level > TRACE_LEVEL_USER && htx) {
497 int full = 0;
498
499 /* Full htx info (level > STATE && verbosity > SIMPLE) */
500 if (src->level > TRACE_LEVEL_STATE) {
501 if (src->verbosity == FCGI_VERB_COMPLETE)
502 full = 1;
503 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
504 full = 1;
505 }
506
507 chunk_memcat(&trace_buf, "\n\t", 2);
508 htx_dump(&trace_buf, htx, full);
509 }
510}
Christopher Faulet99eff652019-08-11 23:11:30 +0200511
512/*****************************************************/
513/* functions below are for dynamic buffer management */
514/*****************************************************/
515
516/* Indicates whether or not the we may call the fcgi_recv() function to attempt
517 * to receive data into the buffer and/or demux pending data. The condition is
518 * a bit complex due to some API limits for now. The rules are the following :
519 * - if an error or a shutdown was detected on the connection and the buffer
520 * is empty, we must not attempt to receive
521 * - if the demux buf failed to be allocated, we must not try to receive and
522 * we know there is nothing pending
523 * - if no flag indicates a blocking condition, we may attempt to receive,
524 * regardless of whether the demux buffer is full or not, so that only
525 * de demux part decides whether or not to block. This is needed because
526 * the connection API indeed prevents us from re-enabling receipt that is
527 * already enabled in a polled state, so we must always immediately stop
528 * as soon as the demux can't proceed so as never to hit an end of read
529 * with data pending in the buffers.
530 * - otherwise must may not attempt
531 */
532static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
533{
534 if (b_data(&fconn->dbuf) == 0 &&
535 (fconn->state == FCGI_CS_CLOSED ||
536 fconn->conn->flags & CO_FL_ERROR ||
537 conn_xprt_read0_pending(fconn->conn)))
538 return 0;
539
540 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
541 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
542 return 1;
543
544 return 0;
545}
546
547/* Restarts reading on the connection if it was not enabled */
548static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
549{
550 if (!fcgi_recv_allowed(fconn))
551 return;
552 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
553 (fconn->wait_event.events & SUB_RETRY_RECV))
554 return;
555 tasklet_wakeup(fconn->wait_event.tasklet);
556}
557
558
559/* Tries to grab a buffer and to re-enable processing on mux <target>. The
560 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
561 * the allocation succeeds, in which case the connection is woken up, or 0 if
562 * it's impossible to wake up and we prefer to be woken up later.
563 */
564static int fcgi_buf_available(void *target)
565{
566 struct fcgi_conn *fconn = target;
567 struct fcgi_strm *fstrm;
568
569 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200570 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 +0200571 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
572 fcgi_conn_restart_reading(fconn, 1);
573 return 1;
574 }
575
576 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200577 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 +0200578 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200579 if (fconn->flags & FCGI_CF_DEM_MROOM) {
580 fconn->flags &= ~FCGI_CF_DEM_MROOM;
581 fcgi_conn_restart_reading(fconn, 1);
582 }
583 return 1;
584 }
585
586 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
587 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
588 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200589 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 +0200590 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
591 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200592 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200593 return 1;
594 }
595
596 return 0;
597}
598
599static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
600{
601 struct buffer *buf = NULL;
602
Willy Tarreau21046592020-02-26 10:39:36 +0100603 if (likely(!MT_LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200604 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
605 fconn->buf_wait.target = fconn;
606 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau21046592020-02-26 10:39:36 +0100607 MT_LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200608 }
609 return buf;
610}
611
612static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
613{
614 if (bptr->size) {
615 b_free(bptr);
616 offer_buffers(NULL, tasks_run_queue);
617 }
618}
619
620static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
621{
622 struct buffer *buf;
623 unsigned int count = 0;
624
625 while (b_size(buf = br_head_pick(fconn->mbuf))) {
626 b_free(buf);
627 count++;
628 }
629 if (count)
630 offer_buffers(NULL, tasks_run_queue);
631}
632
633/* Returns the number of allocatable outgoing streams for the connection taking
634 * the number reserved streams into account.
635 */
636static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
637{
638 int ret;
639
640 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
641 if (ret < 0)
642 ret = 0;
643 return ret;
644}
645
646/* Returns the number of streams in use on a connection to figure if it's
647 * idle or not. We check nb_cs and not nb_streams as the caller will want
648 * to know if it was the last one after a detach().
649 */
650static int fcgi_used_streams(struct connection *conn)
651{
652 struct fcgi_conn *fconn = conn->ctx;
653
654 return fconn->nb_cs;
655}
656
657/* Returns the number of concurrent streams available on the connection */
658static int fcgi_avail_streams(struct connection *conn)
659{
660 struct server *srv = objt_server(conn->target);
661 struct fcgi_conn *fconn = conn->ctx;
662 int ret1, ret2;
663
664 /* Don't open new stream if the connection is closed */
665 if (fconn->state == FCGI_CS_CLOSED)
666 return 0;
667
668 /* May be negative if this setting has changed */
669 ret1 = (fconn->streams_limit - fconn->nb_streams);
670
671 /* we must also consider the limit imposed by stream IDs */
672 ret2 = fcgi_streams_left(fconn);
673 ret1 = MIN(ret1, ret2);
674 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
675 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
676 ret1 = MIN(ret1, ret2);
677 }
678 return ret1;
679}
680
681/*****************************************************************/
682/* functions below are dedicated to the mux setup and management */
683/*****************************************************************/
684
685/* Initializes the mux once it's attached. Only outgoing connections are
686 * supported. So the context is already initialized before installing the
687 * mux. <input> is always used as Input buffer and may contain data. It is the
688 * caller responsibility to not reuse it anymore. Returns < 0 on error.
689 */
690static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
691 struct buffer *input)
692{
693 struct fcgi_conn *fconn;
694 struct fcgi_strm *fstrm;
695 struct fcgi_app *app = get_px_fcgi_app(px);
696 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200697 void *conn_ctx = conn->ctx;
698
699 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200700
701 if (!app)
702 goto fail_conn;
703
704 fconn = pool_alloc(pool_head_fcgi_conn);
705 if (!fconn)
706 goto fail_conn;
707
708 fconn->shut_timeout = fconn->timeout = px->timeout.server;
709 if (tick_isset(px->timeout.serverfin))
710 fconn->shut_timeout = px->timeout.serverfin;
711
712 fconn->flags = FCGI_CF_NONE;
713
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500714 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200715 if (app->flags & FCGI_APP_FL_KEEP_CONN)
716 fconn->flags |= FCGI_CF_KEEP_CONN;
717 if (app->flags & FCGI_APP_FL_GET_VALUES)
718 fconn->flags |= FCGI_CF_GET_VALUES;
719 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
720 fconn->flags |= FCGI_CF_MPXS_CONNS;
721
722 fconn->proxy = px;
723 fconn->app = app;
724 fconn->task = NULL;
725 if (tick_isset(fconn->timeout)) {
726 t = task_new(tid_bit);
727 if (!t)
728 goto fail;
729
730 fconn->task = t;
731 t->process = fcgi_timeout_task;
732 t->context = fconn;
733 t->expire = tick_add(now_ms, fconn->timeout);
734 }
735
736 fconn->wait_event.tasklet = tasklet_new();
737 if (!fconn->wait_event.tasklet)
738 goto fail;
739 fconn->wait_event.tasklet->process = fcgi_io_cb;
740 fconn->wait_event.tasklet->context = fconn;
741 fconn->wait_event.events = 0;
742
743 /* Initialise the context. */
744 fconn->state = FCGI_CS_INIT;
745 fconn->conn = conn;
746 fconn->streams_limit = app->maxreqs;
747 fconn->max_id = -1;
748 fconn->nb_streams = 0;
749 fconn->nb_cs = 0;
750 fconn->nb_reserved = 0;
751 fconn->stream_cnt = 0;
752
753 fconn->dbuf = *input;
754 fconn->dsi = -1;
755
756 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
757 fconn->streams_by_id = EB_ROOT;
758 LIST_INIT(&fconn->send_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100759 MT_LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200760
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200761 conn->ctx = fconn;
762
Christopher Faulet99eff652019-08-11 23:11:30 +0200763 if (t)
764 task_queue(t);
765
766 /* FIXME: this is temporary, for outgoing connections we need to
767 * immediately allocate a stream until the code is modified so that the
768 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200769 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200770 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200771 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200772 if (!fstrm)
773 goto fail;
774
Christopher Faulet99eff652019-08-11 23:11:30 +0200775
776 /* Repare to read something */
777 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200778 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200779 return 0;
780
781 fail:
782 task_destroy(t);
783 if (fconn->wait_event.tasklet)
784 tasklet_free(fconn->wait_event.tasklet);
785 pool_free(pool_head_fcgi_conn, fconn);
786 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200787 conn->ctx = conn_ctx; // restore saved ctx
788 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200789 return -1;
790}
791
792/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
793 * -1 if no more is allocatable.
794 */
795static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
796{
797 int32_t id = (fconn->max_id + 1) | 1;
798
799 if ((id & 0x80000000U))
800 id = -1;
801 return id;
802}
803
804/* Returns the stream associated with id <id> or NULL if not found */
805static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
806{
807 struct eb32_node *node;
808
809 if (id == 0)
810 return (struct fcgi_strm *)fcgi_mgmt_stream;
811
812 if (id > fconn->max_id)
813 return (struct fcgi_strm *)fcgi_unknown_stream;
814
815 node = eb32_lookup(&fconn->streams_by_id, id);
816 if (!node)
817 return (struct fcgi_strm *)fcgi_unknown_stream;
818 return container_of(node, struct fcgi_strm, by_id);
819}
820
821
822/* Release function. This one should be called to free all resources allocated
823 * to the mux.
824 */
825static void fcgi_release(struct fcgi_conn *fconn)
826{
827 struct connection *conn = NULL;;
828
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200829 TRACE_POINT(FCGI_EV_FCONN_END);
830
Christopher Faulet99eff652019-08-11 23:11:30 +0200831 if (fconn) {
832 /* The connection must be attached to this mux to be released */
833 if (fconn->conn && fconn->conn->ctx == fconn)
834 conn = fconn->conn;
835
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200836 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
837
Willy Tarreau21046592020-02-26 10:39:36 +0100838 if (MT_LIST_ADDED(&fconn->buf_wait.list))
839 MT_LIST_DEL(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200840
841 fcgi_release_buf(fconn, &fconn->dbuf);
842 fcgi_release_mbuf(fconn);
843
844 if (fconn->task) {
845 fconn->task->context = NULL;
846 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
847 fconn->task = NULL;
848 }
849 if (fconn->wait_event.tasklet)
850 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200851 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200852 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
853 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200854
855 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200856 }
857
858 if (conn) {
859 conn->mux = NULL;
860 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200861 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200862
863 conn_stop_tracking(conn);
864 conn_full_close(conn);
865 if (conn->destroy_cb)
866 conn->destroy_cb(conn);
867 conn_free(conn);
868 }
869}
870
871
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500872/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200873static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
874{
875 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
876 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
877 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
878 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
879 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
880 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
881 conn_xprt_read0_pending(fconn->conn))))
882 return 1;
883 return 0;
884}
885
886
887/********************************************************/
888/* functions below are for the FCGI protocol processing */
889/********************************************************/
890
Christopher Faulet99eff652019-08-11 23:11:30 +0200891/* Marks an error on the stream. */
892static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
893{
894 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200895 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
896 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200897 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200898 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
899 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200900 if (fstrm->cs)
901 cs_set_error(fstrm->cs);
902 }
903}
904
905/* Attempts to notify the data layer of recv availability */
906static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
907{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100908 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200909 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100910 tasklet_wakeup(fstrm->subs->tasklet);
911 fstrm->subs->events &= ~SUB_RETRY_RECV;
912 if (!fstrm->subs->events)
913 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200914 }
915}
916
917/* Attempts to notify the data layer of send availability */
918static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
919{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100920 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200921 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100922 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100923 tasklet_wakeup(fstrm->subs->tasklet);
924 fstrm->subs->events &= ~SUB_RETRY_SEND;
925 if (!fstrm->subs->events)
926 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200927 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100928 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
929 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
930 tasklet_wakeup(fstrm->shut_tl);
931 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200932}
933
934/* Alerts the data layer, trying to wake it up by all means, following
935 * this sequence :
936 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
937 * for recv
938 * - if its subscribed to send, then it's woken up for send
939 * - if it was subscribed to neither, its ->wake() callback is called
940 * It is safe to call this function with a closed stream which doesn't have a
941 * conn_stream anymore.
942 */
943static void fcgi_strm_alert(struct fcgi_strm *fstrm)
944{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200945 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100946 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100947 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200948 fcgi_strm_notify_recv(fstrm);
949 fcgi_strm_notify_send(fstrm);
950 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200951 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
952 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200953 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200954 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200955}
956
957/* Writes the 16-bit record size <len> at address <record> */
958static inline void fcgi_set_record_size(void *record, uint16_t len)
959{
960 uint8_t *out = (record + 4);
961
962 *out = (len >> 8);
963 *(out + 1) = (len & 0xff);
964}
965
966/* Writes the 16-bit stream id <id> at address <record> */
967static inline void fcgi_set_record_id(void *record, uint16_t id)
968{
969 uint8_t *out = (record + 2);
970
971 *out = (id >> 8);
972 *(out + 1) = (id & 0xff);
973}
974
975/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
976 * its connection if the stream was not yet closed. Please use this exclusively
977 * before closing a stream to ensure stream count is well maintained.
978 */
979static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
980{
981 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200982 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200983 fstrm->fconn->nb_streams--;
984 if (!fstrm->id)
985 fstrm->fconn->nb_reserved--;
986 if (fstrm->cs) {
987 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
988 fcgi_strm_notify_recv(fstrm);
989 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200990 fstrm->state = FCGI_SS_CLOSED;
991 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
992 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200993 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200994}
995
996/* Detaches a FCGI stream from its FCGI connection and releases it to the
997 * fcgi_strm pool.
998 */
999static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1000{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001001 struct connection *conn = fstrm->fconn->conn;
1002
1003 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1004
Christopher Faulet99eff652019-08-11 23:11:30 +02001005 fcgi_strm_close(fstrm);
1006 eb32_delete(&fstrm->by_id);
1007 if (b_size(&fstrm->rxbuf)) {
1008 b_free(&fstrm->rxbuf);
1009 offer_buffers(NULL, tasks_run_queue);
1010 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001011 if (fstrm->subs)
1012 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001013 /* There's no need to explicitly call unsubscribe here, the only
1014 * reference left would be in the fconn send_list/fctl_list, and if
1015 * we're in it, we're getting out anyway
1016 */
1017 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001018 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001019 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001020
1021 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001022}
1023
1024/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1025 * stream tree. In case of error, nothing is added and NULL is returned. The
1026 * causes of errors can be any failed memory allocation. The caller is
1027 * responsible for checking if the connection may support an extra stream prior
1028 * to calling this function.
1029 */
1030static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1031{
1032 struct fcgi_strm *fstrm;
1033
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001034 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1035
Christopher Faulet99eff652019-08-11 23:11:30 +02001036 fstrm = pool_alloc(pool_head_fcgi_strm);
1037 if (!fstrm)
1038 goto out;
1039
Willy Tarreau7aad7032020-01-16 17:20:57 +01001040 fstrm->shut_tl = tasklet_new();
1041 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001042 pool_free(pool_head_fcgi_strm, fstrm);
1043 goto out;
1044 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001045 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001046 fstrm->shut_tl->process = fcgi_deferred_shut;
1047 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001048 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001049 fstrm->fconn = fconn;
1050 fstrm->cs = NULL;
1051 fstrm->flags = FCGI_SF_NONE;
1052 fstrm->proto_status = 0;
1053 fstrm->state = FCGI_SS_IDLE;
1054 fstrm->rxbuf = BUF_NULL;
1055
1056 h1m_init_res(&fstrm->h1m);
1057 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1058 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1059
1060 fstrm->by_id.key = fstrm->id = id;
1061 if (id > 0)
1062 fconn->max_id = id;
1063 else
1064 fconn->nb_reserved++;
1065
1066 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1067 fconn->nb_streams++;
1068 fconn->stream_cnt++;
1069
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001070 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001071 return fstrm;
1072
1073 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001074 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 +02001075 return NULL;
1076}
1077
1078/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1079 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1080 * highest possible stream ID was reached.
1081 */
1082static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1083 struct session *sess)
1084{
1085 struct fcgi_strm *fstrm = NULL;
1086
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001087 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1088 if (fconn->nb_streams >= fconn->streams_limit) {
1089 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 +02001090 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001091 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001092
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001093 if (fcgi_streams_left(fconn) < 1) {
1094 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 +02001095 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001096 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001097
1098 /* Defer choosing the ID until we send the first message to create the stream */
1099 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001100 if (!fstrm) {
1101 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 +02001102 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001103 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001104
1105 fstrm->cs = cs;
1106 fstrm->sess = sess;
1107 cs->ctx = fstrm;
1108 fconn->nb_cs++;
1109
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001110 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001111 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001112
1113 out:
1114 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001115}
1116
1117/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1118 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1119 * automatically updated accordingly. If the stream is orphaned, it is
1120 * destroyed.
1121 */
1122static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1123{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001124 struct fcgi_conn *fconn = fstrm->fconn;
1125
1126 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1127
Christopher Faulet99eff652019-08-11 23:11:30 +02001128 if (!fstrm->cs) {
1129 /* this stream was already orphaned */
1130 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001131 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001132 return;
1133 }
1134
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001135 if (conn_xprt_read0_pending(fconn->conn)) {
1136 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001137 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001138 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1139 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001140 else if (fstrm->state == FCGI_SS_HLOC)
1141 fcgi_strm_close(fstrm);
1142 }
1143
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001144 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001145 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1146 if (fstrm->cs->flags & CS_FL_EOS)
1147 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001148
1149 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001150 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001151 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1152 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001153 }
1154
1155 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001156
1157 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001158}
1159
1160/* Wakes unassigned streams (ID == 0) attached to the connection. */
1161static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1162{
1163 struct eb32_node *node;
1164 struct fcgi_strm *fstrm;
1165
1166 node = eb32_lookup(&fconn->streams_by_id, 0);
1167 while (node) {
1168 fstrm = container_of(node, struct fcgi_strm, by_id);
1169 if (fstrm->id > 0)
1170 break;
1171 node = eb32_next(node);
1172 fcgi_strm_wake_one_stream(fstrm);
1173 }
1174}
1175
1176/* Wakes the streams attached to the connection, whose id is greater than <last>
1177 * or unassigned.
1178 */
1179static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1180{
1181 struct eb32_node *node;
1182 struct fcgi_strm *fstrm;
1183
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001184 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1185
Christopher Faulet99eff652019-08-11 23:11:30 +02001186 /* Wake all streams with ID > last */
1187 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1188 while (node) {
1189 fstrm = container_of(node, struct fcgi_strm, by_id);
1190 node = eb32_next(node);
1191 fcgi_strm_wake_one_stream(fstrm);
1192 }
1193 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001194
1195 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001196}
1197
1198static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1199 struct htx *htx, struct htx_sl *sl,
1200 struct fcgi_strm_params *params)
1201{
1202 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1203 struct ist p;
1204
1205 if (!sl)
1206 goto error;
1207
1208 if (!(params->mask & FCGI_SP_DOC_ROOT))
1209 params->docroot = fconn->app->docroot;
1210
1211 if (!(params->mask & FCGI_SP_REQ_METH)) {
1212 p = htx_sl_req_meth(sl);
1213 params->meth = ist2(b_tail(params->p), p.len);
1214 chunk_memcat(params->p, p.ptr, p.len);
1215 }
1216 if (!(params->mask & FCGI_SP_REQ_URI)) {
1217 p = htx_sl_req_uri(sl);
1218 params->uri = ist2(b_tail(params->p), p.len);
1219 chunk_memcat(params->p, p.ptr, p.len);
1220 }
1221 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1222 p = htx_sl_req_vsn(sl);
1223 params->vsn = ist2(b_tail(params->p), p.len);
1224 chunk_memcat(params->p, p.ptr, p.len);
1225 }
1226 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1227 char *end;
1228 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001229 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001230 port = get_host_port(cli_conn->dst);
1231 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1232 if (!end)
1233 goto error;
1234 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1235 params->p->data += params->srv_port.len;
1236 }
1237 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1238 /* If no Host header found, use the server address to fill
1239 * srv_name */
1240 if (!istlen(params->srv_name)) {
1241 char *ptr = NULL;
1242
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001243 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001244 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1245 ptr = b_tail(params->p);
1246 if (ptr) {
1247 params->srv_name = ist2(ptr, strlen(ptr));
1248 params->p->data += params->srv_name.len;
1249 }
1250 }
1251 }
1252 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1253 char *ptr = NULL;
1254
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001255 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001256 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1257 ptr = b_tail(params->p);
1258 if (ptr) {
1259 params->rem_addr = ist2(ptr, strlen(ptr));
1260 params->p->data += params->rem_addr.len;
1261 }
1262 }
1263 if (!(params->mask & FCGI_SP_REM_PORT)) {
1264 char *end;
1265 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001266 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001267 port = get_host_port(cli_conn->src);
1268 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1269 if (!end)
1270 goto error;
1271 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1272 params->p->data += params->rem_port.len;
1273 }
1274 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1275 struct htx_blk *blk;
1276 enum htx_blk_type type;
1277 char *end;
1278 size_t len = 0;
1279
1280 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1281 type = htx_get_blk_type(blk);
1282
1283 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1284 break;
1285 if (type == HTX_BLK_DATA)
1286 len += htx_get_blksz(blk);
1287 }
1288 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1289 if (!end)
1290 goto error;
1291 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1292 params->p->data += params->cont_len.len;
1293 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001294#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001295 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001296 if (cli_conn)
1297 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001298 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001299#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001300 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1301 /* one of scriptname, pathinfo or query_string is no set */
1302 struct ist path = http_get_path(params->uri);
1303 int len;
1304
1305 /* Decode the path. it must first be copied to keep the URI
1306 * untouched.
1307 */
1308 chunk_memcat(params->p, path.ptr, path.len);
1309 path.ptr = b_tail(params->p) - path.len;
1310 path.ptr[path.len] = '\0';
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001311 len = url_decode(path.ptr, 0);
Christopher Faulet99eff652019-08-11 23:11:30 +02001312 if (len < 0)
1313 goto error;
1314 path.len = len;
1315
1316 /* No scrit_name set but no valid path ==> error */
1317 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1318 goto error;
1319
1320 /* Find limit between the path and the query-string */
1321 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++);
1322
1323 /* If there is a query-string, Set it if not already set */
1324 if (!(params->mask & FCGI_SP_REQ_QS) && len < path.len)
1325 params->qs = ist2(path.ptr+len+1, path.len-len-1);
1326
1327 /* If the script_name is set, don't try to deduce the path_info
1328 * too. The opposite is not true.
1329 */
1330 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1331 params->mask |= FCGI_SP_PATH_INFO;
1332 goto end;
1333 }
1334
1335 /* script_name not set, preset it with the path for now */
1336 params->scriptname = ist2(path.ptr, len);
1337
1338 /* If there is no regex to match the pathinfo, just to the last
1339 * part and see if the index must be used.
1340 */
1341 if (!fconn->app->pathinfo_re)
1342 goto check_index;
1343
Christopher Faulet28cb3662020-02-14 14:47:37 +01001344 /* If some special characters are found in the decoded path (\n
1345 * or \0), the PATH_INFO regex cannot match. This is theorically
1346 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001347 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001348 * case.
1349 */
1350 if (istchr(path, '\n') || istchr(path, '\0'))
1351 goto error;
1352
Christopher Faulet99eff652019-08-11 23:11:30 +02001353 /* The regex does not match, just to the last part and see if
1354 * the index must be used.
1355 */
1356 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1357 goto check_index;
1358
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001359 /* We must have at least 1 capture for the script name,
1360 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001361 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001362 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001363 goto check_index;
1364
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001365 /* Finally we can set the script_name and the path_info. The
1366 * path_info is set if not already defined, and if it was
1367 * captured
1368 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001369 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001370 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1371 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001372
1373 check_index:
1374 len = params->scriptname.len;
1375 /* the script_name if finished by a '/' so we can add the index
1376 * part, if any.
1377 */
1378 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1379 struct ist sn = params->scriptname;
1380
1381 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1382 chunk_memcat(params->p, sn.ptr, sn.len);
1383 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1384 }
1385 }
1386
1387 end:
1388 return 1;
1389 error:
1390 return 0;
1391}
1392
1393static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1394 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1395{
1396 struct fcgi_param p;
1397
1398 if (params->mask & flag)
1399 return 1;
1400
1401 chunk_reset(&trash);
1402
1403 switch (flag) {
1404 case FCGI_SP_CGI_GATEWAY:
1405 p.n = ist("GATEWAY_INTERFACE");
1406 p.v = ist("CGI/1.1");
1407 goto encode;
1408 case FCGI_SP_DOC_ROOT:
1409 p.n = ist("DOCUMENT_ROOT");
1410 p.v = params->docroot;
1411 goto encode;
1412 case FCGI_SP_SCRIPT_NAME:
1413 p.n = ist("SCRIPT_NAME");
1414 p.v = params->scriptname;
1415 goto encode;
1416 case FCGI_SP_PATH_INFO:
1417 p.n = ist("PATH_INFO");
1418 p.v = params->pathinfo;
1419 goto encode;
1420 case FCGI_SP_REQ_URI:
1421 p.n = ist("REQUEST_URI");
1422 p.v = params->uri;
1423 goto encode;
1424 case FCGI_SP_REQ_METH:
1425 p.n = ist("REQUEST_METHOD");
1426 p.v = params->meth;
1427 goto encode;
1428 case FCGI_SP_REQ_QS:
1429 p.n = ist("QUERY_STRING");
1430 p.v = params->qs;
1431 goto encode;
1432 case FCGI_SP_SRV_NAME:
1433 p.n = ist("SERVER_NAME");
1434 p.v = params->srv_name;
1435 goto encode;
1436 case FCGI_SP_SRV_PORT:
1437 p.n = ist("SERVER_PORT");
1438 p.v = params->srv_port;
1439 goto encode;
1440 case FCGI_SP_SRV_PROTO:
1441 p.n = ist("SERVER_PROTOCOL");
1442 p.v = params->vsn;
1443 goto encode;
1444 case FCGI_SP_REM_ADDR:
1445 p.n = ist("REMOTE_ADDR");
1446 p.v = params->rem_addr;
1447 goto encode;
1448 case FCGI_SP_REM_PORT:
1449 p.n = ist("REMOTE_PORT");
1450 p.v = params->rem_port;
1451 goto encode;
1452 case FCGI_SP_SCRIPT_FILE:
1453 p.n = ist("SCRIPT_FILENAME");
1454 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1455 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1456 p.v = ist2(b_head(&trash), b_data(&trash));
1457 goto encode;
1458 case FCGI_SP_PATH_TRANS:
1459 if (!istlen(params->pathinfo))
1460 goto skip;
1461 p.n = ist("PATH_TRANSLATED");
1462 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1463 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1464 p.v = ist2(b_head(&trash), b_data(&trash));
1465 goto encode;
1466 case FCGI_SP_CONT_LEN:
1467 p.n = ist("CONTENT_LENGTH");
1468 p.v = params->cont_len;
1469 goto encode;
1470 case FCGI_SP_HTTPS:
1471 if (!params->https)
1472 goto skip;
1473 p.n = ist("HTTPS");
1474 p.v = ist("on");
1475 goto encode;
1476 default:
1477 goto skip;
1478 }
1479
1480 encode:
1481 if (!istlen(p.v))
1482 goto skip;
1483 if (!fcgi_encode_param(outbuf, &p))
1484 return 0;
1485 skip:
1486 params->mask |= flag;
1487 return 1;
1488}
1489
1490/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1491 * anything. It is highly unexpected, but if the record is larger than a buffer
1492 * and cannot be encoded in one time, an error is triggered and the connection is
1493 * closed. GET_VALUES record cannot be split.
1494 */
1495static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1496{
1497 struct buffer outbuf;
1498 struct buffer *mbuf;
1499 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1500 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001501 int ret = 0;
1502
1503 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001504
1505 mbuf = br_tail(fconn->mbuf);
1506 retry:
1507 if (!fcgi_get_buf(fconn, mbuf)) {
1508 fconn->flags |= FCGI_CF_MUX_MALLOC;
1509 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001510 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1511 ret = 0;
1512 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001513 }
1514
1515 while (1) {
1516 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1517 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1518 break;
1519 realign_again:
1520 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1521 }
1522
1523 if (outbuf.size < 8)
1524 goto full;
1525
1526 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1527 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1528 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1529 outbuf.data = 8;
1530
1531 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1532 * handled by HAProxy.
1533 */
1534 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1535 goto full;
1536
1537 /* update the record's size now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001538 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 +02001539 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1540 b_add(mbuf, outbuf.data);
1541 ret = 1;
1542
1543 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001544 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001545 return ret;
1546 full:
1547 /* Too large to be encoded. For GET_VALUES records, it is an error */
1548 if (!b_data(mbuf))
1549 goto fail;
1550
1551 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1552 goto retry;
1553 fconn->flags |= FCGI_CF_MUX_MFULL;
1554 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001555 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001556 ret = 0;
1557 goto end;
1558 fail:
1559 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001560 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1561 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1562 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001563}
1564
1565/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1566 * couldn't do anything. It is highly unexpected, but if the record is larger
1567 * than a buffer and cannot be decoded in one time, an error is triggered and
1568 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1569 */
1570static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1571{
1572 struct buffer inbuf;
1573 struct buffer *dbuf;
1574 size_t offset;
1575
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001576 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1577
Christopher Faulet99eff652019-08-11 23:11:30 +02001578 dbuf = &fconn->dbuf;
1579
1580 /* Record too large to be fully decoded */
1581 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1582 goto fail;
1583
1584 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001585 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1586 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001587 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001588 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001589
1590 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1591 /* Realign the dmux buffer if the record wraps. It is unexpected
1592 * at this stage because it should be the first record received
1593 * from the FCGI application.
1594 */
1595 b_slow_realign(dbuf, trash.area, 0);
1596 }
1597
1598 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1599
1600 for (offset = 0; offset < b_data(&inbuf); ) {
1601 struct fcgi_param p;
1602 size_t ret;
1603
1604 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1605 if (!ret) {
1606 /* name or value too large to be decoded at once */
1607 goto fail;
1608 }
1609 offset += ret;
1610
1611 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001612 if (isteq(p.v, ist("1"))) {
Christopher Faulet08618a72019-10-08 11:59:47 +02001613 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001614 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001615 }
1616 else {
Christopher Faulet08618a72019-10-08 11:59:47 +02001617 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn,,, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001618 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001619 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001620 }
1621 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1622 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Christopher Faulet08618a72019-10-08 11:59:47 +02001623 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 +02001624 }
1625 /*
1626 * Ignore all other params
1627 */
1628 }
1629
1630 /* Reset the number of concurrent streams supported if the FCGI
1631 * application does not support connection multiplexing
1632 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001633 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001634 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001635 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1636 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001637
1638 /* We must be sure to have read exactly the announced record length, no
1639 * more no less
1640 */
1641 if (offset != fconn->drl)
1642 goto fail;
1643
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001644 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 +02001645 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1646 fconn->drl = 0;
1647 fconn->drp = 0;
1648 fconn->state = FCGI_CS_RECORD_H;
1649 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001650 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1651 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001652 return 1;
1653 fail:
1654 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001655 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1656 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 +02001657 return 0;
1658}
1659
1660/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1661 * excluded, as the streams which already received the end-of-stream. It returns
1662 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1663 */
1664static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1665{
1666 struct eb32_node *node;
1667 struct fcgi_strm *fstrm;
1668
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001669 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1670
Christopher Faulet99eff652019-08-11 23:11:30 +02001671 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1672 while (node) {
1673 fstrm = container_of(node, struct fcgi_strm, by_id);
1674 node = eb32_next(node);
1675 if (fstrm->state != FCGI_SS_CLOSED &&
1676 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1677 !fcgi_strm_send_abort(fconn, fstrm))
1678 return 0;
1679 }
1680 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001681 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1682 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001683 return 1;
1684}
1685
1686/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1687 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1688 * space to proceed. It is small enough to be encoded in an empty buffer.
1689 */
1690static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1691{
1692 struct buffer outbuf;
1693 struct buffer *mbuf;
1694 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1695 int ret;
1696
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001697 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1698
Christopher Faulet99eff652019-08-11 23:11:30 +02001699 mbuf = br_tail(fconn->mbuf);
1700 retry:
1701 if (!fcgi_get_buf(fconn, mbuf)) {
1702 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001703 fstrm->flags |= FCGI_SF_BLK_MROOM;
1704 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1705 ret = 0;
1706 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001707 }
1708
1709 while (1) {
1710 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1711 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1712 break;
1713 realign_again:
1714 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1715 }
1716
1717 if (outbuf.size < 8)
1718 goto full;
1719
1720 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1721 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1722 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1723 fcgi_set_record_id(outbuf.area, fstrm->id);
1724 outbuf.data = 8;
1725
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001726 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1727 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001728 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001729 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001730 if (!fcgi_encode_begin_request(&outbuf, &rec))
1731 goto full;
1732
1733 /* commit the record */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001734 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 +02001735 b_add(mbuf, outbuf.data);
1736 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1737 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001738 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001739 ret = 1;
1740
1741 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001742 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001743 return ret;
1744 full:
1745 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1746 goto retry;
1747 fconn->flags |= FCGI_CF_MUX_MFULL;
1748 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001749 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 +02001750 ret = 0;
1751 goto end;
1752}
1753
1754/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1755 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1756 * space to proceed. It is small enough to be encoded in an empty buffer.
1757 */
1758static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1759 enum fcgi_record_type rtype)
1760{
1761 struct buffer outbuf;
1762 struct buffer *mbuf;
1763 int ret;
1764
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001765 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001766 mbuf = br_tail(fconn->mbuf);
1767 retry:
1768 if (!fcgi_get_buf(fconn, mbuf)) {
1769 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001770 fstrm->flags |= FCGI_SF_BLK_MROOM;
1771 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1772 ret = 0;
1773 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001774 }
1775
1776 while (1) {
1777 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1778 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1779 break;
1780 realign_again:
1781 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1782 }
1783
1784 if (outbuf.size < 8)
1785 goto full;
1786
1787 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1788 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1789 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1790 outbuf.area[1] = rtype;
1791 fcgi_set_record_id(outbuf.area, fstrm->id);
1792 outbuf.data = 8;
1793
1794 /* commit the record */
1795 b_add(mbuf, outbuf.data);
1796 ret = 1;
1797
1798 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001799 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001800 return ret;
1801 full:
1802 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1803 goto retry;
1804 fconn->flags |= FCGI_CF_MUX_MFULL;
1805 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001806 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 +02001807 ret = 0;
1808 goto end;
1809}
1810
1811
1812/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1813 * marks the end of params.
1814 */
1815static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1816{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001817 int ret;
1818
1819 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1820 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1821 if (ret)
1822 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1823 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001824}
1825
1826/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1827 * marks the end of input. On success, all the request was successfully sent.
1828 */
1829static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1830{
1831 int ret;
1832
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001834 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001835 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001836 fstrm->flags |= FCGI_SF_ES_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001837 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm,, (size_t[]){0});
1838 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1839 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1840 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001841 return ret;
1842}
1843
1844/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1845 * stops the request processing.
1846 */
1847static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1848{
1849 int ret;
1850
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001851 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001852 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001853 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001854 fstrm->flags |= FCGI_SF_ABRT_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001855 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm,, (size_t[]){0});
1856 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1857 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1858 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001859 return ret;
1860}
1861
1862/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1863 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1864 * several records are sent. However, a K/V param cannot be split between 2
1865 * records.
1866 */
1867static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1868 struct htx *htx)
1869{
1870 struct buffer outbuf;
1871 struct buffer *mbuf;
1872 struct htx_blk *blk;
1873 struct htx_sl *sl = NULL;
1874 struct fcgi_strm_params params;
1875 size_t total = 0;
1876
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001877 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1878
Christopher Faulet99eff652019-08-11 23:11:30 +02001879 memset(&params, 0, sizeof(params));
1880 params.p = get_trash_chunk();
1881
1882 mbuf = br_tail(fconn->mbuf);
1883 retry:
1884 if (!fcgi_get_buf(fconn, mbuf)) {
1885 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001886 fstrm->flags |= FCGI_SF_BLK_MROOM;
1887 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1888 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001889 }
1890
1891 while (1) {
1892 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1893 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1894 break;
1895 realign_again:
1896 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1897 }
1898
1899 if (outbuf.size < 8)
1900 goto full;
1901
1902 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1903 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1904 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1905 fcgi_set_record_id(outbuf.area, fstrm->id);
1906 outbuf.data = 8;
1907
1908 blk = htx_get_head_blk(htx);
1909 while (blk) {
1910 enum htx_blk_type type;
1911 uint32_t size = htx_get_blksz(blk);
1912 struct fcgi_param p;
1913
1914 type = htx_get_blk_type(blk);
1915 switch (type) {
1916 case HTX_BLK_REQ_SL:
1917 sl = htx_get_blk_ptr(htx, blk);
1918 if (sl->info.req.meth == HTTP_METH_HEAD)
1919 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1920 if (sl->flags & HTX_SL_F_VER_11)
1921 fstrm->h1m.flags |= H1_MF_VER_11;
1922 break;
1923
1924 case HTX_BLK_HDR:
1925 p.n = htx_get_blk_name(htx, blk);
1926 p.v = htx_get_blk_value(htx, blk);
1927
1928 if (istmatch(p.n, ist(":fcgi-"))) {
1929 p.n.ptr += 6;
1930 p.n.len -= 6;
1931 if (isteq(p.n, ist("gateway_interface")))
1932 params.mask |= FCGI_SP_CGI_GATEWAY;
1933 else if (isteq(p.n, ist("document_root"))) {
1934 params.mask |= FCGI_SP_DOC_ROOT;
1935 params.docroot = p.v;
1936 }
1937 else if (isteq(p.n, ist("script_name"))) {
1938 params.mask |= FCGI_SP_SCRIPT_NAME;
1939 params.scriptname = p.v;
1940 }
1941 else if (isteq(p.n, ist("path_info"))) {
1942 params.mask |= FCGI_SP_PATH_INFO;
1943 params.pathinfo = p.v;
1944 }
1945 else if (isteq(p.n, ist("request_uri"))) {
1946 params.mask |= FCGI_SP_REQ_URI;
1947 params.uri = p.v;
1948 }
1949 else if (isteq(p.n, ist("request_meth")))
1950 params.mask |= FCGI_SP_REQ_METH;
1951 else if (isteq(p.n, ist("query_string")))
1952 params.mask |= FCGI_SP_REQ_QS;
1953 else if (isteq(p.n, ist("server_name")))
1954 params.mask |= FCGI_SP_SRV_NAME;
1955 else if (isteq(p.n, ist("server_port")))
1956 params.mask |= FCGI_SP_SRV_PORT;
1957 else if (isteq(p.n, ist("server_protocol")))
1958 params.mask |= FCGI_SP_SRV_PROTO;
1959 else if (isteq(p.n, ist("remote_addr")))
1960 params.mask |= FCGI_SP_REM_ADDR;
1961 else if (isteq(p.n, ist("remote_port")))
1962 params.mask |= FCGI_SP_REM_PORT;
1963 else if (isteq(p.n, ist("script_filename")))
1964 params.mask |= FCGI_SP_SCRIPT_FILE;
1965 else if (isteq(p.n, ist("path_translated")))
1966 params.mask |= FCGI_SP_PATH_TRANS;
1967 else if (isteq(p.n, ist("https")))
1968 params.mask |= FCGI_SP_HTTPS;
1969 }
1970 else if (isteq(p.n, ist("content-length"))) {
1971 p.n = ist("CONTENT_LENGTH");
1972 params.mask |= FCGI_SP_CONT_LEN;
1973 }
1974 else if (isteq(p.n, ist("content-type")))
1975 p.n = ist("CONTENT_TYPE");
1976 else {
1977 if (isteq(p.n, ist("host")))
1978 params.srv_name = p.v;
1979
Christopher Faulet67d58092019-10-02 10:51:38 +02001980 /* Skip header if same name is used to add the server name */
1981 if (fconn->proxy->server_id_hdr_name &&
1982 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1983 break;
1984
Christopher Faulet99eff652019-08-11 23:11:30 +02001985 memcpy(trash.area, "http_", 5);
1986 memcpy(trash.area+5, p.n.ptr, p.n.len);
1987 p.n = ist2(trash.area, p.n.len+5);
1988 }
1989
1990 if (!fcgi_encode_param(&outbuf, &p)) {
1991 if (b_space_wraps(mbuf))
1992 goto realign_again;
1993 if (outbuf.data == 8)
1994 goto full;
1995 goto done;
1996 }
1997 break;
1998
1999 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002000 if (fconn->proxy->server_id_hdr_name) {
2001 struct server *srv = objt_server(fconn->conn->target);
2002
2003 if (!srv)
2004 goto done;
2005
2006 memcpy(trash.area, "http_", 5);
2007 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2008 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2009 p.v = ist(srv->id);
2010
2011 if (!fcgi_encode_param(&outbuf, &p)) {
2012 if (b_space_wraps(mbuf))
2013 goto realign_again;
2014 if (outbuf.data == 8)
2015 goto full;
2016 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002017 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002018 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002019 goto done;
2020
2021 default:
2022 break;
2023 }
2024 total += size;
2025 blk = htx_remove_blk(htx, blk);
2026 }
2027
2028 done:
2029 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2030 goto error;
2031
2032 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2033 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2034 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2035 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2036 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2037 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2047 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2048 goto error;
2049
2050 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002051 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 +02002052 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2053 b_add(mbuf, outbuf.data);
2054
2055 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002056 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002057 return total;
2058 full:
2059 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2060 goto retry;
2061 fconn->flags |= FCGI_CF_MUX_MFULL;
2062 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002063 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 +02002064 if (total)
2065 goto error;
2066 goto end;
2067
2068 error:
2069 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002070 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002071 fcgi_strm_error(fstrm);
2072 goto end;
2073}
2074
2075/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2076 * anything. STDIN records contain the request body.
2077 */
2078static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2079 struct htx *htx, size_t count, struct buffer *buf)
2080{
2081 struct buffer outbuf;
2082 struct buffer *mbuf;
2083 struct htx_blk *blk;
2084 enum htx_blk_type type;
2085 uint32_t size;
2086 size_t total = 0;
2087
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002088 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002089 if (!count)
2090 goto end;
2091
2092 mbuf = br_tail(fconn->mbuf);
2093 retry:
2094 if (!fcgi_get_buf(fconn, mbuf)) {
2095 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002096 fstrm->flags |= FCGI_SF_BLK_MROOM;
2097 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2098 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002099 }
2100
2101 /* Perform some optimizations to reduce the number of buffer copies.
2102 * First, if the mux's buffer is empty and the htx area contains exactly
2103 * one data block of the same size as the requested count, and this
2104 * count fits within the record size, then it's possible to simply swap
2105 * the caller's buffer with the mux's output buffer and adjust offsets
2106 * and length to match the entire DATA HTX block in the middle. In this
2107 * case we perform a true zero-copy operation from end-to-end. This is
2108 * the situation that happens all the time with large files. Second, if
2109 * this is not possible, but the mux's output buffer is empty, we still
2110 * have an opportunity to avoid the copy to the intermediary buffer, by
2111 * making the intermediary buffer's area point to the output buffer's
2112 * area. In this case we want to skip the HTX header to make sure that
2113 * copies remain aligned and that this operation remains possible all
2114 * the time. This goes for headers, data blocks and any data extracted
2115 * from the HTX blocks.
2116 */
2117 blk = htx_get_head_blk(htx);
2118 if (!blk)
2119 goto end;
2120 type = htx_get_blk_type(blk);
2121 size = htx_get_blksz(blk);
2122 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2123 void *old_area = mbuf->area;
2124
2125 if (b_data(mbuf)) {
2126 /* Too bad there are data left there. We're willing to memcpy/memmove
2127 * up to 1/4 of the buffer, which means that it's OK to copy a large
2128 * record into a buffer containing few data if it needs to be realigned,
2129 * and that it's also OK to copy few data without realigning. Otherwise
2130 * we'll pretend the mbuf is full and wait for it to become empty.
2131 */
2132 if (size + 8 <= b_room(mbuf) &&
2133 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2134 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2135 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002136 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002137 }
2138
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002139 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 +02002140 /* map a FCGI record to the HTX block so that we can put the
2141 * record header there.
2142 */
2143 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2144 outbuf.area = b_head(mbuf);
2145
2146 /* prepend a FCGI record header just before the DATA block */
2147 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2148 fcgi_set_record_id(outbuf.area, fstrm->id);
2149 fcgi_set_record_size(outbuf.area, size);
2150
2151 /* and exchange with our old area */
2152 buf->area = old_area;
2153 buf->data = buf->head = 0;
2154 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002155
2156 htx = (struct htx *)buf->area;
2157 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002158 goto end;
2159 }
2160
2161 copy:
2162 while (1) {
2163 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2164 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2165 break;
2166 realign_again:
2167 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2168 }
2169
2170 if (outbuf.size < 8)
2171 goto full;
2172
2173 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2174 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2175 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2176 fcgi_set_record_id(outbuf.area, fstrm->id);
2177 outbuf.data = 8;
2178
2179 blk = htx_get_head_blk(htx);
2180 while (blk && count) {
2181 enum htx_blk_type type = htx_get_blk_type(blk);
2182 uint32_t size = htx_get_blksz(blk);
2183 struct ist v;
2184
2185 switch (type) {
2186 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002187 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 +02002188 v = htx_get_blk_value(htx, blk);
2189 if (v.len > count)
2190 v.len = count;
2191
2192 if (v.len > b_room(&outbuf)) {
2193 /* It doesn't fit at once. If it at least fits once split and
2194 * the amount of data to move is low, let's defragment the
2195 * buffer now.
2196 */
2197 if (b_space_wraps(mbuf) &&
2198 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2199 b_data(mbuf) <= MAX_DATA_REALIGN)
2200 goto realign_again;
2201 v.len = b_room(&outbuf);
2202 }
2203 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2204 if (outbuf.data == 8)
2205 goto full;
2206 goto done;
2207 }
2208 if (v.len != size) {
2209 total += v.len;
2210 count -= v.len;
2211 htx_cut_data_blk(htx, blk, v.len);
2212 goto done;
2213 }
2214 break;
2215
2216 case HTX_BLK_EOM:
2217 goto done;
2218
2219 default:
2220 break;
2221 }
2222 total += size;
2223 count -= size;
2224 blk = htx_remove_blk(htx, blk);
2225 }
2226
2227 done:
2228 /* update the record's size */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002229 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 +02002230 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2231 b_add(mbuf, outbuf.data);
2232
2233 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002234 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002235 return total;
2236 full:
2237 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2238 goto retry;
2239 fconn->flags |= FCGI_CF_MUX_MFULL;
2240 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002241 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 +02002242 goto end;
2243}
2244
2245/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2246 * anything. STDOUT records contain the entire response. All the content is
2247 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2248 */
2249static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2250{
2251 struct buffer *dbuf;
2252 size_t ret;
2253 size_t max;
2254
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002255 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2256
Christopher Faulet99eff652019-08-11 23:11:30 +02002257 dbuf = &fconn->dbuf;
2258
2259 /* Only padding remains */
2260 if (fconn->state == FCGI_CS_RECORD_P)
2261 goto end_transfer;
2262
2263 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2264 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2265 buf_room_for_htx_data(dbuf))
2266 goto fail; // incomplete record
2267
2268 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2269 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002270 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2271 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002272 }
2273
2274 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2275 max = buf_room_for_htx_data(&fstrm->rxbuf);
2276 if (!b_data(&fstrm->rxbuf))
2277 fstrm->rxbuf.head = sizeof(struct htx);
2278 if (max > fconn->drl)
2279 max = fconn->drl;
2280
2281 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2282 if (!ret)
2283 goto fail;
2284 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002285 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){ret});
2286 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 +02002287
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002288 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002289 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002290 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2291 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002292
2293 if (fconn->drl)
2294 goto fail;
2295
2296 end_transfer:
2297 fconn->drl += fconn->drp;
2298 fconn->drp = 0;
2299 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2300 b_del(&fconn->dbuf, ret);
2301 fconn->drl -= ret;
2302 if (fconn->drl)
2303 goto fail;
2304
2305 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002306 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2307 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002308 return 1;
2309 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002310 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 +02002311 return 0;
2312}
2313
2314
2315/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2316 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002317 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002318 */
2319static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2320{
2321 int ret;
2322
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002323 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2324
Christopher Faulet99eff652019-08-11 23:11:30 +02002325 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002326 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002327 fconn->drl += fconn->drp;
2328 fconn->drp = 0;
2329 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2330 b_del(&fconn->dbuf, ret);
2331 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002332 if (fconn->drl) {
2333 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 +02002334 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002335 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002336 fconn->state = FCGI_CS_RECORD_H;
2337 fstrm->state |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002338 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm,, (size_t[]){0});
2339 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);
2340 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002341 return 1;
2342}
2343
2344/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2345 * anything.
2346 */
2347static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2348{
2349 struct buffer *dbuf;
2350 struct buffer tag;
2351 size_t ret;
2352
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002353 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002354 dbuf = &fconn->dbuf;
2355
2356 /* Only padding remains */
2357 if (fconn->state == FCGI_CS_RECORD_P)
2358 goto end_transfer;
2359
2360 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2361 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2362 buf_room_for_htx_data(dbuf))
2363 goto fail; // incomplete record
2364
2365 chunk_reset(&trash);
2366 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2367 if (!ret)
2368 goto fail;
2369 fconn->drl -= ret;
Christopher Faulet08618a72019-10-08 11:59:47 +02002370 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 +02002371
2372 trash.area[ret] = '\n';
2373 trash.area[ret+1] = '\0';
2374 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002375 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002376
2377 if (fconn->drl)
2378 goto fail;
2379
2380 end_transfer:
2381 fconn->drl += fconn->drp;
2382 fconn->drp = 0;
2383 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2384 b_del(&fconn->dbuf, ret);
2385 fconn->drl -= ret;
2386 if (fconn->drl)
2387 goto fail;
2388 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002389 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2390 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002391 return 1;
2392 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002393 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 +02002394 return 0;
2395}
2396
2397/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2398 * anything. If the empty STDOUT record is not already received, this one marks
2399 * the end of the response. It is highly unexpected, but if the record is larger
2400 * than a buffer and cannot be decoded in one time, an error is triggered and
2401 * the connection is closed. END_REQUEST record cannot be split.
2402 */
2403static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2404{
2405 struct buffer inbuf;
2406 struct buffer *dbuf;
2407 struct fcgi_end_request endreq;
2408
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002409 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002410 dbuf = &fconn->dbuf;
2411
2412 /* Record too large to be fully decoded */
2413 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2414 goto fail;
2415
2416 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002417 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2418 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002419 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002420 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002421
2422 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2423 /* Realign the dmux buffer if the record wraps. It is unexpected
2424 * at this stage because it should be the first record received
2425 * from the FCGI application.
2426 */
2427 b_slow_realign(dbuf, trash.area, 0);
2428 }
2429
2430 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2431
2432 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2433 goto fail;
2434
2435 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002436 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
2437 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 +02002438 fstrm->proto_status = endreq.errcode;
2439 fcgi_strm_close(fstrm);
2440
2441 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2442 fconn->drl = 0;
2443 fconn->drp = 0;
2444 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002445 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2446 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002447 return 1;
2448
2449 fail:
2450 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002451 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 +02002452 return 0;
2453}
2454
2455/* process Rx records to be demultiplexed */
2456static void fcgi_process_demux(struct fcgi_conn *fconn)
2457{
2458 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2459 struct fcgi_header hdr;
2460 int ret;
2461
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002462 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2463
Christopher Faulet99eff652019-08-11 23:11:30 +02002464 if (fconn->state == FCGI_CS_CLOSED)
2465 return;
2466
2467 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002468 if (fconn->state == FCGI_CS_INIT) {
2469 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2470 return;
2471 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002472 if (fconn->state == FCGI_CS_SETTINGS) {
2473 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002474 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002475 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2476 if (!ret)
2477 goto fail;
2478 b_del(&fconn->dbuf, ret);
2479
2480 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2481 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002482 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);
2483 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 +02002484 goto fail;
2485 }
2486 goto new_record;
2487 }
2488 }
2489
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002490 /* process as many incoming records as possible below */
2491 while (1) {
2492 if (!b_data(&fconn->dbuf)) {
2493 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2494 break;
2495 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002497 if (fconn->state == FCGI_CS_CLOSED) {
2498 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002499 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002500 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002501
2502 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002503 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002504 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2505 if (!ret)
2506 break;
2507 b_del(&fconn->dbuf, ret);
2508
2509 new_record:
2510 fconn->dsi = hdr.id;
2511 fconn->drt = hdr.type;
2512 fconn->drl = hdr.len;
2513 fconn->drp = hdr.padding;
2514 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002515 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 +02002516 }
2517
2518 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2519 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2520
2521 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2522 (b_data(&fstrm->rxbuf) ||
2523 conn_xprt_read0_pending(fconn->conn) ||
2524 fstrm->state == FCGI_SS_CLOSED ||
2525 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2526 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2527 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002528 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 +02002529 fstrm->cs->flags |= CS_FL_RCV_MORE;
2530 fcgi_strm_notify_recv(fstrm);
2531 }
2532 fstrm = tmp_fstrm;
2533
2534 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2535 /* ignore all record for closed streams */
2536 goto ignore_record;
2537 }
2538 if (fstrm->state == FCGI_SS_IDLE) {
2539 /* ignore all record for unknown streams */
2540 goto ignore_record;
2541 }
2542
2543 switch (fconn->drt) {
2544 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002545 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 +02002546 ret = fcgi_conn_handle_values_result(fconn);
2547 break;
2548
2549 case FCGI_STDOUT:
2550 if (fstrm->flags & FCGI_SF_ES_RCVD)
2551 goto ignore_record;
2552
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002553 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002554 if (fconn->drl)
2555 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2556 else
2557 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2558 break;
2559
2560 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002561 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002562 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2563 break;
2564
2565 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002566 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 +02002567 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2568 break;
2569
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002570 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002571 default:
2572 ignore_record:
2573 /* drop records that we ignore. They may be
2574 * larger than the buffer so we drain all of
2575 * their contents until we reach the end.
2576 */
2577 fconn->state = FCGI_CS_RECORD_P;
2578 fconn->drl += fconn->drp;
2579 fconn->drp = 0;
2580 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002581 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm,, (size_t[]){ret});
2582 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002583 b_del(&fconn->dbuf, ret);
2584 fconn->drl -= ret;
2585 ret = (fconn->drl == 0);
2586 }
2587
2588 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002589 if (ret <= 0) {
2590 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002591 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002592 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002593
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002594 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002595 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2597 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002598 }
2599
2600 fail:
2601 /* we can go here on missing data, blocked response or error */
2602 if (fstrm && fstrm->cs &&
2603 (b_data(&fstrm->rxbuf) ||
2604 conn_xprt_read0_pending(fconn->conn) ||
2605 fstrm->state == FCGI_SS_CLOSED ||
2606 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2607 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2608 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002609 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 +02002610 fstrm->cs->flags |= CS_FL_RCV_MORE;
2611 fcgi_strm_notify_recv(fstrm);
2612 }
2613
2614 fcgi_conn_restart_reading(fconn, 0);
2615}
2616
2617/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2618 * the end.
2619 */
2620static int fcgi_process_mux(struct fcgi_conn *fconn)
2621{
2622 struct fcgi_strm *fstrm, *fstrm_back;
2623
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002624 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2625
Christopher Faulet99eff652019-08-11 23:11:30 +02002626 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2627 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2628 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2629 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002630 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 +02002631 fcgi_wake_unassigned_streams(fconn);
2632 goto mux;
2633 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002634 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002635 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2636 goto fail;
2637 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002638 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 +02002639 }
2640 /* need to wait for the other side */
2641 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002642 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002643 }
2644
2645 mux:
2646 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2647 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2648 break;
2649
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002650 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002651 continue;
2652
Willy Tarreau7aad7032020-01-16 17:20:57 +01002653 /* If the sender changed his mind and unsubscribed, let's just
2654 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002655 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002656 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2657 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002658 LIST_DEL_INIT(&fstrm->send_list);
2659 continue;
2660 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002661
2662 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002663 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2664 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002665 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002666 tasklet_wakeup(fstrm->subs->tasklet);
2667 fstrm->subs->events &= ~SUB_RETRY_SEND;
2668 if (!fstrm->subs->events)
2669 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002670 } else {
2671 /* it's the shut request that was queued */
2672 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2673 tasklet_wakeup(fstrm->shut_tl);
2674 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002675 }
2676
2677 fail:
2678 if (fconn->state == FCGI_CS_CLOSED) {
2679 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2680 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002681 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2682 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002683 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002684 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002685 }
2686 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002687
2688 done:
2689 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 return 1;
2691}
2692
2693
2694/* Attempt to read data, and subscribe if none available.
2695 * The function returns 1 if data has been received, otherwise zero.
2696 */
2697static int fcgi_recv(struct fcgi_conn *fconn)
2698{
2699 struct connection *conn = fconn->conn;
2700 struct buffer *buf;
2701 int max;
2702 size_t ret;
2703
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002704 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2705
2706 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2707 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002708 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002709 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002710
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002711 if (!fcgi_recv_allowed(fconn)) {
2712 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002713 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002714 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002715
2716 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2717 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002718 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002719 fconn->flags |= FCGI_CF_DEM_DALLOC;
2720 return 0;
2721 }
2722
2723 b_realign_if_empty(buf);
2724 if (!b_data(buf)) {
2725 /* try to pre-align the buffer like the
2726 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002728 * HTX block to alias it upon recv. We cannot use the
2729 * head because rcv_buf() will realign the buffer if
2730 * it's empty. Thus we cheat and pretend we already
2731 * have a few bytes there.
2732 */
2733 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2734 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2735 }
2736 else
2737 max = buf_room_for_htx_data(buf);
2738
2739 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2740
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002741 if (max && !ret && fcgi_recv_allowed(fconn)) {
2742 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002743 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002744 }
2745 else
Christopher Faulet76014fd2019-12-10 11:47:22 +01002746 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002747
2748 if (!b_data(buf)) {
2749 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002750 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002751 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2752 }
2753
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002754 if (ret == max) {
2755 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002756 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002757 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002758
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002759 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002760 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2761}
2762
2763
2764/* Try to send data if possible.
2765 * The function returns 1 if data have been sent, otherwise zero.
2766 */
2767static int fcgi_send(struct fcgi_conn *fconn)
2768{
2769 struct connection *conn = fconn->conn;
2770 int done;
2771 int sent = 0;
2772
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002773 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2774
2775 if (conn->flags & CO_FL_ERROR) {
2776 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002777 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002778 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002779
2780
Willy Tarreau911db9b2020-01-23 16:27:54 +01002781 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002782 /* a handshake was requested */
2783 goto schedule;
2784 }
2785
2786 /* This loop is quite simple : it tries to fill as much as it can from
2787 * pending streams into the existing buffer until it's reportedly full
2788 * or the end of send requests is reached. Then it tries to send this
2789 * buffer's contents out, marks it not full if at least one byte could
2790 * be sent, and tries again.
2791 *
2792 * The snd_buf() function normally takes a "flags" argument which may
2793 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2794 * data immediately comes and CO_SFL_STREAMER to indicate that the
2795 * connection is streaming lots of data (used to increase TLS record
2796 * size at the expense of latency). The former can be sent any time
2797 * there's a buffer full flag, as it indicates at least one stream
2798 * attempted to send and failed so there are pending data. An
2799 * alternative would be to set it as long as there's an active stream
2800 * but that would be problematic for ACKs until we have an absolute
2801 * guarantee that all waiters have at least one byte to send. The
2802 * latter should possibly not be set for now.
2803 */
2804
2805 done = 0;
2806 while (!done) {
2807 unsigned int flags = 0;
2808 unsigned int released = 0;
2809 struct buffer *buf;
2810
2811 /* fill as much as we can into the current buffer */
2812 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2813 done = fcgi_process_mux(fconn);
2814
2815 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2816 done = 1; // we won't go further without extra buffers
2817
2818 if (conn->flags & CO_FL_ERROR)
2819 break;
2820
2821 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2822 flags |= CO_SFL_MSG_MORE;
2823
2824 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2825 if (b_data(buf)) {
2826 int ret;
2827
2828 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2829 if (!ret) {
2830 done = 1;
2831 break;
2832 }
2833 sent = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002834 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn,,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002835 b_del(buf, ret);
2836 if (b_data(buf)) {
2837 done = 1;
2838 break;
2839 }
2840 }
2841 b_free(buf);
2842 released++;
2843 }
2844
2845 if (released)
2846 offer_buffers(NULL, tasks_run_queue);
2847
2848 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002849 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2850 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002851 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2852 }
2853
2854 if (conn->flags & CO_FL_SOCK_WR_SH) {
2855 /* output closed, nothing to send, clear the buffer to release it */
2856 b_reset(br_tail(fconn->mbuf));
2857 }
2858 /* We're not full anymore, so we can wake any task that are waiting
2859 * for us.
2860 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002861 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002862 struct fcgi_strm *fstrm;
2863
2864 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2865 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2866 break;
2867
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002868 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 continue;
2870
Willy Tarreau7aad7032020-01-16 17:20:57 +01002871 /* If the sender changed his mind and unsubscribed, let's just
2872 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002873 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002874 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2875 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002876 LIST_DEL_INIT(&fstrm->send_list);
2877 continue;
2878 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002879
2880 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002881 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002882 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002883 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002884 tasklet_wakeup(fstrm->subs->tasklet);
2885 fstrm->subs->events &= ~SUB_RETRY_SEND;
2886 if (!fstrm->subs->events)
2887 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002888 } else {
2889 /* it's the shut request that was queued */
2890 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2891 tasklet_wakeup(fstrm->shut_tl);
2892 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002893 }
2894 }
2895 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002896 if (!br_data(fconn->mbuf)) {
2897 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002898 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002899 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002900schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002901 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2902 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002903 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002904 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002905
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002906 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002907 return sent;
2908}
2909
2910/* this is the tasklet referenced in fconn->wait_event.tasklet */
2911static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2912{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002913 struct connection *conn;
2914 struct fcgi_conn *fconn;
2915 struct tasklet *tl = (struct tasklet *)t;
2916 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002917 int ret = 0;
2918
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002919
2920 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
2921 if (tl->context == NULL) {
2922 /* The connection has been taken over by another thread,
2923 * we're no longer responsible for it, so just free the
2924 * tasklet, and do nothing.
2925 */
2926 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
2927 tasklet_free(tl);
2928 return NULL;
2929
2930 }
2931 fconn = ctx;
2932 conn = fconn->conn;
2933
2934 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2935
2936 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2937 if (conn_in_list)
2938 MT_LIST_DEL(&conn->list);
2939
2940 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002941
Christopher Faulet99eff652019-08-11 23:11:30 +02002942 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2943 ret = fcgi_send(fconn);
2944 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2945 ret |= fcgi_recv(fconn);
2946 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002947 ret = fcgi_process(fconn);
2948
2949 /* If we were in an idle list, we want to add it back into it,
2950 * unless fcgi_process() returned -1, which mean it has destroyed
2951 * the connection (testing !ret is enough, if fcgi_process() wasn't
2952 * called then ret will be 0 anyway.
2953 */
2954 if (!ret && conn_in_list) {
2955 struct server *srv = objt_server(conn->target);
2956
2957 if (conn_in_list == CO_FL_SAFE_LIST)
2958 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
2959 else
2960 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
2961 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002962 return NULL;
2963}
2964
2965/* callback called on any event by the connection handler.
2966 * It applies changes and returns zero, or < 0 if it wants immediate
2967 * destruction of the connection (which normally doesn not happen in FCGI).
2968 */
2969static int fcgi_process(struct fcgi_conn *fconn)
2970{
2971 struct connection *conn = fconn->conn;
2972
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002973 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2974
Christopher Faulet99eff652019-08-11 23:11:30 +02002975 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2976 fcgi_process_demux(fconn);
2977
2978 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2979 b_reset(&fconn->dbuf);
2980
2981 if (buf_room_for_htx_data(&fconn->dbuf))
2982 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2983 }
2984 fcgi_send(fconn);
2985
2986 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2987 /* frontend is stopping, reload likely in progress, let's try
2988 * to announce a graceful shutdown if not yet done. We don't
2989 * care if it fails, it will be tried again later.
2990 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002991 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 +02002992 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
2993 if (fconn->stream_cnt - fconn->nb_reserved > 0)
2994 fcgi_conn_send_aborts(fconn);
2995 }
2996 }
2997
2998 /*
2999 * If we received early data, and the handshake is done, wake
3000 * any stream that was waiting for it.
3001 */
3002 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003003 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003004 struct eb32_node *node;
3005 struct fcgi_strm *fstrm;
3006
3007 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3008 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3009
3010 while (node) {
3011 fstrm = container_of(node, struct fcgi_strm, by_id);
3012 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3013 fcgi_strm_notify_recv(fstrm);
3014 node = eb32_next(node);
3015 }
3016 }
3017
3018 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
3019 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3020 eb_is_empty(&fconn->streams_by_id)) {
3021 fcgi_wake_some_streams(fconn, 0);
3022
3023 if (eb_is_empty(&fconn->streams_by_id)) {
3024 /* no more stream, kill the connection now */
3025 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003026 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003027 return -1;
3028 }
3029 }
3030
3031 if (!b_data(&fconn->dbuf))
3032 fcgi_release_buf(fconn, &fconn->dbuf);
3033
3034 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3035 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3036 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3037 fcgi_release_mbuf(fconn);
3038
3039 if (fconn->task) {
3040 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3041 task_queue(fconn->task);
3042 }
3043
3044 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003045 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003046 return 0;
3047}
3048
3049
3050/* wake-up function called by the connection layer (mux_ops.wake) */
3051static int fcgi_wake(struct connection *conn)
3052{
3053 struct fcgi_conn *fconn = conn->ctx;
3054
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003055 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003056 return (fcgi_process(fconn));
3057}
3058
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003059
3060static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3061{
3062 int ret = 0;
3063 switch (mux_ctl) {
3064 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003065 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003066 ret |= MUX_STATUS_READY;
3067 return ret;
3068 default:
3069 return -1;
3070 }
3071}
3072
Christopher Faulet99eff652019-08-11 23:11:30 +02003073/* Connection timeout management. The principle is that if there's no receipt
3074 * nor sending for a certain amount of time, the connection is closed. If the
3075 * MUX buffer still has lying data or is not allocatable, the connection is
3076 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003077 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003078 */
3079static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3080{
3081 struct fcgi_conn *fconn = context;
3082 int expired = tick_is_expired(t->expire, now_ms);
3083
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003084 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3085
3086 if (!expired && fconn) {
3087 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003088 return t;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003089 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003090
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003091 /* We're about to destroy the connection, so make sure nobody attempts
3092 * to steal it from us.
3093 */
3094 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3095
3096 if (fconn && fconn->conn->flags & CO_FL_LIST_MASK)
3097 MT_LIST_DEL(&fconn->conn->list);
3098
3099 /* Somebody already stole the connection from us, so we should not
3100 * free it, we just have to free the task.
3101 */
3102 if (!t->context)
3103 fconn = NULL;
3104
3105 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3106
Christopher Faulet99eff652019-08-11 23:11:30 +02003107 task_destroy(t);
3108
3109 if (!fconn) {
3110 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003111 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003112 return NULL;
3113 }
3114
3115 fconn->task = NULL;
3116 fconn->state = FCGI_CS_CLOSED;
3117 fcgi_wake_some_streams(fconn, 0);
3118
3119 if (br_data(fconn->mbuf)) {
3120 /* don't even try to send aborts, the buffer is stuck */
3121 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3122 goto end;
3123 }
3124
3125 /* try to send but no need to insist */
3126 if (!fcgi_conn_send_aborts(fconn))
3127 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3128
3129 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3130 conn_xprt_ready(fconn->conn)) {
3131 unsigned int released = 0;
3132 struct buffer *buf;
3133
3134 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3135 if (b_data(buf)) {
3136 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3137 buf, b_data(buf), 0);
3138 if (!ret)
3139 break;
3140 b_del(buf, ret);
3141 if (b_data(buf))
3142 break;
3143 b_free(buf);
3144 released++;
3145 }
3146 }
3147
3148 if (released)
3149 offer_buffers(NULL, tasks_run_queue);
3150 }
3151
3152 end:
3153 /* either we can release everything now or it will be done later once
3154 * the last stream closes.
3155 */
3156 if (eb_is_empty(&fconn->streams_by_id))
3157 fcgi_release(fconn);
3158
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003159 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003160 return NULL;
3161}
3162
3163
3164/*******************************************/
3165/* functions below are used by the streams */
3166/*******************************************/
3167
3168/* Append the description of what is present in error snapshot <es> into <out>.
3169 * The description must be small enough to always fit in a buffer. The output
3170 * buffer may be the trash so the trash must not be used inside this function.
3171 */
3172static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3173{
3174 chunk_appendf(out,
3175 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3176 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3177 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3178 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3179 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3180 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3181}
3182/*
3183 * Capture a bad response and archive it in the proxy's structure. By default
3184 * it tries to report the error position as h1m->err_pos. However if this one is
3185 * not set, it will then report h1m->next, which is the last known parsing
3186 * point. The function is able to deal with wrapping buffers. It always displays
3187 * buffers as a contiguous area starting at buf->p. The direction is determined
3188 * thanks to the h1m's flags.
3189 */
3190static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3191 struct h1m *h1m, struct buffer *buf)
3192{
3193 struct session *sess = fstrm->sess;
3194 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003195 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003196 union error_snapshot_ctx ctx;
3197
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003198 if (fstrm->cs && fstrm->cs->data) {
3199 if (sess == NULL)
3200 sess = si_strm(fstrm->cs->data)->sess;
3201 if (!(h1m->flags & H1_MF_RESP))
3202 other_end = si_strm(fstrm->cs->data)->be;
3203 else
3204 other_end = sess->fe;
3205 } else
3206 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003207 /* http-specific part now */
3208 ctx.h1.state = h1m->state;
3209 ctx.h1.c_flags = fconn->flags;
3210 ctx.h1.s_flags = fstrm->flags;
3211 ctx.h1.m_flags = h1m->flags;
3212 ctx.h1.m_clen = h1m->curr_len;
3213 ctx.h1.m_blen = h1m->body_len;
3214
3215 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3216 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3217 &ctx, fcgi_show_error_snapshot);
3218}
3219
3220static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3221 struct buffer *buf, size_t *ofs, size_t max)
3222{
3223 int ret;
3224
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003225 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003226 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3227 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003228 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 +02003229 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003230 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 +02003231 fcgi_strm_error(fstrm);
3232 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3233 }
3234 goto end;
3235 }
3236
3237 *ofs += ret;
3238 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003239 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003240 return ret;
3241
3242}
3243
Christopher Fauletaf542632019-10-01 21:52:49 +02003244static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003245 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3246{
3247 int ret;
3248
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003249 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003250 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003251 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003252 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 +02003253 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003254 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 +02003255 fcgi_strm_error(fstrm);
3256 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3257 }
3258 goto end;
3259 }
3260 *ofs += ret;
3261 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003262 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003263 return ret;
3264}
3265
3266static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3267 struct buffer *buf, size_t *ofs, size_t max)
3268{
3269 int ret;
3270
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003271 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003272 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003273 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003274 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 +02003275 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003276 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 +02003277 fcgi_strm_error(fstrm);
3278 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3279 }
3280 goto end;
3281 }
3282 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003283 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003284 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003285 return ret;
3286}
3287
3288static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003289 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003290{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003291 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003292
Willy Tarreaubf5b4912020-04-23 17:24:59 +02003293 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003294 ret = h1_parse_msg_eom(h1m, htx, max);
3295 if (!ret) {
3296 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3297 if (htx->flags & HTX_FL_PARSING_ERROR) {
3298 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3299 fcgi_strm_error(fstrm);
3300 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3301 }
3302 goto end;
3303 }
3304 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3305 end:
3306 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm,, (size_t[]){ret});
3307 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003308}
3309
3310static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3311{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003312 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003313 struct htx *htx;
3314 struct h1m *h1m = &fstrm->h1m;
3315 size_t ret, data, total = 0;
3316
3317 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003318 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3319
Christopher Faulet99eff652019-08-11 23:11:30 +02003320 data = htx->data;
3321 if (fstrm->state == FCGI_SS_ERROR)
3322 goto end;
3323
3324 do {
3325 size_t used = htx_used_space(htx);
3326
3327 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003328 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003329 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3330 if (!ret)
3331 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003332
3333 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3334
Christopher Faulet99eff652019-08-11 23:11:30 +02003335 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3336 struct htx_blk *blk = htx_get_head_blk(htx);
3337 struct htx_sl *sl;
3338
3339 if (!blk)
3340 break;
3341 sl = htx_get_blk_ptr(htx, blk);
3342 sl->flags |= HTX_SL_F_XFER_LEN;
3343 htx->extra = 0;
3344 }
3345 }
3346 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003347 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003348 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003349 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003350 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003351
3352 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 +02003353 }
3354 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003355 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3356 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3357 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003358 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003359
Christopher Faulet76014fd2019-12-10 11:47:22 +01003360 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 +02003361 }
3362 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003363 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3364 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3365 break;
3366
3367 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3368 }
3369
Christopher Faulet99eff652019-08-11 23:11:30 +02003370 if (b_data(&fstrm->rxbuf) > total) {
3371 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003372 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003373 fcgi_strm_error(fstrm);
3374 }
3375 break;
3376 }
3377 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003378 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003379 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003380
Christopher Faulet99eff652019-08-11 23:11:30 +02003381 if (fstrm->state != FCGI_SS_ERROR &&
3382 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003383 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003384 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3385 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3386 h1m->state = H1_MSG_DONE;
3387 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 +02003388 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003389 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003390 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003391
3392 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003393 }
3394 else {
3395 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003396 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003397 fcgi_strm_error(fstrm);
3398 break;
3399 }
3400
3401 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003402 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003403
3404 if (fstrm->state == FCGI_SS_ERROR) {
3405 b_reset(&fstrm->rxbuf);
3406 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003407 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003408 return 0;
3409 }
3410
3411 b_del(&fstrm->rxbuf, total);
3412
3413 end:
3414 htx_to_buf(htx, buf);
3415 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003416 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003417 return ret;
3418}
3419
3420/*
3421 * Attach a new stream to a connection
3422 * (Used for outgoing connections)
3423 */
3424static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3425{
3426 struct conn_stream *cs;
3427 struct fcgi_strm *fstrm;
3428 struct fcgi_conn *fconn = conn->ctx;
3429
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003430 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 cs = cs_new(conn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003432 if (!cs) {
3433 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003434 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003435 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003436 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3437 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003438 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003439 cs_free(cs);
3440 return NULL;
3441 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003442 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003443 return cs;
3444}
3445
3446/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3447 * We have to scan because we may have some orphan streams. It might be
3448 * beneficial to scan backwards from the end to reduce the likeliness to find
3449 * orphans.
3450 */
3451static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3452{
3453 struct fcgi_conn *fconn = conn->ctx;
3454 struct fcgi_strm *fstrm;
3455 struct eb32_node *node;
3456
3457 node = eb32_first(&fconn->streams_by_id);
3458 while (node) {
3459 fstrm = container_of(node, struct fcgi_strm, by_id);
3460 if (fstrm->cs)
3461 return fstrm->cs;
3462 node = eb32_next(node);
3463 }
3464 return NULL;
3465}
3466
3467/*
3468 * Destroy the mux and the associated connection, if it is no longer used
3469 */
3470static void fcgi_destroy(void *ctx)
3471{
3472 struct fcgi_conn *fconn = ctx;
3473
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003474 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003475 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3476 fcgi_release(fconn);
3477}
3478
3479/*
3480 * Detach the stream from the connection and possibly release the connection.
3481 */
3482static void fcgi_detach(struct conn_stream *cs)
3483{
3484 struct fcgi_strm *fstrm = cs->ctx;
3485 struct fcgi_conn *fconn;
3486 struct session *sess;
3487
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003488 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3489
Christopher Faulet99eff652019-08-11 23:11:30 +02003490 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003491 if (!fstrm) {
3492 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003493 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003494 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003495
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003496 /* there's no txbuf so we're certain no to be able to send anything */
3497 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003498
3499 sess = fstrm->sess;
3500 fconn = fstrm->fconn;
3501 fstrm->cs = NULL;
3502 fconn->nb_cs--;
3503
3504 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3505 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3506 fconn->streams_limit = 1;
3507 }
3508 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3509 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3510 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3511 fconn->state = FCGI_CS_CLOSED;
3512 }
3513
3514 /* this stream may be blocked waiting for some data to leave, so orphan
3515 * it in this case.
3516 */
3517 if (!(cs->conn->flags & CO_FL_ERROR) &&
3518 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003519 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003520 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003521 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003522 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003523 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003524
3525 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3526 /* unblock the connection if it was blocked on this stream. */
3527 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3528 fcgi_conn_restart_reading(fconn, 1);
3529 }
3530
3531 fcgi_strm_destroy(fstrm);
3532
3533 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003534 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003535 /* Never ever allow to reuse a connection from a non-reuse backend */
3536 if ((fconn->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3537 fconn->conn->flags |= CO_FL_PRIVATE;
3538 if (!fconn->conn->owner && (fconn->conn->flags & CO_FL_PRIVATE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003539 fconn->conn->owner = sess;
3540 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3541 fconn->conn->owner = NULL;
3542 if (eb_is_empty(&fconn->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003543 /* let's kill the connection right away */
3544 fconn->conn->mux->destroy(fconn);
3545 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003546 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003547 }
3548 }
3549 }
3550 if (eb_is_empty(&fconn->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003551 if (sess && fconn->conn->owner == sess &&
3552 session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003553 /* The connection is destroyed, let's leave */
3554 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003555 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003556 }
Olivier Houchard2444aa52020-01-20 13:56:01 +01003557 if (!(fconn->conn->flags & CO_FL_PRIVATE)) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003558 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003559 /* The server doesn't want it, let's kill the connection right away */
3560 fconn->conn->mux->destroy(fconn);
3561 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3562 return;
3563 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003564 /* At this point, the connection has been added to the
3565 * server idle list, so another thread may already have
3566 * hijacked it, so we can't do anything with it.
3567 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003568 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3569 return;
3570 }
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003571 } else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003572 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003573 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003574 }
3575
Christopher Faulet99eff652019-08-11 23:11:30 +02003576 }
3577
3578 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003579 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003580 */
3581 if (fcgi_conn_is_dead(fconn)) {
3582 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003583 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003584 fcgi_release(fconn);
3585 }
3586 else if (fconn->task) {
3587 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3588 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003589 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003590 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003591 else
3592 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003593}
3594
3595
3596/* Performs a synchronous or asynchronous shutr(). */
3597static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3598{
3599 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003600
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003601 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3602
Christopher Faulet99eff652019-08-11 23:11:30 +02003603 if (fstrm->state == FCGI_SS_CLOSED)
3604 goto done;
3605
3606 /* a connstream may require us to immediately kill the whole connection
3607 * for example because of a "tcp-request content reject" rule that is
3608 * normally used to limit abuse.
3609 */
3610 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003611 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3612 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003613 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003614 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003615 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003616 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003617 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3618 !fcgi_strm_send_abort(fconn, fstrm))
3619 goto add_to_list;
3620 }
3621
3622 fcgi_strm_close(fstrm);
3623
3624 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3625 tasklet_wakeup(fconn->wait_event.tasklet);
3626 done:
3627 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003628 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003629 return;
3630
3631 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003632 /* Let the handler know we want to shutr, and add ourselves to the
3633 * send list if not yet done. fcgi_deferred_shut() will be
3634 * automatically called via the shut_tl tasklet when there's room
3635 * again.
3636 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003638 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003639 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3640 }
3641 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003642 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003643 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003644 return;
3645}
3646
3647/* Performs a synchronous or asynchronous shutw(). */
3648static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3649{
3650 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003651
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003652 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3653
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3655 goto done;
3656
3657 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3658 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3659 !fcgi_strm_send_abort(fconn, fstrm))
3660 goto add_to_list;
3661
3662 if (fstrm->state == FCGI_SS_HREM)
3663 fcgi_strm_close(fstrm);
3664 else
3665 fstrm->state = FCGI_SS_HLOC;
3666 } else {
3667 /* a connstream may require us to immediately kill the whole connection
3668 * for example because of a "tcp-request content reject" rule that is
3669 * normally used to limit abuse.
3670 */
3671 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003672 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3673 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003674 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003675 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003676
3677 fcgi_strm_close(fstrm);
3678 }
3679
3680 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3681 tasklet_wakeup(fconn->wait_event.tasklet);
3682 done:
3683 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003684 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003685 return;
3686
3687 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003688 /* Let the handler know we want to shutr, and add ourselves to the
3689 * send list if not yet done. fcgi_deferred_shut() will be
3690 * automatically called via the shut_tl tasklet when there's room
3691 * again.
3692 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003693 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003694 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003695 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3696 }
3697 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003698 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003699 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003700 return;
3701}
3702
Willy Tarreau7aad7032020-01-16 17:20:57 +01003703/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003704 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003705 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003706 */
3707static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3708{
3709 struct fcgi_strm *fstrm = ctx;
3710 struct fcgi_conn *fconn = fstrm->fconn;
3711
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003712 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3713
Willy Tarreau7aad7032020-01-16 17:20:57 +01003714 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3715 /* some data processing remains to be done first */
3716 goto end;
3717 }
3718
Christopher Faulet99eff652019-08-11 23:11:30 +02003719 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3720 fcgi_do_shutw(fstrm);
3721
3722 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3723 fcgi_do_shutr(fstrm);
3724
3725 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3726 /* We're done trying to send, remove ourself from the send_list */
3727 LIST_DEL_INIT(&fstrm->send_list);
3728
3729 if (!fstrm->cs) {
3730 fcgi_strm_destroy(fstrm);
3731 if (fcgi_conn_is_dead(fconn))
3732 fcgi_release(fconn);
3733 }
3734 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003735 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003736 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003737 return NULL;
3738}
3739
3740/* shutr() called by the conn_stream (mux_ops.shutr) */
3741static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3742{
3743 struct fcgi_strm *fstrm = cs->ctx;
3744
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003745 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003746 if (cs->flags & CS_FL_KILL_CONN)
3747 fstrm->flags |= FCGI_SF_KILL_CONN;
3748
3749 if (!mode)
3750 return;
3751
3752 fcgi_do_shutr(fstrm);
3753}
3754
3755/* shutw() called by the conn_stream (mux_ops.shutw) */
3756static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3757{
3758 struct fcgi_strm *fstrm = cs->ctx;
3759
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003760 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003761 if (cs->flags & CS_FL_KILL_CONN)
3762 fstrm->flags |= FCGI_SF_KILL_CONN;
3763
3764 fcgi_do_shutw(fstrm);
3765}
3766
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003767/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3768 * event subscriber <es> is not allowed to change from a previous call as long
3769 * as at least one event is still subscribed. The <event_type> must only be a
3770 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003771 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003772static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003773{
Christopher Faulet99eff652019-08-11 23:11:30 +02003774 struct fcgi_strm *fstrm = cs->ctx;
3775 struct fcgi_conn *fconn = fstrm->fconn;
3776
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003777 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003778 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003779
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003780 es->events |= event_type;
3781 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003782
3783 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003784 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003785
Christopher Faulet99eff652019-08-11 23:11:30 +02003786 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003787 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003788 if (!LIST_ADDED(&fstrm->send_list))
3789 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003790 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003791 return 0;
3792}
3793
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003794/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3795 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3796 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003797 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003798static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003799{
Christopher Faulet99eff652019-08-11 23:11:30 +02003800 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003801 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003802
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003803 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003804 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003805
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003806 es->events &= ~event_type;
3807 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003808 fstrm->subs = NULL;
3809
3810 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003811 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003812
Christopher Faulet99eff652019-08-11 23:11:30 +02003813 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003814 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003815 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003816 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3817 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003818 }
3819 return 0;
3820}
3821
3822/* Called from the upper layer, to receive data */
3823static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3824{
3825 struct fcgi_strm *fstrm = cs->ctx;
3826 struct fcgi_conn *fconn = fstrm->fconn;
3827 size_t ret = 0;
3828
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003829 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3830
Christopher Faulet99eff652019-08-11 23:11:30 +02003831 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3832 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003833 else
3834 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003835
Christopher Faulet76014fd2019-12-10 11:47:22 +01003836 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 +02003837 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3838 else {
3839 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003840 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003841 cs->flags |= CS_FL_EOI;
3842 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3843 cs->flags |= CS_FL_EOS;
3844 }
3845 if (conn_xprt_read0_pending(fconn->conn))
3846 cs->flags |= CS_FL_EOS;
3847 if (cs->flags & CS_FL_ERR_PENDING)
3848 cs->flags |= CS_FL_ERROR;
3849 fcgi_release_buf(fconn, &fstrm->rxbuf);
3850 }
3851
3852 if (ret && fconn->dsi == fstrm->id) {
3853 /* demux is blocking on this stream's buffer */
3854 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3855 fcgi_conn_restart_reading(fconn, 1);
3856 }
3857
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003858 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003859 return ret;
3860}
3861
3862
Christopher Faulet99eff652019-08-11 23:11:30 +02003863/* Called from the upper layer, to send data from buffer <buf> for no more than
3864 * <count> bytes. Returns the number of bytes effectively sent. Some status
3865 * flags may be updated on the conn_stream.
3866 */
3867static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3868{
3869 struct fcgi_strm *fstrm = cs->ctx;
3870 struct fcgi_conn *fconn = fstrm->fconn;
3871 size_t total = 0;
3872 size_t ret;
3873 struct htx *htx = NULL;
3874 struct htx_sl *sl;
3875 struct htx_blk *blk;
3876 uint32_t bsize;
3877
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003878 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm,, (size_t[]){count});
3879
Christopher Faulet99eff652019-08-11 23:11:30 +02003880 /* If we were not just woken because we wanted to send but couldn't,
3881 * and there's somebody else that is waiting to send, do nothing,
3882 * we will subscribe later and be put at the end of the list
3883 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003884 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003885 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 +02003886 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003887 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003888 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003889
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003890 if (fconn->state < FCGI_CS_RECORD_H) {
3891 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003893 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003894
3895 htx = htxbuf(buf);
3896 if (fstrm->id == 0) {
3897 int32_t id = fcgi_conn_get_next_sid(fconn);
3898
3899 if (id < 0) {
3900 fcgi_strm_close(fstrm);
3901 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003902 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 +02003903 return 0;
3904 }
3905
3906 eb32_delete(&fstrm->by_id);
3907 fstrm->by_id.key = fstrm->id = id;
3908 fconn->max_id = id;
3909 fconn->nb_reserved--;
3910 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3911
3912
3913 /* Check if length of the body is known or if the message is
3914 * full. Otherwise, the request is invalid.
3915 */
3916 sl = http_get_stline(htx);
3917 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3918 htx->flags |= HTX_FL_PARSING_ERROR;
3919 fcgi_strm_error(fstrm);
3920 goto done;
3921 }
3922 }
3923
3924 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003925 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 +02003926 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3927 goto done;
3928 }
3929
3930 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3931 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3932
Christopher Fauletfe410d62020-05-19 15:13:00 +02003933 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003934 count && !htx_is_empty(htx)) {
3935 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003936 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003937 bsize = htx_get_blksz(blk);
3938
3939 switch (htx_get_blk_type(blk)) {
3940 case HTX_BLK_REQ_SL:
3941 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003942 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 +02003943 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3944 if (!ret) {
3945 goto done;
3946 }
3947 total += ret;
3948 count -= ret;
3949 break;
3950
3951 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003952 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 +02003953 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3954 if (!ret)
3955 goto done;
3956 goto remove_blk;
3957
3958 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003959 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 +02003960 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3961 if (ret > 0) {
3962 htx = htx_from_buf(buf);
3963 total += ret;
3964 count -= ret;
3965 if (ret < bsize)
3966 goto done;
3967 }
3968 break;
3969
3970 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003971 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 +02003972 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3973 if (!ret)
3974 goto done;
3975 goto remove_blk;
3976
3977 default:
3978 remove_blk:
3979 htx_remove_blk(htx, blk);
3980 total += bsize;
3981 count -= bsize;
3982 break;
3983 }
3984 }
3985
3986 done:
3987 if (fstrm->state >= FCGI_SS_HLOC) {
3988 /* trim any possibly pending data after we close (extra CR-LF,
3989 * unprocessed trailers, abnormal extra data, ...)
3990 */
3991 total += count;
3992 count = 0;
3993 }
3994
3995 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003996 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 +02003997 cs_set_error(cs);
3998 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
3999 fcgi_strm_close(fstrm);
4000 }
4001
4002 if (htx)
4003 htx_to_buf(htx, buf);
4004
Christopher Faulet99eff652019-08-11 23:11:30 +02004005 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004006 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4007 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 +02004008 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004009 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004010
4011 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004012 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4013 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004014 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004015
4016 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004017 return total;
4018}
4019
4020/* for debugging with CLI's "show fd" command */
4021static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
4022{
4023 struct fcgi_conn *fconn = conn->ctx;
4024 struct fcgi_strm *fstrm = NULL;
4025 struct eb32_node *node;
4026 int send_cnt = 0;
4027 int tree_cnt = 0;
4028 int orph_cnt = 0;
4029 struct buffer *hmbuf, *tmbuf;
4030
4031 if (!fconn)
4032 return;
4033
4034 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4035 send_cnt++;
4036
4037 fstrm = NULL;
4038 node = eb32_first(&fconn->streams_by_id);
4039 while (node) {
4040 fstrm = container_of(node, struct fcgi_strm, by_id);
4041 tree_cnt++;
4042 if (!fstrm->cs)
4043 orph_cnt++;
4044 node = eb32_next(node);
4045 }
4046
4047 hmbuf = br_head(fconn->mbuf);
4048 tmbuf = br_tail(fconn->mbuf);
4049 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4050 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4051 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4052 fconn->state, fconn->max_id, fconn->flags,
4053 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4054 fconn->wait_event.events, fconn->dsi,
4055 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4056 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4057 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4058 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4059 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4060 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4061 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4062
4063 if (fstrm) {
4064 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4065 fstrm, fstrm->id, fstrm->flags,
4066 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4067 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4068 fstrm->cs);
4069 if (fstrm->cs)
4070 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4071 fstrm->cs->flags, fstrm->cs->data);
4072 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004073}
4074
4075/* Migrate the the connection to the current thread.
4076 * Return 0 if successful, non-zero otherwise.
4077 * Expected to be called with the old thread lock held.
4078 */
4079static int fcgi_takeover(struct connection *conn)
4080{
4081 struct fcgi_conn *fcgi = conn->ctx;
4082
4083 if (fd_takeover(conn->handle.fd, conn) != 0)
4084 return -1;
4085 if (fcgi->wait_event.events)
4086 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4087 fcgi->wait_event.events, &fcgi->wait_event);
4088 /* To let the tasklet know it should free itself, and do nothing else,
4089 * set its context to NULL;
4090 */
4091 fcgi->wait_event.tasklet->context = NULL;
4092 tasklet_wakeup(fcgi->wait_event.tasklet);
4093 if (fcgi->task) {
4094 fcgi->task->context = NULL;
4095 /* Wake the task, to let it free itself */
4096 task_wakeup(fcgi->task, TASK_WOKEN_OTHER);
4097
4098 fcgi->task = task_new(tid_bit);
4099 if (!fcgi->task) {
4100 fcgi_release(fcgi);
4101 return -1;
4102 }
4103 fcgi->task->process = fcgi_timeout_task;
4104 fcgi->task->context = fcgi;
4105 }
4106 fcgi->wait_event.tasklet = tasklet_new();
4107 if (!fcgi->wait_event.tasklet) {
4108 fcgi_release(fcgi);
4109 return -1;
4110 }
4111 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4112 fcgi->wait_event.tasklet->context = fcgi;
4113 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4114 SUB_RETRY_RECV, &fcgi->wait_event);
4115
4116 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004117}
4118
4119/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004120/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004121/****************************************/
4122
4123/* The mux operations */
4124static const struct mux_ops mux_fcgi_ops = {
4125 .init = fcgi_init,
4126 .wake = fcgi_wake,
4127 .attach = fcgi_attach,
4128 .get_first_cs = fcgi_get_first_cs,
4129 .detach = fcgi_detach,
4130 .destroy = fcgi_destroy,
4131 .avail_streams = fcgi_avail_streams,
4132 .used_streams = fcgi_used_streams,
4133 .rcv_buf = fcgi_rcv_buf,
4134 .snd_buf = fcgi_snd_buf,
4135 .subscribe = fcgi_subscribe,
4136 .unsubscribe = fcgi_unsubscribe,
4137 .shutr = fcgi_shutr,
4138 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004139 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004140 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004141 .takeover = fcgi_takeover,
Christopher Faulet99eff652019-08-11 23:11:30 +02004142 .flags = MX_FL_HTX,
4143 .name = "FCGI",
4144};
4145
4146
4147/* this mux registers FCGI proto */
4148static struct mux_proto_list mux_proto_fcgi =
4149{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4150
4151INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4152
4153/*
4154 * Local variables:
4155 * c-indent-level: 8
4156 * c-basic-offset: 8
4157 * End:
4158 */