blob: 4a518accb5b8cd487df05763d0f66d34d907ac51 [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 */
45/* 0x00000080 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +020048#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
49#define H1C_F_CS_SHUTDOWN 0x00002000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
50/* 0x00000040 unused */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010051#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
52 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020055#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020056
Christopher Faulet46230362019-10-17 16:04:20 +020057#define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
58#define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
59
Christopher Faulet089acd52020-09-21 10:57:52 +020060#define H1C_F_WAIT_OPPOSITE 0x00100000 /* Don't read more data for now, waiting sync with opposite side */
Christopher Fauletae635762020-09-21 11:47:16 +020061#define H1C_F_WANT_SPLICE 0x00200000 /* Don't read into a bufffer because we want to use or we are using splicing */
Christopher Faulet0a799aa2020-09-24 09:52:52 +020062#define H1C_F_IS_BACK 0x00400000 /* Set on outgoing connection */
Christopher Fauletbb8baf42020-09-29 15:18:40 +020063
64#define H1C_F_CS_EMBRYONIC 0x01000000 /* Set when a H1 stream with no conn-stream is attached to the connection */
65#define H1C_F_CS_ATTACHED 0x02000000 /* Set when a H1 stream with a conn-stream is attached to the connection */
66#define H1C_F_CS_ALIVE (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED)
Christopher Faulet51dbc942018-09-13 09:05:15 +020067/*
68 * H1 Stream flags (32 bits)
69 */
Christopher Faulet129817b2018-09-20 16:14:40 +020070#define H1S_F_NONE 0x00000000
Christopher Faulet60ef12c2020-09-24 10:05:44 +020071/* 0x00000001..0x00000004 unsued */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020072#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020073#define H1S_F_WANT_KAL 0x00000010
74#define H1S_F_WANT_TUN 0x00000020
75#define H1S_F_WANT_CLO 0x00000040
76#define H1S_F_WANT_MSK 0x00000070
77#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020078
Christopher Faulet76014fd2019-12-10 11:47:22 +010079#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020080#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
81#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
82#define H1S_F_ERROR 0x00001800 /* stream error mask */
83
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020084#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 +020085#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020086
87/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088struct h1c {
89 struct connection *conn;
90 struct proxy *px;
91 uint32_t flags; /* Connection flags: H1C_F_* */
92
93 struct buffer ibuf; /* Input buffer to store data before parsing */
94 struct buffer obuf; /* Output buffer to store data after reformatting */
95
96 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
97 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
98
99 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100100 struct task *task; /* timeout management task */
101 int timeout; /* idle timeout duration in ticks */
102 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103};
104
105/* H1 stream descriptor */
106struct h1s {
107 struct h1c *h1c;
108 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100109 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100111 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200112
Olivier Houchardf502aca2018-12-14 19:42:40 +0100113 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200114 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200115 struct h1m req;
116 struct h1m res;
117
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500118 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200119 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200120};
121
Christopher Faulet98fbe952019-07-22 16:18:24 +0200122/* Map of headers used to convert outgoing headers */
123struct h1_hdrs_map {
124 char *name;
125 struct eb_root map;
126};
127
128/* An entry in a headers map */
129struct h1_hdr_entry {
130 struct ist name;
131 struct ebpt_node node;
132};
133
134/* Declare the headers map */
135static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
136
137
Christopher Faulet6b81df72019-10-01 22:08:43 +0200138/* trace source and events */
139static void h1_trace(enum trace_level level, uint64_t mask,
140 const struct trace_source *src,
141 const struct ist where, const struct ist func,
142 const void *a1, const void *a2, const void *a3, const void *a4);
143
144/* The event representation is split like this :
145 * h1c - internal H1 connection
146 * h1s - internal H1 stream
147 * strm - application layer
148 * rx - data receipt
149 * tx - data transmission
150 *
151 */
152static const struct trace_event h1_trace_events[] = {
153#define H1_EV_H1C_NEW (1ULL << 0)
154 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
155#define H1_EV_H1C_RECV (1ULL << 1)
156 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
157#define H1_EV_H1C_SEND (1ULL << 2)
158 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
159#define H1_EV_H1C_BLK (1ULL << 3)
160 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
161#define H1_EV_H1C_WAKE (1ULL << 4)
162 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
163#define H1_EV_H1C_END (1ULL << 5)
164 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
165#define H1_EV_H1C_ERR (1ULL << 6)
166 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
167
168#define H1_EV_RX_DATA (1ULL << 7)
169 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
170#define H1_EV_RX_EOI (1ULL << 8)
171 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
172#define H1_EV_RX_HDRS (1ULL << 9)
173 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
174#define H1_EV_RX_BODY (1ULL << 10)
175 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
176#define H1_EV_RX_TLRS (1ULL << 11)
177 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
178
179#define H1_EV_TX_DATA (1ULL << 12)
180 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
181#define H1_EV_TX_EOI (1ULL << 13)
182 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
183#define H1_EV_TX_HDRS (1ULL << 14)
184 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
185#define H1_EV_TX_BODY (1ULL << 15)
186 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
187#define H1_EV_TX_TLRS (1ULL << 16)
188 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
189
190#define H1_EV_H1S_NEW (1ULL << 17)
191 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
192#define H1_EV_H1S_BLK (1ULL << 18)
193 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
194#define H1_EV_H1S_END (1ULL << 19)
195 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
196#define H1_EV_H1S_ERR (1ULL << 20)
197 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
198
199#define H1_EV_STRM_NEW (1ULL << 21)
200 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
201#define H1_EV_STRM_RECV (1ULL << 22)
202 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
203#define H1_EV_STRM_SEND (1ULL << 23)
204 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
205#define H1_EV_STRM_WAKE (1ULL << 24)
206 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
207#define H1_EV_STRM_SHUT (1ULL << 25)
208 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
209#define H1_EV_STRM_END (1ULL << 26)
210 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
211#define H1_EV_STRM_ERR (1ULL << 27)
212 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
213
214 { }
215};
216
217static const struct name_desc h1_trace_lockon_args[4] = {
218 /* arg1 */ { /* already used by the connection */ },
219 /* arg2 */ { .name="h1s", .desc="H1 stream" },
220 /* arg3 */ { },
221 /* arg4 */ { }
222};
223
224static const struct name_desc h1_trace_decoding[] = {
225#define H1_VERB_CLEAN 1
226 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
227#define H1_VERB_MINIMAL 2
228 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
229#define H1_VERB_SIMPLE 3
230 { .name="simple", .desc="add request/response status line or htx info when available" },
231#define H1_VERB_ADVANCED 4
232 { .name="advanced", .desc="add header fields or frame decoding when available" },
233#define H1_VERB_COMPLETE 5
234 { .name="complete", .desc="add full data dump when available" },
235 { /* end */ }
236};
237
238static struct trace_source trace_h1 = {
239 .name = IST("h1"),
240 .desc = "HTTP/1 multiplexer",
241 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
242 .default_cb = h1_trace,
243 .known_events = h1_trace_events,
244 .lockon_args = h1_trace_lockon_args,
245 .decoding = h1_trace_decoding,
246 .report_events = ~0, // report everything by default
247};
248
249#define TRACE_SOURCE &trace_h1
250INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
251
Christopher Faulet51dbc942018-09-13 09:05:15 +0200252/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100253DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
254DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200255
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256static int h1_recv(struct h1c *h1c);
257static int h1_send(struct h1c *h1c);
258static int h1_process(struct h1c *h1c);
259static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100260static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200261static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200262static void h1_wake_stream_for_recv(struct h1s *h1s);
263static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200264
Christopher Faulet6b81df72019-10-01 22:08:43 +0200265/* the H1 traces always expect that arg1, if non-null, is of type connection
266 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
267 * that arg3, if non-null, is a htx for rx/tx headers.
268 */
269static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
270 const struct ist where, const struct ist func,
271 const void *a1, const void *a2, const void *a3, const void *a4)
272{
273 const struct connection *conn = a1;
274 const struct h1c *h1c = conn ? conn->ctx : NULL;
275 const struct h1s *h1s = a2;
276 const struct htx *htx = a3;
277 const size_t *val = a4;
278
279 if (!h1c)
280 h1c = (h1s ? h1s->h1c : NULL);
281
282 if (!h1c || src->verbosity < H1_VERB_CLEAN)
283 return;
284
285 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200286 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200287
288 /* Display request and response states if h1s is defined */
289 if (h1s)
290 chunk_appendf(&trace_buf, " [%s, %s]",
291 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
292
293 if (src->verbosity == H1_VERB_CLEAN)
294 return;
295
296 /* Display the value to the 4th argument (level > STATE) */
297 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100298 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200299
300 /* Display status-line if possible (verbosity > MINIMAL) */
301 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
302 const struct htx_blk *blk = htx_get_head_blk(htx);
303 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
304 enum htx_blk_type type = htx_get_blk_type(blk);
305
306 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
307 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
308 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
309 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
310 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
311 }
312
313 /* Display h1c info and, if defined, h1s info (pointer + flags) */
314 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
315 if (h1s)
316 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
317
318 if (src->verbosity == H1_VERB_MINIMAL)
319 return;
320
321 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
322 if (src->level > TRACE_LEVEL_USER) {
323 if (src->verbosity == H1_VERB_COMPLETE ||
324 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
325 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
326 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
327 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
328 if (src->verbosity == H1_VERB_COMPLETE ||
329 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
330 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
331 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
332 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
333 }
334
335 /* Display htx info if defined (level > USER) */
336 if (src->level > TRACE_LEVEL_USER && htx) {
337 int full = 0;
338
339 /* Full htx info (level > STATE && verbosity > SIMPLE) */
340 if (src->level > TRACE_LEVEL_STATE) {
341 if (src->verbosity == H1_VERB_COMPLETE)
342 full = 1;
343 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
344 full = 1;
345 }
346
347 chunk_memcat(&trace_buf, "\n\t", 2);
348 htx_dump(&trace_buf, htx, full);
349 }
350}
351
352
Christopher Faulet51dbc942018-09-13 09:05:15 +0200353/*****************************************************/
354/* functions below are for dynamic buffer management */
355/*****************************************************/
356/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100357 * Indicates whether or not we may receive data. The rules are the following :
358 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200359 * must not attempt to receive
360 * - if we are waiting for the connection establishment, we must not attempt
361 * to receive
362 * - if an error was detected on the stream we must not attempt to receive
363 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100364 * - if the input buffer failed to be allocated or is full , we must not try
365 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200366 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200367 * - otherwise must may not attempt to receive
368 */
369static inline int h1_recv_allowed(const struct h1c *h1c)
370{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100371 if (h1c->flags & H1C_F_CS_ERROR) {
372 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 +0200373 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200374 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200375
Willy Tarreau2febb842020-07-31 09:15:43 +0200376 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
377 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 +0100378 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200379 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100380
Christopher Faulet119ac872020-09-30 17:33:22 +0200381 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
382 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
383 return 0;
384 }
385
Christopher Faulet089acd52020-09-21 10:57:52 +0200386 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
387 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200388 return 0;
389 }
390
Christopher Fauletd17ad822020-09-24 15:14:29 +0200391 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200392 return 1;
393
Christopher Faulet6b81df72019-10-01 22:08:43 +0200394 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 +0200395 return 0;
396}
397
398/*
399 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
400 * flags are used to figure what buffer was requested. It returns 1 if the
401 * allocation succeeds, in which case the connection is woken up, or 0 if it's
402 * impossible to wake up and we prefer to be woken up later.
403 */
404static int h1_buf_available(void *target)
405{
406 struct h1c *h1c = target;
407
408 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200409 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 +0200410 h1c->flags &= ~H1C_F_IN_ALLOC;
411 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200412 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 return 1;
414 }
415
416 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200417 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 +0200418 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200419 if (h1c->h1s)
420 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200421 return 1;
422 }
423
Christopher Fauletd17ad822020-09-24 15:14:29 +0200424 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
425 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
426 h1c->flags &= ~H1C_F_IN_SALLOC;
427 tasklet_wakeup(h1c->wait_event.tasklet);
428 return 1;
429 }
430
Christopher Faulet51dbc942018-09-13 09:05:15 +0200431 return 0;
432}
433
434/*
435 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
436 */
437static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
438{
439 struct buffer *buf = NULL;
440
Willy Tarreau21046592020-02-26 10:39:36 +0100441 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200442 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
443 h1c->buf_wait.target = h1c;
444 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200445 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446 }
447 return buf;
448}
449
450/*
451 * Release a buffer, if any, and try to wake up entities waiting in the buffer
452 * wait queue.
453 */
454static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
455{
456 if (bptr->size) {
457 b_free(bptr);
458 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
459 }
460}
461
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100462/* returns the number of streams in use on a connection to figure if it's idle
463 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
464 * not. This flag is only set when no H1S is attached and when the previous
465 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100466 */
467static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200468{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100469 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200470
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100471 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200472}
473
Willy Tarreau00f18a32019-01-26 12:19:01 +0100474/* returns the number of streams still available on a connection */
475static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100476{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100477 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100478}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200479
Christopher Faulet7a145d62020-08-05 11:31:16 +0200480/* Refresh the h1c task timeout if necessary */
481static void h1_refresh_timeout(struct h1c *h1c)
482{
483 if (h1c->task) {
Christopher Fauletadcd7892020-10-05 17:13:05 +0200484 if (!(h1c->flags & H1C_F_CS_ALIVE) || (h1c->flags & H1C_F_CS_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200485 /* half-closed or dead connections : switch to clientfin/serverfin
486 * timeouts so that we don't hang too long on clients that have
487 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200488 */
489 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200490 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
491 }
492 else if (b_data(&h1c->obuf)) {
493 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200494 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200495 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
496 }
497 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC))) {
Christopher Faulet268c92e2020-12-01 11:42:53 +0100498 /* front connections waiting for a stream need a timeout. client timeout by
499 * default but http-keep-alive if defined
500 */
501 int timeout = h1c->timeout;
502
503 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
504 timeout = tick_first(timeout, h1c->px->timeout.httpka);
505
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200506 h1c->task->expire = tick_add(now_ms, timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200507 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 +0200508 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200509 else {
510 /* alive back connections of front connections with a conn-stream attached */
511 h1c->task->expire = TICK_ETERNITY;
512 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
513 }
514
515 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
516 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200517 }
518}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200519/*****************************************************************/
520/* functions below are dedicated to the mux setup and management */
521/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200522
523/* returns non-zero if there are input data pending for stream h1s. */
524static inline size_t h1s_data_pending(const struct h1s *h1s)
525{
526 const struct h1m *h1m;
527
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200528 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200529 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100530 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200531
532 return b_data(&h1s->h1c->ibuf);
533}
534
Christopher Faulet26256f82020-09-14 11:40:13 +0200535static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100536{
537 struct conn_stream *cs;
538
Christopher Faulet6b81df72019-10-01 22:08:43 +0200539 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200540 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200541 if (!cs) {
542 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 +0100543 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200544 }
Christopher Faulet47365272018-10-31 17:40:50 +0100545 h1s->cs = cs;
546 cs->ctx = h1s;
547
548 if (h1s->flags & H1S_F_NOT_FIRST)
549 cs->flags |= CS_FL_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200550 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet47365272018-10-31 17:40:50 +0100551
Christopher Faulet27182292020-07-03 15:08:49 +0200552 if (global.tune.options & GTUNE_USE_SPLICE) {
553 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100554 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200555 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100556
Christopher Faulet26256f82020-09-14 11:40:13 +0200557 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200558 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 +0100559 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200560 }
Christopher Faulet26256f82020-09-14 11:40:13 +0200561 *input = BUF_NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200562
563 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100564 return cs;
565
566 err:
567 cs_free(cs);
568 h1s->cs = NULL;
569 return NULL;
570}
571
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200572static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200573{
574 struct h1s *h1s;
575
Christopher Faulet6b81df72019-10-01 22:08:43 +0200576 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
577
Christopher Faulet51dbc942018-09-13 09:05:15 +0200578 h1s = pool_alloc(pool_head_h1s);
579 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100580 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200581 h1s->h1c = h1c;
582 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200583 h1s->sess = NULL;
584 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100585 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100586 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200587 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200588
589 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100590 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200591
Christopher Faulet129817b2018-09-20 16:14:40 +0200592 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100593 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200594
595 h1s->status = 0;
596 h1s->meth = HTTP_METH_OTHER;
597
Christopher Faulet47365272018-10-31 17:40:50 +0100598 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
599 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200600 h1c->flags = (h1c->flags & ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_CS_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100601
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200602 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
603 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200604
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200605 fail:
606 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
607 return NULL;
608}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100609
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200610static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
611{
612 struct session *sess = h1c->conn->owner;
613 struct h1s *h1s;
614
615 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
616
617 h1s = h1s_new(h1c);
618 if (!h1s)
619 goto fail;
620
621 h1s->sess = sess;
622
623 if (h1c->px->options2 & PR_O2_REQBUG_OK)
624 h1s->req.err_pos = -1;
625
626 if (!h1s_new_cs(h1s, &BUF_NULL))
627 goto fail_cs;
628
Christopher Faulet6b81df72019-10-01 22:08:43 +0200629 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200630 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100631
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200632 fail_cs:
Christopher Faulet47365272018-10-31 17:40:50 +0100633 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200634 fail:
635 sess_log(sess);
636 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
637 return NULL;
638}
639
640static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
641{
642 struct h1s *h1s;
643
644 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
645
646 h1s = h1s_new(h1c);
647 if (!h1s)
648 goto fail;
649
650 h1s->cs = cs;
651 h1s->sess = sess;
652 cs->ctx = h1s;
653
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200654 h1c->flags = (h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200655
656 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
657 h1s->res.err_pos = -1;
658
659 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
660 return h1s;
661
662 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200663 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 +0100664 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200665}
666
667static void h1s_destroy(struct h1s *h1s)
668{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200669 if (h1s) {
670 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200671
Christopher Faulet6b81df72019-10-01 22:08:43 +0200672 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200673 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200674
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100675 if (h1s->subs)
676 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200677
Christopher Fauletd17ad822020-09-24 15:14:29 +0200678 h1_release_buf(h1c, &h1s->rxbuf);
679
Christopher Faulet295b8d12020-09-30 17:14:55 +0200680 h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_WANT_SPLICE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED|
681 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
682 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200683 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet47365272018-10-31 17:40:50 +0100684 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200685 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
686 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100687
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200688 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100689 !(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 +0100690 (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 +0100691 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
692 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
693 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
694 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200695 else {
696 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
697 h1c->flags |= H1C_F_CS_SHUTDOWN;
698 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200699 pool_free(pool_head_h1s, h1s);
700 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200701}
702
703/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200704 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500705 * to the existing conn_stream (for outgoing connections or for incoming ones
706 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200707 * establishment). <input> is always used as Input buffer and may contain
708 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
709 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200710 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200711static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
712 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200713{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200714 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100715 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200716 void *conn_ctx = conn->ctx;
717
718 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200719
720 h1c = pool_alloc(pool_head_h1c);
721 if (!h1c)
722 goto fail_h1c;
723 h1c->conn = conn;
724 h1c->px = proxy;
725
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100726 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200727 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200728 h1c->obuf = BUF_NULL;
729 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200730 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200731
Willy Tarreau21046592020-02-26 10:39:36 +0100732 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200733 h1c->wait_event.tasklet = tasklet_new();
734 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200735 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200736 h1c->wait_event.tasklet->process = h1_io_cb;
737 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100738 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200739
Christopher Faulete9b70722019-04-08 10:46:02 +0200740 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200741 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100742 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
743 if (tick_isset(proxy->timeout.serverfin))
744 h1c->shut_timeout = proxy->timeout.serverfin;
745 } else {
746 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
747 if (tick_isset(proxy->timeout.clientfin))
748 h1c->shut_timeout = proxy->timeout.clientfin;
749 }
750 if (tick_isset(h1c->timeout)) {
751 t = task_new(tid_bit);
752 if (!t)
753 goto fail;
754
755 h1c->task = t;
756 t->process = h1_timeout_task;
757 t->context = h1c;
758 t->expire = tick_add(now_ms, h1c->timeout);
759 }
760
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100761 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200762
Christopher Faulet6b81df72019-10-01 22:08:43 +0200763 /* Always Create a new H1S */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200764 if (!(h1c->flags & H1C_F_IS_BACK)) {
765 if (!h1c_frt_stream_new(h1c))
766 goto fail;
767 }
768 else {
769 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
770 goto fail;
771 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100772
773 if (t)
774 task_queue(t);
775
Christopher Faulet51dbc942018-09-13 09:05:15 +0200776 /* Try to read, if nothing is available yet we'll just subscribe */
Christopher Faulet089acd52020-09-21 10:57:52 +0200777 if (!h1_recv_allowed(h1c))
778 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200779
780 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200781 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200782 return 0;
783
784 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200785 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200786 if (h1c->wait_event.tasklet)
787 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200788 pool_free(pool_head_h1c, h1c);
789 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200790 conn->ctx = conn_ctx; // restore saved context
791 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200792 return -1;
793}
794
Christopher Faulet73c12072019-04-08 11:23:22 +0200795/* release function. This one should be called to free all resources allocated
796 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200797 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200798static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200799{
Christopher Faulet61840e72019-04-15 09:33:32 +0200800 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200801
Christopher Faulet6b81df72019-10-01 22:08:43 +0200802 TRACE_POINT(H1_EV_H1C_END);
803
Christopher Faulet51dbc942018-09-13 09:05:15 +0200804 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200805 /* The connection must be aattached to this mux to be released */
806 if (h1c->conn && h1c->conn->ctx == h1c)
807 conn = h1c->conn;
808
Christopher Faulet6b81df72019-10-01 22:08:43 +0200809 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
810
Christopher Faulet61840e72019-04-15 09:33:32 +0200811 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200812 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200813 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200814 /* Make sure we're no longer subscribed to anything */
815 if (h1c->wait_event.events)
816 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
817 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200818 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200819 /* connection successfully upgraded to H2, this
820 * mux was already released */
821 return;
822 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200823 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200824 sess_log(conn->owner); /* Log if the upgrade failed */
825 }
826
Olivier Houchard45c44372019-06-11 14:06:23 +0200827
Willy Tarreau21046592020-02-26 10:39:36 +0100828 if (MT_LIST_ADDED(&h1c->buf_wait.list))
829 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200830
831 h1_release_buf(h1c, &h1c->ibuf);
832 h1_release_buf(h1c, &h1c->obuf);
833
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100834 if (h1c->task) {
835 h1c->task->context = NULL;
836 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
837 h1c->task = NULL;
838 }
839
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200840 if (h1c->wait_event.tasklet)
841 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200842
Christopher Fauletf2824e62018-10-01 12:12:37 +0200843 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200844 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100845 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200846 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200847 pool_free(pool_head_h1c, h1c);
848 }
849
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200850 if (conn) {
851 conn->mux = NULL;
852 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200853 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200854
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200855 conn_stop_tracking(conn);
856 conn_full_close(conn);
857 if (conn->destroy_cb)
858 conn->destroy_cb(conn);
859 conn_free(conn);
860 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200861}
862
863/******************************************************/
864/* functions below are for the H1 protocol processing */
865/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200866/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
867 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200868 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100869static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200870{
Christopher Faulet570d1612018-11-26 11:13:57 +0100871 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200872
Christopher Faulet570d1612018-11-26 11:13:57 +0100873 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200874 (*(p + 5) > '1' ||
875 (*(p + 5) == '1' && *(p + 7) >= '1')))
876 h1m->flags |= H1_MF_VER_11;
877}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200878
Christopher Faulet9768c262018-10-22 09:34:31 +0200879/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
880 * greater or equal to 1.1
881 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100882static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200883{
Christopher Faulet570d1612018-11-26 11:13:57 +0100884 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885
Christopher Faulet570d1612018-11-26 11:13:57 +0100886 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200887 (*(p + 5) > '1' ||
888 (*(p + 5) == '1' && *(p + 7) >= '1')))
889 h1m->flags |= H1_MF_VER_11;
890}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200891
Christopher Fauletf2824e62018-10-01 12:12:37 +0200892/* Deduce the connection mode of the client connection, depending on the
893 * configuration and the H1 message flags. This function is called twice, the
894 * first time when the request is parsed and the second time when the response
895 * is parsed.
896 */
897static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
898{
899 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200900
901 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100902 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200903 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100904 h1s->status == 101) {
905 /* Either we've established an explicit tunnel, or we're
906 * switching the protocol. In both cases, we're very unlikely to
907 * understand the next protocols. We have to switch to tunnel
908 * mode, so that we transfer the request and responses then let
909 * this protocol pass unmodified. When we later implement
910 * specific parsers for such protocols, we'll want to check the
911 * Upgrade header which contains information about that protocol
912 * for responses with status 101 (eg: see RFC2817 about TLS).
913 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200914 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200915 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 +0100916 }
917 else if (h1s->flags & H1S_F_WANT_KAL) {
918 /* By default the client is in KAL mode. CLOSE mode mean
919 * it is imposed by the client itself. So only change
920 * KAL mode here. */
921 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
922 /* no length known or explicit close => close */
923 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200924 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 +0100925 }
926 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
927 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500928 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100929 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200930 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 +0100931 }
932 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933 }
934 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100935 /* Input direction: first pass */
936 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
937 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200938 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200939 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 +0100940 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941 }
942
943 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200944 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200946 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);
947 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948}
949
950/* Deduce the connection mode of the client connection, depending on the
951 * configuration and the H1 message flags. This function is called twice, the
952 * first time when the request is parsed and the second time when the response
953 * is parsed.
954 */
955static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
956{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100957 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100958 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100959 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200960
Christopher Fauletf2824e62018-10-01 12:12:37 +0200961 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100962 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200963 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100964 h1s->status == 101) {
965 /* Either we've established an explicit tunnel, or we're
966 * switching the protocol. In both cases, we're very unlikely to
967 * understand the next protocols. We have to switch to tunnel
968 * mode, so that we transfer the request and responses then let
969 * this protocol pass unmodified. When we later implement
970 * specific parsers for such protocols, we'll want to check the
971 * Upgrade header which contains information about that protocol
972 * for responses with status 101 (eg: see RFC2817 about TLS).
973 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200974 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200975 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 +0100976 }
977 else if (h1s->flags & H1S_F_WANT_KAL) {
978 /* By default the server is in KAL mode. CLOSE mode mean
979 * it is imposed by haproxy itself. So only change KAL
980 * mode here. */
981 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
982 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
983 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
984 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200985 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 +0100986 }
987 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200988 }
Christopher Faulet70033782018-12-05 13:50:11 +0100989 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100990 /* Output direction: first pass */
991 if (h1m->flags & H1_MF_CONN_CLO) {
992 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100993 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200994 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 +0100995 }
996 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
997 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
998 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
999 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1000 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1001 /* no explicit keep-alive option httpclose/server-close => close */
1002 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001003 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 +01001004 }
Christopher Faulet70033782018-12-05 13:50:11 +01001005 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001006
1007 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001008 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001009 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 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);
1011 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001012}
1013
Christopher Fauletb992af02019-03-28 15:42:24 +01001014static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015{
1016 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001017
1018 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1019 * token is found
1020 */
1021 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001022 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001023
1024 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001025 if (!(h1m->flags & H1_MF_VER_11)) {
1026 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 +01001027 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001028 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001029 }
1030 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001031 if (h1m->flags & H1_MF_VER_11) {
1032 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001033 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001034 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001035 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036}
1037
Christopher Fauletb992af02019-03-28 15:42:24 +01001038static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001039{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001040 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1041 * token is found
1042 */
1043 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001044 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001045
1046 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001047 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001048 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1049 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 +01001050 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001051 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001052 }
1053 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001054 if (h1m->flags & H1_MF_VER_11) {
1055 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001056 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001057 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001058 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001059}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001060
Christopher Fauletb992af02019-03-28 15:42:24 +01001061static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001062{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001063 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001064 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001065 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001066 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001067}
1068
Christopher Fauletb992af02019-03-28 15:42:24 +01001069static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1070{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001071 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001072 h1_set_cli_conn_mode(h1s, h1m);
1073 else
1074 h1_set_srv_conn_mode(h1s, h1m);
1075
1076 if (!(h1m->flags & H1_MF_RESP))
1077 h1_update_req_conn_value(h1s, h1m, conn_val);
1078 else
1079 h1_update_res_conn_value(h1s, h1m, conn_val);
1080}
Christopher Faulete44769b2018-11-29 23:01:45 +01001081
Christopher Faulet98fbe952019-07-22 16:18:24 +02001082/* Try to adjust the case of the message header name using the global map
1083 * <hdrs_map>.
1084 */
1085static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1086{
1087 struct ebpt_node *node;
1088 struct h1_hdr_entry *entry;
1089
1090 /* No entry in the map, do nothing */
1091 if (eb_is_empty(&hdrs_map.map))
1092 return;
1093
1094 /* No conversion fo the request headers */
1095 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1096 return;
1097
1098 /* No conversion fo the response headers */
1099 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1100 return;
1101
1102 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1103 if (!node)
1104 return;
1105 entry = container_of(node, struct h1_hdr_entry, node);
1106 name->ptr = entry->name.ptr;
1107 name->len = entry->name.len;
1108}
1109
Christopher Faulete44769b2018-11-29 23:01:45 +01001110/* Append the description of what is present in error snapshot <es> into <out>.
1111 * The description must be small enough to always fit in a buffer. The output
1112 * buffer may be the trash so the trash must not be used inside this function.
1113 */
1114static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1115{
1116 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001117 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1118 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1119 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1120 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1121 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1122 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001123}
1124/*
1125 * Capture a bad request or response and archive it in the proxy's structure.
1126 * By default it tries to report the error position as h1m->err_pos. However if
1127 * this one is not set, it will then report h1m->next, which is the last known
1128 * parsing point. The function is able to deal with wrapping buffers. It always
1129 * displays buffers as a contiguous area starting at buf->p. The direction is
1130 * determined thanks to the h1m's flags.
1131 */
1132static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1133 struct h1m *h1m, struct buffer *buf)
1134{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001135 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001136 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001137 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001138 union error_snapshot_ctx ctx;
1139
Christopher Fauletbb8baf42020-09-29 15:18:40 +02001140 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001141 if (sess == NULL)
1142 sess = si_strm(h1s->cs->data)->sess;
1143 if (!(h1m->flags & H1_MF_RESP))
1144 other_end = si_strm(h1s->cs->data)->be;
1145 else
1146 other_end = sess->fe;
1147 } else
1148 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001149
1150 /* http-specific part now */
1151 ctx.h1.state = h1m->state;
1152 ctx.h1.c_flags = h1c->flags;
1153 ctx.h1.s_flags = h1s->flags;
1154 ctx.h1.m_flags = h1m->flags;
1155 ctx.h1.m_clen = h1m->curr_len;
1156 ctx.h1.m_blen = h1m->body_len;
1157
1158 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1159 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001160 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1161 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001162}
1163
Christopher Fauletadb22202018-12-12 10:32:09 +01001164/* Emit the chunksize followed by a CRLF in front of data of the buffer
1165 * <buf>. It goes backwards and starts with the byte before the buffer's
1166 * head. The caller is responsible for ensuring there is enough room left before
1167 * the buffer's head for the string.
1168 */
1169static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1170{
1171 char *beg, *end;
1172
1173 beg = end = b_head(buf);
1174 *--beg = '\n';
1175 *--beg = '\r';
1176 do {
1177 *--beg = hextab[chksz & 0xF];
1178 } while (chksz >>= 4);
1179 buf->head -= (end - beg);
1180 b_add(buf, end - beg);
1181}
1182
1183/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1184 * ensuring there is enough room left in the buffer for the string. */
1185static void h1_emit_chunk_crlf(struct buffer *buf)
1186{
1187 *(b_peek(buf, b_data(buf))) = '\r';
1188 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1189 b_add(buf, 2);
1190}
1191
Christopher Faulet129817b2018-09-20 16:14:40 +02001192/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001193 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001194 * CONNECT requests. On the client side, if the response is not finished, the
1195 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001196 */
1197static void h1_set_req_tunnel_mode(struct h1s *h1s)
1198{
1199 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1200 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001201 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1202
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001203 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001204 h1s->flags &= ~H1S_F_PARSING_DONE;
1205 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001206 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1207 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 +01001208 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001209 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001210 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1211 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001212 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001213 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 +01001214 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001215}
1216
1217/*
1218 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001219 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001220 * last case, this function takes care to switch the request to tunnel mode if
1221 * possible. On the server side, if the request is not finished, the mux is mark
1222 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001223 */
1224static void h1_set_res_tunnel_mode(struct h1s *h1s)
1225{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001226
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001227 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1228 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001229 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1230
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001231 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001232 h1s->flags &= ~H1S_F_PARSING_DONE;
1233 /* On protocol switching, switch the request to tunnel mode if it is in
1234 * DONE state. Otherwise we will wait the end of the request to switch
1235 * it in tunnel mode.
1236 */
1237 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001238 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1239 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 +01001240 }
1241 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1242 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1243 h1s->req.state = H1_MSG_TUNNEL;
1244 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1245 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001246 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001247 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1248 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001249 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001250 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 +01001251 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001252}
1253
1254/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001255 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001256 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001257 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001258 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001259static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001260 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001261{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001262 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001263 int ret = 0;
1264
Willy Tarreau022e5e52020-09-10 09:33:15 +02001265 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 +02001266
Christopher Faulet89aed322020-06-02 17:33:56 +02001267 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1268 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1269 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001270 /* Try to match H2 preface before parsing the request headers. */
1271 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001272 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001273 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001274 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001275 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001276 else {
1277 if (h1s->meth == HTTP_METH_CONNECT)
1278 h1m->flags |= H1_MF_METH_CONNECT;
1279 if (h1s->meth == HTTP_METH_HEAD)
1280 h1m->flags |= H1_MF_METH_HEAD;
1281 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001282
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001283 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1284 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001285 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 +02001286 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001287 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001288 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 +02001289 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1290 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001291 goto end;
1292 }
1293
Christopher Faulet486498c2019-10-11 14:22:00 +02001294 if (h1m->err_pos >= 0) {
1295 /* Maybe we found an error during the parsing while we were
1296 * configured not to block on that, so we have to capture it
1297 * now.
1298 */
1299 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1300 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1301 }
1302
Christopher Faulet129817b2018-09-20 16:14:40 +02001303 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001304 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001305 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001306 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001307 }
1308 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001309 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001310 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001311 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001312 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001313 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001315
Christopher Faulet129817b2018-09-20 16:14:40 +02001316 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001317 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 +02001318 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001319
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001320 h2c_upgrade:
1321 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Fauleta583af62020-09-24 15:35:37 +02001322 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001323 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001324 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1325 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001326}
1327
1328/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001329 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001330 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1331 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001332 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001333static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001334 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001335 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001336{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001337 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001338
Willy Tarreau022e5e52020-09-10 09:33:15 +02001339 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 +02001340 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001341 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001342 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 +01001343 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001344 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001345 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 +02001346 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001347 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001348 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001349 }
1350
Christopher Faulet02a02532019-11-15 09:36:28 +01001351 *ofs += ret;
1352
1353 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001354 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 +02001355 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001356}
1357
1358/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001359 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1360 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1361 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1362 * responsible to update the parser state <h1m>.
1363 */
1364static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1365 struct buffer *buf, size_t *ofs, size_t max)
1366{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001367 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001368
Willy Tarreau022e5e52020-09-10 09:33:15 +02001369 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 +02001370 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001371 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001372 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 +01001373 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001374 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001375 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 +02001376 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1377 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001378 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001379 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001380
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001381 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001382
1383 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001384 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 +02001385 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001386}
1387
1388/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001389 * 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 +02001390 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001391 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001392static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1393 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001394{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001395 int ret;
1396
Willy Tarreau022e5e52020-09-10 09:33:15 +02001397 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 +01001398 ret = h1_parse_msg_eom(h1m, htx, max);
1399 if (!ret) {
1400 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1401 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001402 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001403 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 +01001404 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1405 }
1406 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001407 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408
Christopher Faulet76014fd2019-12-10 11:47:22 +01001409 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001410 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001411 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 +01001412 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001413}
1414
1415/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001416 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001417 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1418 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001419 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001420static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001421{
Christopher Faulet539e0292018-11-19 10:40:09 +01001422 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001423 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001424 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001425 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001426 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001427
Christopher Faulet539e0292018-11-19 10:40:09 +01001428 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001429 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001430
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001431 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001432
Christopher Faulet74027762019-02-26 14:45:05 +01001433 data = htx->data;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001434 if (h1s->flags & H1S_F_PARSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001435 goto end;
1436
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001437 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001438 size_t used = htx_used_space(htx);
1439
Christopher Faulet129817b2018-09-20 16:14:40 +02001440 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001441 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001442 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001443 if (!ret)
1444 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001445
1446 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1447 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1448
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001449 if ((h1m->flags & H1_MF_RESP) &&
1450 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1451 h1m_init_res(&h1s->res);
1452 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001453 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001454 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001456 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001457 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001458 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001459 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001460 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001461
1462 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1463 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001465 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001466 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1467 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1468 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001469 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001470
Christopher Faulet76014fd2019-12-10 11:47:22 +01001471 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1472 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001473 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001474 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001475 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1476 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1477 break;
1478
1479 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1480 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1481 }
1482
Christopher Fauletf3158e92019-11-15 11:14:23 +01001483 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1484 h1_set_req_tunnel_mode(h1s);
1485 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001486 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1487 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 +02001488 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001489 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001490 else
1491 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001492 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001493 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001494 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001495 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001496 if (!ret)
1497 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001498
1499 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1500 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001501 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001502 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001503 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001504 break;
1505 }
1506
Christopher Faulet30db3d72019-05-17 15:35:33 +02001507 count -= htx_used_space(htx) - used;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001508 } while (!(h1s->flags & H1S_F_PARSING_ERROR));
Christopher Faulet129817b2018-09-20 16:14:40 +02001509
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001510 if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001511 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001512 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001513 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001514
Christopher Faulet539e0292018-11-19 10:40:09 +01001515 b_del(&h1c->ibuf, total);
1516
1517 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001518 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001519 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001520 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001521 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001522 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001523 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001524 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001525
Christopher Fauleta583af62020-09-24 15:35:37 +02001526 if (!(h1m->flags & H1_MF_CHNK) &&
1527 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1528 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1529 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1530 }
1531 else {
1532 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1533 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1534 }
1535
1536 if (h1s->flags & H1S_F_PARSING_DONE)
1537 h1s->cs->flags |= CS_FL_EOI;
1538
Christopher Fauletcf56b992018-12-11 16:12:31 +01001539 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1540
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001541 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001542 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001543 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001544 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001545 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001546 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001547 if (h1m->state == H1_MSG_TUNNEL)
1548 h1s->cs->flags |= CS_FL_EOI;
1549 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001550 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001551 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001552
Christopher Faulet6b81df72019-10-01 22:08:43 +02001553 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001554 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001555
1556 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001557 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001558 htx_to_buf(htx, buf);
Christopher Fauleta583af62020-09-24 15:35:37 +02001559 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001560 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001562}
1563
Christopher Faulet129817b2018-09-20 16:14:40 +02001564/*
1565 * Process outgoing data. It parses data and transfer them from the channel buffer into
1566 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1567 * 0 if it couldn't proceed.
1568 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001569static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1570{
Christopher Faulet129817b2018-09-20 16:14:40 +02001571 struct h1s *h1s = h1c->h1s;
1572 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001573 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001575 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001576 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001577
Christopher Faulet47365272018-10-31 17:40:50 +01001578 if (!count)
1579 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001580
Christopher Faulet94b2c762019-05-24 15:28:57 +02001581 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001582 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1583
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001584 if (htx_is_empty(chn_htx))
1585 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001586
Christopher Faulet51dbc942018-09-13 09:05:15 +02001587 if (!h1_get_buf(h1c, &h1c->obuf)) {
1588 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001589 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 +02001590 goto end;
1591 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001592
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001593 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001594
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001595 if (h1s->flags & H1S_F_PROCESSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001596 goto end;
1597
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001598 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001599 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001600
Willy Tarreau3815b222018-12-11 19:50:43 +01001601 /* Perform some optimizations to reduce the number of buffer copies.
1602 * First, if the mux's buffer is empty and the htx area contains
1603 * exactly one data block of the same size as the requested count,
1604 * then it's possible to simply swap the caller's buffer with the
1605 * mux's output buffer and adjust offsets and length to match the
1606 * entire DATA HTX block in the middle. In this case we perform a
1607 * true zero-copy operation from end-to-end. This is the situation
1608 * that happens all the time with large files. Second, if this is not
1609 * possible, but the mux's output buffer is empty, we still have an
1610 * opportunity to avoid the copy to the intermediary buffer, by making
1611 * the intermediary buffer's area point to the output buffer's area.
1612 * In this case we want to skip the HTX header to make sure that copies
1613 * remain aligned and that this operation remains possible all the
1614 * time. This goes for headers, data blocks and any data extracted from
1615 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001616 */
1617 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001618 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001619 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001620 htx_get_blk_value(chn_htx, blk).len == count) {
1621 void *old_area = h1c->obuf.area;
1622
Christopher Faulet6b81df72019-10-01 22:08:43 +02001623 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 +01001624 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001625 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001626 h1c->obuf.data = count;
1627
1628 buf->area = old_area;
1629 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001630
Christopher Faulet6b81df72019-10-01 22:08:43 +02001631 chn_htx = (struct htx *)buf->area;
1632 htx_reset(chn_htx);
1633
Christopher Fauletadb22202018-12-12 10:32:09 +01001634 /* The message is chunked. We need to emit the chunk
1635 * size. We have at least the size of the struct htx to
1636 * write the chunk envelope. It should be enough.
1637 */
1638 if (h1m->flags & H1_MF_CHNK) {
1639 h1_emit_chunk_size(&h1c->obuf, count);
1640 h1_emit_chunk_crlf(&h1c->obuf);
1641 }
1642
Willy Tarreau3815b222018-12-11 19:50:43 +01001643 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001644 if (h1m->state == H1_MSG_DATA)
1645 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001646 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001647 else
1648 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001649 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001650 goto out;
1651 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001652 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001653 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001654 else
1655 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001656
Christopher Fauletc2518a52019-06-25 21:41:02 +02001657 tmp.data = 0;
1658 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001659 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001660 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001662 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001663 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001664 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001665
Christopher Fauletb2e84162018-12-06 11:39:49 +01001666 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001667 if (type != HTX_BLK_DATA && vlen > count)
1668 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001669
Christopher Faulet94b2c762019-05-24 15:28:57 +02001670 if (type == HTX_BLK_UNUSED)
1671 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001672
Christopher Faulet94b2c762019-05-24 15:28:57 +02001673 switch (h1m->state) {
1674 case H1_MSG_RQBEFORE:
1675 if (type != HTX_BLK_REQ_SL)
1676 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001677 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 +02001678 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001679 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001681 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001682 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001684 if (sl->flags & HTX_SL_F_BODYLESS)
1685 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001687 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1688 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1689 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1690 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1691 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001692 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001693
Christopher Faulet94b2c762019-05-24 15:28:57 +02001694 case H1_MSG_RPBEFORE:
1695 if (type != HTX_BLK_RES_SL)
1696 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001697 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 +02001698 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001699 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001701 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001702 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001703 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001705 if (sl->info.res.status < 200 &&
1706 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001707 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 h1m->state = H1_MSG_HDR_FIRST;
1709 break;
1710
Christopher Faulet94b2c762019-05-24 15:28:57 +02001711 case H1_MSG_HDR_FIRST:
1712 case H1_MSG_HDR_NAME:
1713 case H1_MSG_HDR_L2_LWS:
1714 if (type == HTX_BLK_EOH)
1715 goto last_lf;
1716 if (type != HTX_BLK_HDR)
1717 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001718
Christopher Faulet9768c262018-10-22 09:34:31 +02001719 h1m->state = H1_MSG_HDR_NAME;
1720 n = htx_get_blk_name(chn_htx, blk);
1721 v = htx_get_blk_value(chn_htx, blk);
1722
Christopher Faulet86d144c2019-08-14 16:32:25 +02001723 /* Skip all pseudo-headers */
1724 if (*(n.ptr) == ':')
1725 goto skip_hdr;
1726
Christopher Fauletb045bb22020-02-28 10:42:20 +01001727 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001728 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001729 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001730 /* Only skip C-L header with invalid value. */
1731 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001732 goto skip_hdr;
1733 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001734 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001735 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001736 if (!v.len)
1737 goto skip_hdr;
1738 }
1739
Christopher Faulet67d58092019-10-02 10:51:38 +02001740 /* Skip header if same name is used to add the server name */
1741 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1742 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1743 goto skip_hdr;
1744
Christopher Faulet98fbe952019-07-22 16:18:24 +02001745 /* Try to adjust the case of the header name */
1746 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1747 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001748 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001749 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001750 skip_hdr:
1751 h1m->state = H1_MSG_HDR_L2_LWS;
1752 break;
1753
Christopher Faulet94b2c762019-05-24 15:28:57 +02001754 case H1_MSG_LAST_LF:
1755 if (type != HTX_BLK_EOH)
1756 goto error;
1757 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001758 h1m->state = H1_MSG_LAST_LF;
1759 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001760 /* If the reply comes from haproxy while the request is
1761 * not finished, we force the connection close. */
1762 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1763 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1764 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1765 }
1766
1767 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001768 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001769 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001770 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001771 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001772 /* Try to adjust the case of the header name */
1773 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1774 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001775 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001776 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001777 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001778 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001779 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001780
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001781 if ((h1s->meth != HTTP_METH_CONNECT &&
1782 (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 +01001783 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1784 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1785 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1786 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1787 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001788 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001789 n = ist("transfer-encoding");
1790 v = ist("chunked");
1791 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1792 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001793 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001794 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001795 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001796 h1m->flags |= H1_MF_CHNK;
1797 }
1798
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001799 /* Now add the server name to a header (if requested) */
1800 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1801 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1802 struct server *srv = objt_server(h1c->conn->target);
1803
1804 if (srv) {
1805 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1806 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001807
1808 /* Try to adjust the case of the header name */
1809 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1810 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001811 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001812 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001813 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001814 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001815 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1816 }
1817
Christopher Fauletc2518a52019-06-25 21:41:02 +02001818 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001819 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001820
Christopher Faulet6b81df72019-10-01 22:08:43 +02001821 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1822 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1823
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001824 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1825 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1826 h1_set_req_tunnel_mode(h1s);
1827 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001828 else if ((h1m->flags & H1_MF_RESP) &&
1829 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001830 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001831 * to the client. Switch the response to tunnel mode.
1832 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001833 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001834 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 +01001835 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001836 else if ((h1m->flags & H1_MF_RESP) &&
1837 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1838 h1m_init_res(&h1s->res);
1839 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001840 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001841 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001842 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001843 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001844 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001845 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1846 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001847 else
1848 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001849 break;
1850
Christopher Faulet94b2c762019-05-24 15:28:57 +02001851 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001852 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001853 if (type == HTX_BLK_EOM) {
1854 /* Chunked message without explicit trailers */
1855 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001856 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001857 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001858 }
1859 goto done;
1860 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001861 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001862 /* If the message is not chunked, never
1863 * add the last chunk. */
1864 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001865 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001866 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 +02001867 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001868 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001869 else if (type != HTX_BLK_DATA)
1870 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001871
1872 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 +02001873
1874
1875 if (vlen > count) {
1876 /* Get the maximum amount of data we can xferred */
1877 vlen = count;
1878 }
1879
1880 chklen = 0;
1881 if (h1m->flags & H1_MF_CHNK) {
1882 chklen = b_room(&tmp);
1883 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1884 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1885 (chklen < 1048576) ? 5 : 8);
1886 chklen += 4; /* 2 x CRLF */
1887 }
1888
1889 if (vlen + chklen > b_room(&tmp)) {
1890 /* too large for the buffer */
1891 if (chklen >= b_room(&tmp))
1892 goto full;
1893 vlen = b_room(&tmp) - chklen;
1894 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001895 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001896 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001897 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001898 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001899
1900 if (h1m->state == H1_MSG_DATA)
1901 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001902 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001903 else
1904 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001905 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001906 break;
1907
Christopher Faulet94b2c762019-05-24 15:28:57 +02001908 case H1_MSG_TRAILERS:
1909 if (type == HTX_BLK_EOM)
1910 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001911 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001912 goto error;
1913 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001914 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001915 /* If the message is not chunked, ignore
1916 * trailers. It may happen with H2 messages. */
1917 if (!(h1m->flags & H1_MF_CHNK))
1918 break;
1919
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001920 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001921 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001922 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001923 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1924 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001925 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001926 else { // HTX_BLK_TLR
1927 n = htx_get_blk_name(chn_htx, blk);
1928 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001929
1930 /* Try to adjust the case of the header name */
1931 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1932 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001933 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001934 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001935 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001936 break;
1937
Christopher Faulet94b2c762019-05-24 15:28:57 +02001938 case H1_MSG_DONE:
1939 if (type != HTX_BLK_EOM)
1940 goto error;
1941 done:
1942 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001943 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1944 h1_set_req_tunnel_mode(h1s);
1945 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1946 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001947 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1948 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001949 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02001950 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 +02001951 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001952
1953 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1954 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001955 break;
1956
Christopher Faulet9768c262018-10-22 09:34:31 +02001957 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001958 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001959 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001960 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001961 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001962 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001963 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001964 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1965 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001966 break;
1967 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001968
1969 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001970 total += vlen;
1971 count -= vlen;
1972 if (sz == vlen)
1973 blk = htx_remove_blk(chn_htx, blk);
1974 else {
1975 htx_cut_data_blk(chn_htx, blk, vlen);
1976 break;
1977 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001978 }
1979
Christopher Faulet9768c262018-10-22 09:34:31 +02001980 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001981 /* when the output buffer is empty, tmp shares the same area so that we
1982 * only have to update pointers and lengths.
1983 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001984 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1985 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001986 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001987 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988
Willy Tarreau3815b222018-12-11 19:50:43 +01001989 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001990 out:
1991 if (!buf_room_for_htx_data(&h1c->obuf)) {
1992 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001993 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001994 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001995 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001996 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001997 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001998
1999 full:
2000 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2001 h1c->flags |= H1C_F_OUT_FULL;
2002 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002003}
2004
Christopher Faulet51dbc942018-09-13 09:05:15 +02002005/*********************************************************/
2006/* functions below are I/O callbacks from the connection */
2007/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002008static void h1_wake_stream_for_recv(struct h1s *h1s)
2009{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002010 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002011 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002012 tasklet_wakeup(h1s->subs->tasklet);
2013 h1s->subs->events &= ~SUB_RETRY_RECV;
2014 if (!h1s->subs->events)
2015 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002016 }
2017}
2018static void h1_wake_stream_for_send(struct h1s *h1s)
2019{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002020 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002021 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002022 tasklet_wakeup(h1s->subs->tasklet);
2023 h1s->subs->events &= ~SUB_RETRY_SEND;
2024 if (!h1s->subs->events)
2025 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002026 }
2027}
2028
Christopher Faulet51dbc942018-09-13 09:05:15 +02002029/*
2030 * Attempt to read data, and subscribe if none available
2031 */
2032static int h1_recv(struct h1c *h1c)
2033{
2034 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002035 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002036 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002038 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002039
Christopher Faulet6b81df72019-10-01 22:08:43 +02002040 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2041
2042 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2043 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002044 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002045 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002046
Christopher Fauletae635762020-09-21 11:47:16 +02002047 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2048 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002049 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002050 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002051 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002052
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002053 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2054 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002055 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002056 goto end;
2057 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002058
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002059 /*
2060 * If we only have a small amount of data, realign it,
2061 * it's probably cheaper than doing 2 recv() calls.
2062 */
2063 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2064 b_slow_realign(&h1c->ibuf, trash.area, 0);
2065
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002066 /* avoid useless reads after first responses */
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002067 if (h1s && ((!(h1c->flags & H1C_F_IS_BACK) && h1s->req.state == H1_MSG_RQBEFORE) ||
2068 ((h1c->flags & H1C_F_IS_BACK) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002069 flags |= CO_RFL_READ_ONCE;
2070
Willy Tarreau45f2b892018-12-05 07:59:27 +01002071 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002072 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002073 if (h1c->flags & H1C_F_IN_FULL) {
2074 h1c->flags &= ~H1C_F_IN_FULL;
2075 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2076 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002077
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002078 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002079 if (!b_data(&h1c->ibuf)) {
2080 /* try to pre-align the buffer like the rxbufs will be
2081 * to optimize memory copies.
2082 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002083 h1c->ibuf.head = sizeof(struct htx);
2084 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002085 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086 }
Christopher Faulet47365272018-10-31 17:40:50 +01002087 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002088 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002089 rcvd = 1;
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002090 if (h1c->flags & H1C_F_CS_ATTACHED)
Christopher Faulet37e36072018-12-04 15:54:12 +01002091 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Faulet47365272018-10-31 17:40:50 +01002092 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002093
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002094 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002095 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002096 goto end;
2097 }
2098
Christopher Faulet6b81df72019-10-01 22:08:43 +02002099 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002100 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002101
Christopher Faulet9768c262018-10-22 09:34:31 +02002102 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002103 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2104 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002105
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002106 if (conn_xprt_read0_pending(conn) && h1s) {
2107 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002108 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002109 rcvd = 1;
2110 }
2111
Christopher Faulet51dbc942018-09-13 09:05:15 +02002112 if (!b_data(&h1c->ibuf))
2113 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002114 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002115 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002116 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2117 }
2118
2119 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002120 return rcvd;
2121}
2122
2123
2124/*
2125 * Try to send data if possible
2126 */
2127static int h1_send(struct h1c *h1c)
2128{
2129 struct connection *conn = h1c->conn;
2130 unsigned int flags = 0;
2131 size_t ret;
2132 int sent = 0;
2133
Christopher Faulet6b81df72019-10-01 22:08:43 +02002134 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2135
2136 if (conn->flags & CO_FL_ERROR) {
2137 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002138 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002139 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002140
2141 if (!b_data(&h1c->obuf))
2142 goto end;
2143
Christopher Faulet46230362019-10-17 16:04:20 +02002144 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002145 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002146 if (h1c->flags & H1C_F_CO_STREAMER)
2147 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002148
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002149 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002150 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002151 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002152 if (h1c->flags & H1C_F_OUT_FULL) {
2153 h1c->flags &= ~H1C_F_OUT_FULL;
2154 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2155 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002156 b_del(&h1c->obuf, ret);
2157 sent = 1;
2158 }
2159
Christopher Faulet145aa472018-12-06 10:56:20 +01002160 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002161 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002162 /* error or output closed, nothing to send, clear the buffer to release it */
2163 b_reset(&h1c->obuf);
2164 }
2165
Christopher Faulet51dbc942018-09-13 09:05:15 +02002166 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002167 if (!(h1c->flags & H1C_F_OUT_FULL))
2168 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002169
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170 /* We're done, no more to send */
2171 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002172 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002173 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002174 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002175 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002176 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002177 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002179 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2180 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002181 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002182 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183
Christopher Faulet6b81df72019-10-01 22:08:43 +02002184 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185 return sent;
2186}
2187
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188
2189/* callback called on any event by the connection handler.
2190 * It applies changes and returns zero, or < 0 if it wants immediate
2191 * destruction of the connection.
2192 */
2193static int h1_process(struct h1c * h1c)
2194{
2195 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002196 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002197
Christopher Faulet6b81df72019-10-01 22:08:43 +02002198 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2199
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002200 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002201 return -1;
2202
Christopher Fauletfeb11742018-11-29 15:12:34 +01002203 if (!h1s) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002204 if ((h1c->flags & H1C_F_CS_ERROR) ||
2205 ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002206 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002207 goto release;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002208 if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002209 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002210 if (!h1c_frt_stream_new(h1c))
Christopher Faulet539e0292018-11-19 10:40:09 +01002211 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002212 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002213 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002214 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002215 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216 }
2217
Christopher Fauletae635762020-09-21 11:47:16 +02002218 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2219 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2220 h1_wake_stream_for_recv(h1s);
2221 }
2222
Christopher Faulet4e741552020-09-30 13:47:09 +02002223 if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1)
2224 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002225
Christopher Faulet6b81df72019-10-01 22:08:43 +02002226 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002227 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002228 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2229 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002230
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002231 if (!h1s_data_pending(h1s) && h1s && (h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002232 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002233 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002234 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002235 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002236 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002237 h1s->cs->data_cb->wake(h1s->cs);
2238 }
Christopher Faulet47365272018-10-31 17:40:50 +01002239 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002240 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002241 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002242 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002243
2244 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002245 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002246 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002247 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002248}
2249
2250static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2251{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002252 struct connection *conn;
2253 struct tasklet *tl = (struct tasklet *)t;
2254 int conn_in_list;
2255 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002256 int ret = 0;
2257
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002258
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002259 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002260 if (tl->context == NULL) {
2261 /* The connection has been taken over by another thread,
2262 * we're no longer responsible for it, so just free the
2263 * tasklet, and do nothing.
2264 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002265 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002266 tasklet_free(tl);
2267 return NULL;
2268 }
2269 h1c = ctx;
2270 conn = h1c->conn;
2271
2272 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2273
2274 /* Remove the connection from the list, to be sure nobody attempts
2275 * to use it while we handle the I/O events
2276 */
2277 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2278 if (conn_in_list)
2279 MT_LIST_DEL(&conn->list);
2280
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002281 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002282
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002283 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002284 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002285 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002287 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002288 ret = h1_process(h1c);
2289 /* If we were in an idle list, we want to add it back into it,
2290 * unless h1_process() returned -1, which mean it has destroyed
2291 * the connection (testing !ret is enough, if h1_process() wasn't
2292 * called then ret will be 0 anyway.
2293 */
2294 if (!ret && conn_in_list) {
2295 struct server *srv = objt_server(conn->target);
2296
2297 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002298 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002299 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002300 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002301 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002302 return NULL;
2303}
2304
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002305static void h1_reset(struct connection *conn)
2306{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002307
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002308}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002309
2310static int h1_wake(struct connection *conn)
2311{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002312 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002313 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002314
Christopher Faulet6b81df72019-10-01 22:08:43 +02002315 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2316
Christopher Faulet539e0292018-11-19 10:40:09 +01002317 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002318 ret = h1_process(h1c);
2319 if (ret == 0) {
2320 struct h1s *h1s = h1c->h1s;
2321
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002322 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002323 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002324 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002325 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002326 }
2327 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002328}
2329
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002330/* Connection timeout management. The principle is that if there's no receipt
2331 * nor sending for a certain amount of time, the connection is closed.
2332 */
2333static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2334{
2335 struct h1c *h1c = context;
2336 int expired = tick_is_expired(t->expire, now_ms);
2337
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002338 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002339
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002340 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002341 /* Make sure nobody stole the connection from us */
2342 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2343
2344 /* Somebody already stole the connection from us, so we should not
2345 * free it, we just have to free the task.
2346 */
2347 if (!t->context) {
2348 h1c = NULL;
2349 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2350 goto do_leave;
2351 }
2352
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002353 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002354 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2355 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002356 return t;
2357 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002358
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002359 /* If a conn-stream is still attached to the mux, wait for the
2360 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002361 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002362 if (h1c->flags & H1C_F_CS_ATTACHED) {
2363 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2364 t->expire = TICK_ETERNITY;
2365 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2366 return t;
2367 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002368
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002369 /* We're about to destroy the connection, so make sure nobody attempts
2370 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002371 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002372 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002373 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002374
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002375 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002376 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002377
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002378 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002379 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002380
2381 if (!h1c) {
2382 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002383 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002384 return NULL;
2385 }
2386
2387 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002388 h1_release(h1c);
2389 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002390 return NULL;
2391}
2392
Christopher Faulet51dbc942018-09-13 09:05:15 +02002393/*******************************************/
2394/* functions below are used by the streams */
2395/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002396
Christopher Faulet51dbc942018-09-13 09:05:15 +02002397/*
2398 * Attach a new stream to a connection
2399 * (Used for outgoing connections)
2400 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002401static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002402{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002403 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002404 struct conn_stream *cs = NULL;
2405 struct h1s *h1s;
2406
Christopher Faulet6b81df72019-10-01 22:08:43 +02002407 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2408 if (h1c->flags & H1C_F_CS_ERROR) {
2409 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 +02002410 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002411 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002412
Christopher Faulet236c93b2020-07-02 09:19:54 +02002413 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002414 if (!cs) {
2415 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 +02002416 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002417 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002418
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002419 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002420 if (h1s == NULL) {
2421 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 +02002422 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002423 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002424
Christopher Faulet6b81df72019-10-01 22:08:43 +02002425 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002426 return cs;
2427 end:
2428 cs_free(cs);
2429 return NULL;
2430}
2431
2432/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2433 * this mux, it's easy as we can only store a single conn_stream.
2434 */
2435static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2436{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002437 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002438 struct h1s *h1s = h1c->h1s;
2439
2440 if (h1s)
2441 return h1s->cs;
2442
2443 return NULL;
2444}
2445
Christopher Faulet73c12072019-04-08 11:23:22 +02002446static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447{
Christopher Faulet73c12072019-04-08 11:23:22 +02002448 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002449
Christopher Faulet6b81df72019-10-01 22:08:43 +02002450 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002451 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002452 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453}
2454
2455/*
2456 * Detach the stream from the connection and possibly release the connection.
2457 */
2458static void h1_detach(struct conn_stream *cs)
2459{
2460 struct h1s *h1s = cs->ctx;
2461 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002462 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002463 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002464
Christopher Faulet6b81df72019-10-01 22:08:43 +02002465 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2466
Christopher Faulet51dbc942018-09-13 09:05:15 +02002467 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002468 if (!h1s) {
2469 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002470 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002471 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002472
Olivier Houchardf502aca2018-12-14 19:42:40 +01002473 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002474 h1c = h1s->h1c;
2475 h1s->cs = NULL;
2476
Christopher Faulet42849b02020-10-05 11:35:17 +02002477 sess->accept_date = date;
2478 sess->tv_accept = now;
2479 sess->t_handshake = 0;
2480 sess->t_idle = -1;
2481
Olivier Houchard8a786902018-12-15 16:05:40 +01002482 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2483 h1s_destroy(h1s);
2484
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002485 if ((h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002486 /* If there are any excess server data in the input buffer,
2487 * release it and close the connection ASAP (some data may
2488 * remain in the output buffer). This happens if a server sends
2489 * invalid responses. So in such case, we don't want to reuse
2490 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002491 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002492 if (b_data(&h1c->ibuf)) {
2493 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002494 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002495 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002496 goto release;
2497 }
Christopher Faulet03627242019-07-19 11:34:08 +02002498
Christopher Faulet08016ab2020-07-01 16:10:06 +02002499 if (h1c->conn->flags & CO_FL_PRIVATE) {
2500 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002501 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2502 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002503 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002505 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002506 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002507 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002508 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002509 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2510 goto end;
2511 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002512 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002513 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002514 if (h1c->conn->owner == sess)
2515 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002516 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002517 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002518 /* The server doesn't want it, let's kill the connection right away */
2519 h1c->conn->mux->destroy(h1c);
2520 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2521 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002522 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002523 /* At this point, the connection has been added to the
2524 * server idle list, so another thread may already have
2525 * hijacked it, so we can't do anything with it.
2526 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002527 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002528 }
2529 }
2530
Christopher Fauletf1204b82019-07-19 14:51:06 +02002531 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002532 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002533 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_UPG_H2C)) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002534 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002535 ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002536 !h1c->conn->owner) {
2537 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002538 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002539 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002540 else {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002541 if (h1c->flags & H1C_F_CS_IDLE) {
2542 /* If we have a new request, process it immediately or
2543 * subscribe for reads waiting for new data
2544 */
2545 if (unlikely(b_data(&h1c->ibuf)))
2546 h1_process(h1c);
2547 else
2548 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2549 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002550 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002551 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002552 end:
2553 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002554}
2555
2556
2557static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2558{
2559 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002560 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002561
2562 if (!h1s)
2563 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002564 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002565
Christopher Faulet6b81df72019-10-01 22:08:43 +02002566 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2567
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002568 if (cs->flags & CS_FL_SHR)
2569 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002570 if (cs->flags & CS_FL_KILL_CONN) {
2571 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2572 goto do_shutr;
2573 }
2574 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2575 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002576 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002578
Christopher Faulet6b81df72019-10-01 22:08:43 +02002579 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2580 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2581 goto end;
2582 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583
Christopher Faulet7f366362019-04-08 10:51:20 +02002584 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002585 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2586 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002587 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002588 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002589 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002590 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002591 end:
2592 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002593}
2594
2595static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2596{
2597 struct h1s *h1s = cs->ctx;
2598 struct h1c *h1c;
2599
2600 if (!h1s)
2601 return;
2602 h1c = h1s->h1c;
2603
Christopher Faulet6b81df72019-10-01 22:08:43 +02002604 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2605
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002606 if (cs->flags & CS_FL_SHW)
2607 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002608 if (cs->flags & CS_FL_KILL_CONN) {
2609 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002610 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002611 }
2612 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2613 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2614 goto do_shutw;
2615 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002616
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002617 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002618 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2619 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2620 goto end;
2621 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002622
Christopher Faulet7f366362019-04-08 10:51:20 +02002623 do_shutw:
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002624 h1c->flags |= H1C_F_CS_SHUTDOWN;
2625 if (!b_data(&h1c->obuf))
2626 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002627 end:
2628 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002629}
2630
Christopher Faulet666a0c42019-01-08 11:12:04 +01002631static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002632{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002633 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002634
Christopher Faulet6b81df72019-10-01 22:08:43 +02002635 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002636 conn_xprt_shutw(conn);
2637 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002638 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002639}
2640
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002641/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2642 * The <es> pointer is not allowed to differ from the one passed to the
2643 * subscribe() call. It always returns zero.
2644 */
2645static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002646{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002647 struct h1s *h1s = cs->ctx;
2648
2649 if (!h1s)
2650 return 0;
2651
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002652 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002653 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002654
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002655 es->events &= ~event_type;
2656 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002657 h1s->subs = NULL;
2658
2659 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002660 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002661
2662 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002663 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002664
Christopher Faulet51dbc942018-09-13 09:05:15 +02002665 return 0;
2666}
2667
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002668/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2669 * event subscriber <es> is not allowed to change from a previous call as long
2670 * as at least one event is still subscribed. The <event_type> must only be a
2671 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2672 * the conn_stream <cs> was already detached, in which case it will return -1.
2673 */
2674static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002675{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002676 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002677 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002678
2679 if (!h1s)
2680 return -1;
2681
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002682 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002683 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002684
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002685 es->events |= event_type;
2686 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002687
2688 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002689 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002690
2691
Christopher Faulet6b81df72019-10-01 22:08:43 +02002692 if (event_type & SUB_RETRY_SEND) {
2693 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002694 /*
2695 * If the conn_stream attempt to subscribe, and the
2696 * mux isn't subscribed to the connection, then it
2697 * probably means the connection wasn't established
2698 * yet, so we have to subscribe.
2699 */
2700 h1c = h1s->h1c;
2701 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2702 h1c->conn->xprt->subscribe(h1c->conn,
2703 h1c->conn->xprt_ctx,
2704 SUB_RETRY_SEND,
2705 &h1c->wait_event);
2706 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002707 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002708}
2709
2710/* Called from the upper layer, to receive data */
2711static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2712{
2713 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002714 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002715 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002716 size_t ret = 0;
2717
Willy Tarreau022e5e52020-09-10 09:33:15 +02002718 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002719 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002720 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002721 else
2722 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002723
Christopher Faulete18777b2019-04-16 16:46:36 +02002724 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002725 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02002726 h1c->flags |= H1C_F_WANT_SPLICE;
2727 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 +02002728 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002729 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002730 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002731 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002732 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002733 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002734 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002735 return ret;
2736}
2737
2738
2739/* Called from the upper layer, to send data */
2740static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2741{
2742 struct h1s *h1s = cs->ctx;
2743 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002744 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002745
2746 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002747 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002748 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002749
Willy Tarreau022e5e52020-09-10 09:33:15 +02002750 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002751
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002752 /* If we're not connected yet, or we're waiting for a handshake, stop
2753 * now, as we don't want to remove everything from the channel buffer
2754 * before we're sure we can send it.
2755 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002756 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002757 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002758 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002759 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002760
Christopher Faulet46230362019-10-17 16:04:20 +02002761 /* Inherit some flags from the upper layer */
2762 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2763 if (flags & CO_SFL_MSG_MORE)
2764 h1c->flags |= H1C_F_CO_MSG_MORE;
2765 if (flags & CO_SFL_STREAMER)
2766 h1c->flags |= H1C_F_CO_STREAMER;
2767
Christopher Fauletc31872f2019-06-04 22:09:36 +02002768 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002769 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002770
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002771 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2772 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002773 else
2774 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002775
2776 if ((count - ret) > 0)
2777 h1c->flags |= H1C_F_CO_MSG_MORE;
2778
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002779 if (!ret)
2780 break;
2781 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002782 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002783 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002784 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002785 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002786 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002787 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002788 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002789}
2790
Willy Tarreaue5733232019-05-22 19:24:06 +02002791#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002792/* Send and get, using splicing */
2793static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2794{
2795 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002796 struct h1c *h1c = h1s->h1c;
2797 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002798 int ret = 0;
2799
Willy Tarreau022e5e52020-09-10 09:33:15 +02002800 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002801
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002802 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002803 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002804 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 +02002805 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002806 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002807 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002808 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002809 goto end;
2810 }
2811
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002812 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
2813 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002814 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2815 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002816 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002817 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002818 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002819 }
2820
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002821 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02002822 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2823 goto end;
2824 }
2825
Christopher Faulet1be55f92018-10-02 15:59:23 +02002826 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2827 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002828 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002829 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002830 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002832 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002833 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002835 }
2836
Christopher Faulet1be55f92018-10-02 15:59:23 +02002837 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002838 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002839 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002840 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002841 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002842 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002843
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002844 if ((h1s->flags & H1S_F_REOS) ||
2845 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002846 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002847 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002848 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002849 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002850
Willy Tarreau022e5e52020-09-10 09:33:15 +02002851 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002852 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002853}
2854
2855static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2856{
2857 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002858 int ret = 0;
2859
Willy Tarreau022e5e52020-09-10 09:33:15 +02002860 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002861
Christopher Faulet1be55f92018-10-02 15:59:23 +02002862 if (b_data(&h1s->h1c->obuf))
2863 goto end;
2864
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002865 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002866 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002867 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002868 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2869 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002870 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002871 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002872 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002873
Willy Tarreau022e5e52020-09-10 09:33:15 +02002874 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002875 return ret;
2876}
2877#endif
2878
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002879static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2880{
2881 int ret = 0;
2882 switch (mux_ctl) {
2883 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002884 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002885 ret |= MUX_STATUS_READY;
2886 return ret;
2887 default:
2888 return -1;
2889 }
2890}
2891
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002892/* for debugging with CLI's "show fd" command */
2893static void h1_show_fd(struct buffer *msg, struct connection *conn)
2894{
2895 struct h1c *h1c = conn->ctx;
2896 struct h1s *h1s = h1c->h1s;
2897
Christopher Fauletf376a312019-01-04 15:16:06 +01002898 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2899 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002900 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2901 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2902 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2903 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2904
2905 if (h1s) {
2906 char *method;
2907
2908 if (h1s->meth < HTTP_METH_OTHER)
2909 method = http_known_methods[h1s->meth].ptr;
2910 else
2911 method = "UNKNOWN";
2912 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2913 " .meth=%s status=%d",
2914 h1s, h1s->flags,
2915 h1m_state_str(h1s->req.state),
2916 h1m_state_str(h1s->res.state), method, h1s->status);
2917 if (h1s->cs)
2918 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2919 h1s->cs->flags, h1s->cs->data);
2920 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002921}
2922
2923
2924/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2925static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2926{
2927 struct h1_hdr_entry *entry;
2928
2929 /* Be sure there is a non-empty <to> */
2930 if (!strlen(to)) {
2931 memprintf(err, "expect <to>");
2932 return -1;
2933 }
2934
2935 /* Be sure only the case differs between <from> and <to> */
2936 if (strcasecmp(from, to)) {
2937 memprintf(err, "<from> and <to> must not differ execpt the case");
2938 return -1;
2939 }
2940
2941 /* Be sure <from> does not already existsin the tree */
2942 if (ebis_lookup(&hdrs_map.map, from)) {
2943 memprintf(err, "duplicate entry '%s'", from);
2944 return -1;
2945 }
2946
2947 /* Create the entry and insert it in the tree */
2948 entry = malloc(sizeof(*entry));
2949 if (!entry) {
2950 memprintf(err, "out of memory");
2951 return -1;
2952 }
2953
2954 entry->node.key = strdup(from);
2955 entry->name.ptr = strdup(to);
2956 entry->name.len = strlen(to);
2957 if (!entry->node.key || !entry->name.ptr) {
2958 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002959 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002960 free(entry);
2961 memprintf(err, "out of memory");
2962 return -1;
2963 }
2964 ebis_insert(&hdrs_map.map, &entry->node);
2965 return 0;
2966}
2967
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002968/* Migrate the the connection to the current thread.
2969 * Return 0 if successful, non-zero otherwise.
2970 * Expected to be called with the old thread lock held.
2971 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002972static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002973{
2974 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002975 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002976
2977 if (fd_takeover(conn->handle.fd, conn) != 0)
2978 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02002979
2980 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
2981 /* We failed to takeover the xprt, even if the connection may
2982 * still be valid, flag it as error'd, as we have already
2983 * taken over the fd, and wake the tasklet, so that it will
2984 * destroy it.
2985 */
2986 conn->flags |= CO_FL_ERROR;
2987 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
2988 return -1;
2989 }
2990
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002991 if (h1c->wait_event.events)
2992 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
2993 h1c->wait_event.events, &h1c->wait_event);
2994 /* To let the tasklet know it should free itself, and do nothing else,
2995 * set its context to NULL.
2996 */
2997 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002998 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002999
3000 task = h1c->task;
3001 if (task) {
3002 task->context = NULL;
3003 h1c->task = NULL;
3004 __ha_barrier_store();
3005 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003006
3007 h1c->task = task_new(tid_bit);
3008 if (!h1c->task) {
3009 h1_release(h1c);
3010 return -1;
3011 }
3012 h1c->task->process = h1_timeout_task;
3013 h1c->task->context = h1c;
3014 }
3015 h1c->wait_event.tasklet = tasklet_new();
3016 if (!h1c->wait_event.tasklet) {
3017 h1_release(h1c);
3018 return -1;
3019 }
3020 h1c->wait_event.tasklet->process = h1_io_cb;
3021 h1c->wait_event.tasklet->context = h1c;
3022 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3023 SUB_RETRY_RECV, &h1c->wait_event);
3024
3025 return 0;
3026}
3027
3028
Christopher Faulet98fbe952019-07-22 16:18:24 +02003029static void h1_hdeaders_case_adjust_deinit()
3030{
3031 struct ebpt_node *node, *next;
3032 struct h1_hdr_entry *entry;
3033
3034 node = ebpt_first(&hdrs_map.map);
3035 while (node) {
3036 next = ebpt_next(node);
3037 ebpt_delete(node);
3038 entry = container_of(node, struct h1_hdr_entry, node);
3039 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003040 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003041 free(entry);
3042 node = next;
3043 }
3044 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003045}
3046
Christopher Faulet98fbe952019-07-22 16:18:24 +02003047static int cfg_h1_headers_case_adjust_postparser()
3048{
3049 FILE *file = NULL;
3050 char *c, *key_beg, *key_end, *value_beg, *value_end;
3051 char *err;
3052 int rc, line = 0, err_code = 0;
3053
3054 if (!hdrs_map.name)
3055 goto end;
3056
3057 file = fopen(hdrs_map.name, "r");
3058 if (!file) {
3059 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3060 hdrs_map.name);
3061 err_code |= ERR_ALERT | ERR_FATAL;
3062 goto end;
3063 }
3064
3065 /* now parse all lines. The file may contain only two header name per
3066 * line, separated by spaces. All heading and trailing spaces will be
3067 * ignored. Lines starting with a # are ignored.
3068 */
3069 while (fgets(trash.area, trash.size, file) != NULL) {
3070 line++;
3071 c = trash.area;
3072
3073 /* strip leading spaces and tabs */
3074 while (*c == ' ' || *c == '\t')
3075 c++;
3076
3077 /* ignore emptu lines, or lines beginning with a dash */
3078 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3079 continue;
3080
3081 /* look for the end of the key */
3082 key_beg = c;
3083 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3084 c++;
3085 key_end = c;
3086
3087 /* strip middle spaces and tabs */
3088 while (*c == ' ' || *c == '\t')
3089 c++;
3090
3091 /* look for the end of the value, it is the end of the line */
3092 value_beg = c;
3093 while (*c && *c != '\n' && *c != '\r')
3094 c++;
3095 value_end = c;
3096
3097 /* trim possibly trailing spaces and tabs */
3098 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3099 value_end--;
3100
3101 /* set final \0 and check entries */
3102 *key_end = '\0';
3103 *value_end = '\0';
3104
3105 err = NULL;
3106 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3107 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003108 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3109 hdrs_map.name, err, line);
3110 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003111 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003112 goto end;
3113 }
3114 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003115 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3116 hdrs_map.name, err, line);
3117 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003118 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003119 }
3120 }
3121
3122 end:
3123 if (file)
3124 fclose(file);
3125 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3126 return err_code;
3127}
3128
3129
3130/* config parser for global "h1-outgoing-header-case-adjust" */
3131static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3132 struct proxy *defpx, const char *file, int line,
3133 char **err)
3134{
3135 if (too_many_args(2, args, err, NULL))
3136 return -1;
3137 if (!*(args[1]) || !*(args[2])) {
3138 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3139 return -1;
3140 }
3141 return add_hdr_case_adjust(args[1], args[2], err);
3142}
3143
3144/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3145static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3146 struct proxy *defpx, const char *file, int line,
3147 char **err)
3148{
3149 if (too_many_args(1, args, err, NULL))
3150 return -1;
3151 if (!*(args[1])) {
3152 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3153 return -1;
3154 }
3155 free(hdrs_map.name);
3156 hdrs_map.name = strdup(args[1]);
3157 return 0;
3158}
3159
3160
3161/* config keyword parsers */
3162static struct cfg_kw_list cfg_kws = {{ }, {
3163 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3164 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3165 { 0, NULL, NULL },
3166 }
3167};
3168
3169INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3170REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3171
3172
Christopher Faulet51dbc942018-09-13 09:05:15 +02003173/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003174/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003175/****************************************/
3176
3177/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003178static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003179 .init = h1_init,
3180 .wake = h1_wake,
3181 .attach = h1_attach,
3182 .get_first_cs = h1_get_first_cs,
3183 .detach = h1_detach,
3184 .destroy = h1_destroy,
3185 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003186 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003187 .rcv_buf = h1_rcv_buf,
3188 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003189#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003190 .rcv_pipe = h1_rcv_pipe,
3191 .snd_pipe = h1_snd_pipe,
3192#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003193 .subscribe = h1_subscribe,
3194 .unsubscribe = h1_unsubscribe,
3195 .shutr = h1_shutr,
3196 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003197 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003198 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003199 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003200 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003201 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003202 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003203};
3204
3205
3206/* this mux registers default HTX proto */
3207static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003208{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003209
Willy Tarreau0108d902018-11-25 19:14:37 +01003210INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3211
Christopher Faulet51dbc942018-09-13 09:05:15 +02003212/*
3213 * Local variables:
3214 * c-indent-level: 8
3215 * c-basic-offset: 8
3216 * End:
3217 */