blob: 2c2611bdb04aa466e6a580bc626f00ed1c88d703 [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 Tarreaub2551052020-06-09 09:07:15 +020013#include <import/ist.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020018#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020019#include <haproxy/fcgi-app.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/fcgi.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020021#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020022#include <haproxy/h1_htx.h>
Willy Tarreau87735332020-06-04 09:08:41 +020023#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020024#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020025#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020026#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020027#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020028#include <haproxy/proxy-t.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020029#include <haproxy/regex.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session-t.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020031#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020034#include <haproxy/trace.h>
Christopher Faulet99eff652019-08-11 23:11:30 +020035
Willy Tarreaub2551052020-06-09 09:07:15 +020036
Christopher Faulet99eff652019-08-11 23:11:30 +020037/* FCGI Connection flags (32 bits) */
38#define FCGI_CF_NONE 0x00000000
39
40/* Flags indicating why writing to the mux is blockes */
41#define FCGI_CF_MUX_MALLOC 0x00000001 /* mux is blocked on lack connection's mux buffer */
42#define FCGI_CF_MUX_MFULL 0x00000002 /* mux is blocked on connection's mux buffer full */
43#define FCGI_CF_MUX_BLOCK_ANY 0x00000003 /* mux is blocked on connection's mux buffer full */
44
45/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
50#define FCGI_CF_DEM_DALLOC 0x00000004 /* demux blocked on lack of connection's demux buffer */
51#define FCGI_CF_DEM_DFULL 0x00000008 /* demux blocked on connection's demux buffer full */
52#define FCGI_CF_DEM_MROOM 0x00000010 /* demux blocked on lack of room in mux buffer */
53#define FCGI_CF_DEM_SALLOC 0x00000020 /* demux blocked on lack of stream's rx buffer */
54#define FCGI_CF_DEM_SFULL 0x00000040 /* demux blocked on stream request buffer full */
55#define FCGI_CF_DEM_TOOMANY 0x00000080 /* demux blocked waiting for some conn_streams to leave */
56#define FCGI_CF_DEM_BLOCK_ANY 0x000000F0 /* aggregate of the demux flags above except DALLOC/DFULL */
57
58/* Other flags */
59#define FCGI_CF_MPXS_CONNS 0x00000100 /* connection multiplexing is supported */
60#define FCGI_CF_ABRTS_SENT 0x00000200 /* a record ABORT was successfully sent to all active streams */
61#define FCGI_CF_ABRTS_FAILED 0x00000400 /* failed to abort processing of all streams */
62#define FCGI_CF_WAIT_FOR_HS 0x00000800 /* We did check that at least a stream was waiting for handshake */
63#define FCGI_CF_KEEP_CONN 0x00001000 /* HAproxy is responsible to close the connection */
64#define FCGI_CF_GET_VALUES 0x00002000 /* retrieve settings */
65
66/* FCGI connection state (fcgi_conn->state) */
67enum fcgi_conn_st {
68 FCGI_CS_INIT = 0, /* init done, waiting for sending GET_VALUES record */
69 FCGI_CS_SETTINGS, /* GET_VALUES sent, waiting for the GET_VALUES_RESULT record */
70 FCGI_CS_RECORD_H, /* GET_VALUES_RESULT received, waiting for a record header */
71 FCGI_CS_RECORD_D, /* Record header OK, waiting for a record data */
72 FCGI_CS_RECORD_P, /* Record processed, remains the padding */
73 FCGI_CS_CLOSED, /* abort requests if necessary and close the connection ASAP */
74 FCGI_CS_ENTRIES
75} __attribute__((packed));
76
77/* 32 buffers: one for the ring's root, rest for the mbuf itself */
78#define FCGI_C_MBUF_CNT 32
79
80/* FCGI connection descriptor */
81struct fcgi_conn {
82 struct connection *conn;
83
84 enum fcgi_conn_st state; /* FCGI connection state */
85 int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
86 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
87 uint32_t flags; /* Connection flags: FCGI_CF_* */
88
89 int16_t dsi; /* dmux stream ID (<0 = idle ) */
90 uint16_t drl; /* demux record length (if dsi >= 0) */
91 uint8_t drt; /* demux record type (if dsi >= 0) */
92 uint8_t drp; /* demux record padding (if dsi >= 0) */
93
94 struct buffer dbuf; /* demux buffer */
95 struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
96
97 int timeout; /* idle timeout duration in ticks */
98 int shut_timeout; /* idle timeout duration in ticks after shutdown */
99 unsigned int nb_streams; /* number of streams in the tree */
100 unsigned int nb_cs; /* number of attached conn_streams */
101 unsigned int nb_reserved; /* number of reserved streams */
102 unsigned int stream_cnt; /* total number of streams seen */
103
104 struct proxy *proxy; /* the proxy this connection was created for */
105 struct fcgi_app *app; /* FCGI application used by this mux */
106 struct task *task; /* timeout management task */
107 struct eb_root streams_by_id; /* all active streams by their ID */
108
109 struct list send_list; /* list of blocked streams requesting to send */
Christopher Faulet99eff652019-08-11 23:11:30 +0200110
111 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
112 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
113};
114
115
116/* FCGI stream state, in fcgi_strm->state */
117enum fcgi_strm_st {
118 FCGI_SS_IDLE = 0,
119 FCGI_SS_OPEN,
120 FCGI_SS_HREM, // half-closed(remote)
121 FCGI_SS_HLOC, // half-closed(local)
122 FCGI_SS_ERROR,
123 FCGI_SS_CLOSED,
124 FCGI_SS_ENTRIES
125} __attribute__((packed));
126
127
128/* FCGI stream flags (32 bits) */
129#define FCGI_SF_NONE 0x00000000
130#define FCGI_SF_ES_RCVD 0x00000001 /* end-of-stream received (empty STDOUT or EDN_REQUEST record) */
131#define FCGI_SF_ES_SENT 0x00000002 /* end-of-strem sent (empty STDIN record) */
132#define FCGI_SF_ABRT_SENT 0x00000004 /* abort sent (ABORT_REQUEST record) */
133
134/* Stream flags indicating the reason the stream is blocked */
135#define FCGI_SF_BLK_MBUSY 0x00000010 /* blocked waiting for mux access (transient) */
136#define FCGI_SF_BLK_MROOM 0x00000020 /* blocked waiting for room in the mux */
137#define FCGI_SF_BLK_ANY 0x00000030 /* any of the reasons above */
138
139#define FCGI_SF_BEGIN_SENT 0x00000100 /* a BEGIN_REQUEST record was sent for this stream */
140#define FCGI_SF_OUTGOING_DATA 0x00000200 /* set whenever we've seen outgoing data */
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100141#define FCGI_SF_NOTIFIED 0x00000400 /* a paused stream was notified to try to send again */
Christopher Faulet99eff652019-08-11 23:11:30 +0200142
143#define FCGI_SF_WANT_SHUTR 0x00001000 /* a stream couldn't shutr() (mux full/busy) */
144#define FCGI_SF_WANT_SHUTW 0x00002000 /* a stream couldn't shutw() (mux full/busy) */
145#define FCGI_SF_KILL_CONN 0x00004000 /* kill the whole connection with this stream */
146
147/* Other flags */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100148#define FCGI_SF_H1_PARSING_DONE 0x00010000
Christopher Faulet99eff652019-08-11 23:11:30 +0200149
150/* FCGI stream descriptor */
151struct fcgi_strm {
152 struct conn_stream *cs;
153 struct session *sess;
154 struct fcgi_conn *fconn;
155
156 int32_t id; /* stream ID */
157
158 uint32_t flags; /* Connection flags: FCGI_SF_* */
159 enum fcgi_strm_st state; /* FCGI stream state */
160 int proto_status; /* FCGI_PS_* */
161
162 struct h1m h1m; /* response parser state for H1 */
163
164 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
165
166 struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100167 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet99eff652019-08-11 23:11:30 +0200168 struct list send_list; /* To be used when adding in fcgi_conn->send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +0100169 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to close after we failed to by lack of space */
Christopher Faulet99eff652019-08-11 23:11:30 +0200170};
171
172/* Flags representing all default FCGI parameters */
173#define FCGI_SP_CGI_GATEWAY 0x00000001
174#define FCGI_SP_DOC_ROOT 0x00000002
175#define FCGI_SP_SCRIPT_NAME 0x00000004
176#define FCGI_SP_PATH_INFO 0x00000008
177#define FCGI_SP_REQ_URI 0x00000010
178#define FCGI_SP_REQ_METH 0x00000020
179#define FCGI_SP_REQ_QS 0x00000040
180#define FCGI_SP_SRV_PORT 0x00000080
181#define FCGI_SP_SRV_PROTO 0x00000100
182#define FCGI_SP_SRV_NAME 0x00000200
183#define FCGI_SP_REM_ADDR 0x00000400
184#define FCGI_SP_REM_PORT 0x00000800
185#define FCGI_SP_SCRIPT_FILE 0x00001000
186#define FCGI_SP_PATH_TRANS 0x00002000
187#define FCGI_SP_CONT_LEN 0x00004000
188#define FCGI_SP_HTTPS 0x00008000
189#define FCGI_SP_MASK 0x0000FFFF
190#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
191
192/* FCGI parameters used when PARAMS record is sent */
193struct fcgi_strm_params {
194 uint32_t mask;
195 struct ist docroot;
196 struct ist scriptname;
197 struct ist pathinfo;
198 struct ist meth;
199 struct ist uri;
200 struct ist vsn;
201 struct ist qs;
202 struct ist srv_name;
203 struct ist srv_port;
204 struct ist rem_addr;
205 struct ist rem_port;
206 struct ist cont_len;
207 int https;
208 struct buffer *p;
209};
210
211/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
212#define MAX_DATA_REALIGN 1024
213
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200214/* trace source and events */
215static void fcgi_trace(enum trace_level level, uint64_t mask,
216 const struct trace_source *src,
217 const struct ist where, const struct ist func,
218 const void *a1, const void *a2, const void *a3, const void *a4);
219
220/* The event representation is split like this :
221 * fconn - internal FCGI connection
222 * fstrm - internal FCGI stream
223 * strm - application layer
224 * rx - data receipt
225 * tx - data transmission
226 * rsp - response parsing
227 */
228static const struct trace_event fcgi_trace_events[] = {
229#define FCGI_EV_FCONN_NEW (1ULL << 0)
230 { .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
231#define FCGI_EV_FCONN_RECV (1ULL << 1)
232 { .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
233#define FCGI_EV_FCONN_SEND (1ULL << 2)
234 { .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
235#define FCGI_EV_FCONN_BLK (1ULL << 3)
236 { .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
237#define FCGI_EV_FCONN_WAKE (1ULL << 4)
238 { .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
239#define FCGI_EV_FCONN_END (1ULL << 5)
240 { .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
241#define FCGI_EV_FCONN_ERR (1ULL << 6)
242 { .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
243
244#define FCGI_EV_RX_FHDR (1ULL << 7)
245 { .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
246#define FCGI_EV_RX_RECORD (1ULL << 8)
247 { .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
248#define FCGI_EV_RX_EOI (1ULL << 9)
249 { .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
250#define FCGI_EV_RX_GETVAL (1ULL << 10)
251 { .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
252#define FCGI_EV_RX_STDOUT (1ULL << 11)
253 { .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
254#define FCGI_EV_RX_STDERR (1ULL << 12)
255 { .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
256#define FCGI_EV_RX_ENDREQ (1ULL << 13)
257 { .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
258
259#define FCGI_EV_TX_RECORD (1ULL << 14)
260 { .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
261#define FCGI_EV_TX_EOI (1ULL << 15)
262 { .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
263#define FCGI_EV_TX_BEGREQ (1ULL << 16)
264 { .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
265#define FCGI_EV_TX_GETVAL (1ULL << 17)
266 { .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
267#define FCGI_EV_TX_PARAMS (1ULL << 18)
268 { .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
269#define FCGI_EV_TX_STDIN (1ULL << 19)
270 { .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
271#define FCGI_EV_TX_ABORT (1ULL << 20)
272 { .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
273
274#define FCGI_EV_RSP_DATA (1ULL << 21)
275 { .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
276#define FCGI_EV_RSP_EOM (1ULL << 22)
277 { .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
278#define FCGI_EV_RSP_HDRS (1ULL << 23)
279 { .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
280#define FCGI_EV_RSP_BODY (1ULL << 24)
281 { .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
282#define FCGI_EV_RSP_TLRS (1ULL << 25)
283 { .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
284
285#define FCGI_EV_FSTRM_NEW (1ULL << 26)
286 { .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
287#define FCGI_EV_FSTRM_BLK (1ULL << 27)
288 { .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
289#define FCGI_EV_FSTRM_END (1ULL << 28)
290 { .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
291#define FCGI_EV_FSTRM_ERR (1ULL << 29)
292 { .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
293
294#define FCGI_EV_STRM_NEW (1ULL << 30)
295 { .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
296#define FCGI_EV_STRM_RECV (1ULL << 31)
297 { .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
298#define FCGI_EV_STRM_SEND (1ULL << 32)
299 { .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
300#define FCGI_EV_STRM_FULL (1ULL << 33)
301 { .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
302#define FCGI_EV_STRM_WAKE (1ULL << 34)
303 { .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
304#define FCGI_EV_STRM_SHUT (1ULL << 35)
305 { .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
306#define FCGI_EV_STRM_END (1ULL << 36)
307 { .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
308#define FCGI_EV_STRM_ERR (1ULL << 37)
309 { .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
310
311 { }
312};
313
314static const struct name_desc fcgi_trace_lockon_args[4] = {
315 /* arg1 */ { /* already used by the connection */ },
316 /* arg2 */ { .name="fstrm", .desc="FCGI stream" },
317 /* arg3 */ { },
318 /* arg4 */ { }
319};
320
321
322static const struct name_desc fcgi_trace_decoding[] = {
323#define FCGI_VERB_CLEAN 1
324 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
325#define FCGI_VERB_MINIMAL 2
326 { .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
327#define FCGI_VERB_SIMPLE 3
328 { .name="simple", .desc="add request/response status line or htx info when available" },
329#define FCGI_VERB_ADVANCED 4
330 { .name="advanced", .desc="add header fields or record decoding when available" },
331#define FCGI_VERB_COMPLETE 5
332 { .name="complete", .desc="add full data dump when available" },
333 { /* end */ }
334};
335
336static struct trace_source trace_fcgi = {
337 .name = IST("fcgi"),
338 .desc = "FastCGI multiplexer",
339 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
340 .default_cb = fcgi_trace,
341 .known_events = fcgi_trace_events,
342 .lockon_args = fcgi_trace_lockon_args,
343 .decoding = fcgi_trace_decoding,
344 .report_events = ~0, // report everything by default
345};
346
347#define TRACE_SOURCE &trace_fcgi
348INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
349
Christopher Faulet99eff652019-08-11 23:11:30 +0200350/* FCGI connection and stream pools */
351DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
352DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
353
354static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state);
355static int fcgi_process(struct fcgi_conn *fconn);
356static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short state);
357static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
358static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state);
359static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs, struct session *sess);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200360static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
361static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200362static void fcgi_strm_alert(struct fcgi_strm *fstrm);
363static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
364
365/* a dmumy management stream */
366static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
367 .cs = NULL,
368 .fconn = NULL,
369 .state = FCGI_SS_CLOSED,
370 .flags = FCGI_SF_NONE,
371 .id = 0,
372};
373
374/* and a dummy idle stream for use with any unknown stream */
375static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
376 .cs = NULL,
377 .fconn = NULL,
378 .state = FCGI_SS_IDLE,
379 .flags = FCGI_SF_NONE,
380 .id = 0,
381};
382
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200383/* returns a fconn state as an abbreviated 3-letter string, or "???" if unknown */
384static inline const char *fconn_st_to_str(enum fcgi_conn_st st)
385{
386 switch (st) {
387 case FCGI_CS_INIT : return "INI";
388 case FCGI_CS_SETTINGS : return "STG";
389 case FCGI_CS_RECORD_H : return "RDH";
390 case FCGI_CS_RECORD_D : return "RDD";
391 case FCGI_CS_RECORD_P : return "RDP";
392 case FCGI_CS_CLOSED : return "CLO";
393 default : return "???";
394 }
395}
396
397/* returns a fstrm state as an abbreviated 3-letter string, or "???" if unknown */
398static inline const char *fstrm_st_to_str(enum fcgi_strm_st st)
399{
400 switch (st) {
401 case FCGI_SS_IDLE : return "IDL";
402 case FCGI_SS_OPEN : return "OPN";
403 case FCGI_SS_HREM : return "RCL";
404 case FCGI_SS_HLOC : return "HCL";
405 case FCGI_SS_ERROR : return "ERR";
406 case FCGI_SS_CLOSED : return "CLO";
407 default : return "???";
408 }
409}
410
411
412/* the FCGI traces always expect that arg1, if non-null, is of type connection
413 * (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
414 * and that arg3, if non-null, is a htx for rx/tx headers.
415 */
416static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
417 const struct ist where, const struct ist func,
418 const void *a1, const void *a2, const void *a3, const void *a4)
419{
420 const struct connection *conn = a1;
421 const struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
422 const struct fcgi_strm *fstrm = a2;
423 const struct htx *htx = a3;
424 const size_t *val = a4;
425
426 if (!fconn)
427 fconn = (fstrm ? fstrm->fconn : NULL);
428
429 if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
430 return;
431
432 /* Display the response state if fstrm is defined */
433 if (fstrm)
434 chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
435
436 if (src->verbosity == FCGI_VERB_CLEAN)
437 return;
438
439 /* Display the value to the 4th argument (level > STATE) */
440 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100441 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200442
443 /* Display status-line if possible (verbosity > MINIMAL) */
444 if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
445 const struct htx_blk *blk = htx_get_head_blk(htx);
446 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
447 enum htx_blk_type type = htx_get_blk_type(blk);
448
449 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
450 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
451 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
452 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
453 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
454 }
455
456 /* Display fconn info and, if defined, fstrm info */
457 chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
458 if (fstrm)
459 chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
460
461 if (!fstrm || fstrm->id <= 0)
462 chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
463 if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
464 chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
465
466 if (src->verbosity == FCGI_VERB_MINIMAL)
467 return;
468
469 /* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
470 if (src->level > TRACE_LEVEL_USER) {
471 if (src->verbosity == FCGI_VERB_COMPLETE ||
472 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
473 chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
474 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
475 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
476 if (src->verbosity == FCGI_VERB_COMPLETE ||
477 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
478 struct buffer *hmbuf = br_head((struct buffer *)fconn->mbuf);
479 struct buffer *tmbuf = br_tail((struct buffer *)fconn->mbuf);
480
481 chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
482 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
483 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
484 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
485 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
486 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
487 }
488
489 if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
490 (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
491 chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
492 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
493 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
494 }
495
496 /* Display htx info if defined (level > USER) */
497 if (src->level > TRACE_LEVEL_USER && htx) {
498 int full = 0;
499
500 /* Full htx info (level > STATE && verbosity > SIMPLE) */
501 if (src->level > TRACE_LEVEL_STATE) {
502 if (src->verbosity == FCGI_VERB_COMPLETE)
503 full = 1;
504 else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
505 full = 1;
506 }
507
508 chunk_memcat(&trace_buf, "\n\t", 2);
509 htx_dump(&trace_buf, htx, full);
510 }
511}
Christopher Faulet99eff652019-08-11 23:11:30 +0200512
513/*****************************************************/
514/* functions below are for dynamic buffer management */
515/*****************************************************/
516
517/* Indicates whether or not the we may call the fcgi_recv() function to attempt
518 * to receive data into the buffer and/or demux pending data. The condition is
519 * a bit complex due to some API limits for now. The rules are the following :
520 * - if an error or a shutdown was detected on the connection and the buffer
521 * is empty, we must not attempt to receive
522 * - if the demux buf failed to be allocated, we must not try to receive and
523 * we know there is nothing pending
524 * - if no flag indicates a blocking condition, we may attempt to receive,
525 * regardless of whether the demux buffer is full or not, so that only
526 * de demux part decides whether or not to block. This is needed because
527 * the connection API indeed prevents us from re-enabling receipt that is
528 * already enabled in a polled state, so we must always immediately stop
529 * as soon as the demux can't proceed so as never to hit an end of read
530 * with data pending in the buffers.
531 * - otherwise must may not attempt
532 */
533static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
534{
535 if (b_data(&fconn->dbuf) == 0 &&
536 (fconn->state == FCGI_CS_CLOSED ||
537 fconn->conn->flags & CO_FL_ERROR ||
538 conn_xprt_read0_pending(fconn->conn)))
539 return 0;
540
541 if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
542 !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
543 return 1;
544
545 return 0;
546}
547
548/* Restarts reading on the connection if it was not enabled */
549static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
550{
551 if (!fcgi_recv_allowed(fconn))
552 return;
553 if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
554 (fconn->wait_event.events & SUB_RETRY_RECV))
555 return;
556 tasklet_wakeup(fconn->wait_event.tasklet);
557}
558
559
560/* Tries to grab a buffer and to re-enable processing on mux <target>. The
561 * fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
562 * the allocation succeeds, in which case the connection is woken up, or 0 if
563 * it's impossible to wake up and we prefer to be woken up later.
564 */
565static int fcgi_buf_available(void *target)
566{
567 struct fcgi_conn *fconn = target;
568 struct fcgi_strm *fstrm;
569
570 if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc_margin(&fconn->dbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200571 TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200572 fconn->flags &= ~FCGI_CF_DEM_DALLOC;
573 fcgi_conn_restart_reading(fconn, 1);
574 return 1;
575 }
576
577 if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc_margin(br_tail(fconn->mbuf), 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200578 TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200579 fconn->flags &= ~FCGI_CF_MUX_MALLOC;
Christopher Faulet99eff652019-08-11 23:11:30 +0200580 if (fconn->flags & FCGI_CF_DEM_MROOM) {
581 fconn->flags &= ~FCGI_CF_DEM_MROOM;
582 fcgi_conn_restart_reading(fconn, 1);
583 }
584 return 1;
585 }
586
587 if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
588 (fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fstrm->cs &&
589 b_alloc_margin(&fstrm->rxbuf, 0)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200590 TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200591 fconn->flags &= ~FCGI_CF_DEM_SALLOC;
592 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200593 fcgi_strm_notify_recv(fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200594 return 1;
595 }
596
597 return 0;
598}
599
600static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
601{
602 struct buffer *buf = NULL;
603
Willy Tarreau21046592020-02-26 10:39:36 +0100604 if (likely(!MT_LIST_ADDED(&fconn->buf_wait.list)) &&
Christopher Faulet99eff652019-08-11 23:11:30 +0200605 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
606 fconn->buf_wait.target = fconn;
607 fconn->buf_wait.wakeup_cb = fcgi_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200608 MT_LIST_ADDQ(&buffer_wq, &fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200609 }
610 return buf;
611}
612
613static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
614{
615 if (bptr->size) {
616 b_free(bptr);
617 offer_buffers(NULL, tasks_run_queue);
618 }
619}
620
621static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
622{
623 struct buffer *buf;
624 unsigned int count = 0;
625
626 while (b_size(buf = br_head_pick(fconn->mbuf))) {
627 b_free(buf);
628 count++;
629 }
630 if (count)
631 offer_buffers(NULL, tasks_run_queue);
632}
633
634/* Returns the number of allocatable outgoing streams for the connection taking
635 * the number reserved streams into account.
636 */
637static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
638{
639 int ret;
640
641 ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
642 if (ret < 0)
643 ret = 0;
644 return ret;
645}
646
647/* Returns the number of streams in use on a connection to figure if it's
648 * idle or not. We check nb_cs and not nb_streams as the caller will want
649 * to know if it was the last one after a detach().
650 */
651static int fcgi_used_streams(struct connection *conn)
652{
653 struct fcgi_conn *fconn = conn->ctx;
654
655 return fconn->nb_cs;
656}
657
658/* Returns the number of concurrent streams available on the connection */
659static int fcgi_avail_streams(struct connection *conn)
660{
661 struct server *srv = objt_server(conn->target);
662 struct fcgi_conn *fconn = conn->ctx;
663 int ret1, ret2;
664
665 /* Don't open new stream if the connection is closed */
666 if (fconn->state == FCGI_CS_CLOSED)
667 return 0;
668
669 /* May be negative if this setting has changed */
670 ret1 = (fconn->streams_limit - fconn->nb_streams);
671
672 /* we must also consider the limit imposed by stream IDs */
673 ret2 = fcgi_streams_left(fconn);
674 ret1 = MIN(ret1, ret2);
675 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
676 ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
677 ret1 = MIN(ret1, ret2);
678 }
679 return ret1;
680}
681
682/*****************************************************************/
683/* functions below are dedicated to the mux setup and management */
684/*****************************************************************/
685
686/* Initializes the mux once it's attached. Only outgoing connections are
687 * supported. So the context is already initialized before installing the
688 * mux. <input> is always used as Input buffer and may contain data. It is the
689 * caller responsibility to not reuse it anymore. Returns < 0 on error.
690 */
691static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
692 struct buffer *input)
693{
694 struct fcgi_conn *fconn;
695 struct fcgi_strm *fstrm;
696 struct fcgi_app *app = get_px_fcgi_app(px);
697 struct task *t = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200698 void *conn_ctx = conn->ctx;
699
700 TRACE_ENTER(FCGI_EV_FSTRM_NEW);
Christopher Faulet99eff652019-08-11 23:11:30 +0200701
702 if (!app)
703 goto fail_conn;
704
705 fconn = pool_alloc(pool_head_fcgi_conn);
706 if (!fconn)
707 goto fail_conn;
708
709 fconn->shut_timeout = fconn->timeout = px->timeout.server;
710 if (tick_isset(px->timeout.serverfin))
711 fconn->shut_timeout = px->timeout.serverfin;
712
713 fconn->flags = FCGI_CF_NONE;
714
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500715 /* Retrieve useful info from the FCGI app */
Christopher Faulet99eff652019-08-11 23:11:30 +0200716 if (app->flags & FCGI_APP_FL_KEEP_CONN)
717 fconn->flags |= FCGI_CF_KEEP_CONN;
718 if (app->flags & FCGI_APP_FL_GET_VALUES)
719 fconn->flags |= FCGI_CF_GET_VALUES;
720 if (app->flags & FCGI_APP_FL_MPXS_CONNS)
721 fconn->flags |= FCGI_CF_MPXS_CONNS;
722
723 fconn->proxy = px;
724 fconn->app = app;
725 fconn->task = NULL;
726 if (tick_isset(fconn->timeout)) {
727 t = task_new(tid_bit);
728 if (!t)
729 goto fail;
730
731 fconn->task = t;
732 t->process = fcgi_timeout_task;
733 t->context = fconn;
734 t->expire = tick_add(now_ms, fconn->timeout);
735 }
736
737 fconn->wait_event.tasklet = tasklet_new();
738 if (!fconn->wait_event.tasklet)
739 goto fail;
740 fconn->wait_event.tasklet->process = fcgi_io_cb;
741 fconn->wait_event.tasklet->context = fconn;
742 fconn->wait_event.events = 0;
743
744 /* Initialise the context. */
745 fconn->state = FCGI_CS_INIT;
746 fconn->conn = conn;
747 fconn->streams_limit = app->maxreqs;
748 fconn->max_id = -1;
749 fconn->nb_streams = 0;
750 fconn->nb_cs = 0;
751 fconn->nb_reserved = 0;
752 fconn->stream_cnt = 0;
753
754 fconn->dbuf = *input;
755 fconn->dsi = -1;
756
757 br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
758 fconn->streams_by_id = EB_ROOT;
759 LIST_INIT(&fconn->send_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100760 MT_LIST_INIT(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200761
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200762 conn->ctx = fconn;
763
Christopher Faulet99eff652019-08-11 23:11:30 +0200764 if (t)
765 task_queue(t);
766
767 /* FIXME: this is temporary, for outgoing connections we need to
768 * immediately allocate a stream until the code is modified so that the
769 * caller calls ->attach(). For now the outgoing cs is stored as
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200770 * conn->ctx by the caller and saved in conn_ctx.
Christopher Faulet99eff652019-08-11 23:11:30 +0200771 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200772 fstrm = fcgi_conn_stream_new(fconn, conn_ctx, sess);
Christopher Faulet99eff652019-08-11 23:11:30 +0200773 if (!fstrm)
774 goto fail;
775
Christopher Faulet99eff652019-08-11 23:11:30 +0200776
777 /* Repare to read something */
778 fcgi_conn_restart_reading(fconn, 1);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200779 TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200780 return 0;
781
782 fail:
783 task_destroy(t);
784 if (fconn->wait_event.tasklet)
785 tasklet_free(fconn->wait_event.tasklet);
786 pool_free(pool_head_fcgi_conn, fconn);
787 fail_conn:
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200788 conn->ctx = conn_ctx; // restore saved ctx
789 TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
Christopher Faulet99eff652019-08-11 23:11:30 +0200790 return -1;
791}
792
793/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
794 * -1 if no more is allocatable.
795 */
796static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
797{
798 int32_t id = (fconn->max_id + 1) | 1;
799
800 if ((id & 0x80000000U))
801 id = -1;
802 return id;
803}
804
805/* Returns the stream associated with id <id> or NULL if not found */
806static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
807{
808 struct eb32_node *node;
809
810 if (id == 0)
811 return (struct fcgi_strm *)fcgi_mgmt_stream;
812
813 if (id > fconn->max_id)
814 return (struct fcgi_strm *)fcgi_unknown_stream;
815
816 node = eb32_lookup(&fconn->streams_by_id, id);
817 if (!node)
818 return (struct fcgi_strm *)fcgi_unknown_stream;
819 return container_of(node, struct fcgi_strm, by_id);
820}
821
822
823/* Release function. This one should be called to free all resources allocated
824 * to the mux.
825 */
826static void fcgi_release(struct fcgi_conn *fconn)
827{
William Dauchy477757c2020-08-07 22:19:23 +0200828 struct connection *conn = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200829
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200830 TRACE_POINT(FCGI_EV_FCONN_END);
831
Christopher Faulet99eff652019-08-11 23:11:30 +0200832 if (fconn) {
833 /* The connection must be attached to this mux to be released */
834 if (fconn->conn && fconn->conn->ctx == fconn)
835 conn = fconn->conn;
836
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200837 TRACE_DEVEL("freeing fconn", FCGI_EV_FCONN_END, conn);
838
Willy Tarreau21046592020-02-26 10:39:36 +0100839 if (MT_LIST_ADDED(&fconn->buf_wait.list))
840 MT_LIST_DEL(&fconn->buf_wait.list);
Christopher Faulet99eff652019-08-11 23:11:30 +0200841
842 fcgi_release_buf(fconn, &fconn->dbuf);
843 fcgi_release_mbuf(fconn);
844
845 if (fconn->task) {
846 fconn->task->context = NULL;
847 task_wakeup(fconn->task, TASK_WOKEN_OTHER);
848 fconn->task = NULL;
849 }
850 if (fconn->wait_event.tasklet)
851 tasklet_free(fconn->wait_event.tasklet);
Christopher Fauleta99db932019-09-18 11:11:46 +0200852 if (conn && fconn->wait_event.events != 0)
Christopher Faulet99eff652019-08-11 23:11:30 +0200853 conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
854 &fconn->wait_event);
Christopher Faulet8694f252020-05-02 09:17:52 +0200855
856 pool_free(pool_head_fcgi_conn, fconn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200857 }
858
859 if (conn) {
860 conn->mux = NULL;
861 conn->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200862 TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +0200863
864 conn_stop_tracking(conn);
865 conn_full_close(conn);
866 if (conn->destroy_cb)
867 conn->destroy_cb(conn);
868 conn_free(conn);
869 }
870}
871
Christopher Faulet6670e3e2020-10-08 15:26:33 +0200872/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
873 * pending on the connection AND if there is no more data in the demux
874 * buffer. The function returns 1 to report a read0 or 0 otherwise.
875 */
876static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
877{
878 if (conn_xprt_read0_pending(fconn->conn) && !b_data(&fconn->dbuf))
879 return 1;
880 return 0;
881}
882
Christopher Faulet99eff652019-08-11 23:11:30 +0200883
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500884/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200885static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
886{
887 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
888 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
889 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
890 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
891 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
892 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
893 conn_xprt_read0_pending(fconn->conn))))
894 return 1;
895 return 0;
896}
897
898
899/********************************************************/
900/* functions below are for the FCGI protocol processing */
901/********************************************************/
902
Christopher Faulet99eff652019-08-11 23:11:30 +0200903/* Marks an error on the stream. */
904static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
905{
906 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200907 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
908 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200909 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200910 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
911 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200912 if (fstrm->cs)
913 cs_set_error(fstrm->cs);
914 }
915}
916
917/* Attempts to notify the data layer of recv availability */
918static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
919{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100920 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200921 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100922 tasklet_wakeup(fstrm->subs->tasklet);
923 fstrm->subs->events &= ~SUB_RETRY_RECV;
924 if (!fstrm->subs->events)
925 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200926 }
927}
928
929/* Attempts to notify the data layer of send availability */
930static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
931{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100932 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200933 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100934 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100935 tasklet_wakeup(fstrm->subs->tasklet);
936 fstrm->subs->events &= ~SUB_RETRY_SEND;
937 if (!fstrm->subs->events)
938 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200939 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100940 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
941 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
942 tasklet_wakeup(fstrm->shut_tl);
943 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200944}
945
946/* Alerts the data layer, trying to wake it up by all means, following
947 * this sequence :
948 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
949 * for recv
950 * - if its subscribed to send, then it's woken up for send
951 * - if it was subscribed to neither, its ->wake() callback is called
952 * It is safe to call this function with a closed stream which doesn't have a
953 * conn_stream anymore.
954 */
955static void fcgi_strm_alert(struct fcgi_strm *fstrm)
956{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200957 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100958 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100959 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200960 fcgi_strm_notify_recv(fstrm);
961 fcgi_strm_notify_send(fstrm);
962 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200963 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
964 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200965 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200966 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200967}
968
969/* Writes the 16-bit record size <len> at address <record> */
970static inline void fcgi_set_record_size(void *record, uint16_t len)
971{
972 uint8_t *out = (record + 4);
973
974 *out = (len >> 8);
975 *(out + 1) = (len & 0xff);
976}
977
978/* Writes the 16-bit stream id <id> at address <record> */
979static inline void fcgi_set_record_id(void *record, uint16_t id)
980{
981 uint8_t *out = (record + 2);
982
983 *out = (id >> 8);
984 *(out + 1) = (id & 0xff);
985}
986
987/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
988 * its connection if the stream was not yet closed. Please use this exclusively
989 * before closing a stream to ensure stream count is well maintained.
990 */
991static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
992{
993 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200994 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200995 fstrm->fconn->nb_streams--;
996 if (!fstrm->id)
997 fstrm->fconn->nb_reserved--;
998 if (fstrm->cs) {
999 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
1000 fcgi_strm_notify_recv(fstrm);
1001 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001002 fstrm->state = FCGI_SS_CLOSED;
1003 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
1004 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001005 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001006}
1007
1008/* Detaches a FCGI stream from its FCGI connection and releases it to the
1009 * fcgi_strm pool.
1010 */
1011static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1012{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001013 struct connection *conn = fstrm->fconn->conn;
1014
1015 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1016
Christopher Faulet99eff652019-08-11 23:11:30 +02001017 fcgi_strm_close(fstrm);
1018 eb32_delete(&fstrm->by_id);
1019 if (b_size(&fstrm->rxbuf)) {
1020 b_free(&fstrm->rxbuf);
1021 offer_buffers(NULL, tasks_run_queue);
1022 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001023 if (fstrm->subs)
1024 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001025 /* There's no need to explicitly call unsubscribe here, the only
1026 * reference left would be in the fconn send_list/fctl_list, and if
1027 * we're in it, we're getting out anyway
1028 */
1029 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001030 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001031 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001032
1033 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001034}
1035
1036/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1037 * stream tree. In case of error, nothing is added and NULL is returned. The
1038 * causes of errors can be any failed memory allocation. The caller is
1039 * responsible for checking if the connection may support an extra stream prior
1040 * to calling this function.
1041 */
1042static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1043{
1044 struct fcgi_strm *fstrm;
1045
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001046 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1047
Christopher Faulet99eff652019-08-11 23:11:30 +02001048 fstrm = pool_alloc(pool_head_fcgi_strm);
1049 if (!fstrm)
1050 goto out;
1051
Willy Tarreau7aad7032020-01-16 17:20:57 +01001052 fstrm->shut_tl = tasklet_new();
1053 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001054 pool_free(pool_head_fcgi_strm, fstrm);
1055 goto out;
1056 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001057 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001058 fstrm->shut_tl->process = fcgi_deferred_shut;
1059 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001060 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001061 fstrm->fconn = fconn;
1062 fstrm->cs = NULL;
1063 fstrm->flags = FCGI_SF_NONE;
1064 fstrm->proto_status = 0;
1065 fstrm->state = FCGI_SS_IDLE;
1066 fstrm->rxbuf = BUF_NULL;
1067
1068 h1m_init_res(&fstrm->h1m);
1069 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1070 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1071
1072 fstrm->by_id.key = fstrm->id = id;
1073 if (id > 0)
1074 fconn->max_id = id;
1075 else
1076 fconn->nb_reserved++;
1077
1078 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1079 fconn->nb_streams++;
1080 fconn->stream_cnt++;
1081
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001082 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001083 return fstrm;
1084
1085 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001086 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 +02001087 return NULL;
1088}
1089
1090/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1091 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1092 * highest possible stream ID was reached.
1093 */
1094static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1095 struct session *sess)
1096{
1097 struct fcgi_strm *fstrm = NULL;
1098
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001099 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1100 if (fconn->nb_streams >= fconn->streams_limit) {
1101 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 +02001102 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001103 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001104
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001105 if (fcgi_streams_left(fconn) < 1) {
1106 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 +02001107 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001108 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001109
1110 /* Defer choosing the ID until we send the first message to create the stream */
1111 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001112 if (!fstrm) {
1113 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 +02001114 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001115 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001116
1117 fstrm->cs = cs;
1118 fstrm->sess = sess;
1119 cs->ctx = fstrm;
1120 fconn->nb_cs++;
1121
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001122 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001123 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001124
1125 out:
1126 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001127}
1128
1129/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1130 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1131 * automatically updated accordingly. If the stream is orphaned, it is
1132 * destroyed.
1133 */
1134static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1135{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001136 struct fcgi_conn *fconn = fstrm->fconn;
1137
1138 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1139
Christopher Faulet99eff652019-08-11 23:11:30 +02001140 if (!fstrm->cs) {
1141 /* this stream was already orphaned */
1142 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001143 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001144 return;
1145 }
1146
Christopher Faulet6670e3e2020-10-08 15:26:33 +02001147 if (fcgi_conn_read0_pending(fconn)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001148 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001149 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001150 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1151 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001152 else if (fstrm->state == FCGI_SS_HLOC)
1153 fcgi_strm_close(fstrm);
1154 }
1155
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001156 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001157 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1158 if (fstrm->cs->flags & CS_FL_EOS)
1159 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001160
1161 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001162 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001163 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1164 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001165 }
1166
1167 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001168
1169 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001170}
1171
1172/* Wakes unassigned streams (ID == 0) attached to the connection. */
1173static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1174{
1175 struct eb32_node *node;
1176 struct fcgi_strm *fstrm;
1177
1178 node = eb32_lookup(&fconn->streams_by_id, 0);
1179 while (node) {
1180 fstrm = container_of(node, struct fcgi_strm, by_id);
1181 if (fstrm->id > 0)
1182 break;
1183 node = eb32_next(node);
1184 fcgi_strm_wake_one_stream(fstrm);
1185 }
1186}
1187
1188/* Wakes the streams attached to the connection, whose id is greater than <last>
1189 * or unassigned.
1190 */
1191static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1192{
1193 struct eb32_node *node;
1194 struct fcgi_strm *fstrm;
1195
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001196 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1197
Christopher Faulet99eff652019-08-11 23:11:30 +02001198 /* Wake all streams with ID > last */
1199 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1200 while (node) {
1201 fstrm = container_of(node, struct fcgi_strm, by_id);
1202 node = eb32_next(node);
1203 fcgi_strm_wake_one_stream(fstrm);
1204 }
1205 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001206
1207 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001208}
1209
1210static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1211 struct htx *htx, struct htx_sl *sl,
1212 struct fcgi_strm_params *params)
1213{
1214 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1215 struct ist p;
1216
1217 if (!sl)
1218 goto error;
1219
1220 if (!(params->mask & FCGI_SP_DOC_ROOT))
1221 params->docroot = fconn->app->docroot;
1222
1223 if (!(params->mask & FCGI_SP_REQ_METH)) {
1224 p = htx_sl_req_meth(sl);
1225 params->meth = ist2(b_tail(params->p), p.len);
1226 chunk_memcat(params->p, p.ptr, p.len);
1227 }
1228 if (!(params->mask & FCGI_SP_REQ_URI)) {
1229 p = htx_sl_req_uri(sl);
1230 params->uri = ist2(b_tail(params->p), p.len);
1231 chunk_memcat(params->p, p.ptr, p.len);
1232 }
1233 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1234 p = htx_sl_req_vsn(sl);
1235 params->vsn = ist2(b_tail(params->p), p.len);
1236 chunk_memcat(params->p, p.ptr, p.len);
1237 }
1238 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1239 char *end;
1240 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001241 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001242 port = get_host_port(cli_conn->dst);
1243 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1244 if (!end)
1245 goto error;
1246 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1247 params->p->data += params->srv_port.len;
1248 }
1249 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1250 /* If no Host header found, use the server address to fill
1251 * srv_name */
1252 if (!istlen(params->srv_name)) {
1253 char *ptr = NULL;
1254
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001255 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001256 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1257 ptr = b_tail(params->p);
1258 if (ptr) {
1259 params->srv_name = ist2(ptr, strlen(ptr));
1260 params->p->data += params->srv_name.len;
1261 }
1262 }
1263 }
1264 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1265 char *ptr = NULL;
1266
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001267 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001268 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1269 ptr = b_tail(params->p);
1270 if (ptr) {
1271 params->rem_addr = ist2(ptr, strlen(ptr));
1272 params->p->data += params->rem_addr.len;
1273 }
1274 }
1275 if (!(params->mask & FCGI_SP_REM_PORT)) {
1276 char *end;
1277 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001278 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001279 port = get_host_port(cli_conn->src);
1280 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1281 if (!end)
1282 goto error;
1283 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1284 params->p->data += params->rem_port.len;
1285 }
1286 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1287 struct htx_blk *blk;
1288 enum htx_blk_type type;
1289 char *end;
1290 size_t len = 0;
1291
1292 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1293 type = htx_get_blk_type(blk);
1294
1295 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1296 break;
1297 if (type == HTX_BLK_DATA)
1298 len += htx_get_blksz(blk);
1299 }
1300 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1301 if (!end)
1302 goto error;
1303 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1304 params->p->data += params->cont_len.len;
1305 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001306#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001307 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001308 if (cli_conn)
1309 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001310 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001311#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001312 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1313 /* one of scriptname, pathinfo or query_string is no set */
1314 struct ist path = http_get_path(params->uri);
1315 int len;
1316
Christopher Faulet99eff652019-08-11 23:11:30 +02001317 /* No scrit_name set but no valid path ==> error */
1318 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1319 goto error;
1320
Christopher Faulet99eff652019-08-11 23:11:30 +02001321 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001322 if (!(params->mask & FCGI_SP_REQ_QS)) {
1323 struct ist qs = istfind(path, '?');
1324
1325 /* Update the path length */
1326 path.len -= qs.len;
1327
1328 /* Set the query-string skipping the '?', if any */
1329 if (istlen(qs))
1330 params->qs = istnext(qs);
1331 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001332
1333 /* If the script_name is set, don't try to deduce the path_info
1334 * too. The opposite is not true.
1335 */
1336 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1337 params->mask |= FCGI_SP_PATH_INFO;
1338 goto end;
1339 }
1340
Christopher Faulet0f17a442020-07-23 15:44:37 +02001341 /* Decode the path. it must first be copied to keep the URI
1342 * untouched.
1343 */
1344 chunk_memcat(params->p, path.ptr, path.len);
1345 path.ptr = b_tail(params->p) - path.len;
1346 len = url_decode(ist0(path), 0);
1347 if (len < 0)
1348 goto error;
1349 path.len = len;
1350
Christopher Faulet99eff652019-08-11 23:11:30 +02001351 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001352 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001353
1354 /* If there is no regex to match the pathinfo, just to the last
1355 * part and see if the index must be used.
1356 */
1357 if (!fconn->app->pathinfo_re)
1358 goto check_index;
1359
Christopher Faulet28cb3662020-02-14 14:47:37 +01001360 /* If some special characters are found in the decoded path (\n
1361 * or \0), the PATH_INFO regex cannot match. This is theorically
1362 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001363 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001364 * case.
1365 */
1366 if (istchr(path, '\n') || istchr(path, '\0'))
1367 goto error;
1368
Christopher Faulet99eff652019-08-11 23:11:30 +02001369 /* The regex does not match, just to the last part and see if
1370 * the index must be used.
1371 */
1372 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1373 goto check_index;
1374
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001375 /* We must have at least 1 capture for the script name,
1376 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001377 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001378 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001379 goto check_index;
1380
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001381 /* Finally we can set the script_name and the path_info. The
1382 * path_info is set if not already defined, and if it was
1383 * captured
1384 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001385 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001386 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1387 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001388
1389 check_index:
1390 len = params->scriptname.len;
1391 /* the script_name if finished by a '/' so we can add the index
1392 * part, if any.
1393 */
1394 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1395 struct ist sn = params->scriptname;
1396
1397 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1398 chunk_memcat(params->p, sn.ptr, sn.len);
1399 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1400 }
1401 }
1402
1403 end:
1404 return 1;
1405 error:
1406 return 0;
1407}
1408
1409static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1410 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1411{
1412 struct fcgi_param p;
1413
1414 if (params->mask & flag)
1415 return 1;
1416
1417 chunk_reset(&trash);
1418
1419 switch (flag) {
1420 case FCGI_SP_CGI_GATEWAY:
1421 p.n = ist("GATEWAY_INTERFACE");
1422 p.v = ist("CGI/1.1");
1423 goto encode;
1424 case FCGI_SP_DOC_ROOT:
1425 p.n = ist("DOCUMENT_ROOT");
1426 p.v = params->docroot;
1427 goto encode;
1428 case FCGI_SP_SCRIPT_NAME:
1429 p.n = ist("SCRIPT_NAME");
1430 p.v = params->scriptname;
1431 goto encode;
1432 case FCGI_SP_PATH_INFO:
1433 p.n = ist("PATH_INFO");
1434 p.v = params->pathinfo;
1435 goto encode;
1436 case FCGI_SP_REQ_URI:
1437 p.n = ist("REQUEST_URI");
1438 p.v = params->uri;
1439 goto encode;
1440 case FCGI_SP_REQ_METH:
1441 p.n = ist("REQUEST_METHOD");
1442 p.v = params->meth;
1443 goto encode;
1444 case FCGI_SP_REQ_QS:
1445 p.n = ist("QUERY_STRING");
1446 p.v = params->qs;
1447 goto encode;
1448 case FCGI_SP_SRV_NAME:
1449 p.n = ist("SERVER_NAME");
1450 p.v = params->srv_name;
1451 goto encode;
1452 case FCGI_SP_SRV_PORT:
1453 p.n = ist("SERVER_PORT");
1454 p.v = params->srv_port;
1455 goto encode;
1456 case FCGI_SP_SRV_PROTO:
1457 p.n = ist("SERVER_PROTOCOL");
1458 p.v = params->vsn;
1459 goto encode;
1460 case FCGI_SP_REM_ADDR:
1461 p.n = ist("REMOTE_ADDR");
1462 p.v = params->rem_addr;
1463 goto encode;
1464 case FCGI_SP_REM_PORT:
1465 p.n = ist("REMOTE_PORT");
1466 p.v = params->rem_port;
1467 goto encode;
1468 case FCGI_SP_SCRIPT_FILE:
1469 p.n = ist("SCRIPT_FILENAME");
1470 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1471 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1472 p.v = ist2(b_head(&trash), b_data(&trash));
1473 goto encode;
1474 case FCGI_SP_PATH_TRANS:
1475 if (!istlen(params->pathinfo))
1476 goto skip;
1477 p.n = ist("PATH_TRANSLATED");
1478 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1479 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1480 p.v = ist2(b_head(&trash), b_data(&trash));
1481 goto encode;
1482 case FCGI_SP_CONT_LEN:
1483 p.n = ist("CONTENT_LENGTH");
1484 p.v = params->cont_len;
1485 goto encode;
1486 case FCGI_SP_HTTPS:
1487 if (!params->https)
1488 goto skip;
1489 p.n = ist("HTTPS");
1490 p.v = ist("on");
1491 goto encode;
1492 default:
1493 goto skip;
1494 }
1495
1496 encode:
1497 if (!istlen(p.v))
1498 goto skip;
1499 if (!fcgi_encode_param(outbuf, &p))
1500 return 0;
1501 skip:
1502 params->mask |= flag;
1503 return 1;
1504}
1505
1506/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1507 * anything. It is highly unexpected, but if the record is larger than a buffer
1508 * and cannot be encoded in one time, an error is triggered and the connection is
1509 * closed. GET_VALUES record cannot be split.
1510 */
1511static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1512{
1513 struct buffer outbuf;
1514 struct buffer *mbuf;
1515 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1516 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001517 int ret = 0;
1518
1519 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001520
1521 mbuf = br_tail(fconn->mbuf);
1522 retry:
1523 if (!fcgi_get_buf(fconn, mbuf)) {
1524 fconn->flags |= FCGI_CF_MUX_MALLOC;
1525 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001526 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1527 ret = 0;
1528 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001529 }
1530
1531 while (1) {
1532 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1533 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1534 break;
1535 realign_again:
1536 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1537 }
1538
1539 if (outbuf.size < 8)
1540 goto full;
1541
1542 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1543 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1544 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1545 outbuf.data = 8;
1546
1547 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1548 * handled by HAProxy.
1549 */
1550 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1551 goto full;
1552
1553 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001554 TRACE_PROTO("FCGI GET_VALUES record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn, 0, 0, (size_t[]){outbuf.data-8});
Christopher Faulet99eff652019-08-11 23:11:30 +02001555 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1556 b_add(mbuf, outbuf.data);
1557 ret = 1;
1558
1559 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001560 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001561 return ret;
1562 full:
1563 /* Too large to be encoded. For GET_VALUES records, it is an error */
1564 if (!b_data(mbuf))
1565 goto fail;
1566
1567 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1568 goto retry;
1569 fconn->flags |= FCGI_CF_MUX_MFULL;
1570 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001571 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001572 ret = 0;
1573 goto end;
1574 fail:
1575 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001576 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1577 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1578 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001579}
1580
1581/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1582 * couldn't do anything. It is highly unexpected, but if the record is larger
1583 * than a buffer and cannot be decoded in one time, an error is triggered and
1584 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1585 */
1586static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1587{
1588 struct buffer inbuf;
1589 struct buffer *dbuf;
1590 size_t offset;
1591
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001592 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1593
Christopher Faulet99eff652019-08-11 23:11:30 +02001594 dbuf = &fconn->dbuf;
1595
1596 /* Record too large to be fully decoded */
1597 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1598 goto fail;
1599
1600 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001601 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1602 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001603 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001604 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001605
1606 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1607 /* Realign the dmux buffer if the record wraps. It is unexpected
1608 * at this stage because it should be the first record received
1609 * from the FCGI application.
1610 */
1611 b_slow_realign(dbuf, trash.area, 0);
1612 }
1613
1614 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1615
1616 for (offset = 0; offset < b_data(&inbuf); ) {
1617 struct fcgi_param p;
1618 size_t ret;
1619
1620 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1621 if (!ret) {
1622 /* name or value too large to be decoded at once */
1623 goto fail;
1624 }
1625 offset += ret;
1626
1627 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001628 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001629 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){1});
Christopher Faulet99eff652019-08-11 23:11:30 +02001630 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001631 }
1632 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001633 TRACE_STATE("set mpxs param", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001634 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001635 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001636 }
1637 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1638 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001639 TRACE_STATE("set streams_limit", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->streams_limit});
Christopher Faulet99eff652019-08-11 23:11:30 +02001640 }
1641 /*
1642 * Ignore all other params
1643 */
1644 }
1645
1646 /* Reset the number of concurrent streams supported if the FCGI
1647 * application does not support connection multiplexing
1648 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001649 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001650 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001651 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1652 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001653
1654 /* We must be sure to have read exactly the announced record length, no
1655 * more no less
1656 */
1657 if (offset != fconn->drl)
1658 goto fail;
1659
Willy Tarreau022e5e52020-09-10 09:33:15 +02001660 TRACE_PROTO("FCGI GET_VALUES_RESULT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn, 0, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02001661 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1662 fconn->drl = 0;
1663 fconn->drp = 0;
1664 fconn->state = FCGI_CS_RECORD_H;
1665 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001666 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1667 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001668 return 1;
1669 fail:
1670 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001671 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1672 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 +02001673 return 0;
1674}
1675
1676/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1677 * excluded, as the streams which already received the end-of-stream. It returns
1678 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1679 */
1680static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1681{
1682 struct eb32_node *node;
1683 struct fcgi_strm *fstrm;
1684
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001685 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1686
Christopher Faulet99eff652019-08-11 23:11:30 +02001687 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1688 while (node) {
1689 fstrm = container_of(node, struct fcgi_strm, by_id);
1690 node = eb32_next(node);
1691 if (fstrm->state != FCGI_SS_CLOSED &&
1692 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1693 !fcgi_strm_send_abort(fconn, fstrm))
1694 return 0;
1695 }
1696 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001697 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1698 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001699 return 1;
1700}
1701
1702/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1703 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1704 * space to proceed. It is small enough to be encoded in an empty buffer.
1705 */
1706static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1707{
1708 struct buffer outbuf;
1709 struct buffer *mbuf;
1710 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1711 int ret;
1712
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001713 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1714
Christopher Faulet99eff652019-08-11 23:11:30 +02001715 mbuf = br_tail(fconn->mbuf);
1716 retry:
1717 if (!fcgi_get_buf(fconn, mbuf)) {
1718 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001719 fstrm->flags |= FCGI_SF_BLK_MROOM;
1720 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1721 ret = 0;
1722 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001723 }
1724
1725 while (1) {
1726 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1727 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1728 break;
1729 realign_again:
1730 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1731 }
1732
1733 if (outbuf.size < 8)
1734 goto full;
1735
1736 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1737 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1738 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1739 fcgi_set_record_id(outbuf.area, fstrm->id);
1740 outbuf.data = 8;
1741
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001742 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1743 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001744 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001745 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001746 if (!fcgi_encode_begin_request(&outbuf, &rec))
1747 goto full;
1748
1749 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001750 TRACE_PROTO("FCGI BEGIN_REQUEST record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet99eff652019-08-11 23:11:30 +02001751 b_add(mbuf, outbuf.data);
1752 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1753 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001754 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001755 ret = 1;
1756
1757 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001758 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001759 return ret;
1760 full:
1761 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1762 goto retry;
1763 fconn->flags |= FCGI_CF_MUX_MFULL;
1764 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001765 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 +02001766 ret = 0;
1767 goto end;
1768}
1769
1770/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1771 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1772 * space to proceed. It is small enough to be encoded in an empty buffer.
1773 */
1774static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1775 enum fcgi_record_type rtype)
1776{
1777 struct buffer outbuf;
1778 struct buffer *mbuf;
1779 int ret;
1780
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001781 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001782 mbuf = br_tail(fconn->mbuf);
1783 retry:
1784 if (!fcgi_get_buf(fconn, mbuf)) {
1785 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001786 fstrm->flags |= FCGI_SF_BLK_MROOM;
1787 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1788 ret = 0;
1789 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001790 }
1791
1792 while (1) {
1793 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1794 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1795 break;
1796 realign_again:
1797 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1798 }
1799
1800 if (outbuf.size < 8)
1801 goto full;
1802
1803 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1804 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1805 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1806 outbuf.area[1] = rtype;
1807 fcgi_set_record_id(outbuf.area, fstrm->id);
1808 outbuf.data = 8;
1809
1810 /* commit the record */
1811 b_add(mbuf, outbuf.data);
1812 ret = 1;
1813
1814 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001815 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001816 return ret;
1817 full:
1818 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1819 goto retry;
1820 fconn->flags |= FCGI_CF_MUX_MFULL;
1821 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001822 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 +02001823 ret = 0;
1824 goto end;
1825}
1826
1827
1828/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1829 * marks the end of params.
1830 */
1831static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1832{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001833 int ret;
1834
1835 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1836 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1837 if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02001838 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001839 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001840}
1841
1842/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1843 * marks the end of input. On success, all the request was successfully sent.
1844 */
1845static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1846{
1847 int ret;
1848
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001849 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001850 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001851 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001852 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001853 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001854 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1855 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1856 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 return ret;
1858}
1859
1860/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1861 * stops the request processing.
1862 */
1863static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1864{
1865 int ret;
1866
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001867 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001868 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001869 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001870 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001871 TRACE_PROTO("FCGI ABORT record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001872 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1873 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1874 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001875 return ret;
1876}
1877
1878/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1879 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1880 * several records are sent. However, a K/V param cannot be split between 2
1881 * records.
1882 */
1883static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1884 struct htx *htx)
1885{
1886 struct buffer outbuf;
1887 struct buffer *mbuf;
1888 struct htx_blk *blk;
1889 struct htx_sl *sl = NULL;
1890 struct fcgi_strm_params params;
1891 size_t total = 0;
1892
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001893 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1894
Christopher Faulet99eff652019-08-11 23:11:30 +02001895 memset(&params, 0, sizeof(params));
1896 params.p = get_trash_chunk();
1897
1898 mbuf = br_tail(fconn->mbuf);
1899 retry:
1900 if (!fcgi_get_buf(fconn, mbuf)) {
1901 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001902 fstrm->flags |= FCGI_SF_BLK_MROOM;
1903 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1904 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001905 }
1906
1907 while (1) {
1908 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1909 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1910 break;
1911 realign_again:
1912 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1913 }
1914
1915 if (outbuf.size < 8)
1916 goto full;
1917
1918 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1919 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1920 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1921 fcgi_set_record_id(outbuf.area, fstrm->id);
1922 outbuf.data = 8;
1923
1924 blk = htx_get_head_blk(htx);
1925 while (blk) {
1926 enum htx_blk_type type;
1927 uint32_t size = htx_get_blksz(blk);
1928 struct fcgi_param p;
1929
1930 type = htx_get_blk_type(blk);
1931 switch (type) {
1932 case HTX_BLK_REQ_SL:
1933 sl = htx_get_blk_ptr(htx, blk);
1934 if (sl->info.req.meth == HTTP_METH_HEAD)
1935 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1936 if (sl->flags & HTX_SL_F_VER_11)
1937 fstrm->h1m.flags |= H1_MF_VER_11;
1938 break;
1939
1940 case HTX_BLK_HDR:
1941 p.n = htx_get_blk_name(htx, blk);
1942 p.v = htx_get_blk_value(htx, blk);
1943
1944 if (istmatch(p.n, ist(":fcgi-"))) {
1945 p.n.ptr += 6;
1946 p.n.len -= 6;
1947 if (isteq(p.n, ist("gateway_interface")))
1948 params.mask |= FCGI_SP_CGI_GATEWAY;
1949 else if (isteq(p.n, ist("document_root"))) {
1950 params.mask |= FCGI_SP_DOC_ROOT;
1951 params.docroot = p.v;
1952 }
1953 else if (isteq(p.n, ist("script_name"))) {
1954 params.mask |= FCGI_SP_SCRIPT_NAME;
1955 params.scriptname = p.v;
1956 }
1957 else if (isteq(p.n, ist("path_info"))) {
1958 params.mask |= FCGI_SP_PATH_INFO;
1959 params.pathinfo = p.v;
1960 }
1961 else if (isteq(p.n, ist("request_uri"))) {
1962 params.mask |= FCGI_SP_REQ_URI;
1963 params.uri = p.v;
1964 }
1965 else if (isteq(p.n, ist("request_meth")))
1966 params.mask |= FCGI_SP_REQ_METH;
1967 else if (isteq(p.n, ist("query_string")))
1968 params.mask |= FCGI_SP_REQ_QS;
1969 else if (isteq(p.n, ist("server_name")))
1970 params.mask |= FCGI_SP_SRV_NAME;
1971 else if (isteq(p.n, ist("server_port")))
1972 params.mask |= FCGI_SP_SRV_PORT;
1973 else if (isteq(p.n, ist("server_protocol")))
1974 params.mask |= FCGI_SP_SRV_PROTO;
1975 else if (isteq(p.n, ist("remote_addr")))
1976 params.mask |= FCGI_SP_REM_ADDR;
1977 else if (isteq(p.n, ist("remote_port")))
1978 params.mask |= FCGI_SP_REM_PORT;
1979 else if (isteq(p.n, ist("script_filename")))
1980 params.mask |= FCGI_SP_SCRIPT_FILE;
1981 else if (isteq(p.n, ist("path_translated")))
1982 params.mask |= FCGI_SP_PATH_TRANS;
1983 else if (isteq(p.n, ist("https")))
1984 params.mask |= FCGI_SP_HTTPS;
1985 }
1986 else if (isteq(p.n, ist("content-length"))) {
1987 p.n = ist("CONTENT_LENGTH");
1988 params.mask |= FCGI_SP_CONT_LEN;
1989 }
1990 else if (isteq(p.n, ist("content-type")))
1991 p.n = ist("CONTENT_TYPE");
1992 else {
1993 if (isteq(p.n, ist("host")))
1994 params.srv_name = p.v;
1995
Christopher Faulet67d58092019-10-02 10:51:38 +02001996 /* Skip header if same name is used to add the server name */
1997 if (fconn->proxy->server_id_hdr_name &&
1998 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1999 break;
2000
Christopher Faulet99eff652019-08-11 23:11:30 +02002001 memcpy(trash.area, "http_", 5);
2002 memcpy(trash.area+5, p.n.ptr, p.n.len);
2003 p.n = ist2(trash.area, p.n.len+5);
2004 }
2005
2006 if (!fcgi_encode_param(&outbuf, &p)) {
2007 if (b_space_wraps(mbuf))
2008 goto realign_again;
2009 if (outbuf.data == 8)
2010 goto full;
2011 goto done;
2012 }
2013 break;
2014
2015 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002016 if (fconn->proxy->server_id_hdr_name) {
2017 struct server *srv = objt_server(fconn->conn->target);
2018
2019 if (!srv)
2020 goto done;
2021
2022 memcpy(trash.area, "http_", 5);
2023 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2024 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2025 p.v = ist(srv->id);
2026
2027 if (!fcgi_encode_param(&outbuf, &p)) {
2028 if (b_space_wraps(mbuf))
2029 goto realign_again;
2030 if (outbuf.data == 8)
2031 goto full;
2032 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002033 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002034 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002035 goto done;
2036
2037 default:
2038 break;
2039 }
2040 total += size;
2041 blk = htx_remove_blk(htx, blk);
2042 }
2043
2044 done:
2045 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2046 goto error;
2047
2048 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2049 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2051 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2052 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2053 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2054 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2055 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2056 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2057 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2058 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2059 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2060 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2061 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2062 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2063 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2064 goto error;
2065
2066 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002067 TRACE_PROTO("FCGI PARAMS record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002068 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2069 b_add(mbuf, outbuf.data);
2070
2071 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002072 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002073 return total;
2074 full:
2075 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2076 goto retry;
2077 fconn->flags |= FCGI_CF_MUX_MFULL;
2078 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002079 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 +02002080 if (total)
2081 goto error;
2082 goto end;
2083
2084 error:
2085 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002086 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002087 fcgi_strm_error(fstrm);
2088 goto end;
2089}
2090
2091/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2092 * anything. STDIN records contain the request body.
2093 */
2094static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2095 struct htx *htx, size_t count, struct buffer *buf)
2096{
2097 struct buffer outbuf;
2098 struct buffer *mbuf;
2099 struct htx_blk *blk;
2100 enum htx_blk_type type;
2101 uint32_t size;
2102 size_t total = 0;
2103
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002104 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002105 if (!count)
2106 goto end;
2107
2108 mbuf = br_tail(fconn->mbuf);
2109 retry:
2110 if (!fcgi_get_buf(fconn, mbuf)) {
2111 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002112 fstrm->flags |= FCGI_SF_BLK_MROOM;
2113 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2114 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002115 }
2116
2117 /* Perform some optimizations to reduce the number of buffer copies.
2118 * First, if the mux's buffer is empty and the htx area contains exactly
2119 * one data block of the same size as the requested count, and this
2120 * count fits within the record size, then it's possible to simply swap
2121 * the caller's buffer with the mux's output buffer and adjust offsets
2122 * and length to match the entire DATA HTX block in the middle. In this
2123 * case we perform a true zero-copy operation from end-to-end. This is
2124 * the situation that happens all the time with large files. Second, if
2125 * this is not possible, but the mux's output buffer is empty, we still
2126 * have an opportunity to avoid the copy to the intermediary buffer, by
2127 * making the intermediary buffer's area point to the output buffer's
2128 * area. In this case we want to skip the HTX header to make sure that
2129 * copies remain aligned and that this operation remains possible all
2130 * the time. This goes for headers, data blocks and any data extracted
2131 * from the HTX blocks.
2132 */
2133 blk = htx_get_head_blk(htx);
2134 if (!blk)
2135 goto end;
2136 type = htx_get_blk_type(blk);
2137 size = htx_get_blksz(blk);
2138 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2139 void *old_area = mbuf->area;
2140
2141 if (b_data(mbuf)) {
2142 /* Too bad there are data left there. We're willing to memcpy/memmove
2143 * up to 1/4 of the buffer, which means that it's OK to copy a large
2144 * record into a buffer containing few data if it needs to be realigned,
2145 * and that it's also OK to copy few data without realigning. Otherwise
2146 * we'll pretend the mbuf is full and wait for it to become empty.
2147 */
2148 if (size + 8 <= b_room(mbuf) &&
2149 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2150 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2151 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002152 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002153 }
2154
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002155 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 +02002156 /* map a FCGI record to the HTX block so that we can put the
2157 * record header there.
2158 */
2159 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2160 outbuf.area = b_head(mbuf);
2161
2162 /* prepend a FCGI record header just before the DATA block */
2163 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2164 fcgi_set_record_id(outbuf.area, fstrm->id);
2165 fcgi_set_record_size(outbuf.area, size);
2166
2167 /* and exchange with our old area */
2168 buf->area = old_area;
2169 buf->data = buf->head = 0;
2170 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002171
2172 htx = (struct htx *)buf->area;
2173 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002174 goto end;
2175 }
2176
2177 copy:
2178 while (1) {
2179 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2180 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2181 break;
2182 realign_again:
2183 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2184 }
2185
2186 if (outbuf.size < 8)
2187 goto full;
2188
2189 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2190 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2191 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2192 fcgi_set_record_id(outbuf.area, fstrm->id);
2193 outbuf.data = 8;
2194
2195 blk = htx_get_head_blk(htx);
2196 while (blk && count) {
2197 enum htx_blk_type type = htx_get_blk_type(blk);
2198 uint32_t size = htx_get_blksz(blk);
2199 struct ist v;
2200
2201 switch (type) {
2202 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002203 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 +02002204 v = htx_get_blk_value(htx, blk);
2205 if (v.len > count)
2206 v.len = count;
2207
2208 if (v.len > b_room(&outbuf)) {
2209 /* It doesn't fit at once. If it at least fits once split and
2210 * the amount of data to move is low, let's defragment the
2211 * buffer now.
2212 */
2213 if (b_space_wraps(mbuf) &&
2214 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2215 b_data(mbuf) <= MAX_DATA_REALIGN)
2216 goto realign_again;
2217 v.len = b_room(&outbuf);
2218 }
2219 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2220 if (outbuf.data == 8)
2221 goto full;
2222 goto done;
2223 }
2224 if (v.len != size) {
2225 total += v.len;
2226 count -= v.len;
2227 htx_cut_data_blk(htx, blk, v.len);
2228 goto done;
2229 }
2230 break;
2231
2232 case HTX_BLK_EOM:
2233 goto done;
2234
2235 default:
2236 break;
2237 }
2238 total += size;
2239 count -= size;
2240 blk = htx_remove_blk(htx, blk);
2241 }
2242
2243 done:
2244 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002245 TRACE_PROTO("FCGI STDIN record xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, 0, (size_t[]){outbuf.data - 8});
Christopher Faulet99eff652019-08-11 23:11:30 +02002246 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2247 b_add(mbuf, outbuf.data);
2248
2249 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002250 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002251 return total;
2252 full:
2253 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2254 goto retry;
2255 fconn->flags |= FCGI_CF_MUX_MFULL;
2256 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002257 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 +02002258 goto end;
2259}
2260
2261/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2262 * anything. STDOUT records contain the entire response. All the content is
2263 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2264 */
2265static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2266{
2267 struct buffer *dbuf;
2268 size_t ret;
2269 size_t max;
2270
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002271 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2272
Christopher Faulet99eff652019-08-11 23:11:30 +02002273 dbuf = &fconn->dbuf;
2274
2275 /* Only padding remains */
2276 if (fconn->state == FCGI_CS_RECORD_P)
2277 goto end_transfer;
2278
2279 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2280 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2281 buf_room_for_htx_data(dbuf))
2282 goto fail; // incomplete record
2283
2284 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2285 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002286 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2287 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002288 }
2289
2290 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2291 max = buf_room_for_htx_data(&fstrm->rxbuf);
2292 if (!b_data(&fstrm->rxbuf))
2293 fstrm->rxbuf.head = sizeof(struct htx);
2294 if (max > fconn->drl)
2295 max = fconn->drl;
2296
2297 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2298 if (!ret)
2299 goto fail;
2300 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002301 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2302 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002303
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002304 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002305 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002306 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2307 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002308
2309 if (fconn->drl)
2310 goto fail;
2311
2312 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002313 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002314 fconn->drl += fconn->drp;
2315 fconn->drp = 0;
2316 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2317 b_del(&fconn->dbuf, ret);
2318 fconn->drl -= ret;
2319 if (fconn->drl)
2320 goto fail;
2321
2322 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002323 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2324 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002325 return 1;
2326 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002327 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 +02002328 return 0;
2329}
2330
2331
2332/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2333 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002334 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002335 */
2336static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2337{
2338 int ret;
2339
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002340 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2341
Christopher Faulet99eff652019-08-11 23:11:30 +02002342 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002343 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002344 fconn->drl += fconn->drp;
2345 fconn->drp = 0;
2346 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2347 b_del(&fconn->dbuf, ret);
2348 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002349 if (fconn->drl) {
2350 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 +02002351 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002352 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002353 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002354 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002355 TRACE_PROTO("FCGI STDOUT record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){0});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002356 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);
2357 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002358 return 1;
2359}
2360
2361/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2362 * anything.
2363 */
2364static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2365{
2366 struct buffer *dbuf;
2367 struct buffer tag;
2368 size_t ret;
2369
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002370 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002371 dbuf = &fconn->dbuf;
2372
2373 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002374 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002375 goto end_transfer;
2376
2377 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2378 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2379 buf_room_for_htx_data(dbuf))
2380 goto fail; // incomplete record
2381
2382 chunk_reset(&trash);
2383 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2384 if (!ret)
2385 goto fail;
2386 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002387 TRACE_PROTO("FCGI STDERR record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002388
2389 trash.area[ret] = '\n';
2390 trash.area[ret+1] = '\0';
2391 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002392 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002393
2394 if (fconn->drl)
2395 goto fail;
2396
2397 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002398 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002399 fconn->drl += fconn->drp;
2400 fconn->drp = 0;
2401 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2402 b_del(&fconn->dbuf, ret);
2403 fconn->drl -= ret;
2404 if (fconn->drl)
2405 goto fail;
2406 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002407 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2408 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002409 return 1;
2410 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002411 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 +02002412 return 0;
2413}
2414
2415/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2416 * anything. If the empty STDOUT record is not already received, this one marks
2417 * the end of the response. It is highly unexpected, but if the record is larger
2418 * than a buffer and cannot be decoded in one time, an error is triggered and
2419 * the connection is closed. END_REQUEST record cannot be split.
2420 */
2421static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2422{
2423 struct buffer inbuf;
2424 struct buffer *dbuf;
2425 struct fcgi_end_request endreq;
2426
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002427 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002428 dbuf = &fconn->dbuf;
2429
2430 /* Record too large to be fully decoded */
2431 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2432 goto fail;
2433
2434 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002435 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2436 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002437 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002438 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002439
2440 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2441 /* Realign the dmux buffer if the record wraps. It is unexpected
2442 * at this stage because it should be the first record received
2443 * from the FCGI application.
2444 */
2445 b_slow_realign(dbuf, trash.area, 0);
2446 }
2447
2448 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2449
2450 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2451 goto fail;
2452
2453 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002454 TRACE_STATE("end of script reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ|FCGI_EV_RX_EOI, fconn->conn, fstrm);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002455 TRACE_PROTO("FCGI END_REQUEST record rcvd", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm, 0, (size_t[]){fconn->drl});
Christopher Faulet99eff652019-08-11 23:11:30 +02002456 fstrm->proto_status = endreq.errcode;
2457 fcgi_strm_close(fstrm);
2458
2459 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2460 fconn->drl = 0;
2461 fconn->drp = 0;
2462 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002463 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2464 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002465 return 1;
2466
2467 fail:
2468 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002469 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 +02002470 return 0;
2471}
2472
2473/* process Rx records to be demultiplexed */
2474static void fcgi_process_demux(struct fcgi_conn *fconn)
2475{
2476 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2477 struct fcgi_header hdr;
2478 int ret;
2479
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002480 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2481
Christopher Faulet99eff652019-08-11 23:11:30 +02002482 if (fconn->state == FCGI_CS_CLOSED)
2483 return;
2484
2485 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002486 if (fconn->state == FCGI_CS_INIT) {
2487 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2488 return;
2489 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002490 if (fconn->state == FCGI_CS_SETTINGS) {
2491 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002492 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002493 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2494 if (!ret)
2495 goto fail;
2496 b_del(&fconn->dbuf, ret);
2497
2498 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2499 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002500 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);
2501 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 +02002502 goto fail;
2503 }
2504 goto new_record;
2505 }
2506 }
2507
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002508 /* process as many incoming records as possible below */
2509 while (1) {
2510 if (!b_data(&fconn->dbuf)) {
2511 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2512 break;
2513 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002514
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002515 if (fconn->state == FCGI_CS_CLOSED) {
2516 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002517 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002518 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002519
2520 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002521 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002522 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2523 if (!ret)
2524 break;
2525 b_del(&fconn->dbuf, ret);
2526
2527 new_record:
2528 fconn->dsi = hdr.id;
2529 fconn->drt = hdr.type;
2530 fconn->drl = hdr.len;
2531 fconn->drp = hdr.padding;
2532 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002533 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 +02002534 }
2535
2536 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2537 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2538
2539 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2540 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002541 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002542 fstrm->state == FCGI_SS_CLOSED ||
2543 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2544 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2545 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002546 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 +02002547 fstrm->cs->flags |= CS_FL_RCV_MORE;
2548 fcgi_strm_notify_recv(fstrm);
2549 }
2550 fstrm = tmp_fstrm;
2551
2552 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2553 /* ignore all record for closed streams */
2554 goto ignore_record;
2555 }
2556 if (fstrm->state == FCGI_SS_IDLE) {
2557 /* ignore all record for unknown streams */
2558 goto ignore_record;
2559 }
2560
2561 switch (fconn->drt) {
2562 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002563 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 +02002564 ret = fcgi_conn_handle_values_result(fconn);
2565 break;
2566
2567 case FCGI_STDOUT:
2568 if (fstrm->flags & FCGI_SF_ES_RCVD)
2569 goto ignore_record;
2570
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002571 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002572 if (fconn->drl)
2573 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2574 else
2575 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2576 break;
2577
2578 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002579 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002580 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2581 break;
2582
2583 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002584 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 +02002585 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2586 break;
2587
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002588 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002589 default:
2590 ignore_record:
2591 /* drop records that we ignore. They may be
2592 * larger than the buffer so we drain all of
2593 * their contents until we reach the end.
2594 */
2595 fconn->state = FCGI_CS_RECORD_P;
2596 fconn->drl += fconn->drp;
2597 fconn->drp = 0;
2598 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002599 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002600 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002601 b_del(&fconn->dbuf, ret);
2602 fconn->drl -= ret;
2603 ret = (fconn->drl == 0);
2604 }
2605
2606 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002607 if (ret <= 0) {
2608 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002609 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002610 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002611
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002612 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002613 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002614 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2615 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002616 }
2617
2618 fail:
2619 /* we can go here on missing data, blocked response or error */
2620 if (fstrm && fstrm->cs &&
2621 (b_data(&fstrm->rxbuf) ||
Christopher Faulet6670e3e2020-10-08 15:26:33 +02002622 fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02002623 fstrm->state == FCGI_SS_CLOSED ||
2624 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2625 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2626 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002627 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 +02002628 fstrm->cs->flags |= CS_FL_RCV_MORE;
2629 fcgi_strm_notify_recv(fstrm);
2630 }
2631
2632 fcgi_conn_restart_reading(fconn, 0);
2633}
2634
2635/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2636 * the end.
2637 */
2638static int fcgi_process_mux(struct fcgi_conn *fconn)
2639{
2640 struct fcgi_strm *fstrm, *fstrm_back;
2641
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002642 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2643
Christopher Faulet99eff652019-08-11 23:11:30 +02002644 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2645 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2646 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2647 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002648 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 +02002649 fcgi_wake_unassigned_streams(fconn);
2650 goto mux;
2651 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002652 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002653 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2654 goto fail;
2655 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002656 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 +02002657 }
2658 /* need to wait for the other side */
2659 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002660 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002661 }
2662
2663 mux:
2664 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2665 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2666 break;
2667
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002668 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002669 continue;
2670
Willy Tarreau7aad7032020-01-16 17:20:57 +01002671 /* If the sender changed his mind and unsubscribed, let's just
2672 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002673 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002674 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2675 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002676 LIST_DEL_INIT(&fstrm->send_list);
2677 continue;
2678 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002679
2680 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002681 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2682 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002683 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002684 tasklet_wakeup(fstrm->subs->tasklet);
2685 fstrm->subs->events &= ~SUB_RETRY_SEND;
2686 if (!fstrm->subs->events)
2687 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002688 } else {
2689 /* it's the shut request that was queued */
2690 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2691 tasklet_wakeup(fstrm->shut_tl);
2692 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002693 }
2694
2695 fail:
2696 if (fconn->state == FCGI_CS_CLOSED) {
2697 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2698 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002699 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2700 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002701 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002702 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002703 }
2704 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002705
2706 done:
2707 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002708 return 1;
2709}
2710
2711
2712/* Attempt to read data, and subscribe if none available.
2713 * The function returns 1 if data has been received, otherwise zero.
2714 */
2715static int fcgi_recv(struct fcgi_conn *fconn)
2716{
2717 struct connection *conn = fconn->conn;
2718 struct buffer *buf;
2719 int max;
2720 size_t ret;
2721
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002722 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2723
2724 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2725 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002727 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002728
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002729 if (!fcgi_recv_allowed(fconn)) {
2730 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002731 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002732 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002733
2734 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2735 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002736 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002737 fconn->flags |= FCGI_CF_DEM_DALLOC;
2738 return 0;
2739 }
2740
2741 b_realign_if_empty(buf);
2742 if (!b_data(buf)) {
2743 /* try to pre-align the buffer like the
2744 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002745 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002746 * HTX block to alias it upon recv. We cannot use the
2747 * head because rcv_buf() will realign the buffer if
2748 * it's empty. Thus we cheat and pretend we already
2749 * have a few bytes there.
2750 */
2751 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2752 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2753 }
2754 else
2755 max = buf_room_for_htx_data(buf);
2756
2757 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2758
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002759 if (max && !ret && fcgi_recv_allowed(fconn)) {
2760 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002761 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002762 }
2763 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002764 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002765
2766 if (!b_data(buf)) {
2767 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002768 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002769 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2770 }
2771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002772 if (ret == max) {
2773 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002774 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002775 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002776
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002777 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002778 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2779}
2780
2781
2782/* Try to send data if possible.
2783 * The function returns 1 if data have been sent, otherwise zero.
2784 */
2785static int fcgi_send(struct fcgi_conn *fconn)
2786{
2787 struct connection *conn = fconn->conn;
2788 int done;
2789 int sent = 0;
2790
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002791 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2792
2793 if (conn->flags & CO_FL_ERROR) {
2794 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002795 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002796 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002797
2798
Willy Tarreau911db9b2020-01-23 16:27:54 +01002799 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002800 /* a handshake was requested */
2801 goto schedule;
2802 }
2803
2804 /* This loop is quite simple : it tries to fill as much as it can from
2805 * pending streams into the existing buffer until it's reportedly full
2806 * or the end of send requests is reached. Then it tries to send this
2807 * buffer's contents out, marks it not full if at least one byte could
2808 * be sent, and tries again.
2809 *
2810 * The snd_buf() function normally takes a "flags" argument which may
2811 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2812 * data immediately comes and CO_SFL_STREAMER to indicate that the
2813 * connection is streaming lots of data (used to increase TLS record
2814 * size at the expense of latency). The former can be sent any time
2815 * there's a buffer full flag, as it indicates at least one stream
2816 * attempted to send and failed so there are pending data. An
2817 * alternative would be to set it as long as there's an active stream
2818 * but that would be problematic for ACKs until we have an absolute
2819 * guarantee that all waiters have at least one byte to send. The
2820 * latter should possibly not be set for now.
2821 */
2822
2823 done = 0;
2824 while (!done) {
2825 unsigned int flags = 0;
2826 unsigned int released = 0;
2827 struct buffer *buf;
2828
2829 /* fill as much as we can into the current buffer */
2830 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2831 done = fcgi_process_mux(fconn);
2832
2833 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2834 done = 1; // we won't go further without extra buffers
2835
2836 if (conn->flags & CO_FL_ERROR)
2837 break;
2838
2839 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2840 flags |= CO_SFL_MSG_MORE;
2841
2842 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2843 if (b_data(buf)) {
2844 int ret;
2845
2846 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2847 if (!ret) {
2848 done = 1;
2849 break;
2850 }
2851 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002852 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002853 b_del(buf, ret);
2854 if (b_data(buf)) {
2855 done = 1;
2856 break;
2857 }
2858 }
2859 b_free(buf);
2860 released++;
2861 }
2862
2863 if (released)
2864 offer_buffers(NULL, tasks_run_queue);
2865
2866 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002867 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2868 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2870 }
2871
2872 if (conn->flags & CO_FL_SOCK_WR_SH) {
2873 /* output closed, nothing to send, clear the buffer to release it */
2874 b_reset(br_tail(fconn->mbuf));
2875 }
2876 /* We're not full anymore, so we can wake any task that are waiting
2877 * for us.
2878 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002879 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002880 struct fcgi_strm *fstrm;
2881
2882 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2883 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2884 break;
2885
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002886 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002887 continue;
2888
Willy Tarreau7aad7032020-01-16 17:20:57 +01002889 /* If the sender changed his mind and unsubscribed, let's just
2890 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002891 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002892 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2893 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002894 LIST_DEL_INIT(&fstrm->send_list);
2895 continue;
2896 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002897
2898 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002899 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002900 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002901 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002902 tasklet_wakeup(fstrm->subs->tasklet);
2903 fstrm->subs->events &= ~SUB_RETRY_SEND;
2904 if (!fstrm->subs->events)
2905 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002906 } else {
2907 /* it's the shut request that was queued */
2908 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2909 tasklet_wakeup(fstrm->shut_tl);
2910 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002911 }
2912 }
2913 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002914 if (!br_data(fconn->mbuf)) {
2915 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002916 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002917 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002918schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002919 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2920 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002921 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002922 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002923
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002924 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002925 return sent;
2926}
2927
2928/* this is the tasklet referenced in fconn->wait_event.tasklet */
2929static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2930{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002931 struct connection *conn;
2932 struct fcgi_conn *fconn;
2933 struct tasklet *tl = (struct tasklet *)t;
2934 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002935 int ret = 0;
2936
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002937
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002938 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002939 if (tl->context == NULL) {
2940 /* The connection has been taken over by another thread,
2941 * we're no longer responsible for it, so just free the
2942 * tasklet, and do nothing.
2943 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002944 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002945 tasklet_free(tl);
2946 return NULL;
2947
2948 }
2949 fconn = ctx;
2950 conn = fconn->conn;
2951
2952 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2953
2954 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2955 if (conn_in_list)
2956 MT_LIST_DEL(&conn->list);
2957
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002958 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002959
Christopher Faulet99eff652019-08-11 23:11:30 +02002960 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2961 ret = fcgi_send(fconn);
2962 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2963 ret |= fcgi_recv(fconn);
2964 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002965 ret = fcgi_process(fconn);
2966
2967 /* If we were in an idle list, we want to add it back into it,
2968 * unless fcgi_process() returned -1, which mean it has destroyed
2969 * the connection (testing !ret is enough, if fcgi_process() wasn't
2970 * called then ret will be 0 anyway.
2971 */
2972 if (!ret && conn_in_list) {
2973 struct server *srv = objt_server(conn->target);
2974
2975 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002976 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002977 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002978 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002979 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002980 return NULL;
2981}
2982
2983/* callback called on any event by the connection handler.
2984 * It applies changes and returns zero, or < 0 if it wants immediate
2985 * destruction of the connection (which normally doesn not happen in FCGI).
2986 */
2987static int fcgi_process(struct fcgi_conn *fconn)
2988{
2989 struct connection *conn = fconn->conn;
2990
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002991 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2992
Christopher Faulet99eff652019-08-11 23:11:30 +02002993 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2994 fcgi_process_demux(fconn);
2995
2996 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2997 b_reset(&fconn->dbuf);
2998
2999 if (buf_room_for_htx_data(&fconn->dbuf))
3000 fconn->flags &= ~FCGI_CF_DEM_DFULL;
3001 }
3002 fcgi_send(fconn);
3003
Willy Tarreauc3914d42020-09-24 08:39:22 +02003004 if (unlikely(fconn->proxy->disabled)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003005 /* frontend is stopping, reload likely in progress, let's try
3006 * to announce a graceful shutdown if not yet done. We don't
3007 * care if it fails, it will be tried again later.
3008 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003009 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 +02003010 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3011 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3012 fcgi_conn_send_aborts(fconn);
3013 }
3014 }
3015
3016 /*
3017 * If we received early data, and the handshake is done, wake
3018 * any stream that was waiting for it.
3019 */
3020 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003021 (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 +02003022 struct eb32_node *node;
3023 struct fcgi_strm *fstrm;
3024
3025 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3026 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3027
3028 while (node) {
3029 fstrm = container_of(node, struct fcgi_strm, by_id);
3030 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3031 fcgi_strm_notify_recv(fstrm);
3032 node = eb32_next(node);
3033 }
3034 }
3035
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003036 if ((conn->flags & CO_FL_ERROR) || fcgi_conn_read0_pending(fconn) ||
Christopher Faulet99eff652019-08-11 23:11:30 +02003037 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3038 eb_is_empty(&fconn->streams_by_id)) {
3039 fcgi_wake_some_streams(fconn, 0);
3040
3041 if (eb_is_empty(&fconn->streams_by_id)) {
3042 /* no more stream, kill the connection now */
3043 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003044 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003045 return -1;
3046 }
3047 }
3048
3049 if (!b_data(&fconn->dbuf))
3050 fcgi_release_buf(fconn, &fconn->dbuf);
3051
3052 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3053 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3054 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3055 fcgi_release_mbuf(fconn);
3056
3057 if (fconn->task) {
3058 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3059 task_queue(fconn->task);
3060 }
3061
3062 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003063 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003064 return 0;
3065}
3066
3067
3068/* wake-up function called by the connection layer (mux_ops.wake) */
3069static int fcgi_wake(struct connection *conn)
3070{
3071 struct fcgi_conn *fconn = conn->ctx;
3072
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003073 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003074 return (fcgi_process(fconn));
3075}
3076
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003077
3078static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3079{
3080 int ret = 0;
3081 switch (mux_ctl) {
3082 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003083 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003084 ret |= MUX_STATUS_READY;
3085 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003086 case MUX_EXIT_STATUS:
3087 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003088 default:
3089 return -1;
3090 }
3091}
3092
Christopher Faulet99eff652019-08-11 23:11:30 +02003093/* Connection timeout management. The principle is that if there's no receipt
3094 * nor sending for a certain amount of time, the connection is closed. If the
3095 * MUX buffer still has lying data or is not allocatable, the connection is
3096 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003097 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003098 */
3099static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3100{
3101 struct fcgi_conn *fconn = context;
3102 int expired = tick_is_expired(t->expire, now_ms);
3103
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003104 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3105
Willy Tarreau60814ff2020-06-30 11:19:23 +02003106 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003107 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3108
3109 /* Somebody already stole the connection from us, so we should not
3110 * free it, we just have to free the task.
3111 */
3112 if (!t->context) {
3113 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3114 fconn = NULL;
3115 goto do_leave;
3116 }
3117
Willy Tarreau60814ff2020-06-30 11:19:23 +02003118 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003119 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003120 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3121 return t;
3122 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003123
Willy Tarreau60814ff2020-06-30 11:19:23 +02003124 /* We're about to destroy the connection, so make sure nobody attempts
3125 * to steal it from us.
3126 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003127 if (fconn->conn->flags & CO_FL_LIST_MASK)
3128 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003129
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003130 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003131 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003132
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003133do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003134 task_destroy(t);
3135
3136 if (!fconn) {
3137 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003138 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003139 return NULL;
3140 }
3141
3142 fconn->task = NULL;
3143 fconn->state = FCGI_CS_CLOSED;
3144 fcgi_wake_some_streams(fconn, 0);
3145
3146 if (br_data(fconn->mbuf)) {
3147 /* don't even try to send aborts, the buffer is stuck */
3148 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3149 goto end;
3150 }
3151
3152 /* try to send but no need to insist */
3153 if (!fcgi_conn_send_aborts(fconn))
3154 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3155
3156 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3157 conn_xprt_ready(fconn->conn)) {
3158 unsigned int released = 0;
3159 struct buffer *buf;
3160
3161 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3162 if (b_data(buf)) {
3163 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3164 buf, b_data(buf), 0);
3165 if (!ret)
3166 break;
3167 b_del(buf, ret);
3168 if (b_data(buf))
3169 break;
3170 b_free(buf);
3171 released++;
3172 }
3173 }
3174
3175 if (released)
3176 offer_buffers(NULL, tasks_run_queue);
3177 }
3178
3179 end:
3180 /* either we can release everything now or it will be done later once
3181 * the last stream closes.
3182 */
3183 if (eb_is_empty(&fconn->streams_by_id))
3184 fcgi_release(fconn);
3185
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003186 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003187 return NULL;
3188}
3189
3190
3191/*******************************************/
3192/* functions below are used by the streams */
3193/*******************************************/
3194
3195/* Append the description of what is present in error snapshot <es> into <out>.
3196 * The description must be small enough to always fit in a buffer. The output
3197 * buffer may be the trash so the trash must not be used inside this function.
3198 */
3199static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3200{
3201 chunk_appendf(out,
3202 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3203 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3204 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3205 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3206 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3207 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3208}
3209/*
3210 * Capture a bad response and archive it in the proxy's structure. By default
3211 * it tries to report the error position as h1m->err_pos. However if this one is
3212 * not set, it will then report h1m->next, which is the last known parsing
3213 * point. The function is able to deal with wrapping buffers. It always displays
3214 * buffers as a contiguous area starting at buf->p. The direction is determined
3215 * thanks to the h1m's flags.
3216 */
3217static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3218 struct h1m *h1m, struct buffer *buf)
3219{
3220 struct session *sess = fstrm->sess;
3221 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003222 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003223 union error_snapshot_ctx ctx;
3224
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003225 if (fstrm->cs && fstrm->cs->data) {
3226 if (sess == NULL)
3227 sess = si_strm(fstrm->cs->data)->sess;
3228 if (!(h1m->flags & H1_MF_RESP))
3229 other_end = si_strm(fstrm->cs->data)->be;
3230 else
3231 other_end = sess->fe;
3232 } else
3233 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003234 /* http-specific part now */
3235 ctx.h1.state = h1m->state;
3236 ctx.h1.c_flags = fconn->flags;
3237 ctx.h1.s_flags = fstrm->flags;
3238 ctx.h1.m_flags = h1m->flags;
3239 ctx.h1.m_clen = h1m->curr_len;
3240 ctx.h1.m_blen = h1m->body_len;
3241
3242 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3243 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3244 &ctx, fcgi_show_error_snapshot);
3245}
3246
3247static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3248 struct buffer *buf, size_t *ofs, size_t max)
3249{
3250 int ret;
3251
Willy Tarreau022e5e52020-09-10 09:33:15 +02003252 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003253 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3254 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003255 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 +02003256 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003257 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 +02003258 fcgi_strm_error(fstrm);
3259 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3260 }
3261 goto end;
3262 }
3263
3264 *ofs += ret;
3265 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003266 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003267 return ret;
3268
3269}
3270
Christopher Fauletaf542632019-10-01 21:52:49 +02003271static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003272 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3273{
3274 int ret;
3275
Willy Tarreau022e5e52020-09-10 09:33:15 +02003276 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003277 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003278 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003279 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 +02003280 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003281 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 +02003282 fcgi_strm_error(fstrm);
3283 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3284 }
3285 goto end;
3286 }
3287 *ofs += ret;
3288 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003289 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003290 return ret;
3291}
3292
3293static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3294 struct buffer *buf, size_t *ofs, size_t max)
3295{
3296 int ret;
3297
Willy Tarreau022e5e52020-09-10 09:33:15 +02003298 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet99eff652019-08-11 23:11:30 +02003299 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003300 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003301 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 +02003302 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003303 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 +02003304 fcgi_strm_error(fstrm);
3305 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3306 }
3307 goto end;
3308 }
3309 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003310 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003311 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003312 return ret;
3313}
3314
3315static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003316 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003317{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003318 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003319
Willy Tarreau022e5e52020-09-10 09:33:15 +02003320 TRACE_ENTER(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003321 ret = h1_parse_msg_eom(h1m, htx, max);
3322 if (!ret) {
3323 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3324 if (htx->flags & HTX_FL_PARSING_ERROR) {
3325 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3326 fcgi_strm_error(fstrm);
3327 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3328 }
3329 goto end;
3330 }
3331 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3332 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003333 TRACE_LEAVE(FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01003334 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003335}
3336
3337static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3338{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003339 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003340 struct htx *htx;
3341 struct h1m *h1m = &fstrm->h1m;
3342 size_t ret, data, total = 0;
3343
3344 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003345 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3346
Christopher Faulet99eff652019-08-11 23:11:30 +02003347 data = htx->data;
3348 if (fstrm->state == FCGI_SS_ERROR)
3349 goto end;
3350
3351 do {
3352 size_t used = htx_used_space(htx);
3353
3354 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003355 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003356 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3357 if (!ret)
3358 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003359
3360 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3361
Christopher Faulet99eff652019-08-11 23:11:30 +02003362 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3363 struct htx_blk *blk = htx_get_head_blk(htx);
3364 struct htx_sl *sl;
3365
3366 if (!blk)
3367 break;
3368 sl = htx_get_blk_ptr(htx, blk);
3369 sl->flags |= HTX_SL_F_XFER_LEN;
3370 htx->extra = 0;
3371 }
3372 }
3373 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003374 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003375 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003376 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003377 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003378
3379 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 +02003380 }
3381 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003382 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3383 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3384 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003385 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003386
Christopher Faulet76014fd2019-12-10 11:47:22 +01003387 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 +02003388 }
3389 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003390 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3391 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3392 break;
3393
3394 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3395 }
3396
Christopher Faulet99eff652019-08-11 23:11:30 +02003397 if (b_data(&fstrm->rxbuf) > total) {
3398 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003399 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003400 fcgi_strm_error(fstrm);
3401 }
3402 break;
3403 }
3404 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003405 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003406 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003407
Christopher Faulet99eff652019-08-11 23:11:30 +02003408 if (fstrm->state != FCGI_SS_ERROR &&
3409 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003410 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003411 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3412 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3413 h1m->state = H1_MSG_DONE;
3414 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 +02003415 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003416 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003417 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003418
3419 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003420 }
3421 else {
3422 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003423 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003424 fcgi_strm_error(fstrm);
3425 break;
3426 }
3427
3428 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003429 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003430
3431 if (fstrm->state == FCGI_SS_ERROR) {
3432 b_reset(&fstrm->rxbuf);
3433 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003434 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003435 return 0;
3436 }
3437
3438 b_del(&fstrm->rxbuf, total);
3439
3440 end:
3441 htx_to_buf(htx, buf);
3442 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003443 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003444 return ret;
3445}
3446
3447/*
3448 * Attach a new stream to a connection
3449 * (Used for outgoing connections)
3450 */
3451static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3452{
3453 struct conn_stream *cs;
3454 struct fcgi_strm *fstrm;
3455 struct fcgi_conn *fconn = conn->ctx;
3456
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003457 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003458 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003459 if (!cs) {
3460 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003461 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003462 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003463 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3464 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003465 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003466 cs_free(cs);
3467 return NULL;
3468 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003469 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003470 return cs;
3471}
3472
3473/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3474 * We have to scan because we may have some orphan streams. It might be
3475 * beneficial to scan backwards from the end to reduce the likeliness to find
3476 * orphans.
3477 */
3478static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3479{
3480 struct fcgi_conn *fconn = conn->ctx;
3481 struct fcgi_strm *fstrm;
3482 struct eb32_node *node;
3483
3484 node = eb32_first(&fconn->streams_by_id);
3485 while (node) {
3486 fstrm = container_of(node, struct fcgi_strm, by_id);
3487 if (fstrm->cs)
3488 return fstrm->cs;
3489 node = eb32_next(node);
3490 }
3491 return NULL;
3492}
3493
3494/*
3495 * Destroy the mux and the associated connection, if it is no longer used
3496 */
3497static void fcgi_destroy(void *ctx)
3498{
3499 struct fcgi_conn *fconn = ctx;
3500
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003501 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003502 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3503 fcgi_release(fconn);
3504}
3505
3506/*
3507 * Detach the stream from the connection and possibly release the connection.
3508 */
3509static void fcgi_detach(struct conn_stream *cs)
3510{
3511 struct fcgi_strm *fstrm = cs->ctx;
3512 struct fcgi_conn *fconn;
3513 struct session *sess;
3514
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003515 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3516
Christopher Faulet99eff652019-08-11 23:11:30 +02003517 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003518 if (!fstrm) {
3519 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003520 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003521 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003522
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003523 /* there's no txbuf so we're certain no to be able to send anything */
3524 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003525
3526 sess = fstrm->sess;
3527 fconn = fstrm->fconn;
3528 fstrm->cs = NULL;
3529 fconn->nb_cs--;
3530
3531 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3532 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3533 fconn->streams_limit = 1;
3534 }
3535 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3536 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3537 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3538 fconn->state = FCGI_CS_CLOSED;
3539 }
3540
3541 /* this stream may be blocked waiting for some data to leave, so orphan
3542 * it in this case.
3543 */
3544 if (!(cs->conn->flags & CO_FL_ERROR) &&
3545 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003546 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003547 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003548 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003549 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003550 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003551
3552 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3553 /* unblock the connection if it was blocked on this stream. */
3554 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3555 fcgi_conn_restart_reading(fconn, 1);
3556 }
3557
3558 fcgi_strm_destroy(fstrm);
3559
3560 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003561 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003562 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003563 /* Add the connection in the session serverlist, if not already done */
3564 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3565 fconn->conn->owner = NULL;
3566 if (eb_is_empty(&fconn->streams_by_id)) {
3567 /* let's kill the connection right away */
3568 fconn->conn->mux->destroy(fconn);
3569 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3570 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003571 }
3572 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003573 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003574 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3575 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003576 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003577 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003578 }
3579 }
3580 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003581 else {
3582 if (eb_is_empty(&fconn->streams_by_id)) {
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003583 /* If the connection is owned by the session, first remove it
3584 * from its list
3585 */
3586 if (fconn->conn->owner) {
3587 session_unown_conn(fconn->conn->owner, fconn->conn);
3588 fconn->conn->owner = NULL;
3589 }
3590
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003591 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003592 /* The server doesn't want it, let's kill the connection right away */
3593 fconn->conn->mux->destroy(fconn);
3594 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3595 return;
3596 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003597 /* At this point, the connection has been added to the
3598 * server idle list, so another thread may already have
3599 * hijacked it, so we can't do anything with it.
3600 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003601 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3602 return;
3603 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003604 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
Amaury Denoyelle46f041d2020-10-14 18:17:11 +02003605 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target) &&
3606 !LIST_ADDED(&fconn->conn->session_list)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003607 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003608 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003609 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003610 }
3611
3612 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003613 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003614 */
3615 if (fcgi_conn_is_dead(fconn)) {
3616 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003617 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003618 fcgi_release(fconn);
3619 }
3620 else if (fconn->task) {
3621 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3622 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003623 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003624 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003625 else
3626 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003627}
3628
3629
3630/* Performs a synchronous or asynchronous shutr(). */
3631static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3632{
3633 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003634
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003635 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3636
Christopher Faulet99eff652019-08-11 23:11:30 +02003637 if (fstrm->state == FCGI_SS_CLOSED)
3638 goto done;
3639
3640 /* a connstream may require us to immediately kill the whole connection
3641 * for example because of a "tcp-request content reject" rule that is
3642 * normally used to limit abuse.
3643 */
3644 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003645 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3646 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003647 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003648 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003649 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003650 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003651 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3652 !fcgi_strm_send_abort(fconn, fstrm))
3653 goto add_to_list;
3654 }
3655
3656 fcgi_strm_close(fstrm);
3657
3658 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3659 tasklet_wakeup(fconn->wait_event.tasklet);
3660 done:
3661 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003662 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003663 return;
3664
3665 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003666 /* Let the handler know we want to shutr, and add ourselves to the
3667 * send list if not yet done. fcgi_deferred_shut() will be
3668 * automatically called via the shut_tl tasklet when there's room
3669 * again.
3670 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003671 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003672 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003673 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3674 }
3675 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003676 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003677 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003678 return;
3679}
3680
3681/* Performs a synchronous or asynchronous shutw(). */
3682static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3683{
3684 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003685
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003686 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3687
Christopher Faulet99eff652019-08-11 23:11:30 +02003688 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3689 goto done;
3690
3691 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3692 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3693 !fcgi_strm_send_abort(fconn, fstrm))
3694 goto add_to_list;
3695
3696 if (fstrm->state == FCGI_SS_HREM)
3697 fcgi_strm_close(fstrm);
3698 else
3699 fstrm->state = FCGI_SS_HLOC;
3700 } else {
3701 /* a connstream may require us to immediately kill the whole connection
3702 * for example because of a "tcp-request content reject" rule that is
3703 * normally used to limit abuse.
3704 */
3705 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003706 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3707 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003708 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003709 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003710
3711 fcgi_strm_close(fstrm);
3712 }
3713
3714 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3715 tasklet_wakeup(fconn->wait_event.tasklet);
3716 done:
3717 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003718 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003719 return;
3720
3721 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003722 /* Let the handler know we want to shutr, and add ourselves to the
3723 * send list if not yet done. fcgi_deferred_shut() will be
3724 * automatically called via the shut_tl tasklet when there's room
3725 * again.
3726 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003727 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003728 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003729 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3730 }
3731 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003732 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003733 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003734 return;
3735}
3736
Willy Tarreau7aad7032020-01-16 17:20:57 +01003737/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003738 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003739 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003740 */
3741static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3742{
3743 struct fcgi_strm *fstrm = ctx;
3744 struct fcgi_conn *fconn = fstrm->fconn;
3745
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003746 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3747
Willy Tarreau7aad7032020-01-16 17:20:57 +01003748 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3749 /* some data processing remains to be done first */
3750 goto end;
3751 }
3752
Christopher Faulet99eff652019-08-11 23:11:30 +02003753 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3754 fcgi_do_shutw(fstrm);
3755
3756 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3757 fcgi_do_shutr(fstrm);
3758
3759 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3760 /* We're done trying to send, remove ourself from the send_list */
3761 LIST_DEL_INIT(&fstrm->send_list);
3762
3763 if (!fstrm->cs) {
3764 fcgi_strm_destroy(fstrm);
3765 if (fcgi_conn_is_dead(fconn))
3766 fcgi_release(fconn);
3767 }
3768 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003769 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003770 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003771 return NULL;
3772}
3773
3774/* shutr() called by the conn_stream (mux_ops.shutr) */
3775static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3776{
3777 struct fcgi_strm *fstrm = cs->ctx;
3778
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003779 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003780 if (cs->flags & CS_FL_KILL_CONN)
3781 fstrm->flags |= FCGI_SF_KILL_CONN;
3782
3783 if (!mode)
3784 return;
3785
3786 fcgi_do_shutr(fstrm);
3787}
3788
3789/* shutw() called by the conn_stream (mux_ops.shutw) */
3790static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3791{
3792 struct fcgi_strm *fstrm = cs->ctx;
3793
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003794 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003795 if (cs->flags & CS_FL_KILL_CONN)
3796 fstrm->flags |= FCGI_SF_KILL_CONN;
3797
3798 fcgi_do_shutw(fstrm);
3799}
3800
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003801/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3802 * event subscriber <es> is not allowed to change from a previous call as long
3803 * as at least one event is still subscribed. The <event_type> must only be a
3804 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003805 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003806static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003807{
Christopher Faulet99eff652019-08-11 23:11:30 +02003808 struct fcgi_strm *fstrm = cs->ctx;
3809 struct fcgi_conn *fconn = fstrm->fconn;
3810
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003811 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003812 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003813
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003814 es->events |= event_type;
3815 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003816
3817 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003818 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003819
Christopher Faulet99eff652019-08-11 23:11:30 +02003820 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003821 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003822 if (!LIST_ADDED(&fstrm->send_list))
3823 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003824 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 return 0;
3826}
3827
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003828/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3829 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3830 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003831 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003832static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003833{
Christopher Faulet99eff652019-08-11 23:11:30 +02003834 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003835 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003836
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003837 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003838 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003839
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003840 es->events &= ~event_type;
3841 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003842 fstrm->subs = NULL;
3843
3844 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003845 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003846
Christopher Faulet99eff652019-08-11 23:11:30 +02003847 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003848 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003849 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003850 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3851 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003852 }
3853 return 0;
3854}
3855
3856/* Called from the upper layer, to receive data */
3857static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3858{
3859 struct fcgi_strm *fstrm = cs->ctx;
3860 struct fcgi_conn *fconn = fstrm->fconn;
3861 size_t ret = 0;
3862
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003863 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3864
Christopher Faulet99eff652019-08-11 23:11:30 +02003865 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3866 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003867 else
3868 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003869
Christopher Faulet76014fd2019-12-10 11:47:22 +01003870 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 +02003871 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3872 else {
3873 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003874 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003875 cs->flags |= CS_FL_EOI;
3876 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3877 cs->flags |= CS_FL_EOS;
3878 }
Christopher Faulet6670e3e2020-10-08 15:26:33 +02003879 if (fcgi_conn_read0_pending(fconn))
Christopher Faulet99eff652019-08-11 23:11:30 +02003880 cs->flags |= CS_FL_EOS;
3881 if (cs->flags & CS_FL_ERR_PENDING)
3882 cs->flags |= CS_FL_ERROR;
3883 fcgi_release_buf(fconn, &fstrm->rxbuf);
3884 }
3885
3886 if (ret && fconn->dsi == fstrm->id) {
3887 /* demux is blocking on this stream's buffer */
3888 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3889 fcgi_conn_restart_reading(fconn, 1);
3890 }
3891
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003892 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003893 return ret;
3894}
3895
3896
Christopher Faulet99eff652019-08-11 23:11:30 +02003897/* Called from the upper layer, to send data from buffer <buf> for no more than
3898 * <count> bytes. Returns the number of bytes effectively sent. Some status
3899 * flags may be updated on the conn_stream.
3900 */
3901static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3902{
3903 struct fcgi_strm *fstrm = cs->ctx;
3904 struct fcgi_conn *fconn = fstrm->fconn;
3905 size_t total = 0;
3906 size_t ret;
3907 struct htx *htx = NULL;
3908 struct htx_sl *sl;
3909 struct htx_blk *blk;
3910 uint32_t bsize;
3911
Willy Tarreau022e5e52020-09-10 09:33:15 +02003912 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003913
Christopher Faulet99eff652019-08-11 23:11:30 +02003914 /* If we were not just woken because we wanted to send but couldn't,
3915 * and there's somebody else that is waiting to send, do nothing,
3916 * we will subscribe later and be put at the end of the list
3917 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003918 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003919 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 +02003920 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003921 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003922 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003923
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003924 if (fconn->state < FCGI_CS_RECORD_H) {
3925 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003926 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003927 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003928
3929 htx = htxbuf(buf);
3930 if (fstrm->id == 0) {
3931 int32_t id = fcgi_conn_get_next_sid(fconn);
3932
3933 if (id < 0) {
3934 fcgi_strm_close(fstrm);
3935 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003936 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 +02003937 return 0;
3938 }
3939
3940 eb32_delete(&fstrm->by_id);
3941 fstrm->by_id.key = fstrm->id = id;
3942 fconn->max_id = id;
3943 fconn->nb_reserved--;
3944 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3945
3946
3947 /* Check if length of the body is known or if the message is
3948 * full. Otherwise, the request is invalid.
3949 */
3950 sl = http_get_stline(htx);
3951 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3952 htx->flags |= HTX_FL_PARSING_ERROR;
3953 fcgi_strm_error(fstrm);
3954 goto done;
3955 }
3956 }
3957
3958 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003959 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 +02003960 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3961 goto done;
3962 }
3963
3964 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3965 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3966
Christopher Fauletfe410d62020-05-19 15:13:00 +02003967 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003968 count && !htx_is_empty(htx)) {
3969 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003970 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003971 bsize = htx_get_blksz(blk);
3972
3973 switch (htx_get_blk_type(blk)) {
3974 case HTX_BLK_REQ_SL:
3975 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003976 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 +02003977 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3978 if (!ret) {
3979 goto done;
3980 }
3981 total += ret;
3982 count -= ret;
3983 break;
3984
3985 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003986 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 +02003987 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3988 if (!ret)
3989 goto done;
3990 goto remove_blk;
3991
3992 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003993 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 +02003994 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3995 if (ret > 0) {
3996 htx = htx_from_buf(buf);
3997 total += ret;
3998 count -= ret;
3999 if (ret < bsize)
4000 goto done;
4001 }
4002 break;
4003
4004 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004005 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 +02004006 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
4007 if (!ret)
4008 goto done;
4009 goto remove_blk;
4010
4011 default:
4012 remove_blk:
4013 htx_remove_blk(htx, blk);
4014 total += bsize;
4015 count -= bsize;
4016 break;
4017 }
4018 }
4019
4020 done:
4021 if (fstrm->state >= FCGI_SS_HLOC) {
4022 /* trim any possibly pending data after we close (extra CR-LF,
4023 * unprocessed trailers, abnormal extra data, ...)
4024 */
4025 total += count;
4026 count = 0;
4027 }
4028
4029 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004030 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 +02004031 cs_set_error(cs);
4032 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4033 fcgi_strm_close(fstrm);
4034 }
4035
4036 if (htx)
4037 htx_to_buf(htx, buf);
4038
Christopher Faulet99eff652019-08-11 23:11:30 +02004039 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004040 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4041 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 +02004042 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004043 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004044
4045 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004046 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4047 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004048 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004049
4050 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004051 return total;
4052}
4053
4054/* for debugging with CLI's "show fd" command */
4055static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
4056{
4057 struct fcgi_conn *fconn = conn->ctx;
4058 struct fcgi_strm *fstrm = NULL;
4059 struct eb32_node *node;
4060 int send_cnt = 0;
4061 int tree_cnt = 0;
4062 int orph_cnt = 0;
4063 struct buffer *hmbuf, *tmbuf;
4064
4065 if (!fconn)
4066 return;
4067
4068 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4069 send_cnt++;
4070
4071 fstrm = NULL;
4072 node = eb32_first(&fconn->streams_by_id);
4073 while (node) {
4074 fstrm = container_of(node, struct fcgi_strm, by_id);
4075 tree_cnt++;
4076 if (!fstrm->cs)
4077 orph_cnt++;
4078 node = eb32_next(node);
4079 }
4080
4081 hmbuf = br_head(fconn->mbuf);
4082 tmbuf = br_tail(fconn->mbuf);
4083 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4084 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4085 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4086 fconn->state, fconn->max_id, fconn->flags,
4087 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4088 fconn->wait_event.events, fconn->dsi,
4089 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4090 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4091 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4092 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4093 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4094 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4095 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4096
4097 if (fstrm) {
4098 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4099 fstrm, fstrm->id, fstrm->flags,
4100 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4101 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4102 fstrm->cs);
4103 if (fstrm->cs)
4104 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4105 fstrm->cs->flags, fstrm->cs->data);
4106 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004107}
4108
4109/* Migrate the the connection to the current thread.
4110 * Return 0 if successful, non-zero otherwise.
4111 * Expected to be called with the old thread lock held.
4112 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004113static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004114{
4115 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004116 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004117
4118 if (fd_takeover(conn->handle.fd, conn) != 0)
4119 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004120
4121 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4122 /* We failed to takeover the xprt, even if the connection may
4123 * still be valid, flag it as error'd, as we have already
4124 * taken over the fd, and wake the tasklet, so that it will
4125 * destroy it.
4126 */
4127 conn->flags |= CO_FL_ERROR;
4128 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4129 return -1;
4130 }
4131
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004132 if (fcgi->wait_event.events)
4133 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4134 fcgi->wait_event.events, &fcgi->wait_event);
4135 /* To let the tasklet know it should free itself, and do nothing else,
4136 * set its context to NULL;
4137 */
4138 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004139 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004140
4141 task = fcgi->task;
4142 if (task) {
4143 task->context = NULL;
4144 fcgi->task = NULL;
4145 __ha_barrier_store();
4146 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004147
4148 fcgi->task = task_new(tid_bit);
4149 if (!fcgi->task) {
4150 fcgi_release(fcgi);
4151 return -1;
4152 }
4153 fcgi->task->process = fcgi_timeout_task;
4154 fcgi->task->context = fcgi;
4155 }
4156 fcgi->wait_event.tasklet = tasklet_new();
4157 if (!fcgi->wait_event.tasklet) {
4158 fcgi_release(fcgi);
4159 return -1;
4160 }
4161 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4162 fcgi->wait_event.tasklet->context = fcgi;
4163 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4164 SUB_RETRY_RECV, &fcgi->wait_event);
4165
4166 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004167}
4168
4169/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004170/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004171/****************************************/
4172
4173/* The mux operations */
4174static const struct mux_ops mux_fcgi_ops = {
4175 .init = fcgi_init,
4176 .wake = fcgi_wake,
4177 .attach = fcgi_attach,
4178 .get_first_cs = fcgi_get_first_cs,
4179 .detach = fcgi_detach,
4180 .destroy = fcgi_destroy,
4181 .avail_streams = fcgi_avail_streams,
4182 .used_streams = fcgi_used_streams,
4183 .rcv_buf = fcgi_rcv_buf,
4184 .snd_buf = fcgi_snd_buf,
4185 .subscribe = fcgi_subscribe,
4186 .unsubscribe = fcgi_unsubscribe,
4187 .shutr = fcgi_shutr,
4188 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004189 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004190 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004191 .takeover = fcgi_takeover,
Amaury Denoyelle3d3c0912020-10-14 18:17:06 +02004192 .flags = MX_FL_HTX|MX_FL_HOL_RISK,
Christopher Faulet99eff652019-08-11 23:11:30 +02004193 .name = "FCGI",
4194};
4195
4196
4197/* this mux registers FCGI proto */
4198static struct mux_proto_list mux_proto_fcgi =
4199{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4200
4201INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4202
4203/*
4204 * Local variables:
4205 * c-indent-level: 8
4206 * c-basic-offset: 8
4207 * End:
4208 */