blob: 7be05b6e863e6db5c9149417ad872573bae88d6f [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
872
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500873/* Returns true if the FCGI connection must be release */
Christopher Faulet99eff652019-08-11 23:11:30 +0200874static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
875{
876 if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
877 (!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
878 (fconn->conn->flags & CO_FL_ERROR) || /* errors close immediately */
879 (fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
880 (!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
881 (!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
882 conn_xprt_read0_pending(fconn->conn))))
883 return 1;
884 return 0;
885}
886
887
888/********************************************************/
889/* functions below are for the FCGI protocol processing */
890/********************************************************/
891
Christopher Faulet99eff652019-08-11 23:11:30 +0200892/* Marks an error on the stream. */
893static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
894{
895 if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200896 TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
897 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200898 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200899 TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
900 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200901 if (fstrm->cs)
902 cs_set_error(fstrm->cs);
903 }
904}
905
906/* Attempts to notify the data layer of recv availability */
907static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
908{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100909 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200910 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100911 tasklet_wakeup(fstrm->subs->tasklet);
912 fstrm->subs->events &= ~SUB_RETRY_RECV;
913 if (!fstrm->subs->events)
914 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200915 }
916}
917
918/* Attempts to notify the data layer of send availability */
919static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
920{
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100921 if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200922 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreauf11be0e2020-01-16 16:59:45 +0100923 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100924 tasklet_wakeup(fstrm->subs->tasklet);
925 fstrm->subs->events &= ~SUB_RETRY_SEND;
926 if (!fstrm->subs->events)
927 fstrm->subs = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +0200928 }
Willy Tarreau7aad7032020-01-16 17:20:57 +0100929 else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
930 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
931 tasklet_wakeup(fstrm->shut_tl);
932 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200933}
934
935/* Alerts the data layer, trying to wake it up by all means, following
936 * this sequence :
937 * - if the fcgi stream' data layer is subscribed to recv, then it's woken up
938 * for recv
939 * - if its subscribed to send, then it's woken up for send
940 * - if it was subscribed to neither, its ->wake() callback is called
941 * It is safe to call this function with a closed stream which doesn't have a
942 * conn_stream anymore.
943 */
944static void fcgi_strm_alert(struct fcgi_strm *fstrm)
945{
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200946 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +0100947 if (fstrm->subs ||
Willy Tarreau7aad7032020-01-16 17:20:57 +0100948 (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
Christopher Faulet99eff652019-08-11 23:11:30 +0200949 fcgi_strm_notify_recv(fstrm);
950 fcgi_strm_notify_send(fstrm);
951 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200952 else if (fstrm->cs && fstrm->cs->data_cb->wake != NULL) {
953 TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200954 fstrm->cs->data_cb->wake(fstrm->cs);
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200955 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200956}
957
958/* Writes the 16-bit record size <len> at address <record> */
959static inline void fcgi_set_record_size(void *record, uint16_t len)
960{
961 uint8_t *out = (record + 4);
962
963 *out = (len >> 8);
964 *(out + 1) = (len & 0xff);
965}
966
967/* Writes the 16-bit stream id <id> at address <record> */
968static inline void fcgi_set_record_id(void *record, uint16_t id)
969{
970 uint8_t *out = (record + 2);
971
972 *out = (id >> 8);
973 *(out + 1) = (id & 0xff);
974}
975
976/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
977 * its connection if the stream was not yet closed. Please use this exclusively
978 * before closing a stream to ensure stream count is well maintained.
979 */
980static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
981{
982 if (fstrm->state != FCGI_SS_CLOSED) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200983 TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200984 fstrm->fconn->nb_streams--;
985 if (!fstrm->id)
986 fstrm->fconn->nb_reserved--;
987 if (fstrm->cs) {
988 if (!(fstrm->cs->flags & CS_FL_EOS) && !b_data(&fstrm->rxbuf))
989 fcgi_strm_notify_recv(fstrm);
990 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +0200991 fstrm->state = FCGI_SS_CLOSED;
992 TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
993 TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +0200994 }
Christopher Faulet99eff652019-08-11 23:11:30 +0200995}
996
997/* Detaches a FCGI stream from its FCGI connection and releases it to the
998 * fcgi_strm pool.
999 */
1000static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
1001{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001002 struct connection *conn = fstrm->fconn->conn;
1003
1004 TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
1005
Christopher Faulet99eff652019-08-11 23:11:30 +02001006 fcgi_strm_close(fstrm);
1007 eb32_delete(&fstrm->by_id);
1008 if (b_size(&fstrm->rxbuf)) {
1009 b_free(&fstrm->rxbuf);
1010 offer_buffers(NULL, tasks_run_queue);
1011 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001012 if (fstrm->subs)
1013 fstrm->subs->events = 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001014 /* There's no need to explicitly call unsubscribe here, the only
1015 * reference left would be in the fconn send_list/fctl_list, and if
1016 * we're in it, we're getting out anyway
1017 */
1018 LIST_DEL_INIT(&fstrm->send_list);
Willy Tarreau7aad7032020-01-16 17:20:57 +01001019 tasklet_free(fstrm->shut_tl);
Christopher Faulet99eff652019-08-11 23:11:30 +02001020 pool_free(pool_head_fcgi_strm, fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001021
1022 TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001023}
1024
1025/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
1026 * stream tree. In case of error, nothing is added and NULL is returned. The
1027 * causes of errors can be any failed memory allocation. The caller is
1028 * responsible for checking if the connection may support an extra stream prior
1029 * to calling this function.
1030 */
1031static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
1032{
1033 struct fcgi_strm *fstrm;
1034
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001035 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1036
Christopher Faulet99eff652019-08-11 23:11:30 +02001037 fstrm = pool_alloc(pool_head_fcgi_strm);
1038 if (!fstrm)
1039 goto out;
1040
Willy Tarreau7aad7032020-01-16 17:20:57 +01001041 fstrm->shut_tl = tasklet_new();
1042 if (!fstrm->shut_tl) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001043 pool_free(pool_head_fcgi_strm, fstrm);
1044 goto out;
1045 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01001046 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01001047 fstrm->shut_tl->process = fcgi_deferred_shut;
1048 fstrm->shut_tl->context = fstrm;
Christopher Faulet99eff652019-08-11 23:11:30 +02001049 LIST_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02001050 fstrm->fconn = fconn;
1051 fstrm->cs = NULL;
1052 fstrm->flags = FCGI_SF_NONE;
1053 fstrm->proto_status = 0;
1054 fstrm->state = FCGI_SS_IDLE;
1055 fstrm->rxbuf = BUF_NULL;
1056
1057 h1m_init_res(&fstrm->h1m);
1058 fstrm->h1m.err_pos = -1; // don't care about errors on the request path
1059 fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
1060
1061 fstrm->by_id.key = fstrm->id = id;
1062 if (id > 0)
1063 fconn->max_id = id;
1064 else
1065 fconn->nb_reserved++;
1066
1067 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
1068 fconn->nb_streams++;
1069 fconn->stream_cnt++;
1070
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001071 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001072 return fstrm;
1073
1074 out:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001075 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 +02001076 return NULL;
1077}
1078
1079/* Allocates a new stream associated to conn_stream <cs> on the FCGI connection
1080 * <fconn> and returns it, or NULL in case of memory allocation error or if the
1081 * highest possible stream ID was reached.
1082 */
1083static struct fcgi_strm *fcgi_conn_stream_new(struct fcgi_conn *fconn, struct conn_stream *cs,
1084 struct session *sess)
1085{
1086 struct fcgi_strm *fstrm = NULL;
1087
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001088 TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
1089 if (fconn->nb_streams >= fconn->streams_limit) {
1090 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 +02001091 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001092 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001093
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001094 if (fcgi_streams_left(fconn) < 1) {
1095 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 +02001096 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001097 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001098
1099 /* Defer choosing the ID until we send the first message to create the stream */
1100 fstrm = fcgi_strm_new(fconn, 0);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001101 if (!fstrm) {
1102 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 +02001103 goto out;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001104 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001105
1106 fstrm->cs = cs;
1107 fstrm->sess = sess;
1108 cs->ctx = fstrm;
1109 fconn->nb_cs++;
1110
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001111 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001112 return fstrm;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001113
1114 out:
1115 return NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02001116}
1117
1118/* Wakes a specific stream and assign its conn_stream some CS_FL_* flags among
1119 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state is
1120 * automatically updated accordingly. If the stream is orphaned, it is
1121 * destroyed.
1122 */
1123static void fcgi_strm_wake_one_stream(struct fcgi_strm *fstrm)
1124{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001125 struct fcgi_conn *fconn = fstrm->fconn;
1126
1127 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
1128
Christopher Faulet99eff652019-08-11 23:11:30 +02001129 if (!fstrm->cs) {
1130 /* this stream was already orphaned */
1131 fcgi_strm_destroy(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001132 TRACE_DEVEL("leaving with no fstrm", FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001133 return;
1134 }
1135
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001136 if (conn_xprt_read0_pending(fconn->conn)) {
1137 if (fstrm->state == FCGI_SS_OPEN) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001138 fstrm->state = FCGI_SS_HREM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001139 TRACE_STATE("swtiching to HREM", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1140 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001141 else if (fstrm->state == FCGI_SS_HLOC)
1142 fcgi_strm_close(fstrm);
1143 }
1144
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001145 if ((fconn->state == FCGI_CS_CLOSED || fconn->conn->flags & CO_FL_ERROR)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001146 fstrm->cs->flags |= CS_FL_ERR_PENDING;
1147 if (fstrm->cs->flags & CS_FL_EOS)
1148 fstrm->cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001149
1150 if (fstrm->state < FCGI_SS_ERROR) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001151 fstrm->state = FCGI_SS_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001152 TRACE_STATE("switching to ERROR", FCGI_EV_STRM_WAKE|FCGI_EV_FSTRM_END, fconn->conn, fstrm);
1153 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001154 }
1155
1156 fcgi_strm_alert(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001157
1158 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001159}
1160
1161/* Wakes unassigned streams (ID == 0) attached to the connection. */
1162static void fcgi_wake_unassigned_streams(struct fcgi_conn *fconn)
1163{
1164 struct eb32_node *node;
1165 struct fcgi_strm *fstrm;
1166
1167 node = eb32_lookup(&fconn->streams_by_id, 0);
1168 while (node) {
1169 fstrm = container_of(node, struct fcgi_strm, by_id);
1170 if (fstrm->id > 0)
1171 break;
1172 node = eb32_next(node);
1173 fcgi_strm_wake_one_stream(fstrm);
1174 }
1175}
1176
1177/* Wakes the streams attached to the connection, whose id is greater than <last>
1178 * or unassigned.
1179 */
1180static void fcgi_wake_some_streams(struct fcgi_conn *fconn, int last)
1181{
1182 struct eb32_node *node;
1183 struct fcgi_strm *fstrm;
1184
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001185 TRACE_ENTER(FCGI_EV_STRM_WAKE, fconn->conn);
1186
Christopher Faulet99eff652019-08-11 23:11:30 +02001187 /* Wake all streams with ID > last */
1188 node = eb32_lookup_ge(&fconn->streams_by_id, last + 1);
1189 while (node) {
1190 fstrm = container_of(node, struct fcgi_strm, by_id);
1191 node = eb32_next(node);
1192 fcgi_strm_wake_one_stream(fstrm);
1193 }
1194 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001195
1196 TRACE_LEAVE(FCGI_EV_STRM_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001197}
1198
1199static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1200 struct htx *htx, struct htx_sl *sl,
1201 struct fcgi_strm_params *params)
1202{
1203 struct connection *cli_conn = objt_conn(fstrm->sess->origin);
1204 struct ist p;
1205
1206 if (!sl)
1207 goto error;
1208
1209 if (!(params->mask & FCGI_SP_DOC_ROOT))
1210 params->docroot = fconn->app->docroot;
1211
1212 if (!(params->mask & FCGI_SP_REQ_METH)) {
1213 p = htx_sl_req_meth(sl);
1214 params->meth = ist2(b_tail(params->p), p.len);
1215 chunk_memcat(params->p, p.ptr, p.len);
1216 }
1217 if (!(params->mask & FCGI_SP_REQ_URI)) {
1218 p = htx_sl_req_uri(sl);
1219 params->uri = ist2(b_tail(params->p), p.len);
1220 chunk_memcat(params->p, p.ptr, p.len);
1221 }
1222 if (!(params->mask & FCGI_SP_SRV_PROTO)) {
1223 p = htx_sl_req_vsn(sl);
1224 params->vsn = ist2(b_tail(params->p), p.len);
1225 chunk_memcat(params->p, p.ptr, p.len);
1226 }
1227 if (!(params->mask & FCGI_SP_SRV_PORT)) {
1228 char *end;
1229 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001230 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001231 port = get_host_port(cli_conn->dst);
1232 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1233 if (!end)
1234 goto error;
1235 params->srv_port = ist2(b_tail(params->p), end - b_tail(params->p));
1236 params->p->data += params->srv_port.len;
1237 }
1238 if (!(params->mask & FCGI_SP_SRV_NAME)) {
1239 /* If no Host header found, use the server address to fill
1240 * srv_name */
1241 if (!istlen(params->srv_name)) {
1242 char *ptr = NULL;
1243
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001244 if (cli_conn && conn_get_dst(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001245 if (addr_to_str(cli_conn->dst, b_tail(params->p), b_room(params->p)) != -1)
1246 ptr = b_tail(params->p);
1247 if (ptr) {
1248 params->srv_name = ist2(ptr, strlen(ptr));
1249 params->p->data += params->srv_name.len;
1250 }
1251 }
1252 }
1253 if (!(params->mask & FCGI_SP_REM_ADDR)) {
1254 char *ptr = NULL;
1255
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001256 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001257 if (addr_to_str(cli_conn->src, b_tail(params->p), b_room(params->p)) != -1)
1258 ptr = b_tail(params->p);
1259 if (ptr) {
1260 params->rem_addr = ist2(ptr, strlen(ptr));
1261 params->p->data += params->rem_addr.len;
1262 }
1263 }
1264 if (!(params->mask & FCGI_SP_REM_PORT)) {
1265 char *end;
1266 int port = 0;
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001267 if (cli_conn && conn_get_src(cli_conn))
Christopher Faulet99eff652019-08-11 23:11:30 +02001268 port = get_host_port(cli_conn->src);
1269 end = ultoa_o(port, b_tail(params->p), b_room(params->p));
1270 if (!end)
1271 goto error;
1272 params->rem_port = ist2(b_tail(params->p), end - b_tail(params->p));
1273 params->p->data += params->rem_port.len;
1274 }
1275 if (!(params->mask & FCGI_SP_CONT_LEN)) {
1276 struct htx_blk *blk;
1277 enum htx_blk_type type;
1278 char *end;
1279 size_t len = 0;
1280
1281 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1282 type = htx_get_blk_type(blk);
1283
1284 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1285 break;
1286 if (type == HTX_BLK_DATA)
1287 len += htx_get_blksz(blk);
1288 }
1289 end = ultoa_o(len, b_tail(params->p), b_room(params->p));
1290 if (!end)
1291 goto error;
1292 params->cont_len = ist2(b_tail(params->p), end - b_tail(params->p));
1293 params->p->data += params->cont_len.len;
1294 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001295#ifdef USE_OPENSSL
Christopher Faulet99eff652019-08-11 23:11:30 +02001296 if (!(params->mask & FCGI_SP_HTTPS)) {
Christopher Fauletbb86a0f2020-04-24 07:19:04 +02001297 if (cli_conn)
1298 params->https = ssl_sock_is_ssl(cli_conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001299 }
Christopher Fauletd66700a2019-09-17 13:46:47 +02001300#endif
Christopher Faulet99eff652019-08-11 23:11:30 +02001301 if ((params->mask & FCGI_SP_URI_MASK) != FCGI_SP_URI_MASK) {
1302 /* one of scriptname, pathinfo or query_string is no set */
1303 struct ist path = http_get_path(params->uri);
1304 int len;
1305
Christopher Faulet99eff652019-08-11 23:11:30 +02001306 /* No scrit_name set but no valid path ==> error */
1307 if (!(params->mask & FCGI_SP_SCRIPT_NAME) && !istlen(path))
1308 goto error;
1309
Christopher Faulet99eff652019-08-11 23:11:30 +02001310 /* If there is a query-string, Set it if not already set */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001311 if (!(params->mask & FCGI_SP_REQ_QS)) {
1312 struct ist qs = istfind(path, '?');
1313
1314 /* Update the path length */
1315 path.len -= qs.len;
1316
1317 /* Set the query-string skipping the '?', if any */
1318 if (istlen(qs))
1319 params->qs = istnext(qs);
1320 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001321
1322 /* If the script_name is set, don't try to deduce the path_info
1323 * too. The opposite is not true.
1324 */
1325 if (params->mask & FCGI_SP_SCRIPT_NAME) {
1326 params->mask |= FCGI_SP_PATH_INFO;
1327 goto end;
1328 }
1329
Christopher Faulet0f17a442020-07-23 15:44:37 +02001330 /* Decode the path. it must first be copied to keep the URI
1331 * untouched.
1332 */
1333 chunk_memcat(params->p, path.ptr, path.len);
1334 path.ptr = b_tail(params->p) - path.len;
1335 len = url_decode(ist0(path), 0);
1336 if (len < 0)
1337 goto error;
1338 path.len = len;
1339
Christopher Faulet99eff652019-08-11 23:11:30 +02001340 /* script_name not set, preset it with the path for now */
Christopher Faulet0f17a442020-07-23 15:44:37 +02001341 params->scriptname = path;
Christopher Faulet99eff652019-08-11 23:11:30 +02001342
1343 /* If there is no regex to match the pathinfo, just to the last
1344 * part and see if the index must be used.
1345 */
1346 if (!fconn->app->pathinfo_re)
1347 goto check_index;
1348
Christopher Faulet28cb3662020-02-14 14:47:37 +01001349 /* If some special characters are found in the decoded path (\n
1350 * or \0), the PATH_INFO regex cannot match. This is theorically
1351 * valid, but probably unexpected, to have such characters. So,
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001352 * to avoid any surprises, an error is triggered in this
Christopher Faulet28cb3662020-02-14 14:47:37 +01001353 * case.
1354 */
1355 if (istchr(path, '\n') || istchr(path, '\0'))
1356 goto error;
1357
Christopher Faulet99eff652019-08-11 23:11:30 +02001358 /* The regex does not match, just to the last part and see if
1359 * the index must be used.
1360 */
1361 if (!regex_exec_match2(fconn->app->pathinfo_re, path.ptr, len, MAX_MATCH, pmatch, 0))
1362 goto check_index;
1363
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001364 /* We must have at least 1 capture for the script name,
1365 * otherwise we do nothing and jump to the last part.
Christopher Faulet99eff652019-08-11 23:11:30 +02001366 */
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001367 if (pmatch[1].rm_so == -1 || pmatch[1].rm_eo == -1)
Christopher Faulet99eff652019-08-11 23:11:30 +02001368 goto check_index;
1369
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001370 /* Finally we can set the script_name and the path_info. The
1371 * path_info is set if not already defined, and if it was
1372 * captured
1373 */
Christopher Faulet99eff652019-08-11 23:11:30 +02001374 params->scriptname = ist2(path.ptr + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
Christopher Faulet6c57f2d2020-02-14 16:55:52 +01001375 if (!(params->mask & FCGI_SP_PATH_INFO) && (pmatch[2].rm_so == -1 || pmatch[2].rm_eo == -1))
1376 params->pathinfo = ist2(path.ptr + pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so);
Christopher Faulet99eff652019-08-11 23:11:30 +02001377
1378 check_index:
1379 len = params->scriptname.len;
1380 /* the script_name if finished by a '/' so we can add the index
1381 * part, if any.
1382 */
1383 if (istlen(fconn->app->index) && params->scriptname.ptr[len-1] == '/') {
1384 struct ist sn = params->scriptname;
1385
1386 params->scriptname = ist2(b_tail(params->p), len+fconn->app->index.len);
1387 chunk_memcat(params->p, sn.ptr, sn.len);
1388 chunk_memcat(params->p, fconn->app->index.ptr, fconn->app->index.len);
1389 }
1390 }
1391
1392 end:
1393 return 1;
1394 error:
1395 return 0;
1396}
1397
1398static int fcgi_encode_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1399 struct fcgi_strm_params *params, struct buffer *outbuf, int flag)
1400{
1401 struct fcgi_param p;
1402
1403 if (params->mask & flag)
1404 return 1;
1405
1406 chunk_reset(&trash);
1407
1408 switch (flag) {
1409 case FCGI_SP_CGI_GATEWAY:
1410 p.n = ist("GATEWAY_INTERFACE");
1411 p.v = ist("CGI/1.1");
1412 goto encode;
1413 case FCGI_SP_DOC_ROOT:
1414 p.n = ist("DOCUMENT_ROOT");
1415 p.v = params->docroot;
1416 goto encode;
1417 case FCGI_SP_SCRIPT_NAME:
1418 p.n = ist("SCRIPT_NAME");
1419 p.v = params->scriptname;
1420 goto encode;
1421 case FCGI_SP_PATH_INFO:
1422 p.n = ist("PATH_INFO");
1423 p.v = params->pathinfo;
1424 goto encode;
1425 case FCGI_SP_REQ_URI:
1426 p.n = ist("REQUEST_URI");
1427 p.v = params->uri;
1428 goto encode;
1429 case FCGI_SP_REQ_METH:
1430 p.n = ist("REQUEST_METHOD");
1431 p.v = params->meth;
1432 goto encode;
1433 case FCGI_SP_REQ_QS:
1434 p.n = ist("QUERY_STRING");
1435 p.v = params->qs;
1436 goto encode;
1437 case FCGI_SP_SRV_NAME:
1438 p.n = ist("SERVER_NAME");
1439 p.v = params->srv_name;
1440 goto encode;
1441 case FCGI_SP_SRV_PORT:
1442 p.n = ist("SERVER_PORT");
1443 p.v = params->srv_port;
1444 goto encode;
1445 case FCGI_SP_SRV_PROTO:
1446 p.n = ist("SERVER_PROTOCOL");
1447 p.v = params->vsn;
1448 goto encode;
1449 case FCGI_SP_REM_ADDR:
1450 p.n = ist("REMOTE_ADDR");
1451 p.v = params->rem_addr;
1452 goto encode;
1453 case FCGI_SP_REM_PORT:
1454 p.n = ist("REMOTE_PORT");
1455 p.v = params->rem_port;
1456 goto encode;
1457 case FCGI_SP_SCRIPT_FILE:
1458 p.n = ist("SCRIPT_FILENAME");
1459 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1460 chunk_memcat(&trash, params->scriptname.ptr, params->scriptname.len);
1461 p.v = ist2(b_head(&trash), b_data(&trash));
1462 goto encode;
1463 case FCGI_SP_PATH_TRANS:
1464 if (!istlen(params->pathinfo))
1465 goto skip;
1466 p.n = ist("PATH_TRANSLATED");
1467 chunk_memcat(&trash, params->docroot.ptr, params->docroot.len);
1468 chunk_memcat(&trash, params->pathinfo.ptr, params->pathinfo.len);
1469 p.v = ist2(b_head(&trash), b_data(&trash));
1470 goto encode;
1471 case FCGI_SP_CONT_LEN:
1472 p.n = ist("CONTENT_LENGTH");
1473 p.v = params->cont_len;
1474 goto encode;
1475 case FCGI_SP_HTTPS:
1476 if (!params->https)
1477 goto skip;
1478 p.n = ist("HTTPS");
1479 p.v = ist("on");
1480 goto encode;
1481 default:
1482 goto skip;
1483 }
1484
1485 encode:
1486 if (!istlen(p.v))
1487 goto skip;
1488 if (!fcgi_encode_param(outbuf, &p))
1489 return 0;
1490 skip:
1491 params->mask |= flag;
1492 return 1;
1493}
1494
1495/* Sends a GET_VALUES record. Returns > 0 on success, 0 if it couldn't do
1496 * anything. It is highly unexpected, but if the record is larger than a buffer
1497 * and cannot be encoded in one time, an error is triggered and the connection is
1498 * closed. GET_VALUES record cannot be split.
1499 */
1500static int fcgi_conn_send_get_values(struct fcgi_conn *fconn)
1501{
1502 struct buffer outbuf;
1503 struct buffer *mbuf;
1504 struct fcgi_param max_reqs = { .n = ist("FCGI_MAX_REQS"), .v = ist("")};
1505 struct fcgi_param mpxs_conns = { .n = ist("FCGI_MPXS_CONNS"), .v = ist("")};
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001506 int ret = 0;
1507
1508 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001509
1510 mbuf = br_tail(fconn->mbuf);
1511 retry:
1512 if (!fcgi_get_buf(fconn, mbuf)) {
1513 fconn->flags |= FCGI_CF_MUX_MALLOC;
1514 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001515 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
1516 ret = 0;
1517 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001518 }
1519
1520 while (1) {
1521 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1522 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1523 break;
1524 realign_again:
1525 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1526 }
1527
1528 if (outbuf.size < 8)
1529 goto full;
1530
1531 /* vsn: 1(FCGI_VERSION), type: (9)FCGI_GET_VALUES, id: 0x0000,
1532 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1533 memcpy(outbuf.area, "\x01\x09\x00\x00\x00\x00\x00\x00", 8);
1534 outbuf.data = 8;
1535
1536 /* Note: Don't send the param FCGI_MAX_CONNS because its value cannot be
1537 * handled by HAProxy.
1538 */
1539 if (!fcgi_encode_param(&outbuf, &max_reqs) || !fcgi_encode_param(&outbuf, &mpxs_conns))
1540 goto full;
1541
1542 /* update the record's size now */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001543 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 +02001544 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
1545 b_add(mbuf, outbuf.data);
1546 ret = 1;
1547
1548 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001549 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001550 return ret;
1551 full:
1552 /* Too large to be encoded. For GET_VALUES records, it is an error */
1553 if (!b_data(mbuf))
1554 goto fail;
1555
1556 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1557 goto retry;
1558 fconn->flags |= FCGI_CF_MUX_MFULL;
1559 fconn->flags |= FCGI_CF_DEM_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001560 TRACE_STATE("mbuf ring full", FCGI_EV_TX_RECORD|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001561 ret = 0;
1562 goto end;
1563 fail:
1564 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001565 TRACE_STATE("switching to CLOSED", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_END, fconn->conn);
1566 TRACE_DEVEL("leaving on error", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL|FCGI_EV_FCONN_ERR, fconn->conn);
1567 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02001568}
1569
1570/* Processes a GET_VALUES_RESULT record. Returns > 0 on success, 0 if it
1571 * couldn't do anything. It is highly unexpected, but if the record is larger
1572 * than a buffer and cannot be decoded in one time, an error is triggered and
1573 * the connection is closed. GET_VALUES_RESULT record cannot be split.
1574 */
1575static int fcgi_conn_handle_values_result(struct fcgi_conn *fconn)
1576{
1577 struct buffer inbuf;
1578 struct buffer *dbuf;
1579 size_t offset;
1580
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001581 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1582
Christopher Faulet99eff652019-08-11 23:11:30 +02001583 dbuf = &fconn->dbuf;
1584
1585 /* Record too large to be fully decoded */
1586 if (b_size(dbuf) < (fconn->drl + fconn->drp))
1587 goto fail;
1588
1589 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001590 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
1591 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001592 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001593 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001594
1595 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
1596 /* Realign the dmux buffer if the record wraps. It is unexpected
1597 * at this stage because it should be the first record received
1598 * from the FCGI application.
1599 */
1600 b_slow_realign(dbuf, trash.area, 0);
1601 }
1602
1603 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
1604
1605 for (offset = 0; offset < b_data(&inbuf); ) {
1606 struct fcgi_param p;
1607 size_t ret;
1608
1609 ret = fcgi_aligned_decode_param(&inbuf, offset, &p);
1610 if (!ret) {
1611 /* name or value too large to be decoded at once */
1612 goto fail;
1613 }
1614 offset += ret;
1615
1616 if (isteqi(p.n, ist("FCGI_MPXS_CONNS"))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001617 if (isteq(p.v, ist("1"))) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001618 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 +02001619 fconn->flags |= FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001620 }
1621 else {
Willy Tarreau022e5e52020-09-10 09:33:15 +02001622 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 +02001623 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001624 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001625 }
1626 else if (isteqi(p.n, ist("FCGI_MAX_REQS"))) {
1627 fconn->streams_limit = strl2ui(p.v.ptr, p.v.len);
Willy Tarreau022e5e52020-09-10 09:33:15 +02001628 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 +02001629 }
1630 /*
1631 * Ignore all other params
1632 */
1633 }
1634
1635 /* Reset the number of concurrent streams supported if the FCGI
1636 * application does not support connection multiplexing
1637 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001638 if (!(fconn->flags & FCGI_CF_MPXS_CONNS)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001639 fconn->streams_limit = 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001640 TRACE_STATE("no mpxs for streams_limit to 1", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1641 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001642
1643 /* We must be sure to have read exactly the announced record length, no
1644 * more no less
1645 */
1646 if (offset != fconn->drl)
1647 goto fail;
1648
Willy Tarreau022e5e52020-09-10 09:33:15 +02001649 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 +02001650 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
1651 fconn->drl = 0;
1652 fconn->drp = 0;
1653 fconn->state = FCGI_CS_RECORD_H;
1654 fcgi_wake_unassigned_streams(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001655 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
1656 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001657 return 1;
1658 fail:
1659 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001660 TRACE_STATE("switching to CLOSED", FCGI_EV_RX_RECORD|FCGI_EV_RX_GETVAL, fconn->conn);
1661 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 +02001662 return 0;
1663}
1664
1665/* Sends an ABORT_REQUEST record for each active streams. Closed streams are
1666 * excluded, as the streams which already received the end-of-stream. It returns
1667 * > 0 if the record was sent tp all streams. Otherwise it returns 0.
1668 */
1669static int fcgi_conn_send_aborts(struct fcgi_conn *fconn)
1670{
1671 struct eb32_node *node;
1672 struct fcgi_strm *fstrm;
1673
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001674 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn);
1675
Christopher Faulet99eff652019-08-11 23:11:30 +02001676 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
1677 while (node) {
1678 fstrm = container_of(node, struct fcgi_strm, by_id);
1679 node = eb32_next(node);
1680 if (fstrm->state != FCGI_SS_CLOSED &&
1681 !(fstrm->flags & (FCGI_SF_ES_RCVD|FCGI_SF_ABRT_SENT)) &&
1682 !fcgi_strm_send_abort(fconn, fstrm))
1683 return 0;
1684 }
1685 fconn->flags |= FCGI_CF_ABRTS_SENT;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001686 TRACE_STATE("aborts sent to all fstrms", FCGI_EV_TX_RECORD, fconn->conn);
1687 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02001688 return 1;
1689}
1690
1691/* Sends a BEGIN_REQUEST record. It returns > 0 on success, 0 if it couldn't do
1692 * anything. BEGIN_REQUEST record cannot be split. So we wait to have enough
1693 * space to proceed. It is small enough to be encoded in an empty buffer.
1694 */
1695static int fcgi_strm_send_begin_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1696{
1697 struct buffer outbuf;
1698 struct buffer *mbuf;
1699 struct fcgi_begin_request rec = { .role = FCGI_RESPONDER, .flags = 0};
1700 int ret;
1701
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001702 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
1703
Christopher Faulet99eff652019-08-11 23:11:30 +02001704 mbuf = br_tail(fconn->mbuf);
1705 retry:
1706 if (!fcgi_get_buf(fconn, mbuf)) {
1707 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001708 fstrm->flags |= FCGI_SF_BLK_MROOM;
1709 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1710 ret = 0;
1711 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001712 }
1713
1714 while (1) {
1715 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1716 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1717 break;
1718 realign_again:
1719 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1720 }
1721
1722 if (outbuf.size < 8)
1723 goto full;
1724
1725 /* vsn: 1(FCGI_VERSION), type: (1)FCGI_BEGIN_REQUEST, id: fstrm->id,
1726 * len: 0x0008, padding: 0x00, rsv: 0x00 */
1727 memcpy(outbuf.area, "\x01\x01\x00\x00\x00\x08\x00\x00", 8);
1728 fcgi_set_record_id(outbuf.area, fstrm->id);
1729 outbuf.data = 8;
1730
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001731 if (fconn->flags & FCGI_CF_KEEP_CONN) {
1732 TRACE_STATE("keep connection opened", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001733 rec.flags |= FCGI_KEEP_CONN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001734 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001735 if (!fcgi_encode_begin_request(&outbuf, &rec))
1736 goto full;
1737
1738 /* commit the record */
Willy Tarreau022e5e52020-09-10 09:33:15 +02001739 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 +02001740 b_add(mbuf, outbuf.data);
1741 fstrm->flags |= FCGI_SF_BEGIN_SENT;
1742 fstrm->state = FCGI_SS_OPEN;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001743 TRACE_STATE("switching to OPEN", FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001744 ret = 1;
1745
1746 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001747 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_BEGREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001748 return ret;
1749 full:
1750 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1751 goto retry;
1752 fconn->flags |= FCGI_CF_MUX_MFULL;
1753 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001754 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 +02001755 ret = 0;
1756 goto end;
1757}
1758
1759/* Sends an empty record of type <rtype>. It returns > 0 on success, 0 if it
1760 * couldn't do anything. Empty record cannot be split. So we wait to have enough
1761 * space to proceed. It is small enough to be encoded in an empty buffer.
1762 */
1763static int fcgi_strm_send_empty_record(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1764 enum fcgi_record_type rtype)
1765{
1766 struct buffer outbuf;
1767 struct buffer *mbuf;
1768 int ret;
1769
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001770 TRACE_ENTER(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001771 mbuf = br_tail(fconn->mbuf);
1772 retry:
1773 if (!fcgi_get_buf(fconn, mbuf)) {
1774 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001775 fstrm->flags |= FCGI_SF_BLK_MROOM;
1776 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1777 ret = 0;
1778 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001779 }
1780
1781 while (1) {
1782 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1783 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1784 break;
1785 realign_again:
1786 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1787 }
1788
1789 if (outbuf.size < 8)
1790 goto full;
1791
1792 /* vsn: 1(FCGI_VERSION), type: rtype, id: fstrm->id,
1793 * len: 0x0000, padding: 0x00, rsv: 0x00 */
1794 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
1795 outbuf.area[1] = rtype;
1796 fcgi_set_record_id(outbuf.area, fstrm->id);
1797 outbuf.data = 8;
1798
1799 /* commit the record */
1800 b_add(mbuf, outbuf.data);
1801 ret = 1;
1802
1803 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001804 TRACE_LEAVE(FCGI_EV_TX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001805 return ret;
1806 full:
1807 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
1808 goto retry;
1809 fconn->flags |= FCGI_CF_MUX_MFULL;
1810 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001811 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 +02001812 ret = 0;
1813 goto end;
1814}
1815
1816
1817/* Sends an empty PARAMS record. It relies on fcgi_strm_send_empty_record(). It
1818 * marks the end of params.
1819 */
1820static int fcgi_strm_send_empty_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1821{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001822 int ret;
1823
1824 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
1825 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_PARAMS);
1826 if (ret)
Willy Tarreau022e5e52020-09-10 09:33:15 +02001827 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 +02001828 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02001829}
1830
1831/* Sends an empty STDIN record. It relies on fcgi_strm_send_empty_record(). It
1832 * marks the end of input. On success, all the request was successfully sent.
1833 */
1834static int fcgi_strm_send_empty_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1835{
1836 int ret;
1837
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001838 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001839 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_STDIN);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001840 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001841 fstrm->flags |= FCGI_SF_ES_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001842 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 +02001843 TRACE_USER("FCGI request fully xferred", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1844 TRACE_STATE("stdin data fully sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN|FCGI_EV_TX_EOI, fconn->conn, fstrm);
1845 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001846 return ret;
1847}
1848
1849/* Sends an ABORT_REQUEST record. It relies on fcgi_strm_send_empty_record(). It
1850 * stops the request processing.
1851 */
1852static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
1853{
1854 int ret;
1855
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001856 TRACE_POINT(FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02001857 ret = fcgi_strm_send_empty_record(fconn, fstrm, FCGI_ABORT_REQUEST);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001858 if (ret) {
Christopher Faulet99eff652019-08-11 23:11:30 +02001859 fstrm->flags |= FCGI_SF_ABRT_SENT;
Willy Tarreau022e5e52020-09-10 09:33:15 +02001860 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 +02001861 TRACE_USER("FCGI request aborted", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1862 TRACE_STATE("abort sent", FCGI_EV_TX_RECORD|FCGI_EV_TX_ABORT, fconn->conn, fstrm);
1863 }
Christopher Faulet99eff652019-08-11 23:11:30 +02001864 return ret;
1865}
1866
1867/* Sends a PARAMS record. Returns > 0 on success, 0 if it couldn't do
1868 * anything. If there are too much K/V params to be encoded in a PARAMS record,
1869 * several records are sent. However, a K/V param cannot be split between 2
1870 * records.
1871 */
1872static size_t fcgi_strm_send_params(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
1873 struct htx *htx)
1874{
1875 struct buffer outbuf;
1876 struct buffer *mbuf;
1877 struct htx_blk *blk;
1878 struct htx_sl *sl = NULL;
1879 struct fcgi_strm_params params;
1880 size_t total = 0;
1881
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001882 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx);
1883
Christopher Faulet99eff652019-08-11 23:11:30 +02001884 memset(&params, 0, sizeof(params));
1885 params.p = get_trash_chunk();
1886
1887 mbuf = br_tail(fconn->mbuf);
1888 retry:
1889 if (!fcgi_get_buf(fconn, mbuf)) {
1890 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02001891 fstrm->flags |= FCGI_SF_BLK_MROOM;
1892 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
1893 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02001894 }
1895
1896 while (1) {
1897 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
1898 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
1899 break;
1900 realign_again:
1901 b_slow_realign(mbuf, trash.area, b_data(mbuf));
1902 }
1903
1904 if (outbuf.size < 8)
1905 goto full;
1906
1907 /* vsn: 1(FCGI_VERSION), type: (4)FCGI_PARAMS, id: fstrm->id,
1908 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
1909 memcpy(outbuf.area, "\x01\x04\x00\x00\x00\x00\x00\x00", 8);
1910 fcgi_set_record_id(outbuf.area, fstrm->id);
1911 outbuf.data = 8;
1912
1913 blk = htx_get_head_blk(htx);
1914 while (blk) {
1915 enum htx_blk_type type;
1916 uint32_t size = htx_get_blksz(blk);
1917 struct fcgi_param p;
1918
1919 type = htx_get_blk_type(blk);
1920 switch (type) {
1921 case HTX_BLK_REQ_SL:
1922 sl = htx_get_blk_ptr(htx, blk);
1923 if (sl->info.req.meth == HTTP_METH_HEAD)
1924 fstrm->h1m.flags |= H1_MF_METH_HEAD;
1925 if (sl->flags & HTX_SL_F_VER_11)
1926 fstrm->h1m.flags |= H1_MF_VER_11;
1927 break;
1928
1929 case HTX_BLK_HDR:
1930 p.n = htx_get_blk_name(htx, blk);
1931 p.v = htx_get_blk_value(htx, blk);
1932
1933 if (istmatch(p.n, ist(":fcgi-"))) {
1934 p.n.ptr += 6;
1935 p.n.len -= 6;
1936 if (isteq(p.n, ist("gateway_interface")))
1937 params.mask |= FCGI_SP_CGI_GATEWAY;
1938 else if (isteq(p.n, ist("document_root"))) {
1939 params.mask |= FCGI_SP_DOC_ROOT;
1940 params.docroot = p.v;
1941 }
1942 else if (isteq(p.n, ist("script_name"))) {
1943 params.mask |= FCGI_SP_SCRIPT_NAME;
1944 params.scriptname = p.v;
1945 }
1946 else if (isteq(p.n, ist("path_info"))) {
1947 params.mask |= FCGI_SP_PATH_INFO;
1948 params.pathinfo = p.v;
1949 }
1950 else if (isteq(p.n, ist("request_uri"))) {
1951 params.mask |= FCGI_SP_REQ_URI;
1952 params.uri = p.v;
1953 }
1954 else if (isteq(p.n, ist("request_meth")))
1955 params.mask |= FCGI_SP_REQ_METH;
1956 else if (isteq(p.n, ist("query_string")))
1957 params.mask |= FCGI_SP_REQ_QS;
1958 else if (isteq(p.n, ist("server_name")))
1959 params.mask |= FCGI_SP_SRV_NAME;
1960 else if (isteq(p.n, ist("server_port")))
1961 params.mask |= FCGI_SP_SRV_PORT;
1962 else if (isteq(p.n, ist("server_protocol")))
1963 params.mask |= FCGI_SP_SRV_PROTO;
1964 else if (isteq(p.n, ist("remote_addr")))
1965 params.mask |= FCGI_SP_REM_ADDR;
1966 else if (isteq(p.n, ist("remote_port")))
1967 params.mask |= FCGI_SP_REM_PORT;
1968 else if (isteq(p.n, ist("script_filename")))
1969 params.mask |= FCGI_SP_SCRIPT_FILE;
1970 else if (isteq(p.n, ist("path_translated")))
1971 params.mask |= FCGI_SP_PATH_TRANS;
1972 else if (isteq(p.n, ist("https")))
1973 params.mask |= FCGI_SP_HTTPS;
1974 }
1975 else if (isteq(p.n, ist("content-length"))) {
1976 p.n = ist("CONTENT_LENGTH");
1977 params.mask |= FCGI_SP_CONT_LEN;
1978 }
1979 else if (isteq(p.n, ist("content-type")))
1980 p.n = ist("CONTENT_TYPE");
1981 else {
1982 if (isteq(p.n, ist("host")))
1983 params.srv_name = p.v;
1984
Christopher Faulet67d58092019-10-02 10:51:38 +02001985 /* Skip header if same name is used to add the server name */
1986 if (fconn->proxy->server_id_hdr_name &&
1987 isteq(p.n, ist2(fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len)))
1988 break;
1989
Christopher Faulet99eff652019-08-11 23:11:30 +02001990 memcpy(trash.area, "http_", 5);
1991 memcpy(trash.area+5, p.n.ptr, p.n.len);
1992 p.n = ist2(trash.area, p.n.len+5);
1993 }
1994
1995 if (!fcgi_encode_param(&outbuf, &p)) {
1996 if (b_space_wraps(mbuf))
1997 goto realign_again;
1998 if (outbuf.data == 8)
1999 goto full;
2000 goto done;
2001 }
2002 break;
2003
2004 case HTX_BLK_EOH:
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002005 if (fconn->proxy->server_id_hdr_name) {
2006 struct server *srv = objt_server(fconn->conn->target);
2007
2008 if (!srv)
2009 goto done;
2010
2011 memcpy(trash.area, "http_", 5);
2012 memcpy(trash.area+5, fconn->proxy->server_id_hdr_name, fconn->proxy->server_id_hdr_len);
2013 p.n = ist2(trash.area, fconn->proxy->server_id_hdr_len+5);
2014 p.v = ist(srv->id);
2015
2016 if (!fcgi_encode_param(&outbuf, &p)) {
2017 if (b_space_wraps(mbuf))
2018 goto realign_again;
2019 if (outbuf.data == 8)
2020 goto full;
2021 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002022 TRACE_STATE("add server name header", FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002023 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002024 goto done;
2025
2026 default:
2027 break;
2028 }
2029 total += size;
2030 blk = htx_remove_blk(htx, blk);
2031 }
2032
2033 done:
2034 if (!fcgi_set_default_param(fconn, fstrm, htx, sl, &params))
2035 goto error;
2036
2037 if (!fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CGI_GATEWAY) ||
2038 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_DOC_ROOT) ||
2039 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_NAME) ||
2040 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_INFO) ||
2041 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_URI) ||
2042 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_METH) ||
2043 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REQ_QS) ||
2044 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_NAME) ||
2045 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PORT) ||
2046 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SRV_PROTO) ||
2047 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_ADDR) ||
2048 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_REM_PORT) ||
2049 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_SCRIPT_FILE) ||
2050 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_PATH_TRANS) ||
2051 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_CONT_LEN) ||
2052 !fcgi_encode_default_param(fconn, fstrm, &params, &outbuf, FCGI_SP_HTTPS))
2053 goto error;
2054
2055 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002056 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 +02002057 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2058 b_add(mbuf, outbuf.data);
2059
2060 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002061 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_PARAMS, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002062 return total;
2063 full:
2064 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2065 goto retry;
2066 fconn->flags |= FCGI_CF_MUX_MFULL;
2067 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002068 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 +02002069 if (total)
2070 goto error;
2071 goto end;
2072
2073 error:
2074 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002075 TRACE_PROTO("processing error", FCGI_EV_TX_RECORD|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002076 fcgi_strm_error(fstrm);
2077 goto end;
2078}
2079
2080/* Sends a STDIN record. Returns > 0 on success, 0 if it couldn't do
2081 * anything. STDIN records contain the request body.
2082 */
2083static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
2084 struct htx *htx, size_t count, struct buffer *buf)
2085{
2086 struct buffer outbuf;
2087 struct buffer *mbuf;
2088 struct htx_blk *blk;
2089 enum htx_blk_type type;
2090 uint32_t size;
2091 size_t total = 0;
2092
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002093 TRACE_ENTER(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){count});
Christopher Faulet99eff652019-08-11 23:11:30 +02002094 if (!count)
2095 goto end;
2096
2097 mbuf = br_tail(fconn->mbuf);
2098 retry:
2099 if (!fcgi_get_buf(fconn, mbuf)) {
2100 fconn->flags |= FCGI_CF_MUX_MALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002101 fstrm->flags |= FCGI_SF_BLK_MROOM;
2102 TRACE_STATE("waiting for fconn mbuf ring allocation", FCGI_EV_TX_RECORD|FCGI_EV_FSTRM_BLK|FCGI_EV_FCONN_BLK, fconn->conn, fstrm);
2103 goto end;
Christopher Faulet99eff652019-08-11 23:11:30 +02002104 }
2105
2106 /* Perform some optimizations to reduce the number of buffer copies.
2107 * First, if the mux's buffer is empty and the htx area contains exactly
2108 * one data block of the same size as the requested count, and this
2109 * count fits within the record size, then it's possible to simply swap
2110 * the caller's buffer with the mux's output buffer and adjust offsets
2111 * and length to match the entire DATA HTX block in the middle. In this
2112 * case we perform a true zero-copy operation from end-to-end. This is
2113 * the situation that happens all the time with large files. Second, if
2114 * this is not possible, but the mux's output buffer is empty, we still
2115 * have an opportunity to avoid the copy to the intermediary buffer, by
2116 * making the intermediary buffer's area point to the output buffer's
2117 * area. In this case we want to skip the HTX header to make sure that
2118 * copies remain aligned and that this operation remains possible all
2119 * the time. This goes for headers, data blocks and any data extracted
2120 * from the HTX blocks.
2121 */
2122 blk = htx_get_head_blk(htx);
2123 if (!blk)
2124 goto end;
2125 type = htx_get_blk_type(blk);
2126 size = htx_get_blksz(blk);
2127 if (unlikely(size == count && htx_nbblks(htx) == 1 && type == HTX_BLK_DATA)) {
2128 void *old_area = mbuf->area;
2129
2130 if (b_data(mbuf)) {
2131 /* Too bad there are data left there. We're willing to memcpy/memmove
2132 * up to 1/4 of the buffer, which means that it's OK to copy a large
2133 * record into a buffer containing few data if it needs to be realigned,
2134 * and that it's also OK to copy few data without realigning. Otherwise
2135 * we'll pretend the mbuf is full and wait for it to become empty.
2136 */
2137 if (size + 8 <= b_room(mbuf) &&
2138 (b_data(mbuf) <= b_size(mbuf) / 4 ||
2139 (size <= b_size(mbuf) / 4 && size + 8 <= b_contig_space(mbuf))))
2140 goto copy;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002141 goto full;
Christopher Faulet99eff652019-08-11 23:11:30 +02002142 }
2143
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002144 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 +02002145 /* map a FCGI record to the HTX block so that we can put the
2146 * record header there.
2147 */
2148 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 8, size + 8);
2149 outbuf.area = b_head(mbuf);
2150
2151 /* prepend a FCGI record header just before the DATA block */
2152 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2153 fcgi_set_record_id(outbuf.area, fstrm->id);
2154 fcgi_set_record_size(outbuf.area, size);
2155
2156 /* and exchange with our old area */
2157 buf->area = old_area;
2158 buf->data = buf->head = 0;
2159 total += size;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002160
2161 htx = (struct htx *)buf->area;
2162 htx_reset(htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02002163 goto end;
2164 }
2165
2166 copy:
2167 while (1) {
2168 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
2169 if (outbuf.size >= 8 || !b_space_wraps(mbuf))
2170 break;
2171 realign_again:
2172 b_slow_realign(mbuf, trash.area, b_data(mbuf));
2173 }
2174
2175 if (outbuf.size < 8)
2176 goto full;
2177
2178 /* vsn: 1(FCGI_VERSION), type: (5)FCGI_STDIN, id: fstrm->id,
2179 * len: 0x0000 (fill later), padding: 0x00, rsv: 0x00 */
2180 memcpy(outbuf.area, "\x01\x05\x00\x00\x00\x00\x00\x00", 8);
2181 fcgi_set_record_id(outbuf.area, fstrm->id);
2182 outbuf.data = 8;
2183
2184 blk = htx_get_head_blk(htx);
2185 while (blk && count) {
2186 enum htx_blk_type type = htx_get_blk_type(blk);
2187 uint32_t size = htx_get_blksz(blk);
2188 struct ist v;
2189
2190 switch (type) {
2191 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002192 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 +02002193 v = htx_get_blk_value(htx, blk);
2194 if (v.len > count)
2195 v.len = count;
2196
2197 if (v.len > b_room(&outbuf)) {
2198 /* It doesn't fit at once. If it at least fits once split and
2199 * the amount of data to move is low, let's defragment the
2200 * buffer now.
2201 */
2202 if (b_space_wraps(mbuf) &&
2203 b_data(&outbuf) + v.len <= b_room(mbuf) &&
2204 b_data(mbuf) <= MAX_DATA_REALIGN)
2205 goto realign_again;
2206 v.len = b_room(&outbuf);
2207 }
2208 if (!v.len || !chunk_memcat(&outbuf, v.ptr, v.len)) {
2209 if (outbuf.data == 8)
2210 goto full;
2211 goto done;
2212 }
2213 if (v.len != size) {
2214 total += v.len;
2215 count -= v.len;
2216 htx_cut_data_blk(htx, blk, v.len);
2217 goto done;
2218 }
2219 break;
2220
2221 case HTX_BLK_EOM:
2222 goto done;
2223
2224 default:
2225 break;
2226 }
2227 total += size;
2228 count -= size;
2229 blk = htx_remove_blk(htx, blk);
2230 }
2231
2232 done:
2233 /* update the record's size */
Willy Tarreau022e5e52020-09-10 09:33:15 +02002234 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 +02002235 fcgi_set_record_size(outbuf.area, outbuf.data - 8);
2236 b_add(mbuf, outbuf.data);
2237
2238 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002239 TRACE_LEAVE(FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02002240 return total;
2241 full:
2242 if ((mbuf = br_tail_add(fconn->mbuf)) != NULL)
2243 goto retry;
2244 fconn->flags |= FCGI_CF_MUX_MFULL;
2245 fstrm->flags |= FCGI_SF_BLK_MROOM;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002246 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 +02002247 goto end;
2248}
2249
2250/* Processes a STDOUT record. Returns > 0 on success, 0 if it couldn't do
2251 * anything. STDOUT records contain the entire response. All the content is
2252 * copied in the stream's rxbuf. The parsing will be handled in fcgi_rcv_buf().
2253 */
2254static int fcgi_strm_handle_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2255{
2256 struct buffer *dbuf;
2257 size_t ret;
2258 size_t max;
2259
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002260 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2261
Christopher Faulet99eff652019-08-11 23:11:30 +02002262 dbuf = &fconn->dbuf;
2263
2264 /* Only padding remains */
2265 if (fconn->state == FCGI_CS_RECORD_P)
2266 goto end_transfer;
2267
2268 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2269 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2270 buf_room_for_htx_data(dbuf))
2271 goto fail; // incomplete record
2272
2273 if (!fcgi_get_buf(fconn, &fstrm->rxbuf)) {
2274 fconn->flags |= FCGI_CF_DEM_SALLOC;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002275 TRACE_STATE("waiting for fstrm rxbuf allocation", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2276 goto fail;
Christopher Faulet99eff652019-08-11 23:11:30 +02002277 }
2278
2279 /*max = MIN(b_room(&fstrm->rxbuf), fconn->drl);*/
2280 max = buf_room_for_htx_data(&fstrm->rxbuf);
2281 if (!b_data(&fstrm->rxbuf))
2282 fstrm->rxbuf.head = sizeof(struct htx);
2283 if (max > fconn->drl)
2284 max = fconn->drl;
2285
2286 ret = b_xfer(&fstrm->rxbuf, dbuf, max);
2287 if (!ret)
2288 goto fail;
2289 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002290 TRACE_DATA("move some data to fstrm rxbuf", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm, 0, (size_t[]){ret});
2291 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 +02002292
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002293 if (!buf_room_for_htx_data(&fstrm->rxbuf)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002294 fconn->flags |= FCGI_CF_DEM_SFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002295 TRACE_STATE("fstrm rxbuf full", FCGI_EV_RX_RECORD|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
2296 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002297
2298 if (fconn->drl)
2299 goto fail;
2300
2301 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002302 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002303 fconn->drl += fconn->drp;
2304 fconn->drp = 0;
2305 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2306 b_del(&fconn->dbuf, ret);
2307 fconn->drl -= ret;
2308 if (fconn->drl)
2309 goto fail;
2310
2311 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002312 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2313 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002314 return 1;
2315 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002316 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 +02002317 return 0;
2318}
2319
2320
2321/* Processes an empty STDOUT. Returns > 0 on success, 0 if it couldn't do
2322 * anything. It only skip the padding in fact, there is no payload for such
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002323 * records. It marks the end of the response.
Christopher Faulet99eff652019-08-11 23:11:30 +02002324 */
2325static int fcgi_strm_handle_empty_stdout(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2326{
2327 int ret;
2328
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002329 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
2330
Christopher Faulet99eff652019-08-11 23:11:30 +02002331 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002332 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002333 fconn->drl += fconn->drp;
2334 fconn->drp = 0;
2335 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2336 b_del(&fconn->dbuf, ret);
2337 fconn->drl -= ret;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002338 if (fconn->drl) {
2339 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 +02002340 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002341 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002342 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet3b3096e2020-07-15 16:04:49 +02002343 fstrm->flags |= FCGI_SF_ES_RCVD;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002344 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 +02002345 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);
2346 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002347 return 1;
2348}
2349
2350/* Processes a STDERR record. Returns > 0 on success, 0 if it couldn't do
2351 * anything.
2352 */
2353static int fcgi_strm_handle_stderr(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2354{
2355 struct buffer *dbuf;
2356 struct buffer tag;
2357 size_t ret;
2358
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002359 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002360 dbuf = &fconn->dbuf;
2361
2362 /* Only padding remains */
Christopher Faulet7f854332020-07-15 15:46:30 +02002363 if (fconn->state == FCGI_CS_RECORD_P || !fconn->drl)
Christopher Faulet99eff652019-08-11 23:11:30 +02002364 goto end_transfer;
2365
2366 if (b_data(dbuf) < (fconn->drl + fconn->drp) &&
2367 b_size(dbuf) > (fconn->drl + fconn->drp) &&
2368 buf_room_for_htx_data(dbuf))
2369 goto fail; // incomplete record
2370
2371 chunk_reset(&trash);
2372 ret = b_xfer(&trash, dbuf, MIN(b_room(&trash), fconn->drl));
2373 if (!ret)
2374 goto fail;
2375 fconn->drl -= ret;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002376 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 +02002377
2378 trash.area[ret] = '\n';
2379 trash.area[ret+1] = '\0';
2380 tag.area = fconn->app->name; tag.data = strlen(fconn->app->name);
Christopher Fauletc45791a2019-09-24 14:30:46 +02002381 app_log(&fconn->app->logsrvs, &tag, LOG_ERR, "%s", trash.area);
Christopher Faulet99eff652019-08-11 23:11:30 +02002382
2383 if (fconn->drl)
2384 goto fail;
2385
2386 end_transfer:
Christopher Faulet6c99d3b2020-07-15 15:55:52 +02002387 fconn->state = FCGI_CS_RECORD_P;
Christopher Faulet99eff652019-08-11 23:11:30 +02002388 fconn->drl += fconn->drp;
2389 fconn->drp = 0;
2390 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
2391 b_del(&fconn->dbuf, ret);
2392 fconn->drl -= ret;
2393 if (fconn->drl)
2394 goto fail;
2395 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002396 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2397 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002398 return 1;
2399 fail:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002400 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 +02002401 return 0;
2402}
2403
2404/* Processes an END_REQUEST record. Returns > 0 on success, 0 if it couldn't do
2405 * anything. If the empty STDOUT record is not already received, this one marks
2406 * the end of the response. It is highly unexpected, but if the record is larger
2407 * than a buffer and cannot be decoded in one time, an error is triggered and
2408 * the connection is closed. END_REQUEST record cannot be split.
2409 */
2410static int fcgi_strm_handle_end_request(struct fcgi_conn *fconn, struct fcgi_strm *fstrm)
2411{
2412 struct buffer inbuf;
2413 struct buffer *dbuf;
2414 struct fcgi_end_request endreq;
2415
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002416 TRACE_ENTER(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002417 dbuf = &fconn->dbuf;
2418
2419 /* Record too large to be fully decoded */
2420 if (b_size(dbuf) < (fconn->drl + fconn->drp))
2421 goto fail;
2422
2423 /* process full record only */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002424 if (b_data(dbuf) < (fconn->drl + fconn->drp)) {
2425 TRACE_DEVEL("leaving on missing data", FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002426 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002427 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002428
2429 if (unlikely(b_contig_data(dbuf, b_head_ofs(dbuf)) < fconn->drl)) {
2430 /* Realign the dmux buffer if the record wraps. It is unexpected
2431 * at this stage because it should be the first record received
2432 * from the FCGI application.
2433 */
2434 b_slow_realign(dbuf, trash.area, 0);
2435 }
2436
2437 inbuf = b_make(b_head(dbuf), b_data(dbuf), 0, fconn->drl);
2438
2439 if (!fcgi_decode_end_request(&inbuf, 0, &endreq))
2440 goto fail;
2441
2442 fstrm->flags |= FCGI_SF_ES_RCVD;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002443 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 +02002444 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 +02002445 fstrm->proto_status = endreq.errcode;
2446 fcgi_strm_close(fstrm);
2447
2448 b_del(&fconn->dbuf, fconn->drl + fconn->drp);
2449 fconn->drl = 0;
2450 fconn->drp = 0;
2451 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002452 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn, fstrm);
2453 TRACE_LEAVE(FCGI_EV_RX_RECORD|FCGI_EV_RX_ENDREQ, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002454 return 1;
2455
2456 fail:
2457 fcgi_strm_error(fstrm);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002458 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 +02002459 return 0;
2460}
2461
2462/* process Rx records to be demultiplexed */
2463static void fcgi_process_demux(struct fcgi_conn *fconn)
2464{
2465 struct fcgi_strm *fstrm = NULL, *tmp_fstrm;
2466 struct fcgi_header hdr;
2467 int ret;
2468
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002469 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2470
Christopher Faulet99eff652019-08-11 23:11:30 +02002471 if (fconn->state == FCGI_CS_CLOSED)
2472 return;
2473
2474 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002475 if (fconn->state == FCGI_CS_INIT) {
2476 TRACE_STATE("waiting FCGI GET_VALUES to be sent", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR|FCGI_EV_RX_GETVAL, fconn->conn);
2477 return;
2478 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002479 if (fconn->state == FCGI_CS_SETTINGS) {
2480 /* ensure that what is pending is a valid GET_VALUES_RESULT record. */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002481 TRACE_STATE("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002482 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2483 if (!ret)
2484 goto fail;
2485 b_del(&fconn->dbuf, ret);
2486
2487 if (hdr.id || (hdr.type != FCGI_GET_VALUES_RESULT && hdr.type != FCGI_UNKNOWN_TYPE)) {
2488 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002489 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);
2490 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 +02002491 goto fail;
2492 }
2493 goto new_record;
2494 }
2495 }
2496
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002497 /* process as many incoming records as possible below */
2498 while (1) {
2499 if (!b_data(&fconn->dbuf)) {
2500 TRACE_DEVEL("no more Rx data", FCGI_EV_RX_RECORD, fconn->conn);
2501 break;
2502 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002503
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002504 if (fconn->state == FCGI_CS_CLOSED) {
2505 TRACE_STATE("end of connection reported", FCGI_EV_RX_RECORD|FCGI_EV_RX_EOI, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002506 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002507 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002508
2509 if (fconn->state == FCGI_CS_RECORD_H) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002510 TRACE_PROTO("receiving FCGI record header", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002511 ret = fcgi_decode_record_hdr(&fconn->dbuf, 0, &hdr);
2512 if (!ret)
2513 break;
2514 b_del(&fconn->dbuf, ret);
2515
2516 new_record:
2517 fconn->dsi = hdr.id;
2518 fconn->drt = hdr.type;
2519 fconn->drl = hdr.len;
2520 fconn->drp = hdr.padding;
2521 fconn->state = FCGI_CS_RECORD_D;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002522 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 +02002523 }
2524
2525 /* Only FCGI_CS_RECORD_D or FCGI_CS_RECORD_P */
2526 tmp_fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi);
2527
2528 if (tmp_fstrm != fstrm && fstrm && fstrm->cs &&
2529 (b_data(&fstrm->rxbuf) ||
2530 conn_xprt_read0_pending(fconn->conn) ||
2531 fstrm->state == FCGI_SS_CLOSED ||
2532 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2533 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2534 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002535 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 +02002536 fstrm->cs->flags |= CS_FL_RCV_MORE;
2537 fcgi_strm_notify_recv(fstrm);
2538 }
2539 fstrm = tmp_fstrm;
2540
2541 if (fstrm->state == FCGI_SS_CLOSED && fconn->dsi != 0) {
2542 /* ignore all record for closed streams */
2543 goto ignore_record;
2544 }
2545 if (fstrm->state == FCGI_SS_IDLE) {
2546 /* ignore all record for unknown streams */
2547 goto ignore_record;
2548 }
2549
2550 switch (fconn->drt) {
2551 case FCGI_GET_VALUES_RESULT:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002552 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 +02002553 ret = fcgi_conn_handle_values_result(fconn);
2554 break;
2555
2556 case FCGI_STDOUT:
2557 if (fstrm->flags & FCGI_SF_ES_RCVD)
2558 goto ignore_record;
2559
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002560 TRACE_PROTO("receiving FCGI STDOUT record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDOUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002561 if (fconn->drl)
2562 ret = fcgi_strm_handle_stdout(fconn, fstrm);
2563 else
2564 ret = fcgi_strm_handle_empty_stdout(fconn, fstrm);
2565 break;
2566
2567 case FCGI_STDERR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002568 TRACE_PROTO("receiving FCGI STDERR record", FCGI_EV_RX_RECORD|FCGI_EV_RX_STDERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002569 ret = fcgi_strm_handle_stderr(fconn, fstrm);
2570 break;
2571
2572 case FCGI_END_REQUEST:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002573 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 +02002574 ret = fcgi_strm_handle_end_request(fconn, fstrm);
2575 break;
2576
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002577 /* implement all extra record types here */
Christopher Faulet99eff652019-08-11 23:11:30 +02002578 default:
2579 ignore_record:
2580 /* drop records that we ignore. They may be
2581 * larger than the buffer so we drain all of
2582 * their contents until we reach the end.
2583 */
2584 fconn->state = FCGI_CS_RECORD_P;
2585 fconn->drl += fconn->drp;
2586 fconn->drp = 0;
2587 ret = MIN(b_data(&fconn->dbuf), fconn->drl);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002588 TRACE_PROTO("receiving FCGI ignored record", FCGI_EV_RX_RECORD, fconn->conn, fstrm, 0, (size_t[]){ret});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002589 TRACE_STATE("switching to RECORD_P", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002590 b_del(&fconn->dbuf, ret);
2591 fconn->drl -= ret;
2592 ret = (fconn->drl == 0);
2593 }
2594
2595 /* error or missing data condition met above ? */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002596 if (ret <= 0) {
2597 TRACE_DEVEL("insufficient data to proceed", FCGI_EV_RX_RECORD, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02002598 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002599 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002600
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002601 if (fconn->state != FCGI_CS_RECORD_H && !(fconn->drl+fconn->drp)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002602 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002603 TRACE_STATE("switching to RECORD_H", FCGI_EV_RX_RECORD|FCGI_EV_RX_FHDR, fconn->conn);
2604 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002605 }
2606
2607 fail:
2608 /* we can go here on missing data, blocked response or error */
2609 if (fstrm && fstrm->cs &&
2610 (b_data(&fstrm->rxbuf) ||
2611 conn_xprt_read0_pending(fconn->conn) ||
2612 fstrm->state == FCGI_SS_CLOSED ||
2613 (fstrm->flags & FCGI_SF_ES_RCVD) ||
2614 (fstrm->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
2615 /* we may have to signal the upper layers */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002616 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 +02002617 fstrm->cs->flags |= CS_FL_RCV_MORE;
2618 fcgi_strm_notify_recv(fstrm);
2619 }
2620
2621 fcgi_conn_restart_reading(fconn, 0);
2622}
2623
2624/* process Tx records from streams to be multiplexed. Returns > 0 if it reached
2625 * the end.
2626 */
2627static int fcgi_process_mux(struct fcgi_conn *fconn)
2628{
2629 struct fcgi_strm *fstrm, *fstrm_back;
2630
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002631 TRACE_ENTER(FCGI_EV_FCONN_WAKE, fconn->conn);
2632
Christopher Faulet99eff652019-08-11 23:11:30 +02002633 if (unlikely(fconn->state < FCGI_CS_RECORD_H)) {
2634 if (unlikely(fconn->state == FCGI_CS_INIT)) {
2635 if (!(fconn->flags & FCGI_CF_GET_VALUES)) {
2636 fconn->state = FCGI_CS_RECORD_H;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002637 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 +02002638 fcgi_wake_unassigned_streams(fconn);
2639 goto mux;
2640 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002641 TRACE_PROTO("sending FCGI GET_VALUES record", FCGI_EV_TX_RECORD|FCGI_EV_TX_GETVAL, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002642 if (unlikely(!fcgi_conn_send_get_values(fconn)))
2643 goto fail;
2644 fconn->state = FCGI_CS_SETTINGS;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002645 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 +02002646 }
2647 /* need to wait for the other side */
2648 if (fconn->state < FCGI_CS_RECORD_H)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002649 goto done;
Christopher Faulet99eff652019-08-11 23:11:30 +02002650 }
2651
2652 mux:
2653 list_for_each_entry_safe(fstrm, fstrm_back, &fconn->send_list, send_list) {
2654 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2655 break;
2656
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002657 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002658 continue;
2659
Willy Tarreau7aad7032020-01-16 17:20:57 +01002660 /* If the sender changed his mind and unsubscribed, let's just
2661 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002662 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002663 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2664 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002665 LIST_DEL_INIT(&fstrm->send_list);
2666 continue;
2667 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002668
2669 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002670 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2671 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002672 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002673 tasklet_wakeup(fstrm->subs->tasklet);
2674 fstrm->subs->events &= ~SUB_RETRY_SEND;
2675 if (!fstrm->subs->events)
2676 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002677 } else {
2678 /* it's the shut request that was queued */
2679 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2680 tasklet_wakeup(fstrm->shut_tl);
2681 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002682 }
2683
2684 fail:
2685 if (fconn->state == FCGI_CS_CLOSED) {
2686 if (fconn->stream_cnt - fconn->nb_reserved > 0) {
2687 fcgi_conn_send_aborts(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002688 if (fconn->flags & FCGI_CF_MUX_BLOCK_ANY) {
2689 TRACE_DEVEL("leaving in blocked situation", FCGI_EV_FCONN_WAKE|FCGI_EV_FCONN_BLK, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002690 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002691 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002692 }
2693 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002694
2695 done:
2696 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002697 return 1;
2698}
2699
2700
2701/* Attempt to read data, and subscribe if none available.
2702 * The function returns 1 if data has been received, otherwise zero.
2703 */
2704static int fcgi_recv(struct fcgi_conn *fconn)
2705{
2706 struct connection *conn = fconn->conn;
2707 struct buffer *buf;
2708 int max;
2709 size_t ret;
2710
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002711 TRACE_ENTER(FCGI_EV_FCONN_RECV, conn);
2712
2713 if (fconn->wait_event.events & SUB_RETRY_RECV) {
2714 TRACE_DEVEL("leaving on sub_recv", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002715 return (b_data(&fconn->dbuf));
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002716 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002717
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002718 if (!fcgi_recv_allowed(fconn)) {
2719 TRACE_DEVEL("leaving on !recv_allowed", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002720 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002721 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002722
2723 buf = fcgi_get_buf(fconn, &fconn->dbuf);
2724 if (!buf) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002725 TRACE_DEVEL("waiting for fconn dbuf allocation", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002726 fconn->flags |= FCGI_CF_DEM_DALLOC;
2727 return 0;
2728 }
2729
2730 b_realign_if_empty(buf);
2731 if (!b_data(buf)) {
2732 /* try to pre-align the buffer like the
2733 * rxbufs will be to optimize memory copies. We'll make
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002734 * sure that the record header lands at the end of the
Christopher Faulet99eff652019-08-11 23:11:30 +02002735 * HTX block to alias it upon recv. We cannot use the
2736 * head because rcv_buf() will realign the buffer if
2737 * it's empty. Thus we cheat and pretend we already
2738 * have a few bytes there.
2739 */
2740 max = buf_room_for_htx_data(buf) + (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2741 buf->head = sizeof(struct htx) - (fconn->state == FCGI_CS_RECORD_H ? 8 : 0);
2742 }
2743 else
2744 max = buf_room_for_htx_data(buf);
2745
2746 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
2747
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002748 if (max && !ret && fcgi_recv_allowed(fconn)) {
2749 TRACE_DATA("failed to receive data, subscribing", FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002750 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002751 }
2752 else
Willy Tarreau022e5e52020-09-10 09:33:15 +02002753 TRACE_DATA("recv data", FCGI_EV_FCONN_RECV, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002754
2755 if (!b_data(buf)) {
2756 fcgi_release_buf(fconn, &fconn->dbuf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002757 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002758 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
2759 }
2760
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002761 if (ret == max) {
2762 TRACE_DEVEL("fconn dbuf full", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002763 fconn->flags |= FCGI_CF_DEM_DFULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002764 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002765
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002766 TRACE_LEAVE(FCGI_EV_FCONN_RECV, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002767 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
2768}
2769
2770
2771/* Try to send data if possible.
2772 * The function returns 1 if data have been sent, otherwise zero.
2773 */
2774static int fcgi_send(struct fcgi_conn *fconn)
2775{
2776 struct connection *conn = fconn->conn;
2777 int done;
2778 int sent = 0;
2779
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002780 TRACE_ENTER(FCGI_EV_FCONN_SEND, conn);
2781
2782 if (conn->flags & CO_FL_ERROR) {
2783 TRACE_DEVEL("leaving on connection error", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002784 return 1;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002785 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002786
2787
Willy Tarreau911db9b2020-01-23 16:27:54 +01002788 if (conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002789 /* a handshake was requested */
2790 goto schedule;
2791 }
2792
2793 /* This loop is quite simple : it tries to fill as much as it can from
2794 * pending streams into the existing buffer until it's reportedly full
2795 * or the end of send requests is reached. Then it tries to send this
2796 * buffer's contents out, marks it not full if at least one byte could
2797 * be sent, and tries again.
2798 *
2799 * The snd_buf() function normally takes a "flags" argument which may
2800 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2801 * data immediately comes and CO_SFL_STREAMER to indicate that the
2802 * connection is streaming lots of data (used to increase TLS record
2803 * size at the expense of latency). The former can be sent any time
2804 * there's a buffer full flag, as it indicates at least one stream
2805 * attempted to send and failed so there are pending data. An
2806 * alternative would be to set it as long as there's an active stream
2807 * but that would be problematic for ACKs until we have an absolute
2808 * guarantee that all waiters have at least one byte to send. The
2809 * latter should possibly not be set for now.
2810 */
2811
2812 done = 0;
2813 while (!done) {
2814 unsigned int flags = 0;
2815 unsigned int released = 0;
2816 struct buffer *buf;
2817
2818 /* fill as much as we can into the current buffer */
2819 while (((fconn->flags & (FCGI_CF_MUX_MFULL|FCGI_CF_MUX_MALLOC)) == 0) && !done)
2820 done = fcgi_process_mux(fconn);
2821
2822 if (fconn->flags & FCGI_CF_MUX_MALLOC)
2823 done = 1; // we won't go further without extra buffers
2824
2825 if (conn->flags & CO_FL_ERROR)
2826 break;
2827
2828 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2829 flags |= CO_SFL_MSG_MORE;
2830
2831 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
2832 if (b_data(buf)) {
2833 int ret;
2834
2835 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
2836 if (!ret) {
2837 done = 1;
2838 break;
2839 }
2840 sent = 1;
Willy Tarreau022e5e52020-09-10 09:33:15 +02002841 TRACE_DATA("send data", FCGI_EV_FCONN_SEND, conn, 0, 0, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02002842 b_del(buf, ret);
2843 if (b_data(buf)) {
2844 done = 1;
2845 break;
2846 }
2847 }
2848 b_free(buf);
2849 released++;
2850 }
2851
2852 if (released)
2853 offer_buffers(NULL, tasks_run_queue);
2854
2855 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002856 if (fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM))
2857 TRACE_STATE("fconn mbuf ring not fill anymore", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002858 fconn->flags &= ~(FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM);
2859 }
2860
2861 if (conn->flags & CO_FL_SOCK_WR_SH) {
2862 /* output closed, nothing to send, clear the buffer to release it */
2863 b_reset(br_tail(fconn->mbuf));
2864 }
2865 /* We're not full anymore, so we can wake any task that are waiting
2866 * for us.
2867 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002868 if (!(fconn->flags & (FCGI_CF_MUX_MFULL | FCGI_CF_DEM_MROOM)) && fconn->state >= FCGI_CS_RECORD_H) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002869 struct fcgi_strm *fstrm;
2870
2871 list_for_each_entry(fstrm, &fconn->send_list, send_list) {
2872 if (fconn->state == FCGI_CS_CLOSED || fconn->flags & FCGI_CF_MUX_BLOCK_ANY)
2873 break;
2874
Willy Tarreauf11be0e2020-01-16 16:59:45 +01002875 if (fstrm->flags & FCGI_SF_NOTIFIED)
Christopher Faulet99eff652019-08-11 23:11:30 +02002876 continue;
2877
Willy Tarreau7aad7032020-01-16 17:20:57 +01002878 /* If the sender changed his mind and unsubscribed, let's just
2879 * remove the stream from the send_list.
Christopher Faulet99eff652019-08-11 23:11:30 +02002880 */
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002881 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)) &&
2882 (!fstrm->subs || !(fstrm->subs->events & SUB_RETRY_SEND))) {
Christopher Faulet99eff652019-08-11 23:11:30 +02002883 LIST_DEL_INIT(&fstrm->send_list);
2884 continue;
2885 }
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002886
2887 if (fstrm->subs && fstrm->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7aad7032020-01-16 17:20:57 +01002888 TRACE_DEVEL("waking up pending stream", FCGI_EV_FCONN_SEND|FCGI_EV_STRM_WAKE, conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002889 fstrm->flags &= ~FCGI_SF_BLK_ANY;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002890 fstrm->flags |= FCGI_SF_NOTIFIED;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01002891 tasklet_wakeup(fstrm->subs->tasklet);
2892 fstrm->subs->events &= ~SUB_RETRY_SEND;
2893 if (!fstrm->subs->events)
2894 fstrm->subs = NULL;
Willy Tarreau7aad7032020-01-16 17:20:57 +01002895 } else {
2896 /* it's the shut request that was queued */
2897 TRACE_POINT(FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
2898 tasklet_wakeup(fstrm->shut_tl);
2899 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002900 }
2901 }
2902 /* We're done, no more to send */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002903 if (!br_data(fconn->mbuf)) {
2904 TRACE_DEVEL("leaving with everything sent", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002905 return sent;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002906 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002907schedule:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002908 if (!(conn->flags & CO_FL_ERROR) && !(fconn->wait_event.events & SUB_RETRY_SEND)) {
2909 TRACE_STATE("more data to send, subscribing", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002910 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &fconn->wait_event);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002911 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002912
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002913 TRACE_DEVEL("leaving with some data left to send", FCGI_EV_FCONN_SEND, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02002914 return sent;
2915}
2916
2917/* this is the tasklet referenced in fconn->wait_event.tasklet */
2918static struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned short status)
2919{
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002920 struct connection *conn;
2921 struct fcgi_conn *fconn;
2922 struct tasklet *tl = (struct tasklet *)t;
2923 int conn_in_list;
Christopher Faulet99eff652019-08-11 23:11:30 +02002924 int ret = 0;
2925
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002926
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002927 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002928 if (tl->context == NULL) {
2929 /* The connection has been taken over by another thread,
2930 * we're no longer responsible for it, so just free the
2931 * tasklet, and do nothing.
2932 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002933 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002934 tasklet_free(tl);
2935 return NULL;
2936
2937 }
2938 fconn = ctx;
2939 conn = fconn->conn;
2940
2941 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2942
2943 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2944 if (conn_in_list)
2945 MT_LIST_DEL(&conn->list);
2946
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002947 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002948
Christopher Faulet99eff652019-08-11 23:11:30 +02002949 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
2950 ret = fcgi_send(fconn);
2951 if (!(fconn->wait_event.events & SUB_RETRY_RECV))
2952 ret |= fcgi_recv(fconn);
2953 if (ret || b_data(&fconn->dbuf))
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002954 ret = fcgi_process(fconn);
2955
2956 /* If we were in an idle list, we want to add it back into it,
2957 * unless fcgi_process() returned -1, which mean it has destroyed
2958 * the connection (testing !ret is enough, if fcgi_process() wasn't
2959 * called then ret will be 0 anyway.
2960 */
2961 if (!ret && conn_in_list) {
2962 struct server *srv = objt_server(conn->target);
2963
2964 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002965 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002966 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002967 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01002968 }
Christopher Faulet99eff652019-08-11 23:11:30 +02002969 return NULL;
2970}
2971
2972/* callback called on any event by the connection handler.
2973 * It applies changes and returns zero, or < 0 if it wants immediate
2974 * destruction of the connection (which normally doesn not happen in FCGI).
2975 */
2976static int fcgi_process(struct fcgi_conn *fconn)
2977{
2978 struct connection *conn = fconn->conn;
2979
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002980 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
2981
Christopher Faulet99eff652019-08-11 23:11:30 +02002982 if (b_data(&fconn->dbuf) && !(fconn->flags & FCGI_CF_DEM_BLOCK_ANY)) {
2983 fcgi_process_demux(fconn);
2984
2985 if (fconn->state == FCGI_CS_CLOSED || conn->flags & CO_FL_ERROR)
2986 b_reset(&fconn->dbuf);
2987
2988 if (buf_room_for_htx_data(&fconn->dbuf))
2989 fconn->flags &= ~FCGI_CF_DEM_DFULL;
2990 }
2991 fcgi_send(fconn);
2992
2993 if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
2994 /* frontend is stopping, reload likely in progress, let's try
2995 * to announce a graceful shutdown if not yet done. We don't
2996 * care if it fails, it will be tried again later.
2997 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02002998 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 +02002999 if (!(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3000 if (fconn->stream_cnt - fconn->nb_reserved > 0)
3001 fcgi_conn_send_aborts(fconn);
3002 }
3003 }
3004
3005 /*
3006 * If we received early data, and the handshake is done, wake
3007 * any stream that was waiting for it.
3008 */
3009 if (!(fconn->flags & FCGI_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003010 (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 +02003011 struct eb32_node *node;
3012 struct fcgi_strm *fstrm;
3013
3014 fconn->flags |= FCGI_CF_WAIT_FOR_HS;
3015 node = eb32_lookup_ge(&fconn->streams_by_id, 1);
3016
3017 while (node) {
3018 fstrm = container_of(node, struct fcgi_strm, by_id);
3019 if (fstrm->cs && fstrm->cs->flags & CS_FL_WAIT_FOR_HS)
3020 fcgi_strm_notify_recv(fstrm);
3021 node = eb32_next(node);
3022 }
3023 }
3024
3025 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) ||
3026 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3027 eb_is_empty(&fconn->streams_by_id)) {
3028 fcgi_wake_some_streams(fconn, 0);
3029
3030 if (eb_is_empty(&fconn->streams_by_id)) {
3031 /* no more stream, kill the connection now */
3032 fcgi_release(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003033 TRACE_DEVEL("leaving after releasing the connection", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003034 return -1;
3035 }
3036 }
3037
3038 if (!b_data(&fconn->dbuf))
3039 fcgi_release_buf(fconn, &fconn->dbuf);
3040
3041 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3042 fconn->state == FCGI_CS_CLOSED || (fconn->flags & FCGI_CF_ABRTS_FAILED) ||
3043 (!br_data(fconn->mbuf) && ((fconn->flags & FCGI_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&fconn->send_list))))
3044 fcgi_release_mbuf(fconn);
3045
3046 if (fconn->task) {
3047 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3048 task_queue(fconn->task);
3049 }
3050
3051 fcgi_send(fconn);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003052 TRACE_LEAVE(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003053 return 0;
3054}
3055
3056
3057/* wake-up function called by the connection layer (mux_ops.wake) */
3058static int fcgi_wake(struct connection *conn)
3059{
3060 struct fcgi_conn *fconn = conn->ctx;
3061
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003062 TRACE_POINT(FCGI_EV_FCONN_WAKE, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003063 return (fcgi_process(fconn));
3064}
3065
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003066
3067static int fcgi_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3068{
3069 int ret = 0;
3070 switch (mux_ctl) {
3071 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003072 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003073 ret |= MUX_STATUS_READY;
3074 return ret;
3075 default:
3076 return -1;
3077 }
3078}
3079
Christopher Faulet99eff652019-08-11 23:11:30 +02003080/* Connection timeout management. The principle is that if there's no receipt
3081 * nor sending for a certain amount of time, the connection is closed. If the
3082 * MUX buffer still has lying data or is not allocatable, the connection is
3083 * immediately killed. If it's allocatable and empty, we attempt to send a
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003084 * ABORT records.
Christopher Faulet99eff652019-08-11 23:11:30 +02003085 */
3086static struct task *fcgi_timeout_task(struct task *t, void *context, unsigned short state)
3087{
3088 struct fcgi_conn *fconn = context;
3089 int expired = tick_is_expired(t->expire, now_ms);
3090
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003091 TRACE_ENTER(FCGI_EV_FCONN_WAKE, (fconn ? fconn->conn : NULL));
3092
Willy Tarreau60814ff2020-06-30 11:19:23 +02003093 if (fconn) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003094 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3095
3096 /* Somebody already stole the connection from us, so we should not
3097 * free it, we just have to free the task.
3098 */
3099 if (!t->context) {
3100 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
3101 fconn = NULL;
3102 goto do_leave;
3103 }
3104
Willy Tarreau60814ff2020-06-30 11:19:23 +02003105 if (!expired) {
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003106 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003107 TRACE_DEVEL("leaving (not expired)", FCGI_EV_FCONN_WAKE, fconn->conn);
3108 return t;
3109 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003110
Willy Tarreau60814ff2020-06-30 11:19:23 +02003111 /* We're about to destroy the connection, so make sure nobody attempts
3112 * to steal it from us.
3113 */
Willy Tarreau60814ff2020-06-30 11:19:23 +02003114 if (fconn->conn->flags & CO_FL_LIST_MASK)
3115 MT_LIST_DEL(&fconn->conn->list);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003116
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02003117 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau60814ff2020-06-30 11:19:23 +02003118 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01003119
Olivier Houchard48ce6a32020-07-02 11:58:05 +02003120do_leave:
Christopher Faulet99eff652019-08-11 23:11:30 +02003121 task_destroy(t);
3122
3123 if (!fconn) {
3124 /* resources were already deleted */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003125 TRACE_DEVEL("leaving (not more fconn)", FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003126 return NULL;
3127 }
3128
3129 fconn->task = NULL;
3130 fconn->state = FCGI_CS_CLOSED;
3131 fcgi_wake_some_streams(fconn, 0);
3132
3133 if (br_data(fconn->mbuf)) {
3134 /* don't even try to send aborts, the buffer is stuck */
3135 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3136 goto end;
3137 }
3138
3139 /* try to send but no need to insist */
3140 if (!fcgi_conn_send_aborts(fconn))
3141 fconn->flags |= FCGI_CF_ABRTS_FAILED;
3142
3143 if (br_data(fconn->mbuf) && !(fconn->flags & FCGI_CF_ABRTS_FAILED) &&
3144 conn_xprt_ready(fconn->conn)) {
3145 unsigned int released = 0;
3146 struct buffer *buf;
3147
3148 for (buf = br_head(fconn->mbuf); b_size(buf); buf = br_del_head(fconn->mbuf)) {
3149 if (b_data(buf)) {
3150 int ret = fconn->conn->xprt->snd_buf(fconn->conn, fconn->conn->xprt_ctx,
3151 buf, b_data(buf), 0);
3152 if (!ret)
3153 break;
3154 b_del(buf, ret);
3155 if (b_data(buf))
3156 break;
3157 b_free(buf);
3158 released++;
3159 }
3160 }
3161
3162 if (released)
3163 offer_buffers(NULL, tasks_run_queue);
3164 }
3165
3166 end:
3167 /* either we can release everything now or it will be done later once
3168 * the last stream closes.
3169 */
3170 if (eb_is_empty(&fconn->streams_by_id))
3171 fcgi_release(fconn);
3172
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003173 TRACE_LEAVE(FCGI_EV_FCONN_WAKE);
Christopher Faulet99eff652019-08-11 23:11:30 +02003174 return NULL;
3175}
3176
3177
3178/*******************************************/
3179/* functions below are used by the streams */
3180/*******************************************/
3181
3182/* Append the description of what is present in error snapshot <es> into <out>.
3183 * The description must be small enough to always fit in a buffer. The output
3184 * buffer may be the trash so the trash must not be used inside this function.
3185 */
3186static void fcgi_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
3187{
3188 chunk_appendf(out,
3189 " FCGI connection flags 0x%08x, FCGI stream flags 0x%08x\n"
3190 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
3191 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
3192 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
3193 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
3194 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
3195}
3196/*
3197 * Capture a bad response and archive it in the proxy's structure. By default
3198 * it tries to report the error position as h1m->err_pos. However if this one is
3199 * not set, it will then report h1m->next, which is the last known parsing
3200 * point. The function is able to deal with wrapping buffers. It always displays
3201 * buffers as a contiguous area starting at buf->p. The direction is determined
3202 * thanks to the h1m's flags.
3203 */
3204static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_strm *fstrm,
3205 struct h1m *h1m, struct buffer *buf)
3206{
3207 struct session *sess = fstrm->sess;
3208 struct proxy *proxy = fconn->proxy;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003209 struct proxy *other_end;
Christopher Faulet99eff652019-08-11 23:11:30 +02003210 union error_snapshot_ctx ctx;
3211
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003212 if (fstrm->cs && fstrm->cs->data) {
3213 if (sess == NULL)
3214 sess = si_strm(fstrm->cs->data)->sess;
3215 if (!(h1m->flags & H1_MF_RESP))
3216 other_end = si_strm(fstrm->cs->data)->be;
3217 else
3218 other_end = sess->fe;
3219 } else
3220 other_end = NULL;
Christopher Faulet99eff652019-08-11 23:11:30 +02003221 /* http-specific part now */
3222 ctx.h1.state = h1m->state;
3223 ctx.h1.c_flags = fconn->flags;
3224 ctx.h1.s_flags = fstrm->flags;
3225 ctx.h1.m_flags = h1m->flags;
3226 ctx.h1.m_clen = h1m->curr_len;
3227 ctx.h1.m_blen = h1m->body_len;
3228
3229 proxy_capture_error(proxy, 1, other_end, fconn->conn->target, sess, buf, 0, 0,
3230 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
3231 &ctx, fcgi_show_error_snapshot);
3232}
3233
3234static size_t fcgi_strm_parse_headers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3235 struct buffer *buf, size_t *ofs, size_t max)
3236{
3237 int ret;
3238
Willy Tarreau022e5e52020-09-10 09:33:15 +02003239 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 +02003240 ret = h1_parse_msg_hdrs(h1m, NULL, htx, buf, *ofs, max);
3241 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003242 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 +02003243 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003244 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 +02003245 fcgi_strm_error(fstrm);
3246 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3247 }
3248 goto end;
3249 }
3250
3251 *ofs += ret;
3252 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003253 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 +02003254 return ret;
3255
3256}
3257
Christopher Fauletaf542632019-10-01 21:52:49 +02003258static size_t fcgi_strm_parse_data(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx **htx,
Christopher Faulet99eff652019-08-11 23:11:30 +02003259 struct buffer *buf, size_t *ofs, size_t max, struct buffer *htxbuf)
3260{
3261 int ret;
3262
Willy Tarreau022e5e52020-09-10 09:33:15 +02003263 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 +02003264 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003265 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003266 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 +02003267 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003268 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 +02003269 fcgi_strm_error(fstrm);
3270 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3271 }
3272 goto end;
3273 }
3274 *ofs += ret;
3275 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003276 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 +02003277 return ret;
3278}
3279
3280static size_t fcgi_strm_parse_trailers(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
3281 struct buffer *buf, size_t *ofs, size_t max)
3282{
3283 int ret;
3284
Willy Tarreau022e5e52020-09-10 09:33:15 +02003285 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 +02003286 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003287 if (!ret) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003288 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 +02003289 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003290 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 +02003291 fcgi_strm_error(fstrm);
3292 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3293 }
3294 goto end;
3295 }
3296 *ofs += ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003297 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003298 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 +02003299 return ret;
3300}
3301
3302static size_t fcgi_strm_add_eom(struct fcgi_strm *fstrm, struct h1m *h1m, struct htx *htx,
Christopher Faulet76014fd2019-12-10 11:47:22 +01003303 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet99eff652019-08-11 23:11:30 +02003304{
Christopher Faulet76014fd2019-12-10 11:47:22 +01003305 int ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003306
Willy Tarreau022e5e52020-09-10 09:33:15 +02003307 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 +01003308 ret = h1_parse_msg_eom(h1m, htx, max);
3309 if (!ret) {
3310 TRACE_DEVEL("leaving on missing data or error", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fstrm->fconn->conn, fstrm);
3311 if (htx->flags & HTX_FL_PARSING_ERROR) {
3312 TRACE_USER("rejected H1 response", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM|FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
3313 fcgi_strm_error(fstrm);
3314 fcgi_strm_capture_bad_message(fstrm->fconn, fstrm, h1m, buf);
3315 }
3316 goto end;
3317 }
3318 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3319 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003320 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 +01003321 return ret;
Christopher Faulet99eff652019-08-11 23:11:30 +02003322}
3323
3324static size_t fcgi_strm_parse_response(struct fcgi_strm *fstrm, struct buffer *buf, size_t count)
3325{
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003326 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003327 struct htx *htx;
3328 struct h1m *h1m = &fstrm->h1m;
3329 size_t ret, data, total = 0;
3330
3331 htx = htx_from_buf(buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003332 TRACE_ENTER(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){count});
3333
Christopher Faulet99eff652019-08-11 23:11:30 +02003334 data = htx->data;
3335 if (fstrm->state == FCGI_SS_ERROR)
3336 goto end;
3337
3338 do {
3339 size_t used = htx_used_space(htx);
3340
3341 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003342 TRACE_PROTO("parsing response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003343 ret = fcgi_strm_parse_headers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3344 if (!ret)
3345 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003346
3347 TRACE_USER("rcvd H1 response headers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_HDRS, fconn->conn, fstrm, htx);
3348
Christopher Faulet99eff652019-08-11 23:11:30 +02003349 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) == H1_MF_VER_11) {
3350 struct htx_blk *blk = htx_get_head_blk(htx);
3351 struct htx_sl *sl;
3352
3353 if (!blk)
3354 break;
3355 sl = htx_get_blk_ptr(htx, blk);
3356 sl->flags |= HTX_SL_F_XFER_LEN;
3357 htx->extra = 0;
3358 }
3359 }
3360 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003361 TRACE_PROTO("parsing response payload", FCGI_EV_RSP_DATA|FCGI_EV_RSP_BODY, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003362 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003363 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003364 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003365
3366 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 +02003367 }
3368 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003369 TRACE_PROTO("parsing response trailers", FCGI_EV_RSP_DATA|FCGI_EV_RSP_TLRS, fconn->conn, fstrm);
3370 ret = fcgi_strm_parse_trailers(fstrm, h1m, htx, &fstrm->rxbuf, &total, count);
3371 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003372 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003373
Christopher Faulet76014fd2019-12-10 11:47:22 +01003374 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 +02003375 }
3376 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01003377 if (!(fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
3378 if (!fcgi_strm_add_eom(fstrm, h1m, htx, &fstrm->rxbuf, &total, count))
3379 break;
3380
3381 TRACE_USER("H1 response fully rcvd", FCGI_EV_RSP_DATA|FCGI_EV_RSP_EOM, fconn->conn, fstrm, htx);
3382 }
3383
Christopher Faulet99eff652019-08-11 23:11:30 +02003384 if (b_data(&fstrm->rxbuf) > total) {
3385 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003386 TRACE_PROTO("too much data, parsing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003387 fcgi_strm_error(fstrm);
3388 }
3389 break;
3390 }
3391 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003392 TRACE_PROTO("parsing response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Fauletaf542632019-10-01 21:52:49 +02003393 ret = fcgi_strm_parse_data(fstrm, h1m, &htx, &fstrm->rxbuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003394
Christopher Faulet99eff652019-08-11 23:11:30 +02003395 if (fstrm->state != FCGI_SS_ERROR &&
3396 (fstrm->flags & FCGI_SF_ES_RCVD) && b_data(&fstrm->rxbuf) == total) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003397 TRACE_DEVEL("end of tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003398 if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_LEN)) != H1_MF_VER_11)
3399 fstrm->flags |= FCGI_SF_H1_PARSING_DONE;
3400 h1m->state = H1_MSG_DONE;
3401 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 +02003402 }
Christopher Faulet76014fd2019-12-10 11:47:22 +01003403 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet99eff652019-08-11 23:11:30 +02003404 break;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003405
3406 TRACE_PROTO("rcvd H1 response tunneled data", FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003407 }
3408 else {
3409 htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003410 TRACE_PROTO("processing error", FCGI_EV_RSP_DATA, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003411 fcgi_strm_error(fstrm);
3412 break;
3413 }
3414
3415 count -= htx_used_space(htx) - used;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003416 } while (fstrm->state != FCGI_SS_ERROR);
Christopher Faulet99eff652019-08-11 23:11:30 +02003417
3418 if (fstrm->state == FCGI_SS_ERROR) {
3419 b_reset(&fstrm->rxbuf);
3420 htx_to_buf(htx, buf);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003421 TRACE_DEVEL("leaving on error", FCGI_EV_RSP_DATA|FCGI_EV_STRM_ERR, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003422 return 0;
3423 }
3424
3425 b_del(&fstrm->rxbuf, total);
3426
3427 end:
3428 htx_to_buf(htx, buf);
3429 ret = htx->data - data;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003430 TRACE_LEAVE(FCGI_EV_RSP_DATA, fconn->conn, fstrm, htx, (size_t[]){ret});
Christopher Faulet99eff652019-08-11 23:11:30 +02003431 return ret;
3432}
3433
3434/*
3435 * Attach a new stream to a connection
3436 * (Used for outgoing connections)
3437 */
3438static struct conn_stream *fcgi_attach(struct connection *conn, struct session *sess)
3439{
3440 struct conn_stream *cs;
3441 struct fcgi_strm *fstrm;
3442 struct fcgi_conn *fconn = conn->ctx;
3443
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003444 TRACE_ENTER(FCGI_EV_FSTRM_NEW, conn);
Christopher Faulet236c93b2020-07-02 09:19:54 +02003445 cs = cs_new(conn, conn->target);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003446 if (!cs) {
3447 TRACE_DEVEL("leaving on CS allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003448 return NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003449 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003450 fstrm = fcgi_conn_stream_new(fconn, cs, sess);
3451 if (!fstrm) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003452 TRACE_DEVEL("leaving on stream creation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR, conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003453 cs_free(cs);
3454 return NULL;
3455 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003456 TRACE_LEAVE(FCGI_EV_FSTRM_NEW, conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003457 return cs;
3458}
3459
3460/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3461 * We have to scan because we may have some orphan streams. It might be
3462 * beneficial to scan backwards from the end to reduce the likeliness to find
3463 * orphans.
3464 */
3465static const struct conn_stream *fcgi_get_first_cs(const struct connection *conn)
3466{
3467 struct fcgi_conn *fconn = conn->ctx;
3468 struct fcgi_strm *fstrm;
3469 struct eb32_node *node;
3470
3471 node = eb32_first(&fconn->streams_by_id);
3472 while (node) {
3473 fstrm = container_of(node, struct fcgi_strm, by_id);
3474 if (fstrm->cs)
3475 return fstrm->cs;
3476 node = eb32_next(node);
3477 }
3478 return NULL;
3479}
3480
3481/*
3482 * Destroy the mux and the associated connection, if it is no longer used
3483 */
3484static void fcgi_destroy(void *ctx)
3485{
3486 struct fcgi_conn *fconn = ctx;
3487
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003488 TRACE_POINT(FCGI_EV_FCONN_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003489 if (eb_is_empty(&fconn->streams_by_id) || !fconn->conn || fconn->conn->ctx != fconn)
3490 fcgi_release(fconn);
3491}
3492
3493/*
3494 * Detach the stream from the connection and possibly release the connection.
3495 */
3496static void fcgi_detach(struct conn_stream *cs)
3497{
3498 struct fcgi_strm *fstrm = cs->ctx;
3499 struct fcgi_conn *fconn;
3500 struct session *sess;
3501
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003502 TRACE_ENTER(FCGI_EV_STRM_END, (fstrm ? fstrm->fconn->conn : NULL), fstrm);
3503
Christopher Faulet99eff652019-08-11 23:11:30 +02003504 cs->ctx = NULL;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003505 if (!fstrm) {
3506 TRACE_LEAVE(FCGI_EV_STRM_END);
Christopher Faulet99eff652019-08-11 23:11:30 +02003507 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003508 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003509
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003510 /* there's no txbuf so we're certain no to be able to send anything */
3511 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003512
3513 sess = fstrm->sess;
3514 fconn = fstrm->fconn;
3515 fstrm->cs = NULL;
3516 fconn->nb_cs--;
3517
3518 if (fstrm->proto_status == FCGI_PS_CANT_MPX_CONN) {
3519 fconn->flags &= ~FCGI_CF_MPXS_CONNS;
3520 fconn->streams_limit = 1;
3521 }
3522 else if (fstrm->proto_status == FCGI_PS_OVERLOADED ||
3523 fstrm->proto_status == FCGI_PS_UNKNOWN_ROLE) {
3524 fconn->flags &= ~FCGI_CF_KEEP_CONN;
3525 fconn->state = FCGI_CS_CLOSED;
3526 }
3527
3528 /* this stream may be blocked waiting for some data to leave, so orphan
3529 * it in this case.
3530 */
3531 if (!(cs->conn->flags & CO_FL_ERROR) &&
3532 (fconn->state != FCGI_CS_CLOSED) &&
Willy Tarreau7aad7032020-01-16 17:20:57 +01003533 (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) &&
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003534 (fstrm->subs || (fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003535 TRACE_DEVEL("leaving on stream blocked", FCGI_EV_STRM_END|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003536 return;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003537 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003538
3539 if ((fconn->flags & FCGI_CF_DEM_BLOCK_ANY && fstrm->id == fconn->dsi)) {
3540 /* unblock the connection if it was blocked on this stream. */
3541 fconn->flags &= ~FCGI_CF_DEM_BLOCK_ANY;
3542 fcgi_conn_restart_reading(fconn, 1);
3543 }
3544
3545 fcgi_strm_destroy(fstrm);
3546
3547 if (!(fconn->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) &&
Christopher Faulet9bcd9732020-05-02 09:21:24 +02003548 (fconn->flags & FCGI_CF_KEEP_CONN)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003549 if (fconn->conn->flags & CO_FL_PRIVATE) {
Christopher Faulet08016ab2020-07-01 16:10:06 +02003550 /* Add the connection in the session serverlist, if not already done */
3551 if (!session_add_conn(sess, fconn->conn, fconn->conn->target)) {
3552 fconn->conn->owner = NULL;
3553 if (eb_is_empty(&fconn->streams_by_id)) {
3554 /* let's kill the connection right away */
3555 fconn->conn->mux->destroy(fconn);
3556 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3557 return;
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003558 }
3559 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003560 if (eb_is_empty(&fconn->streams_by_id)) {
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003561 if (session_check_idle_conn(fconn->conn->owner, fconn->conn) != 0) {
3562 /* The connection is destroyed, let's leave */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003563 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
Christopher Faulet66cd57e2020-05-02 09:08:54 +02003564 return;
Christopher Faulet99eff652019-08-11 23:11:30 +02003565 }
3566 }
3567 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003568 else {
3569 if (eb_is_empty(&fconn->streams_by_id)) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003570 if (!srv_add_to_idle_list(objt_server(fconn->conn->target), fconn->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003571 /* The server doesn't want it, let's kill the connection right away */
3572 fconn->conn->mux->destroy(fconn);
3573 TRACE_DEVEL("outgoing connection killed", FCGI_EV_STRM_END|FCGI_EV_FCONN_ERR);
3574 return;
3575 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003576 /* At this point, the connection has been added to the
3577 * server idle list, so another thread may already have
3578 * hijacked it, so we can't do anything with it.
3579 */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003580 TRACE_DEVEL("reusable idle connection", FCGI_EV_STRM_END, fconn->conn);
3581 return;
3582 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003583 else if (MT_LIST_ISEMPTY(&fconn->conn->list) &&
3584 fcgi_avail_streams(fconn->conn) > 0 && objt_server(fconn->conn->target)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003585 LIST_ADD(&__objt_server(fconn->conn->target)->available_conns[tid], mt_list_to_list(&fconn->conn->list));
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003586 }
Christopher Faulet29ae7ff2020-07-01 15:51:46 +02003587 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003588 }
3589
3590 /* We don't want to close right now unless we're removing the last
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003591 * stream and the connection is in error.
Christopher Faulet99eff652019-08-11 23:11:30 +02003592 */
3593 if (fcgi_conn_is_dead(fconn)) {
3594 /* no more stream will come, kill it now */
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003595 TRACE_DEVEL("leaving, killing dead connection", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003596 fcgi_release(fconn);
3597 }
3598 else if (fconn->task) {
3599 fconn->task->expire = tick_add(now_ms, (fconn->state == FCGI_CS_CLOSED ? fconn->shut_timeout : fconn->timeout));
3600 task_queue(fconn->task);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003601 TRACE_DEVEL("leaving, refreshing connection's timeout", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003602 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003603 else
3604 TRACE_DEVEL("leaving", FCGI_EV_STRM_END, fconn->conn);
Christopher Faulet99eff652019-08-11 23:11:30 +02003605}
3606
3607
3608/* Performs a synchronous or asynchronous shutr(). */
3609static void fcgi_do_shutr(struct fcgi_strm *fstrm)
3610{
3611 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003612
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003613 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3614
Christopher Faulet99eff652019-08-11 23:11:30 +02003615 if (fstrm->state == FCGI_SS_CLOSED)
3616 goto done;
3617
3618 /* a connstream may require us to immediately kill the whole connection
3619 * for example because of a "tcp-request content reject" rule that is
3620 * normally used to limit abuse.
3621 */
3622 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003623 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3624 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003625 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003626 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003627 else if (fstrm->flags & FCGI_SF_BEGIN_SENT) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003628 TRACE_STATE("no headers sent yet, trying a retryable abort", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003629 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3630 !fcgi_strm_send_abort(fconn, fstrm))
3631 goto add_to_list;
3632 }
3633
3634 fcgi_strm_close(fstrm);
3635
3636 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3637 tasklet_wakeup(fconn->wait_event.tasklet);
3638 done:
3639 fstrm->flags &= ~FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003640 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003641 return;
3642
3643 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003644 /* Let the handler know we want to shutr, and add ourselves to the
3645 * send list if not yet done. fcgi_deferred_shut() will be
3646 * automatically called via the shut_tl tasklet when there's room
3647 * again.
3648 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003649 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003650 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003651 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3652 }
3653 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003654 fstrm->flags |= FCGI_SF_WANT_SHUTR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003655 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003656 return;
3657}
3658
3659/* Performs a synchronous or asynchronous shutw(). */
3660static void fcgi_do_shutw(struct fcgi_strm *fstrm)
3661{
3662 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003663
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003664 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3665
Christopher Faulet99eff652019-08-11 23:11:30 +02003666 if (fstrm->state != FCGI_SS_HLOC || fstrm->state == FCGI_SS_CLOSED)
3667 goto done;
3668
3669 if (fstrm->state != FCGI_SS_ERROR && (fstrm->flags & FCGI_SF_BEGIN_SENT)) {
3670 if (!(fstrm->flags & (FCGI_SF_ES_SENT|FCGI_SF_ABRT_SENT)) &&
3671 !fcgi_strm_send_abort(fconn, fstrm))
3672 goto add_to_list;
3673
3674 if (fstrm->state == FCGI_SS_HREM)
3675 fcgi_strm_close(fstrm);
3676 else
3677 fstrm->state = FCGI_SS_HLOC;
3678 } else {
3679 /* a connstream may require us to immediately kill the whole connection
3680 * for example because of a "tcp-request content reject" rule that is
3681 * normally used to limit abuse.
3682 */
3683 if ((fstrm->flags & FCGI_SF_KILL_CONN) &&
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003684 !(fconn->flags & (FCGI_CF_ABRTS_SENT|FCGI_CF_ABRTS_FAILED))) {
3685 TRACE_STATE("stream wants to kill the connection", FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003686 fconn->state = FCGI_CS_CLOSED;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003687 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003688
3689 fcgi_strm_close(fstrm);
3690 }
3691
3692 if (!(fconn->wait_event.events & SUB_RETRY_SEND))
3693 tasklet_wakeup(fconn->wait_event.tasklet);
3694 done:
3695 fstrm->flags &= ~FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003696 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003697 return;
3698
3699 add_to_list:
Willy Tarreau7aad7032020-01-16 17:20:57 +01003700 /* Let the handler know we want to shutr, and add ourselves to the
3701 * send list if not yet done. fcgi_deferred_shut() will be
3702 * automatically called via the shut_tl tasklet when there's room
3703 * again.
3704 */
Christopher Faulet99eff652019-08-11 23:11:30 +02003705 if (!LIST_ADDED(&fstrm->send_list)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003706 if (fstrm->flags & (FCGI_SF_BLK_MBUSY|FCGI_SF_BLK_MROOM)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003707 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
3708 }
3709 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003710 fstrm->flags |= FCGI_SF_WANT_SHUTW;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003711 TRACE_LEAVE(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003712 return;
3713}
3714
Willy Tarreau7aad7032020-01-16 17:20:57 +01003715/* This is the tasklet referenced in fstrm->shut_tl, it is used for
Christopher Faulet99eff652019-08-11 23:11:30 +02003716 * deferred shutdowns when the fcgi_detach() was done but the mux buffer was full
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003717 * and prevented the last record from being emitted.
Christopher Faulet99eff652019-08-11 23:11:30 +02003718 */
3719static struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned short state)
3720{
3721 struct fcgi_strm *fstrm = ctx;
3722 struct fcgi_conn *fconn = fstrm->fconn;
3723
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003724 TRACE_ENTER(FCGI_EV_STRM_SHUT, fconn->conn, fstrm);
3725
Willy Tarreau7aad7032020-01-16 17:20:57 +01003726 if (fstrm->flags & FCGI_SF_NOTIFIED) {
3727 /* some data processing remains to be done first */
3728 goto end;
3729 }
3730
Christopher Faulet99eff652019-08-11 23:11:30 +02003731 if (fstrm->flags & FCGI_SF_WANT_SHUTW)
3732 fcgi_do_shutw(fstrm);
3733
3734 if (fstrm->flags & FCGI_SF_WANT_SHUTR)
3735 fcgi_do_shutr(fstrm);
3736
3737 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
3738 /* We're done trying to send, remove ourself from the send_list */
3739 LIST_DEL_INIT(&fstrm->send_list);
3740
3741 if (!fstrm->cs) {
3742 fcgi_strm_destroy(fstrm);
3743 if (fcgi_conn_is_dead(fconn))
3744 fcgi_release(fconn);
3745 }
3746 }
Willy Tarreau7aad7032020-01-16 17:20:57 +01003747 end:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003748 TRACE_LEAVE(FCGI_EV_STRM_SHUT);
Christopher Faulet99eff652019-08-11 23:11:30 +02003749 return NULL;
3750}
3751
3752/* shutr() called by the conn_stream (mux_ops.shutr) */
3753static void fcgi_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3754{
3755 struct fcgi_strm *fstrm = cs->ctx;
3756
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003757 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003758 if (cs->flags & CS_FL_KILL_CONN)
3759 fstrm->flags |= FCGI_SF_KILL_CONN;
3760
3761 if (!mode)
3762 return;
3763
3764 fcgi_do_shutr(fstrm);
3765}
3766
3767/* shutw() called by the conn_stream (mux_ops.shutw) */
3768static void fcgi_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3769{
3770 struct fcgi_strm *fstrm = cs->ctx;
3771
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003772 TRACE_POINT(FCGI_EV_STRM_SHUT, fstrm->fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003773 if (cs->flags & CS_FL_KILL_CONN)
3774 fstrm->flags |= FCGI_SF_KILL_CONN;
3775
3776 fcgi_do_shutw(fstrm);
3777}
3778
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003779/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3780 * event subscriber <es> is not allowed to change from a previous call as long
3781 * as at least one event is still subscribed. The <event_type> must only be a
3782 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Christopher Faulet99eff652019-08-11 23:11:30 +02003783 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003784static int fcgi_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003785{
Christopher Faulet99eff652019-08-11 23:11:30 +02003786 struct fcgi_strm *fstrm = cs->ctx;
3787 struct fcgi_conn *fconn = fstrm->fconn;
3788
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003789 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003790 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003791
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003792 es->events |= event_type;
3793 fstrm->subs = es;
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003794
3795 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003796 TRACE_DEVEL("unsubscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003797
Christopher Faulet99eff652019-08-11 23:11:30 +02003798 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003799 TRACE_DEVEL("unsubscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003800 if (!LIST_ADDED(&fstrm->send_list))
3801 LIST_ADDQ(&fconn->send_list, &fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003802 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003803 return 0;
3804}
3805
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003806/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3807 * (undo fcgi_subscribe). The <es> pointer is not allowed to differ from the one
3808 * passed to the subscribe() call. It always returns zero.
Christopher Faulet99eff652019-08-11 23:11:30 +02003809 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003810static int fcgi_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet99eff652019-08-11 23:11:30 +02003811{
Christopher Faulet99eff652019-08-11 23:11:30 +02003812 struct fcgi_strm *fstrm = cs->ctx;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003813 struct fcgi_conn *fconn = fstrm->fconn;
Christopher Faulet99eff652019-08-11 23:11:30 +02003814
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003815 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003816 BUG_ON(fstrm->subs && fstrm->subs != es);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003817
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003818 es->events &= ~event_type;
3819 if (!es->events)
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003820 fstrm->subs = NULL;
3821
3822 if (event_type & SUB_RETRY_RECV)
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003823 TRACE_DEVEL("subscribe(recv)", FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003824
Christopher Faulet99eff652019-08-11 23:11:30 +02003825 if (event_type & SUB_RETRY_SEND) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003826 TRACE_DEVEL("subscribe(send)", FCGI_EV_STRM_SEND, fconn->conn, fstrm);
Willy Tarreau8907e4d2020-01-16 17:55:37 +01003827 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Willy Tarreau7aad7032020-01-16 17:20:57 +01003828 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
3829 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02003830 }
3831 return 0;
3832}
3833
3834/* Called from the upper layer, to receive data */
3835static size_t fcgi_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3836{
3837 struct fcgi_strm *fstrm = cs->ctx;
3838 struct fcgi_conn *fconn = fstrm->fconn;
3839 size_t ret = 0;
3840
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003841 TRACE_ENTER(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
3842
Christopher Faulet99eff652019-08-11 23:11:30 +02003843 if (!(fconn->flags & FCGI_CF_DEM_SALLOC))
3844 ret = fcgi_strm_parse_response(fstrm, buf, count);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003845 else
3846 TRACE_STATE("fstrm rxbuf not allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003847
Christopher Faulet76014fd2019-12-10 11:47:22 +01003848 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 +02003849 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
3850 else {
3851 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet76014fd2019-12-10 11:47:22 +01003852 if (fstrm->state == FCGI_SS_ERROR || (fstrm->flags & FCGI_SF_H1_PARSING_DONE)) {
Christopher Faulet99eff652019-08-11 23:11:30 +02003853 cs->flags |= CS_FL_EOI;
3854 if (!(fstrm->h1m.flags & (H1_MF_VER_11|H1_MF_XFER_LEN)))
3855 cs->flags |= CS_FL_EOS;
3856 }
3857 if (conn_xprt_read0_pending(fconn->conn))
3858 cs->flags |= CS_FL_EOS;
3859 if (cs->flags & CS_FL_ERR_PENDING)
3860 cs->flags |= CS_FL_ERROR;
3861 fcgi_release_buf(fconn, &fstrm->rxbuf);
3862 }
3863
3864 if (ret && fconn->dsi == fstrm->id) {
3865 /* demux is blocking on this stream's buffer */
3866 fconn->flags &= ~FCGI_CF_DEM_SFULL;
3867 fcgi_conn_restart_reading(fconn, 1);
3868 }
3869
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003870 TRACE_LEAVE(FCGI_EV_STRM_RECV, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003871 return ret;
3872}
3873
3874
Christopher Faulet99eff652019-08-11 23:11:30 +02003875/* Called from the upper layer, to send data from buffer <buf> for no more than
3876 * <count> bytes. Returns the number of bytes effectively sent. Some status
3877 * flags may be updated on the conn_stream.
3878 */
3879static size_t fcgi_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3880{
3881 struct fcgi_strm *fstrm = cs->ctx;
3882 struct fcgi_conn *fconn = fstrm->fconn;
3883 size_t total = 0;
3884 size_t ret;
3885 struct htx *htx = NULL;
3886 struct htx_sl *sl;
3887 struct htx_blk *blk;
3888 uint32_t bsize;
3889
Willy Tarreau022e5e52020-09-10 09:33:15 +02003890 TRACE_ENTER(FCGI_EV_STRM_SEND, fconn->conn, fstrm, 0, (size_t[]){count});
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003891
Christopher Faulet99eff652019-08-11 23:11:30 +02003892 /* If we were not just woken because we wanted to send but couldn't,
3893 * and there's somebody else that is waiting to send, do nothing,
3894 * we will subscribe later and be put at the end of the list
3895 */
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003896 if (!(fstrm->flags & FCGI_SF_NOTIFIED) && !LIST_ISEMPTY(&fconn->send_list)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003897 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 +02003898 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003899 }
Willy Tarreauf11be0e2020-01-16 16:59:45 +01003900 fstrm->flags &= ~FCGI_SF_NOTIFIED;
Christopher Faulet99eff652019-08-11 23:11:30 +02003901
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003902 if (fconn->state < FCGI_CS_RECORD_H) {
3903 TRACE_STATE("connection not ready, leaving", FCGI_EV_STRM_SEND|FCGI_EV_FSTRM_BLK, fconn->conn, fstrm);
Christopher Faulet99eff652019-08-11 23:11:30 +02003904 return 0;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003905 }
Christopher Faulet99eff652019-08-11 23:11:30 +02003906
3907 htx = htxbuf(buf);
3908 if (fstrm->id == 0) {
3909 int32_t id = fcgi_conn_get_next_sid(fconn);
3910
3911 if (id < 0) {
3912 fcgi_strm_close(fstrm);
3913 cs->flags |= CS_FL_ERROR;
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003914 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 +02003915 return 0;
3916 }
3917
3918 eb32_delete(&fstrm->by_id);
3919 fstrm->by_id.key = fstrm->id = id;
3920 fconn->max_id = id;
3921 fconn->nb_reserved--;
3922 eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
3923
3924
3925 /* Check if length of the body is known or if the message is
3926 * full. Otherwise, the request is invalid.
3927 */
3928 sl = http_get_stline(htx);
3929 if (!sl || (!(sl->flags & HTX_SL_F_CLEN) && (htx_get_tail_type(htx) != HTX_BLK_EOM))) {
3930 htx->flags |= HTX_FL_PARSING_ERROR;
3931 fcgi_strm_error(fstrm);
3932 goto done;
3933 }
3934 }
3935
3936 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT)) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003937 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 +02003938 if (!fcgi_strm_send_begin_request(fconn, fstrm))
3939 goto done;
3940 }
3941
3942 if (!(fstrm->flags & FCGI_SF_OUTGOING_DATA) && count)
3943 fstrm->flags |= FCGI_SF_OUTGOING_DATA;
3944
Christopher Fauletfe410d62020-05-19 15:13:00 +02003945 while (fstrm->state < FCGI_SS_HLOC && !(fstrm->flags & FCGI_SF_BLK_ANY) &&
Christopher Faulet99eff652019-08-11 23:11:30 +02003946 count && !htx_is_empty(htx)) {
3947 blk = htx_get_head_blk(htx);
William Lallemand13ed9fa2019-09-25 21:21:57 +02003948 ALREADY_CHECKED(blk);
Christopher Faulet99eff652019-08-11 23:11:30 +02003949 bsize = htx_get_blksz(blk);
3950
3951 switch (htx_get_blk_type(blk)) {
3952 case HTX_BLK_REQ_SL:
3953 case HTX_BLK_HDR:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003954 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 +02003955 ret = fcgi_strm_send_params(fconn, fstrm, htx);
3956 if (!ret) {
3957 goto done;
3958 }
3959 total += ret;
3960 count -= ret;
3961 break;
3962
3963 case HTX_BLK_EOH:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003964 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 +02003965 ret = fcgi_strm_send_empty_params(fconn, fstrm);
3966 if (!ret)
3967 goto done;
3968 goto remove_blk;
3969
3970 case HTX_BLK_DATA:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003971 TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx);
Christopher Faulet99eff652019-08-11 23:11:30 +02003972 ret = fcgi_strm_send_stdin(fconn, fstrm, htx, count, buf);
3973 if (ret > 0) {
3974 htx = htx_from_buf(buf);
3975 total += ret;
3976 count -= ret;
3977 if (ret < bsize)
3978 goto done;
3979 }
3980 break;
3981
3982 case HTX_BLK_EOM:
Christopher Faulet5c0f8592019-10-04 15:21:17 +02003983 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 +02003984 ret = fcgi_strm_send_empty_stdin(fconn, fstrm);
3985 if (!ret)
3986 goto done;
3987 goto remove_blk;
3988
3989 default:
3990 remove_blk:
3991 htx_remove_blk(htx, blk);
3992 total += bsize;
3993 count -= bsize;
3994 break;
3995 }
3996 }
3997
3998 done:
3999 if (fstrm->state >= FCGI_SS_HLOC) {
4000 /* trim any possibly pending data after we close (extra CR-LF,
4001 * unprocessed trailers, abnormal extra data, ...)
4002 */
4003 total += count;
4004 count = 0;
4005 }
4006
4007 if (fstrm->state == FCGI_SS_ERROR) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004008 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 +02004009 cs_set_error(cs);
4010 if (!(fstrm->flags & FCGI_SF_BEGIN_SENT) || fcgi_strm_send_abort(fconn, fstrm))
4011 fcgi_strm_close(fstrm);
4012 }
4013
4014 if (htx)
4015 htx_to_buf(htx, buf);
4016
Christopher Faulet99eff652019-08-11 23:11:30 +02004017 if (total > 0) {
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004018 if (!(fconn->wait_event.events & SUB_RETRY_SEND)) {
4019 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 +02004020 tasklet_wakeup(fconn->wait_event.tasklet);
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004021 }
Christopher Faulet99eff652019-08-11 23:11:30 +02004022
4023 /* Ok we managed to send something, leave the send_list */
Willy Tarreau7aad7032020-01-16 17:20:57 +01004024 if (!(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW)))
4025 LIST_DEL_INIT(&fstrm->send_list);
Christopher Faulet99eff652019-08-11 23:11:30 +02004026 }
Christopher Faulet5c0f8592019-10-04 15:21:17 +02004027
4028 TRACE_LEAVE(FCGI_EV_STRM_SEND, fconn->conn, fstrm, htx, (size_t[]){total});
Christopher Faulet99eff652019-08-11 23:11:30 +02004029 return total;
4030}
4031
4032/* for debugging with CLI's "show fd" command */
4033static void fcgi_show_fd(struct buffer *msg, struct connection *conn)
4034{
4035 struct fcgi_conn *fconn = conn->ctx;
4036 struct fcgi_strm *fstrm = NULL;
4037 struct eb32_node *node;
4038 int send_cnt = 0;
4039 int tree_cnt = 0;
4040 int orph_cnt = 0;
4041 struct buffer *hmbuf, *tmbuf;
4042
4043 if (!fconn)
4044 return;
4045
4046 list_for_each_entry(fstrm, &fconn->send_list, send_list)
4047 send_cnt++;
4048
4049 fstrm = NULL;
4050 node = eb32_first(&fconn->streams_by_id);
4051 while (node) {
4052 fstrm = container_of(node, struct fcgi_strm, by_id);
4053 tree_cnt++;
4054 if (!fstrm->cs)
4055 orph_cnt++;
4056 node = eb32_next(node);
4057 }
4058
4059 hmbuf = br_head(fconn->mbuf);
4060 tmbuf = br_tail(fconn->mbuf);
4061 chunk_appendf(msg, " fconn.st0=%d .maxid=%d .flg=0x%04x .nbst=%u"
4062 " .nbcs=%u .send_cnt=%d .tree_cnt=%d .orph_cnt=%d .sub=%d "
4063 ".dsi=%d .dbuf=%u@%p+%u/%u .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
4064 fconn->state, fconn->max_id, fconn->flags,
4065 fconn->nb_streams, fconn->nb_cs, send_cnt, tree_cnt, orph_cnt,
4066 fconn->wait_event.events, fconn->dsi,
4067 (unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
4068 (unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf),
4069 br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
4070 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
4071 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
4072 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
4073 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
4074
4075 if (fstrm) {
4076 chunk_appendf(msg, " last_fstrm=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4077 fstrm, fstrm->id, fstrm->flags,
4078 (unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
4079 (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
4080 fstrm->cs);
4081 if (fstrm->cs)
4082 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4083 fstrm->cs->flags, fstrm->cs->data);
4084 }
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004085}
4086
4087/* Migrate the the connection to the current thread.
4088 * Return 0 if successful, non-zero otherwise.
4089 * Expected to be called with the old thread lock held.
4090 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004091static int fcgi_takeover(struct connection *conn, int orig_tid)
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004092{
4093 struct fcgi_conn *fcgi = conn->ctx;
Willy Tarreau88d18f82020-07-01 16:39:33 +02004094 struct task *task;
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004095
4096 if (fd_takeover(conn->handle.fd, conn) != 0)
4097 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02004098
4099 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
4100 /* We failed to takeover the xprt, even if the connection may
4101 * still be valid, flag it as error'd, as we have already
4102 * taken over the fd, and wake the tasklet, so that it will
4103 * destroy it.
4104 */
4105 conn->flags |= CO_FL_ERROR;
4106 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
4107 return -1;
4108 }
4109
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004110 if (fcgi->wait_event.events)
4111 fcgi->conn->xprt->unsubscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4112 fcgi->wait_event.events, &fcgi->wait_event);
4113 /* To let the tasklet know it should free itself, and do nothing else,
4114 * set its context to NULL;
4115 */
4116 fcgi->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02004117 tasklet_wakeup_on(fcgi->wait_event.tasklet, orig_tid);
Willy Tarreau88d18f82020-07-01 16:39:33 +02004118
4119 task = fcgi->task;
4120 if (task) {
4121 task->context = NULL;
4122 fcgi->task = NULL;
4123 __ha_barrier_store();
4124 task_kill(task);
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004125
4126 fcgi->task = task_new(tid_bit);
4127 if (!fcgi->task) {
4128 fcgi_release(fcgi);
4129 return -1;
4130 }
4131 fcgi->task->process = fcgi_timeout_task;
4132 fcgi->task->context = fcgi;
4133 }
4134 fcgi->wait_event.tasklet = tasklet_new();
4135 if (!fcgi->wait_event.tasklet) {
4136 fcgi_release(fcgi);
4137 return -1;
4138 }
4139 fcgi->wait_event.tasklet->process = fcgi_io_cb;
4140 fcgi->wait_event.tasklet->context = fcgi;
4141 fcgi->conn->xprt->subscribe(fcgi->conn, fcgi->conn->xprt_ctx,
4142 SUB_RETRY_RECV, &fcgi->wait_event);
4143
4144 return 0;
Christopher Faulet99eff652019-08-11 23:11:30 +02004145}
4146
4147/****************************************/
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004148/* MUX initialization and instantiation */
Christopher Faulet99eff652019-08-11 23:11:30 +02004149/****************************************/
4150
4151/* The mux operations */
4152static const struct mux_ops mux_fcgi_ops = {
4153 .init = fcgi_init,
4154 .wake = fcgi_wake,
4155 .attach = fcgi_attach,
4156 .get_first_cs = fcgi_get_first_cs,
4157 .detach = fcgi_detach,
4158 .destroy = fcgi_destroy,
4159 .avail_streams = fcgi_avail_streams,
4160 .used_streams = fcgi_used_streams,
4161 .rcv_buf = fcgi_rcv_buf,
4162 .snd_buf = fcgi_snd_buf,
4163 .subscribe = fcgi_subscribe,
4164 .unsubscribe = fcgi_unsubscribe,
4165 .shutr = fcgi_shutr,
4166 .shutw = fcgi_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004167 .ctl = fcgi_ctl,
Christopher Faulet99eff652019-08-11 23:11:30 +02004168 .show_fd = fcgi_show_fd,
Olivier Houcharda41bb0b2020-03-10 18:46:06 +01004169 .takeover = fcgi_takeover,
Christopher Faulet99eff652019-08-11 23:11:30 +02004170 .flags = MX_FL_HTX,
4171 .name = "FCGI",
4172};
4173
4174
4175/* this mux registers FCGI proto */
4176static struct mux_proto_list mux_proto_fcgi =
4177{ .token = IST("fcgi"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BE, .mux = &mux_fcgi_ops };
4178
4179INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_fcgi);
4180
4181/*
4182 * Local variables:
4183 * c-indent-level: 8
4184 * c-basic-offset: 8
4185 * End:
4186 */