blob: 345161425e0e0b7b6098d77e967aec1656a01192 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 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 */
Willy Tarreaub2551052020-06-09 09:07:15 +020012#include <import/ebistree.h>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010045/* 0x00000080 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010048#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no conn-stream is attached to the connection */
49#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream is attached to the connection */
50#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
51 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
52#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
53#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
54#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
55/* 0x00002000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020056
Christopher Faulet3ced1d12020-11-27 16:44:01 +010057#define H1C_F_WAIT_OPPOSITE 0x00010000 /* Don't read more data for now, waiting sync with opposite side */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050058#define H1C_F_WANT_SPLICE 0x00020000 /* Don't read into a buffer because we want to use or we are using splicing */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010059#define H1C_F_ERR_PENDING 0x00040000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
60#define H1C_F_WAIT_NEXT_REQ 0x00080000 /* waiting for the next request to start, use keep-alive timeout */
61#define H1C_F_UPG_H2C 0x00100000 /* set if an upgrade to h2 should be done */
62#define H1C_F_CO_MSG_MORE 0x00200000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
63#define H1C_F_CO_STREAMER 0x00400000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050064/* 0x00800000 - 0x40000000 unused */
Christopher Faulet129817b2018-09-20 16:14:40 +020065
Christopher Faulet3ced1d12020-11-27 16:44:01 +010066#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020067
Christopher Faulet51dbc942018-09-13 09:05:15 +020068/*
69 * H1 Stream flags (32 bits)
70 */
Christopher Faulet129817b2018-09-20 16:14:40 +020071#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050072/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020073#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020074#define H1S_F_WANT_KAL 0x00000010
75#define H1S_F_WANT_TUN 0x00000020
76#define H1S_F_WANT_CLO 0x00000040
77#define H1S_F_WANT_MSK 0x00000070
78#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020079
Christopher Faulet76014fd2019-12-10 11:47:22 +010080#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020081#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
82#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
83#define H1S_F_ERROR 0x00001800 /* stream error mask */
84
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020085#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020086#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020087
88/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020089struct h1c {
90 struct connection *conn;
91 struct proxy *px;
92 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020093 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094 struct buffer ibuf; /* Input buffer to store data before parsing */
95 struct buffer obuf; /* Output buffer to store data after reformatting */
96
97 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
98 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
99
100 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100101 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200102 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
103 int timeout; /* client/server timeout duration */
104 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200105};
106
107/* H1 stream descriptor */
108struct h1s {
109 struct h1c *h1c;
110 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100111 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200112
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100113 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200114
Olivier Houchardf502aca2018-12-14 19:42:40 +0100115 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200116 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200117 struct h1m req;
118 struct h1m res;
119
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500120 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200121 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200122};
123
Christopher Faulet98fbe952019-07-22 16:18:24 +0200124/* Map of headers used to convert outgoing headers */
125struct h1_hdrs_map {
126 char *name;
127 struct eb_root map;
128};
129
130/* An entry in a headers map */
131struct h1_hdr_entry {
132 struct ist name;
133 struct ebpt_node node;
134};
135
136/* Declare the headers map */
137static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
138
139
Christopher Faulet6b81df72019-10-01 22:08:43 +0200140/* trace source and events */
141static void h1_trace(enum trace_level level, uint64_t mask,
142 const struct trace_source *src,
143 const struct ist where, const struct ist func,
144 const void *a1, const void *a2, const void *a3, const void *a4);
145
146/* The event representation is split like this :
147 * h1c - internal H1 connection
148 * h1s - internal H1 stream
149 * strm - application layer
150 * rx - data receipt
151 * tx - data transmission
152 *
153 */
154static const struct trace_event h1_trace_events[] = {
155#define H1_EV_H1C_NEW (1ULL << 0)
156 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
157#define H1_EV_H1C_RECV (1ULL << 1)
158 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
159#define H1_EV_H1C_SEND (1ULL << 2)
160 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
161#define H1_EV_H1C_BLK (1ULL << 3)
162 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
163#define H1_EV_H1C_WAKE (1ULL << 4)
164 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
165#define H1_EV_H1C_END (1ULL << 5)
166 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
167#define H1_EV_H1C_ERR (1ULL << 6)
168 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
169
170#define H1_EV_RX_DATA (1ULL << 7)
171 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
172#define H1_EV_RX_EOI (1ULL << 8)
173 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
174#define H1_EV_RX_HDRS (1ULL << 9)
175 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
176#define H1_EV_RX_BODY (1ULL << 10)
177 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
178#define H1_EV_RX_TLRS (1ULL << 11)
179 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
180
181#define H1_EV_TX_DATA (1ULL << 12)
182 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
183#define H1_EV_TX_EOI (1ULL << 13)
184 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
185#define H1_EV_TX_HDRS (1ULL << 14)
186 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
187#define H1_EV_TX_BODY (1ULL << 15)
188 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
189#define H1_EV_TX_TLRS (1ULL << 16)
190 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
191
192#define H1_EV_H1S_NEW (1ULL << 17)
193 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
194#define H1_EV_H1S_BLK (1ULL << 18)
195 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
196#define H1_EV_H1S_END (1ULL << 19)
197 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
198#define H1_EV_H1S_ERR (1ULL << 20)
199 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
200
201#define H1_EV_STRM_NEW (1ULL << 21)
202 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
203#define H1_EV_STRM_RECV (1ULL << 22)
204 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
205#define H1_EV_STRM_SEND (1ULL << 23)
206 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
207#define H1_EV_STRM_WAKE (1ULL << 24)
208 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
209#define H1_EV_STRM_SHUT (1ULL << 25)
210 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
211#define H1_EV_STRM_END (1ULL << 26)
212 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
213#define H1_EV_STRM_ERR (1ULL << 27)
214 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
215
216 { }
217};
218
219static const struct name_desc h1_trace_lockon_args[4] = {
220 /* arg1 */ { /* already used by the connection */ },
221 /* arg2 */ { .name="h1s", .desc="H1 stream" },
222 /* arg3 */ { },
223 /* arg4 */ { }
224};
225
226static const struct name_desc h1_trace_decoding[] = {
227#define H1_VERB_CLEAN 1
228 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
229#define H1_VERB_MINIMAL 2
230 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
231#define H1_VERB_SIMPLE 3
232 { .name="simple", .desc="add request/response status line or htx info when available" },
233#define H1_VERB_ADVANCED 4
234 { .name="advanced", .desc="add header fields or frame decoding when available" },
235#define H1_VERB_COMPLETE 5
236 { .name="complete", .desc="add full data dump when available" },
237 { /* end */ }
238};
239
240static struct trace_source trace_h1 = {
241 .name = IST("h1"),
242 .desc = "HTTP/1 multiplexer",
243 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
244 .default_cb = h1_trace,
245 .known_events = h1_trace_events,
246 .lockon_args = h1_trace_lockon_args,
247 .decoding = h1_trace_decoding,
248 .report_events = ~0, // report everything by default
249};
250
251#define TRACE_SOURCE &trace_h1
252INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
253
Christopher Faulet51dbc942018-09-13 09:05:15 +0200254/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100255DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
256DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200257
Christopher Faulet51dbc942018-09-13 09:05:15 +0200258static int h1_recv(struct h1c *h1c);
259static int h1_send(struct h1c *h1c);
260static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100261/* h1_io_cb is exported to see it resolved in "show fd" */
262struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100263static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200264static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200265static void h1_wake_stream_for_recv(struct h1s *h1s);
266static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200267
Christopher Faulet6b81df72019-10-01 22:08:43 +0200268/* the H1 traces always expect that arg1, if non-null, is of type connection
269 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
270 * that arg3, if non-null, is a htx for rx/tx headers.
271 */
272static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
273 const struct ist where, const struct ist func,
274 const void *a1, const void *a2, const void *a3, const void *a4)
275{
276 const struct connection *conn = a1;
277 const struct h1c *h1c = conn ? conn->ctx : NULL;
278 const struct h1s *h1s = a2;
279 const struct htx *htx = a3;
280 const size_t *val = a4;
281
282 if (!h1c)
283 h1c = (h1s ? h1s->h1c : NULL);
284
285 if (!h1c || src->verbosity < H1_VERB_CLEAN)
286 return;
287
288 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200289 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200290
291 /* Display request and response states if h1s is defined */
292 if (h1s)
293 chunk_appendf(&trace_buf, " [%s, %s]",
294 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
295
296 if (src->verbosity == H1_VERB_CLEAN)
297 return;
298
299 /* Display the value to the 4th argument (level > STATE) */
300 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100301 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200302
303 /* Display status-line if possible (verbosity > MINIMAL) */
304 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
305 const struct htx_blk *blk = htx_get_head_blk(htx);
306 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
307 enum htx_blk_type type = htx_get_blk_type(blk);
308
309 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
310 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
311 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
312 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
313 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
314 }
315
316 /* Display h1c info and, if defined, h1s info (pointer + flags) */
317 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
318 if (h1s)
319 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
320
321 if (src->verbosity == H1_VERB_MINIMAL)
322 return;
323
324 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
325 if (src->level > TRACE_LEVEL_USER) {
326 if (src->verbosity == H1_VERB_COMPLETE ||
327 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
328 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
329 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
330 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
331 if (src->verbosity == H1_VERB_COMPLETE ||
332 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
333 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
334 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
335 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
336 }
337
338 /* Display htx info if defined (level > USER) */
339 if (src->level > TRACE_LEVEL_USER && htx) {
340 int full = 0;
341
342 /* Full htx info (level > STATE && verbosity > SIMPLE) */
343 if (src->level > TRACE_LEVEL_STATE) {
344 if (src->verbosity == H1_VERB_COMPLETE)
345 full = 1;
346 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
347 full = 1;
348 }
349
350 chunk_memcat(&trace_buf, "\n\t", 2);
351 htx_dump(&trace_buf, htx, full);
352 }
353}
354
355
Christopher Faulet51dbc942018-09-13 09:05:15 +0200356/*****************************************************/
357/* functions below are for dynamic buffer management */
358/*****************************************************/
359/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100360 * Indicates whether or not we may receive data. The rules are the following :
361 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200362 * must not attempt to receive
363 * - if we are waiting for the connection establishment, we must not attempt
364 * to receive
365 * - if an error was detected on the stream we must not attempt to receive
366 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100367 * - if the input buffer failed to be allocated or is full , we must not try
368 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200369 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200370 * - otherwise must may not attempt to receive
371 */
372static inline int h1_recv_allowed(const struct h1c *h1c)
373{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100374 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100375 TRACE_DEVEL("recv not allowed because of error on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200377 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200378
Willy Tarreau2febb842020-07-31 09:15:43 +0200379 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
380 TRACE_DEVEL("recv not allowed because of (error|read0|waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletcf56b992018-12-11 16:12:31 +0100381 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200382 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100383
Christopher Faulet119ac872020-09-30 17:33:22 +0200384 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
385 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
386 return 0;
387 }
388
Christopher Faulet089acd52020-09-21 10:57:52 +0200389 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
390 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200391 return 0;
392 }
393
Christopher Fauletd17ad822020-09-24 15:14:29 +0200394 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200395 return 1;
396
Christopher Faulet6b81df72019-10-01 22:08:43 +0200397 TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200398 return 0;
399}
400
401/*
402 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
403 * flags are used to figure what buffer was requested. It returns 1 if the
404 * allocation succeeds, in which case the connection is woken up, or 0 if it's
405 * impossible to wake up and we prefer to be woken up later.
406 */
407static int h1_buf_available(void *target)
408{
409 struct h1c *h1c = target;
410
411 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200412 TRACE_STATE("unblocking h1c, ibuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 h1c->flags &= ~H1C_F_IN_ALLOC;
414 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200415 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200416 return 1;
417 }
418
419 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200420 TRACE_STATE("unblocking h1s, obuf allocated", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200422 if (h1c->h1s)
423 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424 return 1;
425 }
426
Christopher Fauletd17ad822020-09-24 15:14:29 +0200427 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
428 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
429 h1c->flags &= ~H1C_F_IN_SALLOC;
430 tasklet_wakeup(h1c->wait_event.tasklet);
431 return 1;
432 }
433
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 return 0;
435}
436
437/*
438 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
439 */
440static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
441{
442 struct buffer *buf = NULL;
443
Willy Tarreau21046592020-02-26 10:39:36 +0100444 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200445 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
446 h1c->buf_wait.target = h1c;
447 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200448 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200449 }
450 return buf;
451}
452
453/*
454 * Release a buffer, if any, and try to wake up entities waiting in the buffer
455 * wait queue.
456 */
457static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
458{
459 if (bptr->size) {
460 b_free(bptr);
461 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
462 }
463}
464
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100465/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100466 * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100467 * not. This flag is only set when no H1S is attached and when the previous
468 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100469 */
470static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100472 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100474 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475}
476
Willy Tarreau00f18a32019-01-26 12:19:01 +0100477/* returns the number of streams still available on a connection */
478static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100479{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100480 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100481}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482
Christopher Faulet7a145d62020-08-05 11:31:16 +0200483/* Refresh the h1c task timeout if necessary */
484static void h1_refresh_timeout(struct h1c *h1c)
485{
486 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100487 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200488 /* half-closed or dead connections : switch to clientfin/serverfin
489 * timeouts so that we don't hang too long on clients that have
490 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200491 */
492 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200493 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
494 }
495 else if (b_data(&h1c->obuf)) {
496 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200497 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200498 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
499 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100500 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200501 /* front connections waiting for a stream need a timeout. */
502 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200503 TRACE_DEVEL("refreshing connection's timeout (alive front h1c without a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200504 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200505 else {
506 /* alive back connections of front connections with a conn-stream attached */
507 h1c->task->expire = TICK_ETERNITY;
508 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
509 }
510
Christopher Fauletdbe57792020-10-05 17:50:58 +0200511 /* Finally set the idle expiration date if shorter */
512 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200513 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
514 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200515 }
516}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200517
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200518static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200519{
520 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
521 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
522 h1c->idle_exp = TICK_ETERNITY;
523 return;
524 }
525
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100526 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200527 if (!tick_isset(h1c->idle_exp)) {
528 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
529 !b_data(&h1c->ibuf) && /* No input data */
530 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
531 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
532 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
533 }
534 else {
535 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
536 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
537 }
538 }
539 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100540 else if (h1c->flags & H1C_F_ST_EMBRYONIC) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200541 if (!tick_isset(h1c->idle_exp)) {
542 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
543 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
544 }
545 }
546 else { // CS_ATTACHED or SHUTDOWN
547 h1c->idle_exp = TICK_ETERNITY;
548 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
549 }
550}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200551/*****************************************************************/
552/* functions below are dedicated to the mux setup and management */
553/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200554
555/* returns non-zero if there are input data pending for stream h1s. */
556static inline size_t h1s_data_pending(const struct h1s *h1s)
557{
558 const struct h1m *h1m;
559
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200560 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200561 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100562 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200563
564 return b_data(&h1s->h1c->ibuf);
565}
566
Christopher Faulet16df1782020-12-04 16:47:41 +0100567/* Creates a new conn-stream and the associate stream. <input> is used as input
568 * buffer for the stream. On success, it is transferred to the stream and the
569 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
570 * mux must still take care of it. However, there is nothing special to do
571 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
572 * b_free() on it is always safe. This function returns the conn-stream on
573 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200574static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100575{
576 struct conn_stream *cs;
577
Christopher Faulet6b81df72019-10-01 22:08:43 +0200578 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200579 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200580 if (!cs) {
581 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100582 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200583 }
Christopher Faulet47365272018-10-31 17:40:50 +0100584 h1s->cs = cs;
585 cs->ctx = h1s;
586
587 if (h1s->flags & H1S_F_NOT_FIRST)
588 cs->flags |= CS_FL_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100589 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Christopher Faulet47365272018-10-31 17:40:50 +0100590
Christopher Faulet27182292020-07-03 15:08:49 +0200591 if (global.tune.options & GTUNE_USE_SPLICE) {
592 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100593 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200594 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100595
Christopher Faulet26256f82020-09-14 11:40:13 +0200596 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200597 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100598 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599 }
600
601 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100602 return cs;
603
604 err:
605 cs_free(cs);
606 h1s->cs = NULL;
607 return NULL;
608}
609
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200610static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200611{
612 struct h1s *h1s;
613
Christopher Faulet6b81df72019-10-01 22:08:43 +0200614 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
615
Christopher Faulet51dbc942018-09-13 09:05:15 +0200616 h1s = pool_alloc(pool_head_h1s);
617 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100618 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200619 h1s->h1c = h1c;
620 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200621 h1s->sess = NULL;
622 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100623 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100624 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200625 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200626
627 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100628 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200629
Christopher Faulet129817b2018-09-20 16:14:40 +0200630 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100631 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200632
633 h1s->status = 0;
634 h1s->meth = HTTP_METH_OTHER;
635
Christopher Faulet47365272018-10-31 17:40:50 +0100636 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
637 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100638 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100639
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200640 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
641 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200642
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200643 fail:
644 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
645 return NULL;
646}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100647
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200648static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
649{
650 struct session *sess = h1c->conn->owner;
651 struct h1s *h1s;
652
653 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
654
655 h1s = h1s_new(h1c);
656 if (!h1s)
657 goto fail;
658
659 h1s->sess = sess;
660
661 if (h1c->px->options2 & PR_O2_REQBUG_OK)
662 h1s->req.err_pos = -1;
663
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200664 h1c->idle_exp = TICK_ETERNITY;
665 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200666 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200667 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100668
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200669 fail:
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200670 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
671 return NULL;
672}
673
674static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
675{
676 struct h1s *h1s;
677
678 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
679
680 h1s = h1s_new(h1c);
681 if (!h1s)
682 goto fail;
683
684 h1s->cs = cs;
685 h1s->sess = sess;
686 cs->ctx = h1s;
687
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100688 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200689
690 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
691 h1s->res.err_pos = -1;
692
693 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
694 return h1s;
695
696 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200697 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100698 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699}
700
701static void h1s_destroy(struct h1s *h1s)
702{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200703 if (h1s) {
704 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200705
Christopher Faulet6b81df72019-10-01 22:08:43 +0200706 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200707 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200708
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100709 if (h1s->subs)
710 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200711
Christopher Fauletd17ad822020-09-24 15:14:29 +0200712 h1_release_buf(h1c, &h1s->rxbuf);
713
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100714 h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_WANT_SPLICE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200715 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
716 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200717 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100718 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200719 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
720 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100721
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100722 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100723 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100724 (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100725 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100726 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100727 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
728 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200729 else {
730 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100731 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200732 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200733 pool_free(pool_head_h1s, h1s);
734 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200735}
736
737/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200738 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500739 * to the existing conn_stream (for outgoing connections or for incoming ones
740 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200741 * establishment). <input> is always used as Input buffer and may contain
742 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
743 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200744 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200745static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
746 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200747{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200748 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100749 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200750 void *conn_ctx = conn->ctx;
751
752 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200753
754 h1c = pool_alloc(pool_head_h1c);
755 if (!h1c)
756 goto fail_h1c;
757 h1c->conn = conn;
758 h1c->px = proxy;
759
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100760 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200761 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200762 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200763 h1c->obuf = BUF_NULL;
764 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200765 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200766
Willy Tarreau21046592020-02-26 10:39:36 +0100767 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200768 h1c->wait_event.tasklet = tasklet_new();
769 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200770 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200771 h1c->wait_event.tasklet->process = h1_io_cb;
772 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100773 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200774 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775
Christopher Faulete9b70722019-04-08 10:46:02 +0200776 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200777 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100778 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
779 if (tick_isset(proxy->timeout.serverfin))
780 h1c->shut_timeout = proxy->timeout.serverfin;
781 } else {
782 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
783 if (tick_isset(proxy->timeout.clientfin))
784 h1c->shut_timeout = proxy->timeout.clientfin;
785 }
786 if (tick_isset(h1c->timeout)) {
787 t = task_new(tid_bit);
788 if (!t)
789 goto fail;
790
791 h1c->task = t;
792 t->process = h1_timeout_task;
793 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200794
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100795 t->expire = tick_add(now_ms, h1c->timeout);
796 }
797
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100798 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200799
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200800 if (h1c->flags & H1C_F_IS_BACK) {
801 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200802 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
803 goto fail;
804 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100805
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200806 if (t) {
807 h1_set_idle_expiration(h1c);
808 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100809 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200810 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100811
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200812 /* prepare to read something */
813 if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200814 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200815
816 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200817 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200818 return 0;
819
820 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200821 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200822 if (h1c->wait_event.tasklet)
823 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200824 pool_free(pool_head_h1c, h1c);
825 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200826 conn->ctx = conn_ctx; // restore saved context
827 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200828 return -1;
829}
830
Christopher Faulet73c12072019-04-08 11:23:22 +0200831/* release function. This one should be called to free all resources allocated
832 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200833 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200834static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200835{
Christopher Faulet61840e72019-04-15 09:33:32 +0200836 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200837
Christopher Faulet6b81df72019-10-01 22:08:43 +0200838 TRACE_POINT(H1_EV_H1C_END);
839
Christopher Faulet51dbc942018-09-13 09:05:15 +0200840 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200841 /* The connection must be aattached to this mux to be released */
842 if (h1c->conn && h1c->conn->ctx == h1c)
843 conn = h1c->conn;
844
Christopher Faulet6b81df72019-10-01 22:08:43 +0200845 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
846
Christopher Faulet61840e72019-04-15 09:33:32 +0200847 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200848 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200849 /* Make sure we're no longer subscribed to anything */
850 if (h1c->wait_event.events)
851 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
852 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200853 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200854 /* connection successfully upgraded to H2, this
855 * mux was already released */
856 return;
857 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200858 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200859 sess_log(conn->owner); /* Log if the upgrade failed */
860 }
861
Olivier Houchard45c44372019-06-11 14:06:23 +0200862
Willy Tarreau21046592020-02-26 10:39:36 +0100863 if (MT_LIST_ADDED(&h1c->buf_wait.list))
864 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200865
866 h1_release_buf(h1c, &h1c->ibuf);
867 h1_release_buf(h1c, &h1c->obuf);
868
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100869 if (h1c->task) {
870 h1c->task->context = NULL;
871 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
872 h1c->task = NULL;
873 }
874
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200875 if (h1c->wait_event.tasklet)
876 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200877
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200879 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100880 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200881 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200882 pool_free(pool_head_h1c, h1c);
883 }
884
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200885 if (conn) {
886 conn->mux = NULL;
887 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200888 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200889
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200890 conn_stop_tracking(conn);
891 conn_full_close(conn);
892 if (conn->destroy_cb)
893 conn->destroy_cb(conn);
894 conn_free(conn);
895 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200896}
897
898/******************************************************/
899/* functions below are for the H1 protocol processing */
900/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200901/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
902 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200903 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100904static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200905{
Christopher Faulet570d1612018-11-26 11:13:57 +0100906 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200907
Christopher Faulet570d1612018-11-26 11:13:57 +0100908 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200909 (*(p + 5) > '1' ||
910 (*(p + 5) == '1' && *(p + 7) >= '1')))
911 h1m->flags |= H1_MF_VER_11;
912}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200913
Christopher Faulet9768c262018-10-22 09:34:31 +0200914/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
915 * greater or equal to 1.1
916 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100917static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200918{
Christopher Faulet570d1612018-11-26 11:13:57 +0100919 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920
Christopher Faulet570d1612018-11-26 11:13:57 +0100921 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200922 (*(p + 5) > '1' ||
923 (*(p + 5) == '1' && *(p + 7) >= '1')))
924 h1m->flags |= H1_MF_VER_11;
925}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200926
Christopher Fauletf2824e62018-10-01 12:12:37 +0200927/* Deduce the connection mode of the client connection, depending on the
928 * configuration and the H1 message flags. This function is called twice, the
929 * first time when the request is parsed and the second time when the response
930 * is parsed.
931 */
932static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
933{
934 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200935
936 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100937 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100938 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100939 h1s->status == 101) {
940 /* Either we've established an explicit tunnel, or we're
941 * switching the protocol. In both cases, we're very unlikely to
942 * understand the next protocols. We have to switch to tunnel
943 * mode, so that we transfer the request and responses then let
944 * this protocol pass unmodified. When we later implement
945 * specific parsers for such protocols, we'll want to check the
946 * Upgrade header which contains information about that protocol
947 * for responses with status 101 (eg: see RFC2817 about TLS).
948 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200949 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200950 TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100951 }
952 else if (h1s->flags & H1S_F_WANT_KAL) {
953 /* By default the client is in KAL mode. CLOSE mode mean
954 * it is imposed by the client itself. So only change
955 * KAL mode here. */
956 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
957 /* no length known or explicit close => close */
958 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200959 TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100960 }
961 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
962 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500963 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100964 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200965 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100966 }
967 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200968 }
969 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100970 /* Input direction: first pass */
971 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
972 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200973 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200974 TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100975 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 }
977
978 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200979 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200981 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
982 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983}
984
985/* Deduce the connection mode of the client connection, depending on the
986 * configuration and the H1 message flags. This function is called twice, the
987 * first time when the request is parsed and the second time when the response
988 * is parsed.
989 */
990static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
991{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100992 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100994 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200995
Christopher Fauletf2824e62018-10-01 12:12:37 +0200996 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100997 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100998 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100999 h1s->status == 101) {
1000 /* Either we've established an explicit tunnel, or we're
1001 * switching the protocol. In both cases, we're very unlikely to
1002 * understand the next protocols. We have to switch to tunnel
1003 * mode, so that we transfer the request and responses then let
1004 * this protocol pass unmodified. When we later implement
1005 * specific parsers for such protocols, we'll want to check the
1006 * Upgrade header which contains information about that protocol
1007 * for responses with status 101 (eg: see RFC2817 about TLS).
1008 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001011 }
1012 else if (h1s->flags & H1S_F_WANT_KAL) {
1013 /* By default the server is in KAL mode. CLOSE mode mean
1014 * it is imposed by haproxy itself. So only change KAL
1015 * mode here. */
1016 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1017 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1018 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1019 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001020 TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001021 }
1022 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001023 }
Christopher Faulet70033782018-12-05 13:50:11 +01001024 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001025 /* Output direction: first pass */
1026 if (h1m->flags & H1_MF_CONN_CLO) {
1027 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001028 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001029 TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001030 }
1031 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1032 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1033 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1034 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1035 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1036 /* no explicit keep-alive option httpclose/server-close => close */
1037 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001038 TRACE_STATE("force close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001039 }
Christopher Faulet70033782018-12-05 13:50:11 +01001040 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001041
1042 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001043 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001044 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001045 TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1046 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047}
1048
Christopher Fauletb992af02019-03-28 15:42:24 +01001049static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050{
1051 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001052
1053 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1054 * token is found
1055 */
1056 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001057 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001058
1059 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001060 if (!(h1m->flags & H1_MF_VER_11)) {
1061 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001062 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001063 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001064 }
1065 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001066 if (h1m->flags & H1_MF_VER_11) {
1067 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001068 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001069 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001070 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001071}
1072
Christopher Fauletb992af02019-03-28 15:42:24 +01001073static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001074{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001075 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1076 * token is found
1077 */
1078 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001079 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001080
1081 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001082 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001083 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1084 TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001085 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001086 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001087 }
1088 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001089 if (h1m->flags & H1_MF_VER_11) {
1090 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001091 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001092 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001094}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095
Christopher Fauletb992af02019-03-28 15:42:24 +01001096static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001097{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001098 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001099 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001100 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001102}
1103
Christopher Fauletb992af02019-03-28 15:42:24 +01001104static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1105{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001106 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001107 h1_set_cli_conn_mode(h1s, h1m);
1108 else
1109 h1_set_srv_conn_mode(h1s, h1m);
1110
1111 if (!(h1m->flags & H1_MF_RESP))
1112 h1_update_req_conn_value(h1s, h1m, conn_val);
1113 else
1114 h1_update_res_conn_value(h1s, h1m, conn_val);
1115}
Christopher Faulete44769b2018-11-29 23:01:45 +01001116
Christopher Faulet98fbe952019-07-22 16:18:24 +02001117/* Try to adjust the case of the message header name using the global map
1118 * <hdrs_map>.
1119 */
1120static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1121{
1122 struct ebpt_node *node;
1123 struct h1_hdr_entry *entry;
1124
1125 /* No entry in the map, do nothing */
1126 if (eb_is_empty(&hdrs_map.map))
1127 return;
1128
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001129 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001130 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1131 return;
1132
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001133 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001134 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1135 return;
1136
1137 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1138 if (!node)
1139 return;
1140 entry = container_of(node, struct h1_hdr_entry, node);
1141 name->ptr = entry->name.ptr;
1142 name->len = entry->name.len;
1143}
1144
Christopher Faulete44769b2018-11-29 23:01:45 +01001145/* Append the description of what is present in error snapshot <es> into <out>.
1146 * The description must be small enough to always fit in a buffer. The output
1147 * buffer may be the trash so the trash must not be used inside this function.
1148 */
1149static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1150{
1151 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001152 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1153 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1154 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1155 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1156 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1157 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001158}
1159/*
1160 * Capture a bad request or response and archive it in the proxy's structure.
1161 * By default it tries to report the error position as h1m->err_pos. However if
1162 * this one is not set, it will then report h1m->next, which is the last known
1163 * parsing point. The function is able to deal with wrapping buffers. It always
1164 * displays buffers as a contiguous area starting at buf->p. The direction is
1165 * determined thanks to the h1m's flags.
1166 */
1167static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1168 struct h1m *h1m, struct buffer *buf)
1169{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001170 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001171 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001172 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001173 union error_snapshot_ctx ctx;
1174
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001175 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001176 if (sess == NULL)
1177 sess = si_strm(h1s->cs->data)->sess;
1178 if (!(h1m->flags & H1_MF_RESP))
1179 other_end = si_strm(h1s->cs->data)->be;
1180 else
1181 other_end = sess->fe;
1182 } else
1183 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001184
1185 /* http-specific part now */
1186 ctx.h1.state = h1m->state;
1187 ctx.h1.c_flags = h1c->flags;
1188 ctx.h1.s_flags = h1s->flags;
1189 ctx.h1.m_flags = h1m->flags;
1190 ctx.h1.m_clen = h1m->curr_len;
1191 ctx.h1.m_blen = h1m->body_len;
1192
1193 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1194 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001195 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1196 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001197}
1198
Christopher Fauletadb22202018-12-12 10:32:09 +01001199/* Emit the chunksize followed by a CRLF in front of data of the buffer
1200 * <buf>. It goes backwards and starts with the byte before the buffer's
1201 * head. The caller is responsible for ensuring there is enough room left before
1202 * the buffer's head for the string.
1203 */
1204static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1205{
1206 char *beg, *end;
1207
1208 beg = end = b_head(buf);
1209 *--beg = '\n';
1210 *--beg = '\r';
1211 do {
1212 *--beg = hextab[chksz & 0xF];
1213 } while (chksz >>= 4);
1214 buf->head -= (end - beg);
1215 b_add(buf, end - beg);
1216}
1217
1218/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1219 * ensuring there is enough room left in the buffer for the string. */
1220static void h1_emit_chunk_crlf(struct buffer *buf)
1221{
1222 *(b_peek(buf, b_data(buf))) = '\r';
1223 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1224 b_add(buf, 2);
1225}
1226
Christopher Faulet129817b2018-09-20 16:14:40 +02001227/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001228 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001229 * CONNECT requests. On the client side, if the response is not finished, the
1230 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001231 */
1232static void h1_set_req_tunnel_mode(struct h1s *h1s)
1233{
1234 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1235 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001236 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1237
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001238 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001239 h1s->flags &= ~H1S_F_PARSING_DONE;
1240 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001241 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1242 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001243 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001244 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001245 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1246 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001247 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001248 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001249 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001250}
1251
1252/*
1253 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001254 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001255 * last case, this function takes care to switch the request to tunnel mode if
1256 * possible. On the server side, if the request is not finished, the mux is mark
1257 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001258 */
1259static void h1_set_res_tunnel_mode(struct h1s *h1s)
1260{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001261
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001262 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1263 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001264 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1265
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001266 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001267 h1s->flags &= ~H1S_F_PARSING_DONE;
1268 /* On protocol switching, switch the request to tunnel mode if it is in
1269 * DONE state. Otherwise we will wait the end of the request to switch
1270 * it in tunnel mode.
1271 */
1272 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001273 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1274 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001275 }
1276 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1277 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1278 h1s->req.state = H1_MSG_TUNNEL;
1279 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1280 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001281 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001282 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1283 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001284 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001285 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001286 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001287}
1288
1289/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001290 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001291 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001292 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001294static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001295 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001296{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001297 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001298 int ret = 0;
1299
Willy Tarreau022e5e52020-09-10 09:33:15 +02001300 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001301
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001302 if (h1s->meth == HTTP_METH_CONNECT)
1303 h1m->flags |= H1_MF_METH_CONNECT;
1304 if (h1s->meth == HTTP_METH_HEAD)
1305 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001306
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001307 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1308 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001310 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001311 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001312 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001313 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1314 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001315 goto end;
1316 }
1317
Christopher Faulet486498c2019-10-11 14:22:00 +02001318 if (h1m->err_pos >= 0) {
1319 /* Maybe we found an error during the parsing while we were
1320 * configured not to block on that, so we have to capture it
1321 * now.
1322 */
1323 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1324 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1325 }
1326
Christopher Faulet129817b2018-09-20 16:14:40 +02001327 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001328 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001329 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001330 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001331 }
1332 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001333 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001334 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001335 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001336 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001337 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001338 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001339
Christopher Faulet129817b2018-09-20 16:14:40 +02001340 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001341 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001342 return ret;
1343}
1344
1345/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001346 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001347 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1348 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001349 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001350static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001351 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001352 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001353{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001354 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001355
Willy Tarreau022e5e52020-09-10 09:33:15 +02001356 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001357 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001358 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001359 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001360 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001361 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001362 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001363 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001364 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001365 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001366 }
1367
Christopher Faulet02a02532019-11-15 09:36:28 +01001368 *ofs += ret;
1369
1370 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001371 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001372 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001373}
1374
1375/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001376 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1377 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1378 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1379 * responsible to update the parser state <h1m>.
1380 */
1381static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1382 struct buffer *buf, size_t *ofs, size_t max)
1383{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001384 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001385
Willy Tarreau022e5e52020-09-10 09:33:15 +02001386 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001387 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001388 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001389 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet02a02532019-11-15 09:36:28 +01001390 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001391 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001392 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001393 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1394 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001395 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001396 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001397
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001398 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001399
1400 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001401 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001402 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001403}
1404
1405/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001406 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
Christopher Fauleta583af62020-09-24 15:35:37 +02001407 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001408 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001409static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1410 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001411{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001412 int ret;
1413
Willy Tarreau022e5e52020-09-10 09:33:15 +02001414 TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){max});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001415 ret = h1_parse_msg_eom(h1m, htx, max);
1416 if (!ret) {
1417 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1418 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001419 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001420 TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001421 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1422 }
1423 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001424 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001425
Christopher Faulet76014fd2019-12-10 11:47:22 +01001426 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001427 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001428 TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet76014fd2019-12-10 11:47:22 +01001429 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001430}
1431
1432/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001433 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001434 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1435 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001436 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001437static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001438{
Christopher Faulet539e0292018-11-19 10:40:09 +01001439 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001440 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001441 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001442 size_t data;
1443 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001444 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001445
Christopher Faulet539e0292018-11-19 10:40:09 +01001446 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001447 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001448
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001449 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001450 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001451
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001452 if (h1s->flags & H1S_F_PARSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001453 goto end;
1454
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001455 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001456 size_t used = htx_used_space(htx);
1457
Christopher Faulet129817b2018-09-20 16:14:40 +02001458 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001459 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001460 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001461 if (!ret)
1462 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001463
1464 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1465 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1466
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001467 if ((h1m->flags & H1_MF_RESP) &&
1468 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1469 h1m_init_res(&h1s->res);
1470 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001471 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001472 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001474 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001475 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001476 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001477 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001479
1480 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1481 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001482 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001483 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001484 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1485 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1486 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001487 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001488
Christopher Faulet76014fd2019-12-10 11:47:22 +01001489 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1490 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001491 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001492 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001493 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1494 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1495 break;
1496
1497 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1498 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1499 }
1500
Christopher Fauletf3158e92019-11-15 11:14:23 +01001501 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1502 h1_set_req_tunnel_mode(h1s);
1503 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001504 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1505 TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
Christopher Faulet23021ad2020-07-10 10:01:26 +02001506 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001507 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001508 else
1509 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001511 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001512 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001513 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001514 if (!ret)
1515 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001516
1517 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1518 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001519 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001520 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001521 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001522 break;
1523 }
1524
Christopher Faulet30db3d72019-05-17 15:35:33 +02001525 count -= htx_used_space(htx) - used;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001526 } while (!(h1s->flags & H1S_F_PARSING_ERROR));
Christopher Faulet129817b2018-09-20 16:14:40 +02001527
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001528 if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001529 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001530 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001531 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001532
Christopher Faulet539e0292018-11-19 10:40:09 +01001533 b_del(&h1c->ibuf, total);
1534
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001535 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001536 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1537
Christopher Faulet30db3d72019-05-17 15:35:33 +02001538 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001539 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001540 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001541 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001542 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001543 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001544
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001545 if (!b_data(&h1c->ibuf))
1546 h1_release_buf(h1c, &h1c->ibuf);
1547
1548 if (!h1s->cs) {
1549 if (h1m->state <= H1_MSG_LAST_LF) {
1550 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1551 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1552 goto end;
1553 }
1554
1555 if (!h1s_new_cs(h1s, buf)) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001556 h1c->flags |= H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001557 goto err;
1558 }
1559 }
1560
1561 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001562 if (!(h1m->flags & H1_MF_CHNK) &&
1563 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1564 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1565 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1566 }
1567 else {
1568 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1569 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1570 }
1571
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001572 /* Don't set EOI on the conn-stream for protocol upgrade requests, wait
1573 * the response to do so or not depending on the status code.
1574 */
1575 if ((h1s->flags & H1S_F_PARSING_DONE) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001576 h1s->cs->flags |= CS_FL_EOI;
1577
Christopher Faulet6716cc22019-12-20 17:33:24 +01001578 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001579 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001580 else {
1581 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1582 if (h1s->flags & H1S_F_REOS) {
1583 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001584 if (h1m->state >= H1_MSG_DONE) {
1585 /* DONE or TUNNEL, set EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001586 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001587 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001588 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
1589 h1s->cs->flags |= CS_FL_ERROR;
1590 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001591 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001592
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001593 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001594 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001595 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001596
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001597 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001598 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001599 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001600 if (h1s->cs)
1601 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001602 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001603 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001604}
1605
Christopher Faulet129817b2018-09-20 16:14:40 +02001606/*
1607 * Process outgoing data. It parses data and transfer them from the channel buffer into
1608 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1609 * 0 if it couldn't proceed.
1610 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001611static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1612{
Christopher Faulet129817b2018-09-20 16:14:40 +02001613 struct h1s *h1s = h1c->h1s;
1614 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001615 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001616 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001617 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001618 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001619
Christopher Faulet47365272018-10-31 17:40:50 +01001620 if (!count)
1621 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001622
Christopher Faulet94b2c762019-05-24 15:28:57 +02001623 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001624 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1625
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001626 if (htx_is_empty(chn_htx))
1627 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001629 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1630 goto end;
1631
Christopher Faulet51dbc942018-09-13 09:05:15 +02001632 if (!h1_get_buf(h1c, &h1c->obuf)) {
1633 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001634 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001635 goto end;
1636 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001637
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001638 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001639
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001640 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001641 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001642
Willy Tarreau3815b222018-12-11 19:50:43 +01001643 /* Perform some optimizations to reduce the number of buffer copies.
1644 * First, if the mux's buffer is empty and the htx area contains
1645 * exactly one data block of the same size as the requested count,
1646 * then it's possible to simply swap the caller's buffer with the
1647 * mux's output buffer and adjust offsets and length to match the
1648 * entire DATA HTX block in the middle. In this case we perform a
1649 * true zero-copy operation from end-to-end. This is the situation
1650 * that happens all the time with large files. Second, if this is not
1651 * possible, but the mux's output buffer is empty, we still have an
1652 * opportunity to avoid the copy to the intermediary buffer, by making
1653 * the intermediary buffer's area point to the output buffer's area.
1654 * In this case we want to skip the HTX header to make sure that copies
1655 * remain aligned and that this operation remains possible all the
1656 * time. This goes for headers, data blocks and any data extracted from
1657 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001658 */
1659 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001660 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001661 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001662 htx_get_blk_value(chn_htx, blk).len == count) {
1663 void *old_area = h1c->obuf.area;
1664
Christopher Faulet6b81df72019-10-01 22:08:43 +02001665 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001666 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001667 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001668 h1c->obuf.data = count;
1669
1670 buf->area = old_area;
1671 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001672
Christopher Faulet6b81df72019-10-01 22:08:43 +02001673 chn_htx = (struct htx *)buf->area;
1674 htx_reset(chn_htx);
1675
Christopher Fauletadb22202018-12-12 10:32:09 +01001676 /* The message is chunked. We need to emit the chunk
1677 * size. We have at least the size of the struct htx to
1678 * write the chunk envelope. It should be enough.
1679 */
1680 if (h1m->flags & H1_MF_CHNK) {
1681 h1_emit_chunk_size(&h1c->obuf, count);
1682 h1_emit_chunk_crlf(&h1c->obuf);
1683 }
1684
Willy Tarreau3815b222018-12-11 19:50:43 +01001685 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001686 if (h1m->state == H1_MSG_DATA)
1687 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001688 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001689 else
1690 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001691 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001692 goto out;
1693 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001694 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001695 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001696 else
1697 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001698
Christopher Fauletc2518a52019-06-25 21:41:02 +02001699 tmp.data = 0;
1700 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001701 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001702 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001704 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001706 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707
Christopher Fauletb2e84162018-12-06 11:39:49 +01001708 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001709 if (type != HTX_BLK_DATA && vlen > count)
1710 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001711
Christopher Faulet94b2c762019-05-24 15:28:57 +02001712 if (type == HTX_BLK_UNUSED)
1713 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714
Christopher Faulet94b2c762019-05-24 15:28:57 +02001715 switch (h1m->state) {
1716 case H1_MSG_RQBEFORE:
1717 if (type != HTX_BLK_REQ_SL)
1718 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001719 TRACE_USER("sending request headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001720 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001721 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001722 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001723 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001724 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001725 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001726 if (sl->flags & HTX_SL_F_BODYLESS)
1727 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001728 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001729 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1730 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1731 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1732 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1733 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001734 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001735
Christopher Faulet94b2c762019-05-24 15:28:57 +02001736 case H1_MSG_RPBEFORE:
1737 if (type != HTX_BLK_RES_SL)
1738 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001739 TRACE_USER("sending response headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001740 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001741 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001742 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001743 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001744 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001745 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001746 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001747 if (sl->info.res.status < 200 &&
1748 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001749 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001750 h1m->state = H1_MSG_HDR_FIRST;
1751 break;
1752
Christopher Faulet94b2c762019-05-24 15:28:57 +02001753 case H1_MSG_HDR_FIRST:
1754 case H1_MSG_HDR_NAME:
1755 case H1_MSG_HDR_L2_LWS:
1756 if (type == HTX_BLK_EOH)
1757 goto last_lf;
1758 if (type != HTX_BLK_HDR)
1759 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001760
Christopher Faulet9768c262018-10-22 09:34:31 +02001761 h1m->state = H1_MSG_HDR_NAME;
1762 n = htx_get_blk_name(chn_htx, blk);
1763 v = htx_get_blk_value(chn_htx, blk);
1764
Christopher Faulet86d144c2019-08-14 16:32:25 +02001765 /* Skip all pseudo-headers */
1766 if (*(n.ptr) == ':')
1767 goto skip_hdr;
1768
Christopher Fauletb045bb22020-02-28 10:42:20 +01001769 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001770 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001771 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001772 /* Only skip C-L header with invalid value. */
1773 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001774 goto skip_hdr;
1775 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001776 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001777 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001778 if (!v.len)
1779 goto skip_hdr;
1780 }
1781
Christopher Faulet67d58092019-10-02 10:51:38 +02001782 /* Skip header if same name is used to add the server name */
1783 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1784 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1785 goto skip_hdr;
1786
Christopher Faulet98fbe952019-07-22 16:18:24 +02001787 /* Try to adjust the case of the header name */
1788 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1789 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001790 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001791 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001792 skip_hdr:
1793 h1m->state = H1_MSG_HDR_L2_LWS;
1794 break;
1795
Christopher Faulet94b2c762019-05-24 15:28:57 +02001796 case H1_MSG_LAST_LF:
1797 if (type != HTX_BLK_EOH)
1798 goto error;
1799 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001800 h1m->state = H1_MSG_LAST_LF;
1801 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001802 /* If the reply comes from haproxy while the request is
1803 * not finished, we force the connection close. */
1804 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1805 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1806 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1807 }
1808
1809 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001810 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001811 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001812 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001813 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001814 /* Try to adjust the case of the header name */
1815 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1816 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001817 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001818 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001819 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001820 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001821 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001822
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001823 if ((h1s->meth != HTTP_METH_CONNECT &&
1824 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001825 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Fauletc75668e2020-12-07 18:10:32 +01001826 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && h1s->meth != HTTP_METH_HEAD &&
1827 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001828 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1829 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001830 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001831 n = ist("transfer-encoding");
1832 v = ist("chunked");
1833 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1834 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001835 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001836 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001837 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001838 h1m->flags |= H1_MF_CHNK;
1839 }
1840
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001841 /* Now add the server name to a header (if requested) */
1842 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1843 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1844 struct server *srv = objt_server(h1c->conn->target);
1845
1846 if (srv) {
1847 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1848 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001849
1850 /* Try to adjust the case of the header name */
1851 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1852 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001853 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001854 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001855 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001856 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001857 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1858 }
1859
Christopher Fauletc2518a52019-06-25 21:41:02 +02001860 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001861 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001862
Christopher Faulet6b81df72019-10-01 22:08:43 +02001863 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1864 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1865
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001866 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1867 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1868 h1_set_req_tunnel_mode(h1s);
1869 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001870 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001871 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001872 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001873 * to the client. Switch the response to tunnel mode.
1874 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001875 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001876 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001877 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001878 else if ((h1m->flags & H1_MF_RESP) &&
1879 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1880 h1m_init_res(&h1s->res);
1881 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001882 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001883 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001884 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001885 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001886 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001887 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1888 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001889 else
1890 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001891 break;
1892
Christopher Faulet94b2c762019-05-24 15:28:57 +02001893 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001894 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001895 if (type == HTX_BLK_EOM) {
1896 /* Chunked message without explicit trailers */
1897 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001898 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001899 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001900 }
1901 goto done;
1902 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001903 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001904 /* If the message is not chunked, never
1905 * add the last chunk. */
1906 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001907 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001908 TRACE_PROTO("sending message trailers", H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s, chn_htx);
Christopher Faulet94b2c762019-05-24 15:28:57 +02001909 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001910 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001911 else if (type != HTX_BLK_DATA)
1912 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001913
1914 TRACE_PROTO("sending message data", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){sz});
Christopher Fauletd9233f02019-10-14 14:01:24 +02001915
1916
1917 if (vlen > count) {
1918 /* Get the maximum amount of data we can xferred */
1919 vlen = count;
1920 }
1921
1922 chklen = 0;
1923 if (h1m->flags & H1_MF_CHNK) {
1924 chklen = b_room(&tmp);
1925 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1926 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1927 (chklen < 1048576) ? 5 : 8);
1928 chklen += 4; /* 2 x CRLF */
1929 }
1930
1931 if (vlen + chklen > b_room(&tmp)) {
1932 /* too large for the buffer */
1933 if (chklen >= b_room(&tmp))
1934 goto full;
1935 vlen = b_room(&tmp) - chklen;
1936 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001937 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001938 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001939 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001940 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941
1942 if (h1m->state == H1_MSG_DATA)
1943 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001944 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001945 else
1946 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001947 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001948 break;
1949
Christopher Faulet94b2c762019-05-24 15:28:57 +02001950 case H1_MSG_TRAILERS:
1951 if (type == HTX_BLK_EOM)
1952 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001953 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001954 goto error;
1955 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001956 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001957 /* If the message is not chunked, ignore
1958 * trailers. It may happen with H2 messages. */
1959 if (!(h1m->flags & H1_MF_CHNK))
1960 break;
1961
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001962 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001963 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001964 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001965 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1966 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001967 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001968 else { // HTX_BLK_TLR
1969 n = htx_get_blk_name(chn_htx, blk);
1970 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001971
1972 /* Try to adjust the case of the header name */
1973 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1974 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001975 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001976 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001977 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001978 break;
1979
Christopher Faulet94b2c762019-05-24 15:28:57 +02001980 case H1_MSG_DONE:
1981 if (type != HTX_BLK_EOM)
1982 goto error;
1983 done:
1984 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001985 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1986 h1_set_req_tunnel_mode(h1s);
1987 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1988 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001989 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1990 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001991 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02001992 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001993 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001994
1995 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1996 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001997 break;
1998
Christopher Faulet9768c262018-10-22 09:34:31 +02001999 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002000 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002001 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002002 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002003 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002004 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002005 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2006 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002007 break;
2008 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002009
2010 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002011 total += vlen;
2012 count -= vlen;
2013 if (sz == vlen)
2014 blk = htx_remove_blk(chn_htx, blk);
2015 else {
2016 htx_cut_data_blk(chn_htx, blk, vlen);
2017 break;
2018 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002019 }
2020
Christopher Faulet9768c262018-10-22 09:34:31 +02002021 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002022 /* when the output buffer is empty, tmp shares the same area so that we
2023 * only have to update pointers and lengths.
2024 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002025 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2026 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002027 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002028 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029
Willy Tarreau3815b222018-12-11 19:50:43 +01002030 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002031 out:
Christopher Faulet3e1748b2020-12-07 18:21:27 +01002032 /* Both the request and the response reached the DONE state. So set EOI
2033 * flag on the conn-stream. Most of time, the flag will already be set,
2034 * except for protocol upgrades.
2035 */
2036 if (h1s->cs && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2037 h1s->cs->flags |= CS_FL_EOI;
2038
Christopher Faulet6b81df72019-10-01 22:08:43 +02002039 if (!buf_room_for_htx_data(&h1c->obuf)) {
2040 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002041 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002042 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002043 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002044 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002045 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002046
2047 full:
2048 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2049 h1c->flags |= H1C_F_OUT_FULL;
2050 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002051}
2052
Christopher Faulet51dbc942018-09-13 09:05:15 +02002053/*********************************************************/
2054/* functions below are I/O callbacks from the connection */
2055/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002056static void h1_wake_stream_for_recv(struct h1s *h1s)
2057{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002058 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002059 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002060 tasklet_wakeup(h1s->subs->tasklet);
2061 h1s->subs->events &= ~SUB_RETRY_RECV;
2062 if (!h1s->subs->events)
2063 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002064 }
2065}
2066static void h1_wake_stream_for_send(struct h1s *h1s)
2067{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002068 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002069 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002070 tasklet_wakeup(h1s->subs->tasklet);
2071 h1s->subs->events &= ~SUB_RETRY_SEND;
2072 if (!h1s->subs->events)
2073 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002074 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002075}
2076
2077/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2078 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2079 * retryable errors (allocation error or buffer full). On success, the error is
2080 * copied in the output buffer.
2081*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002082static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002083{
2084 int rc = http_get_status_idx(h1c->errcode);
2085 int ret = 0;
2086
2087 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2088
2089 /* Verify if the error is mapped on /dev/null or any empty file */
2090 /// XXX: do a function !
2091 if (h1c->px->replies[rc] &&
2092 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2093 h1c->px->replies[rc]->body.errmsg &&
2094 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2095 /* Empty error, so claim a success */
2096 ret = 1;
2097 goto out;
2098 }
2099
2100 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2101 h1c->flags |= H1C_F_ERR_PENDING;
2102 goto out;
2103 }
2104
2105 if (!h1_get_buf(h1c, &h1c->obuf)) {
2106 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2107 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2108 goto out;
2109 }
2110 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2111 if (unlikely(ret <= 0)) {
2112 if (!ret) {
2113 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2114 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2115 goto out;
2116 }
2117 else {
2118 /* we cannot report this error, so claim a success */
2119 ret = 1;
2120 }
2121 }
2122 h1c->flags &= ~H1C_F_ERR_PENDING;
2123 out:
2124 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2125 return ret;
2126}
2127
2128/* Try to send a 500 internal error. It relies on h1_send_error to send the
2129 * error. This function takes care of incrementing stats and tracked counters.
2130 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002131static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002132{
2133 struct session *sess = h1c->conn->owner;
2134 int ret = 1;
2135
2136 session_inc_http_req_ctr(sess);
2137 session_inc_http_err_ctr(sess);
2138 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2139 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2140 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2141 if (sess->listener->counters)
2142 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2143
2144 h1c->errcode = 500;
2145 ret = h1_send_error(h1c);
2146 sess_log(sess);
2147 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002148}
2149
Christopher Fauletc18fc232020-10-06 17:41:36 +02002150/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2151 * error. This function takes care of incrementing stats and tracked counters.
2152 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002153static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002154{
2155 struct session *sess = h1c->conn->owner;
2156 int ret = 1;
2157
2158 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2159 goto end;
2160
2161 session_inc_http_req_ctr(sess);
2162 session_inc_http_err_ctr(sess);
2163 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2164 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2165 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2166 if (sess->listener->counters)
2167 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2168
2169 h1c->errcode = 400;
2170 ret = h1_send_error(h1c);
2171 sess_log(sess);
2172
2173 end:
2174 return ret;
2175}
2176
2177/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2178 * error. This function takes care of incrementing stats and tracked counters.
2179 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002180static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002181{
2182 struct session *sess = h1c->conn->owner;
2183 int ret = 1;
2184
2185 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2186 goto end;
2187
2188 session_inc_http_req_ctr(sess);
2189 session_inc_http_err_ctr(sess);
2190 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2191 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2192 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2193 if (sess->listener->counters)
2194 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2195
2196 h1c->errcode = 408;
2197 ret = h1_send_error(h1c);
2198 sess_log(sess);
2199 end:
2200 return ret;
2201}
2202
2203
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204/*
2205 * Attempt to read data, and subscribe if none available
2206 */
2207static int h1_recv(struct h1c *h1c)
2208{
2209 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002210 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002211 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212
Christopher Faulet6b81df72019-10-01 22:08:43 +02002213 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2214
2215 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2216 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002217 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002218 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002219
Christopher Fauletae635762020-09-21 11:47:16 +02002220 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2221 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002222 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002223 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002224
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002225 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2226 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002227 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002228 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002229 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002230
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002231 /*
2232 * If we only have a small amount of data, realign it,
2233 * it's probably cheaper than doing 2 recv() calls.
2234 */
2235 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2236 b_slow_realign(&h1c->ibuf, trash.area, 0);
2237
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002238 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002239 if (!h1c->h1s ||
2240 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2241 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002242 flags |= CO_RFL_READ_ONCE;
2243
Willy Tarreau45f2b892018-12-05 07:59:27 +01002244 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002245 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002246 if (h1c->flags & H1C_F_IN_FULL) {
2247 h1c->flags &= ~H1C_F_IN_FULL;
2248 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2249 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002250
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002251 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002252 if (!b_data(&h1c->ibuf)) {
2253 /* try to pre-align the buffer like the rxbufs will be
2254 * to optimize memory copies.
2255 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002256 h1c->ibuf.head = sizeof(struct htx);
2257 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002258 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002259 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002260 if (max && !ret && h1_recv_allowed(h1c)) {
2261 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2262 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002263 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002264 else {
2265 h1_wake_stream_for_recv(h1c->h1s);
2266 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002267 }
2268
Christopher Faulet51dbc942018-09-13 09:05:15 +02002269 if (!b_data(&h1c->ibuf))
2270 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002271 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002272 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002273 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2274 }
2275
2276 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002277 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002278}
2279
2280
2281/*
2282 * Try to send data if possible
2283 */
2284static int h1_send(struct h1c *h1c)
2285{
2286 struct connection *conn = h1c->conn;
2287 unsigned int flags = 0;
2288 size_t ret;
2289 int sent = 0;
2290
Christopher Faulet6b81df72019-10-01 22:08:43 +02002291 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2292
2293 if (conn->flags & CO_FL_ERROR) {
2294 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002295 b_reset(&h1c->obuf);
2296 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002297 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002298
2299 if (!b_data(&h1c->obuf))
2300 goto end;
2301
Christopher Faulet46230362019-10-17 16:04:20 +02002302 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002303 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002304 if (h1c->flags & H1C_F_CO_STREAMER)
2305 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002306
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002307 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002309 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002310 if (h1c->flags & H1C_F_OUT_FULL) {
2311 h1c->flags &= ~H1C_F_OUT_FULL;
2312 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2313 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002314 b_del(&h1c->obuf, ret);
2315 sent = 1;
2316 }
2317
Christopher Faulet145aa472018-12-06 10:56:20 +01002318 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002319 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002320 /* error or output closed, nothing to send, clear the buffer to release it */
2321 b_reset(&h1c->obuf);
2322 }
2323
Christopher Faulet51dbc942018-09-13 09:05:15 +02002324 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002325 if (!(h1c->flags & H1C_F_OUT_FULL))
2326 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002327
Christopher Faulet51dbc942018-09-13 09:05:15 +02002328 /* We're done, no more to send */
2329 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002330 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002331 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002332 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002333 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002334 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002335 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002336 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002337 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2338 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002339 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002340 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002341
Christopher Faulet6b81df72019-10-01 22:08:43 +02002342 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002343 return sent;
2344}
2345
Christopher Faulet51dbc942018-09-13 09:05:15 +02002346/* callback called on any event by the connection handler.
2347 * It applies changes and returns zero, or < 0 if it wants immediate
2348 * destruction of the connection.
2349 */
2350static int h1_process(struct h1c * h1c)
2351{
2352 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002353 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002354
Christopher Faulet6b81df72019-10-01 22:08:43 +02002355 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2356
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002357 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
2358 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002359 (h1c->flags & (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC)) && /* IDLE h1 connection or no CS attached to the h1 stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002360 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
2361 struct buffer *buf;
2362 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002363
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002364 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2365 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002366 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002367
2368 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002369 if (((h1c->flags & (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) == H1C_F_ST_IDLE) && /* First request with no h1s */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002370 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
2371 /* Try to match H2 preface before parsing the request headers. */
2372 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2373 h1c->flags |= H1C_F_UPG_H2C;
2374 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002375 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002376 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002377 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002378
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002379 /* Create the H1 stream if not already there */
2380 if (!h1s) {
2381 h1s = h1c_frt_stream_new(h1c);
2382 if (!h1s) {
2383 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002384 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002385 goto no_parsing;
2386 }
2387 }
2388
2389 if (h1s->sess->t_idle == -1)
2390 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2391
2392 /* Get the stream rxbuf */
2393 buf = h1_get_buf(h1c, &h1s->rxbuf);
2394 if (!buf) {
2395 h1c->flags |= H1C_F_IN_SALLOC;
2396 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2397 return 0;
2398 }
2399
2400 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2401 h1_process_input(h1c, buf, count);
2402 h1_release_buf(h1c, &h1s->rxbuf);
2403 h1_set_idle_expiration(h1c);
2404
2405 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002406 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002407 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002408 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002409 }
2410 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2411 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002412 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002413 }
Christopher Fauletae635762020-09-21 11:47:16 +02002414 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002415 h1_send(h1c);
2416
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002417 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
2418 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002419 /* No conn-stream */
2420 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002421 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002422 if (h1_handle_bad_req(h1c))
2423 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002424 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002425 }
2426
2427 /* Handle pending error, if any (only possible on frontend connection) */
2428 if (h1c->flags & H1C_F_ERR_PENDING) {
2429 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2430 if (h1_send_error(h1c))
2431 h1_send(h1c);
2432 }
Christopher Fauletae635762020-09-21 11:47:16 +02002433
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002434 /* If there is some pending outgoing data or error, just wait */
2435 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2436 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002437
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002438 /* Otherwise we can release the H1 connection */
2439 goto release;
2440 }
2441 else {
2442 /* Here there is still a H1 stream with a conn-stream.
2443 * Report the connection state at the stream level
2444 */
2445 if (conn_xprt_read0_pending(conn)) {
2446 h1s->flags |= H1S_F_REOS;
2447 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2448 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002449 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002450 h1s->cs->flags |= CS_FL_ERROR;
2451 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2452 if (h1s->cs->data_cb->wake) {
2453 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
2454 h1s->cs->data_cb->wake(h1s->cs);
2455 }
2456 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002457 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002458
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002459 if (!b_data(&h1c->ibuf))
2460 h1_release_buf(h1c, &h1c->ibuf);
2461
2462
2463 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2464 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2465 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002466 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002467
Christopher Faulet47365272018-10-31 17:40:50 +01002468 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002469 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002470 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002471 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002472
2473 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002474 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002475 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002476 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002477}
2478
Willy Tarreau691d5032021-01-20 14:55:01 +01002479struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002480{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002481 struct connection *conn;
2482 struct tasklet *tl = (struct tasklet *)t;
2483 int conn_in_list;
2484 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002485 int ret = 0;
2486
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002487
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002488 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002489 if (tl->context == NULL) {
2490 /* The connection has been taken over by another thread,
2491 * we're no longer responsible for it, so just free the
2492 * tasklet, and do nothing.
2493 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002494 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002495 tasklet_free(tl);
2496 return NULL;
2497 }
2498 h1c = ctx;
2499 conn = h1c->conn;
2500
2501 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2502
2503 /* Remove the connection from the list, to be sure nobody attempts
2504 * to use it while we handle the I/O events
2505 */
2506 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2507 if (conn_in_list)
2508 MT_LIST_DEL(&conn->list);
2509
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002510 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002511
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002512 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002514 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002515 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002516 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002517 ret = h1_process(h1c);
2518 /* If we were in an idle list, we want to add it back into it,
2519 * unless h1_process() returned -1, which mean it has destroyed
2520 * the connection (testing !ret is enough, if h1_process() wasn't
2521 * called then ret will be 0 anyway.
2522 */
2523 if (!ret && conn_in_list) {
2524 struct server *srv = objt_server(conn->target);
2525
2526 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002527 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002528 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002529 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002530 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002531 return NULL;
2532}
2533
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002534static void h1_reset(struct connection *conn)
2535{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002536
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002537}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002538
2539static int h1_wake(struct connection *conn)
2540{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002541 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002542 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002543
Christopher Faulet6b81df72019-10-01 22:08:43 +02002544 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2545
Christopher Faulet539e0292018-11-19 10:40:09 +01002546 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002547 ret = h1_process(h1c);
2548 if (ret == 0) {
2549 struct h1s *h1s = h1c->h1s;
2550
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002551 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data_cb->wake) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002552 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002553 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002554 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002555 }
2556 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002557}
2558
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002559/* Connection timeout management. The principle is that if there's no receipt
2560 * nor sending for a certain amount of time, the connection is closed.
2561 */
2562static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2563{
2564 struct h1c *h1c = context;
2565 int expired = tick_is_expired(t->expire, now_ms);
2566
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002567 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002568
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002569 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002570 /* Make sure nobody stole the connection from us */
2571 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2572
2573 /* Somebody already stole the connection from us, so we should not
2574 * free it, we just have to free the task.
2575 */
2576 if (!t->context) {
2577 h1c = NULL;
2578 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2579 goto do_leave;
2580 }
2581
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002582 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002583 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2584 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002585 return t;
2586 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002587
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002588 /* If a conn-stream is still attached to the mux, wait for the
2589 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002590 */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002591 if (h1c->flags & H1C_F_ST_ATTACHED) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002592 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2593 t->expire = TICK_ETERNITY;
2594 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2595 return t;
2596 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002597
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002598 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002599 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2600 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002601 if (h1_handle_req_tout(h1c))
2602 h1_send(h1c);
2603 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2604 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002605 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002606 return t;
2607 }
2608 }
2609
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002610 /* We're about to destroy the connection, so make sure nobody attempts
2611 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002612 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002613 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002614 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002615
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002616 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002617 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002618
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002619 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002620 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002621
2622 if (!h1c) {
2623 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002624 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002625 return NULL;
2626 }
2627
2628 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002629 h1_release(h1c);
2630 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002631 return NULL;
2632}
2633
Christopher Faulet51dbc942018-09-13 09:05:15 +02002634/*******************************************/
2635/* functions below are used by the streams */
2636/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002637
Christopher Faulet51dbc942018-09-13 09:05:15 +02002638/*
2639 * Attach a new stream to a connection
2640 * (Used for outgoing connections)
2641 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002642static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002643{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002644 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002645 struct conn_stream *cs = NULL;
2646 struct h1s *h1s;
2647
Christopher Faulet6b81df72019-10-01 22:08:43 +02002648 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002649 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002650 TRACE_DEVEL("leaving on h1c error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002651 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002652 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002653
Christopher Faulet236c93b2020-07-02 09:19:54 +02002654 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002655 if (!cs) {
2656 TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002657 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002658 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002659
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002660 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002661 if (h1s == NULL) {
2662 TRACE_DEVEL("leaving on h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002663 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002664 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002665
Christopher Faulet6b81df72019-10-01 22:08:43 +02002666 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002667 return cs;
2668 end:
2669 cs_free(cs);
2670 return NULL;
2671}
2672
2673/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2674 * this mux, it's easy as we can only store a single conn_stream.
2675 */
2676static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2677{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002678 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002679 struct h1s *h1s = h1c->h1s;
2680
2681 if (h1s)
2682 return h1s->cs;
2683
2684 return NULL;
2685}
2686
Christopher Faulet73c12072019-04-08 11:23:22 +02002687static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002688{
Christopher Faulet73c12072019-04-08 11:23:22 +02002689 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002690
Christopher Faulet6b81df72019-10-01 22:08:43 +02002691 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002692 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002693 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002694}
2695
2696/*
2697 * Detach the stream from the connection and possibly release the connection.
2698 */
2699static void h1_detach(struct conn_stream *cs)
2700{
2701 struct h1s *h1s = cs->ctx;
2702 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002703 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002704 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002705
Christopher Faulet6b81df72019-10-01 22:08:43 +02002706 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2707
Christopher Faulet51dbc942018-09-13 09:05:15 +02002708 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002709 if (!h1s) {
2710 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002711 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002712 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002713
Olivier Houchardf502aca2018-12-14 19:42:40 +01002714 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002715 h1c = h1s->h1c;
2716 h1s->cs = NULL;
2717
Christopher Faulet42849b02020-10-05 11:35:17 +02002718 sess->accept_date = date;
2719 sess->tv_accept = now;
2720 sess->t_handshake = 0;
2721 sess->t_idle = -1;
2722
Olivier Houchard8a786902018-12-15 16:05:40 +01002723 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2724 h1s_destroy(h1s);
2725
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002726 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002727 /* If there are any excess server data in the input buffer,
2728 * release it and close the connection ASAP (some data may
2729 * remain in the output buffer). This happens if a server sends
2730 * invalid responses. So in such case, we don't want to reuse
2731 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002732 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002733 if (b_data(&h1c->ibuf)) {
2734 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002735 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002736 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002737 goto release;
2738 }
Christopher Faulet03627242019-07-19 11:34:08 +02002739
Christopher Faulet08016ab2020-07-01 16:10:06 +02002740 if (h1c->conn->flags & CO_FL_PRIVATE) {
2741 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002742 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2743 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002744 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002745 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002746 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002747 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002748 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002749 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002750 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2751 goto end;
2752 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002753 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002754 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002755 if (h1c->conn->owner == sess)
2756 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002757 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002758 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002759 /* The server doesn't want it, let's kill the connection right away */
2760 h1c->conn->mux->destroy(h1c);
2761 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2762 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002763 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002764 /* At this point, the connection has been added to the
2765 * server idle list, so another thread may already have
2766 * hijacked it, so we can't do anything with it.
2767 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002768 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002769 }
2770 }
2771
Christopher Fauletf1204b82019-07-19 14:51:06 +02002772 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002773 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002774 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002775 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002776 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002777 !h1c->conn->owner) {
2778 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002779 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002780 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002781 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002782 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002783 /* If we have a new request, process it immediately or
2784 * subscribe for reads waiting for new data
2785 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01002786 if (unlikely(b_data(&h1c->ibuf))) {
2787 if (h1_process(h1c) == -1)
2788 goto end;
2789 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002790 else
2791 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2792 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002793 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002794 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002795 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002796 end:
2797 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002798}
2799
2800
2801static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2802{
2803 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002804 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002805
2806 if (!h1s)
2807 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002808 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002809
Christopher Faulet6b81df72019-10-01 22:08:43 +02002810 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2811
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002812 if (cs->flags & CS_FL_SHR)
2813 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002814 if (cs->flags & CS_FL_KILL_CONN) {
2815 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2816 goto do_shutr;
2817 }
2818 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2819 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002820 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002821 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002822
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002823 if (h1s->flags & H1S_F_WANT_KAL) {
2824 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002825 goto end;
2826 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002827
Christopher Faulet7f366362019-04-08 10:51:20 +02002828 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002829 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2830 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002832 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002833 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002835 end:
2836 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002837}
2838
2839static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2840{
2841 struct h1s *h1s = cs->ctx;
2842 struct h1c *h1c;
2843
2844 if (!h1s)
2845 return;
2846 h1c = h1s->h1c;
2847
Christopher Faulet6b81df72019-10-01 22:08:43 +02002848 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2849
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002850 if (cs->flags & CS_FL_SHW)
2851 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002852 if (cs->flags & CS_FL_KILL_CONN) {
2853 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002854 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002855 }
2856 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2857 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2858 goto do_shutw;
2859 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002860
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002861 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2862 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002863 goto end;
2864 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002865
Christopher Faulet7f366362019-04-08 10:51:20 +02002866 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002867 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002868 if (!b_data(&h1c->obuf))
2869 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002870 end:
2871 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002872}
2873
Christopher Faulet666a0c42019-01-08 11:12:04 +01002874static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002875{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002876 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002877
Christopher Faulet6b81df72019-10-01 22:08:43 +02002878 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002879 conn_xprt_shutw(conn);
2880 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002881 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002882}
2883
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002884/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2885 * The <es> pointer is not allowed to differ from the one passed to the
2886 * subscribe() call. It always returns zero.
2887 */
2888static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002889{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002890 struct h1s *h1s = cs->ctx;
2891
2892 if (!h1s)
2893 return 0;
2894
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002895 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002896 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002897
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002898 es->events &= ~event_type;
2899 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002900 h1s->subs = NULL;
2901
2902 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002903 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002904
2905 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002906 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002907
Christopher Faulet51dbc942018-09-13 09:05:15 +02002908 return 0;
2909}
2910
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002911/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2912 * event subscriber <es> is not allowed to change from a previous call as long
2913 * as at least one event is still subscribed. The <event_type> must only be a
2914 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2915 * the conn_stream <cs> was already detached, in which case it will return -1.
2916 */
2917static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002918{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002919 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002920 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002921
2922 if (!h1s)
2923 return -1;
2924
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002925 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002926 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002927
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002928 es->events |= event_type;
2929 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002930
2931 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002932 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002933
2934
Christopher Faulet6b81df72019-10-01 22:08:43 +02002935 if (event_type & SUB_RETRY_SEND) {
2936 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002937 /*
2938 * If the conn_stream attempt to subscribe, and the
2939 * mux isn't subscribed to the connection, then it
2940 * probably means the connection wasn't established
2941 * yet, so we have to subscribe.
2942 */
2943 h1c = h1s->h1c;
2944 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2945 h1c->conn->xprt->subscribe(h1c->conn,
2946 h1c->conn->xprt_ctx,
2947 SUB_RETRY_SEND,
2948 &h1c->wait_event);
2949 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002950 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002951}
2952
2953/* Called from the upper layer, to receive data */
2954static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2955{
2956 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002957 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002958 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002959 size_t ret = 0;
2960
Willy Tarreau022e5e52020-09-10 09:33:15 +02002961 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002962 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002963 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002964 else
2965 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002966
Christopher Faulete18777b2019-04-16 16:46:36 +02002967 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002968 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02002969 h1c->flags |= H1C_F_WANT_SPLICE;
2970 TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002971 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002972 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002973 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002974 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002975 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002976 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002977 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002978 return ret;
2979}
2980
2981
2982/* Called from the upper layer, to send data */
2983static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2984{
2985 struct h1s *h1s = cs->ctx;
2986 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002987 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002988
2989 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002990 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002991 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002992
Willy Tarreau022e5e52020-09-10 09:33:15 +02002993 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002994
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002995 /* If we're not connected yet, or we're waiting for a handshake, stop
2996 * now, as we don't want to remove everything from the channel buffer
2997 * before we're sure we can send it.
2998 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002999 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003000 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003001 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003002 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003003
Christopher Faulet46230362019-10-17 16:04:20 +02003004 /* Inherit some flags from the upper layer */
3005 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3006 if (flags & CO_SFL_MSG_MORE)
3007 h1c->flags |= H1C_F_CO_MSG_MORE;
3008 if (flags & CO_SFL_STREAMER)
3009 h1c->flags |= H1C_F_CO_STREAMER;
3010
Christopher Fauletc31872f2019-06-04 22:09:36 +02003011 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003012 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003013
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003014 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3015 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003016 else
3017 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003018
3019 if ((count - ret) > 0)
3020 h1c->flags |= H1C_F_CO_MSG_MORE;
3021
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003022 if (!ret)
3023 break;
3024 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003025 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003026 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003027 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003028 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02003029 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003030 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003031 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003032}
3033
Willy Tarreaue5733232019-05-22 19:24:06 +02003034#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003035/* Send and get, using splicing */
3036static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3037{
3038 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003039 struct h1c *h1c = h1s->h1c;
3040 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003041 int ret = 0;
3042
Willy Tarreau022e5e52020-09-10 09:33:15 +02003043 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003044
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003045 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003046 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003047 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003048 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003049 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003050 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003051 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003052 goto end;
3053 }
3054
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003055 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3056 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003057 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3058 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003059 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003060 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003061 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003062 }
3063
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003064 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003065 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3066 goto end;
3067 }
3068
Christopher Faulet1be55f92018-10-02 15:59:23 +02003069 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3070 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003071 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003072 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003073 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003074 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003075 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003076 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003077 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003078 }
3079
Christopher Faulet1be55f92018-10-02 15:59:23 +02003080 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003081 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003082 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003083 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003084 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003085 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003086
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003087 if ((h1s->flags & H1S_F_REOS) ||
3088 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003089 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003090 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003091 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003092 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003093
Willy Tarreau022e5e52020-09-10 09:33:15 +02003094 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003095 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003096}
3097
3098static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3099{
3100 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003101 int ret = 0;
3102
Willy Tarreau022e5e52020-09-10 09:33:15 +02003103 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003104
Christopher Faulet1be55f92018-10-02 15:59:23 +02003105 if (b_data(&h1s->h1c->obuf))
3106 goto end;
3107
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003108 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003109 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003110 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003111 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3112 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003113 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003114 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003115 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003116
Willy Tarreau022e5e52020-09-10 09:33:15 +02003117 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003118 return ret;
3119}
3120#endif
3121
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003122static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3123{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003124 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003125 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003126
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003127 switch (mux_ctl) {
3128 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003129 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003130 ret |= MUX_STATUS_READY;
3131 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003132 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003133 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3134 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3135 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3136 MUX_ES_SUCCESS)));
3137 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003138 default:
3139 return -1;
3140 }
3141}
3142
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003143/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003144static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003145{
3146 struct h1c *h1c = conn->ctx;
3147 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003148 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003149
Christopher Fauletf376a312019-01-04 15:16:06 +01003150 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3151 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003152 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3153 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3154 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3155 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3156
3157 if (h1s) {
3158 char *method;
3159
3160 if (h1s->meth < HTTP_METH_OTHER)
3161 method = http_known_methods[h1s->meth].ptr;
3162 else
3163 method = "UNKNOWN";
3164 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3165 " .meth=%s status=%d",
3166 h1s, h1s->flags,
3167 h1m_state_str(h1s->req.state),
3168 h1m_state_str(h1s->res.state), method, h1s->status);
3169 if (h1s->cs)
3170 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3171 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003172
3173 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3174 if (h1s->subs) {
3175 if (h1s->subs) {
3176 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3177 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3178 h1s->subs->tasklet->calls,
3179 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003180 if (h1s->subs->tasklet->calls >= 1000000)
3181 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003182 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3183 chunk_appendf(&trash, ")");
3184 }
3185 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003186 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003187 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003188}
3189
3190
3191/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3192static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3193{
3194 struct h1_hdr_entry *entry;
3195
3196 /* Be sure there is a non-empty <to> */
3197 if (!strlen(to)) {
3198 memprintf(err, "expect <to>");
3199 return -1;
3200 }
3201
3202 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003203 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003204 memprintf(err, "<from> and <to> must not differ execpt the case");
3205 return -1;
3206 }
3207
3208 /* Be sure <from> does not already existsin the tree */
3209 if (ebis_lookup(&hdrs_map.map, from)) {
3210 memprintf(err, "duplicate entry '%s'", from);
3211 return -1;
3212 }
3213
3214 /* Create the entry and insert it in the tree */
3215 entry = malloc(sizeof(*entry));
3216 if (!entry) {
3217 memprintf(err, "out of memory");
3218 return -1;
3219 }
3220
3221 entry->node.key = strdup(from);
3222 entry->name.ptr = strdup(to);
3223 entry->name.len = strlen(to);
3224 if (!entry->node.key || !entry->name.ptr) {
3225 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003226 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003227 free(entry);
3228 memprintf(err, "out of memory");
3229 return -1;
3230 }
3231 ebis_insert(&hdrs_map.map, &entry->node);
3232 return 0;
3233}
3234
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003235/* Migrate the the connection to the current thread.
3236 * Return 0 if successful, non-zero otherwise.
3237 * Expected to be called with the old thread lock held.
3238 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003239static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003240{
3241 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003242 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003243
3244 if (fd_takeover(conn->handle.fd, conn) != 0)
3245 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003246
3247 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3248 /* We failed to takeover the xprt, even if the connection may
3249 * still be valid, flag it as error'd, as we have already
3250 * taken over the fd, and wake the tasklet, so that it will
3251 * destroy it.
3252 */
3253 conn->flags |= CO_FL_ERROR;
3254 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3255 return -1;
3256 }
3257
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003258 if (h1c->wait_event.events)
3259 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3260 h1c->wait_event.events, &h1c->wait_event);
3261 /* To let the tasklet know it should free itself, and do nothing else,
3262 * set its context to NULL.
3263 */
3264 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003265 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003266
3267 task = h1c->task;
3268 if (task) {
3269 task->context = NULL;
3270 h1c->task = NULL;
3271 __ha_barrier_store();
3272 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003273
3274 h1c->task = task_new(tid_bit);
3275 if (!h1c->task) {
3276 h1_release(h1c);
3277 return -1;
3278 }
3279 h1c->task->process = h1_timeout_task;
3280 h1c->task->context = h1c;
3281 }
3282 h1c->wait_event.tasklet = tasklet_new();
3283 if (!h1c->wait_event.tasklet) {
3284 h1_release(h1c);
3285 return -1;
3286 }
3287 h1c->wait_event.tasklet->process = h1_io_cb;
3288 h1c->wait_event.tasklet->context = h1c;
3289 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3290 SUB_RETRY_RECV, &h1c->wait_event);
3291
3292 return 0;
3293}
3294
3295
Christopher Faulet98fbe952019-07-22 16:18:24 +02003296static void h1_hdeaders_case_adjust_deinit()
3297{
3298 struct ebpt_node *node, *next;
3299 struct h1_hdr_entry *entry;
3300
3301 node = ebpt_first(&hdrs_map.map);
3302 while (node) {
3303 next = ebpt_next(node);
3304 ebpt_delete(node);
3305 entry = container_of(node, struct h1_hdr_entry, node);
3306 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003307 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003308 free(entry);
3309 node = next;
3310 }
3311 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003312}
3313
Christopher Faulet98fbe952019-07-22 16:18:24 +02003314static int cfg_h1_headers_case_adjust_postparser()
3315{
3316 FILE *file = NULL;
3317 char *c, *key_beg, *key_end, *value_beg, *value_end;
3318 char *err;
3319 int rc, line = 0, err_code = 0;
3320
3321 if (!hdrs_map.name)
3322 goto end;
3323
3324 file = fopen(hdrs_map.name, "r");
3325 if (!file) {
3326 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3327 hdrs_map.name);
3328 err_code |= ERR_ALERT | ERR_FATAL;
3329 goto end;
3330 }
3331
3332 /* now parse all lines. The file may contain only two header name per
3333 * line, separated by spaces. All heading and trailing spaces will be
3334 * ignored. Lines starting with a # are ignored.
3335 */
3336 while (fgets(trash.area, trash.size, file) != NULL) {
3337 line++;
3338 c = trash.area;
3339
3340 /* strip leading spaces and tabs */
3341 while (*c == ' ' || *c == '\t')
3342 c++;
3343
3344 /* ignore emptu lines, or lines beginning with a dash */
3345 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3346 continue;
3347
3348 /* look for the end of the key */
3349 key_beg = c;
3350 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3351 c++;
3352 key_end = c;
3353
3354 /* strip middle spaces and tabs */
3355 while (*c == ' ' || *c == '\t')
3356 c++;
3357
3358 /* look for the end of the value, it is the end of the line */
3359 value_beg = c;
3360 while (*c && *c != '\n' && *c != '\r')
3361 c++;
3362 value_end = c;
3363
3364 /* trim possibly trailing spaces and tabs */
3365 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3366 value_end--;
3367
3368 /* set final \0 and check entries */
3369 *key_end = '\0';
3370 *value_end = '\0';
3371
3372 err = NULL;
3373 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3374 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003375 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3376 hdrs_map.name, err, line);
3377 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003378 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003379 goto end;
3380 }
3381 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003382 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3383 hdrs_map.name, err, line);
3384 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003385 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003386 }
3387 }
3388
3389 end:
3390 if (file)
3391 fclose(file);
3392 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3393 return err_code;
3394}
3395
3396
3397/* config parser for global "h1-outgoing-header-case-adjust" */
3398static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3399 struct proxy *defpx, const char *file, int line,
3400 char **err)
3401{
3402 if (too_many_args(2, args, err, NULL))
3403 return -1;
3404 if (!*(args[1]) || !*(args[2])) {
3405 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3406 return -1;
3407 }
3408 return add_hdr_case_adjust(args[1], args[2], err);
3409}
3410
3411/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3412static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3413 struct proxy *defpx, const char *file, int line,
3414 char **err)
3415{
3416 if (too_many_args(1, args, err, NULL))
3417 return -1;
3418 if (!*(args[1])) {
3419 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3420 return -1;
3421 }
3422 free(hdrs_map.name);
3423 hdrs_map.name = strdup(args[1]);
3424 return 0;
3425}
3426
3427
3428/* config keyword parsers */
3429static struct cfg_kw_list cfg_kws = {{ }, {
3430 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3431 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3432 { 0, NULL, NULL },
3433 }
3434};
3435
3436INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3437REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3438
3439
Christopher Faulet51dbc942018-09-13 09:05:15 +02003440/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003441/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003442/****************************************/
3443
3444/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003445static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003446 .init = h1_init,
3447 .wake = h1_wake,
3448 .attach = h1_attach,
3449 .get_first_cs = h1_get_first_cs,
3450 .detach = h1_detach,
3451 .destroy = h1_destroy,
3452 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003453 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003454 .rcv_buf = h1_rcv_buf,
3455 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003456#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003457 .rcv_pipe = h1_rcv_pipe,
3458 .snd_pipe = h1_snd_pipe,
3459#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003460 .subscribe = h1_subscribe,
3461 .unsubscribe = h1_unsubscribe,
3462 .shutr = h1_shutr,
3463 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003464 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003465 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003466 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003467 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003468 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003469 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003470};
3471
3472
3473/* this mux registers default HTX proto */
3474static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003475{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003476
Willy Tarreau0108d902018-11-25 19:14:37 +01003477INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3478
Christopher Faulet51dbc942018-09-13 09:05:15 +02003479/*
3480 * Local variables:
3481 * c-indent-level: 8
3482 * c-basic-offset: 8
3483 * End:
3484 */