blob: 1be50f509086f2b4b8ebe02bed5423d455c87be6 [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 Faulet51dbc942018-09-13 09:05:15 +020048#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
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 Faulet666a0c42019-01-08 11:12:04 +0100260static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100261static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
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
359 must not attempt to receive
360 * - if the input buffer failed to be allocated or is full , we must not try
361 * to receive
362 * - if he input processing is busy waiting for the output side, we may
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500363 * attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200364 * - otherwise must may not attempt to receive
365 */
366static inline int h1_recv_allowed(const struct h1c *h1c)
367{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100368 if (h1c->flags & H1C_F_CS_ERROR) {
369 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 +0200370 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200371 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200372
Willy Tarreau2febb842020-07-31 09:15:43 +0200373 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
374 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 +0100375 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200376 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100377
Christopher Faulet089acd52020-09-21 10:57:52 +0200378 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
379 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200380 return 0;
381 }
382
Christopher Fauletd17ad822020-09-24 15:14:29 +0200383 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200384 return 1;
385
Christopher Faulet6b81df72019-10-01 22:08:43 +0200386 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 +0200387 return 0;
388}
389
390/*
391 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
392 * flags are used to figure what buffer was requested. It returns 1 if the
393 * allocation succeeds, in which case the connection is woken up, or 0 if it's
394 * impossible to wake up and we prefer to be woken up later.
395 */
396static int h1_buf_available(void *target)
397{
398 struct h1c *h1c = target;
399
400 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200401 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 +0200402 h1c->flags &= ~H1C_F_IN_ALLOC;
403 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200404 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200405 return 1;
406 }
407
408 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200409 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 +0200410 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200411 if (h1c->h1s)
412 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200413 return 1;
414 }
415
Christopher Fauletd17ad822020-09-24 15:14:29 +0200416 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
417 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
418 h1c->flags &= ~H1C_F_IN_SALLOC;
419 tasklet_wakeup(h1c->wait_event.tasklet);
420 return 1;
421 }
422
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 return 0;
424}
425
426/*
427 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
428 */
429static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
430{
431 struct buffer *buf = NULL;
432
Willy Tarreau21046592020-02-26 10:39:36 +0100433 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
435 h1c->buf_wait.target = h1c;
436 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200437 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200438 }
439 return buf;
440}
441
442/*
443 * Release a buffer, if any, and try to wake up entities waiting in the buffer
444 * wait queue.
445 */
446static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
447{
448 if (bptr->size) {
449 b_free(bptr);
450 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
451 }
452}
453
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100454/* returns the number of streams in use on a connection to figure if it's idle
455 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
456 * not. This flag is only set when no H1S is attached and when the previous
457 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100458 */
459static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100461 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200462
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100463 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200464}
465
Willy Tarreau00f18a32019-01-26 12:19:01 +0100466/* returns the number of streams still available on a connection */
467static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100468{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100469 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100470}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471
Christopher Faulet7a145d62020-08-05 11:31:16 +0200472/* Refresh the h1c task timeout if necessary */
473static void h1_refresh_timeout(struct h1c *h1c)
474{
475 if (h1c->task) {
476 h1c->task->expire = TICK_ETERNITY;
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200477 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
478 /* half-closed connections switch to clientfin/serverfin
479 * timeouts so that we don't hang too long on clients
480 * that have gone away (especially in tunnel mode).
481 */
482 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
483 task_queue(h1c->task);
484 TRACE_DEVEL("refreshing connection's timeout (half-closed)", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet268c92e2020-12-01 11:42:53 +0100485 } else if (b_data(&h1c->obuf)) {
486 /* any connection with pending data, need a timeout (server or client).
Christopher Faulet7a145d62020-08-05 11:31:16 +0200487 */
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200488 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet7a145d62020-08-05 11:31:16 +0200489 ? h1c->shut_timeout
490 : h1c->timeout));
491 task_queue(h1c->task);
492 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200493 } else if ((h1c->flags & (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) && !(h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet268c92e2020-12-01 11:42:53 +0100494 /* front connections waiting for a stream need a timeout. client timeout by
495 * default but http-keep-alive if defined
496 */
497 int timeout = h1c->timeout;
498
499 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
500 timeout = tick_first(timeout, h1c->px->timeout.httpka);
501
502 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
503 ? h1c->shut_timeout
504 : timeout));
505 task_queue(h1c->task);
506 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200507 }
508 }
509}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200510/*****************************************************************/
511/* functions below are dedicated to the mux setup and management */
512/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200513
514/* returns non-zero if there are input data pending for stream h1s. */
515static inline size_t h1s_data_pending(const struct h1s *h1s)
516{
517 const struct h1m *h1m;
518
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200519 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200520 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100521 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200522
523 return b_data(&h1s->h1c->ibuf);
524}
525
Christopher Faulet26256f82020-09-14 11:40:13 +0200526static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100527{
528 struct conn_stream *cs;
529
Christopher Faulet6b81df72019-10-01 22:08:43 +0200530 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200531 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200532 if (!cs) {
533 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 +0100534 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200535 }
Christopher Faulet47365272018-10-31 17:40:50 +0100536 h1s->cs = cs;
537 cs->ctx = h1s;
538
539 if (h1s->flags & H1S_F_NOT_FIRST)
540 cs->flags |= CS_FL_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200541 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet47365272018-10-31 17:40:50 +0100542
Christopher Faulet27182292020-07-03 15:08:49 +0200543 if (global.tune.options & GTUNE_USE_SPLICE) {
544 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100545 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200546 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100547
Christopher Faulet26256f82020-09-14 11:40:13 +0200548 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200549 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 +0100550 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200551 }
Christopher Faulet26256f82020-09-14 11:40:13 +0200552 *input = BUF_NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200553
554 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100555 return cs;
556
557 err:
558 cs_free(cs);
559 h1s->cs = NULL;
560 return NULL;
561}
562
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200563static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200564{
565 struct h1s *h1s;
566
Christopher Faulet6b81df72019-10-01 22:08:43 +0200567 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
568
Christopher Faulet51dbc942018-09-13 09:05:15 +0200569 h1s = pool_alloc(pool_head_h1s);
570 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100571 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200572 h1s->h1c = h1c;
573 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200574 h1s->sess = NULL;
575 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100576 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100577 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200578 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200579
580 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100581 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200582
Christopher Faulet129817b2018-09-20 16:14:40 +0200583 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100584 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200585
586 h1s->status = 0;
587 h1s->meth = HTTP_METH_OTHER;
588
Christopher Faulet47365272018-10-31 17:40:50 +0100589 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
590 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200591 h1c->flags = (h1c->flags & ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_CS_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100592
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200593 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
594 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200595
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200596 fail:
597 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
598 return NULL;
599}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100600
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200601static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
602{
603 struct session *sess = h1c->conn->owner;
604 struct h1s *h1s;
605
606 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
607
608 h1s = h1s_new(h1c);
609 if (!h1s)
610 goto fail;
611
612 h1s->sess = sess;
613
614 if (h1c->px->options2 & PR_O2_REQBUG_OK)
615 h1s->req.err_pos = -1;
616
617 if (!h1s_new_cs(h1s, &BUF_NULL))
618 goto fail_cs;
619
Christopher Faulet6b81df72019-10-01 22:08:43 +0200620 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200621 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100622
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200623 fail_cs:
Christopher Faulet47365272018-10-31 17:40:50 +0100624 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200625 fail:
626 sess_log(sess);
627 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
628 return NULL;
629}
630
631static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
632{
633 struct h1s *h1s;
634
635 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
636
637 h1s = h1s_new(h1c);
638 if (!h1s)
639 goto fail;
640
641 h1s->cs = cs;
642 h1s->sess = sess;
643 cs->ctx = h1s;
644
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200645 h1c->flags = (h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200646
647 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
648 h1s->res.err_pos = -1;
649
650 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
651 return h1s;
652
653 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200654 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 +0100655 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200656}
657
658static void h1s_destroy(struct h1s *h1s)
659{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200660 if (h1s) {
661 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200662
Christopher Faulet6b81df72019-10-01 22:08:43 +0200663 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200664 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200665
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100666 if (h1s->subs)
667 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200668
Christopher Fauletd17ad822020-09-24 15:14:29 +0200669 h1_release_buf(h1c, &h1s->rxbuf);
670
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200671 h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200672 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet47365272018-10-31 17:40:50 +0100673 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200674 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
675 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100676
677 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
678 !(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 +0100679 (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 +0100680 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
681 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
682 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
683 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200684 pool_free(pool_head_h1s, h1s);
685 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200686}
687
688/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200689 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500690 * to the existing conn_stream (for outgoing connections or for incoming ones
691 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200692 * establishment). <input> is always used as Input buffer and may contain
693 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
694 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200695 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200696static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
697 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200698{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100700 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200701 void *conn_ctx = conn->ctx;
702
703 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200704
705 h1c = pool_alloc(pool_head_h1c);
706 if (!h1c)
707 goto fail_h1c;
708 h1c->conn = conn;
709 h1c->px = proxy;
710
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100711 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200712 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200713 h1c->obuf = BUF_NULL;
714 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200715 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200716
Willy Tarreau21046592020-02-26 10:39:36 +0100717 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200718 h1c->wait_event.tasklet = tasklet_new();
719 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200720 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200721 h1c->wait_event.tasklet->process = h1_io_cb;
722 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100723 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200724
Christopher Faulete9b70722019-04-08 10:46:02 +0200725 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200726 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100727 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
728 if (tick_isset(proxy->timeout.serverfin))
729 h1c->shut_timeout = proxy->timeout.serverfin;
730 } else {
731 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
732 if (tick_isset(proxy->timeout.clientfin))
733 h1c->shut_timeout = proxy->timeout.clientfin;
734 }
735 if (tick_isset(h1c->timeout)) {
736 t = task_new(tid_bit);
737 if (!t)
738 goto fail;
739
740 h1c->task = t;
741 t->process = h1_timeout_task;
742 t->context = h1c;
743 t->expire = tick_add(now_ms, h1c->timeout);
744 }
745
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100746 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200747
Christopher Faulet6b81df72019-10-01 22:08:43 +0200748 /* Always Create a new H1S */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200749 if (!(h1c->flags & H1C_F_IS_BACK)) {
750 if (!h1c_frt_stream_new(h1c))
751 goto fail;
752 }
753 else {
754 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
755 goto fail;
756 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100757
758 if (t)
759 task_queue(t);
760
Christopher Faulet51dbc942018-09-13 09:05:15 +0200761 /* Try to read, if nothing is available yet we'll just subscribe */
Christopher Faulet089acd52020-09-21 10:57:52 +0200762 if (!h1_recv_allowed(h1c))
763 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200764
765 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200766 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200767 return 0;
768
769 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200770 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200771 if (h1c->wait_event.tasklet)
772 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200773 pool_free(pool_head_h1c, h1c);
774 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200775 conn->ctx = conn_ctx; // restore saved context
776 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777 return -1;
778}
779
Christopher Faulet73c12072019-04-08 11:23:22 +0200780/* release function. This one should be called to free all resources allocated
781 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200782 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200783static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200784{
Christopher Faulet61840e72019-04-15 09:33:32 +0200785 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200786
Christopher Faulet6b81df72019-10-01 22:08:43 +0200787 TRACE_POINT(H1_EV_H1C_END);
788
Christopher Faulet51dbc942018-09-13 09:05:15 +0200789 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200790 /* The connection must be aattached to this mux to be released */
791 if (h1c->conn && h1c->conn->ctx == h1c)
792 conn = h1c->conn;
793
Christopher Faulet6b81df72019-10-01 22:08:43 +0200794 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
795
Christopher Faulet61840e72019-04-15 09:33:32 +0200796 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200797 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200798 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200799 /* Make sure we're no longer subscribed to anything */
800 if (h1c->wait_event.events)
801 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
802 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200803 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200804 /* connection successfully upgraded to H2, this
805 * mux was already released */
806 return;
807 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200808 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200809 sess_log(conn->owner); /* Log if the upgrade failed */
810 }
811
Olivier Houchard45c44372019-06-11 14:06:23 +0200812
Willy Tarreau21046592020-02-26 10:39:36 +0100813 if (MT_LIST_ADDED(&h1c->buf_wait.list))
814 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200815
816 h1_release_buf(h1c, &h1c->ibuf);
817 h1_release_buf(h1c, &h1c->obuf);
818
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100819 if (h1c->task) {
820 h1c->task->context = NULL;
821 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
822 h1c->task = NULL;
823 }
824
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200825 if (h1c->wait_event.tasklet)
826 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200827
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200829 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100830 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200831 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200832 pool_free(pool_head_h1c, h1c);
833 }
834
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200835 if (conn) {
836 conn->mux = NULL;
837 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200838 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200839
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200840 conn_stop_tracking(conn);
841 conn_full_close(conn);
842 if (conn->destroy_cb)
843 conn->destroy_cb(conn);
844 conn_free(conn);
845 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200846}
847
848/******************************************************/
849/* functions below are for the H1 protocol processing */
850/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200851/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
852 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200853 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100854static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200855{
Christopher Faulet570d1612018-11-26 11:13:57 +0100856 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200857
Christopher Faulet570d1612018-11-26 11:13:57 +0100858 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200859 (*(p + 5) > '1' ||
860 (*(p + 5) == '1' && *(p + 7) >= '1')))
861 h1m->flags |= H1_MF_VER_11;
862}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200863
Christopher Faulet9768c262018-10-22 09:34:31 +0200864/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
865 * greater or equal to 1.1
866 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100867static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200868{
Christopher Faulet570d1612018-11-26 11:13:57 +0100869 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200870
Christopher Faulet570d1612018-11-26 11:13:57 +0100871 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200872 (*(p + 5) > '1' ||
873 (*(p + 5) == '1' && *(p + 7) >= '1')))
874 h1m->flags |= H1_MF_VER_11;
875}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200876
Christopher Fauletf2824e62018-10-01 12:12:37 +0200877/* Deduce the connection mode of the client connection, depending on the
878 * configuration and the H1 message flags. This function is called twice, the
879 * first time when the request is parsed and the second time when the response
880 * is parsed.
881 */
882static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
883{
884 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200885
886 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100887 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200888 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100889 h1s->status == 101) {
890 /* Either we've established an explicit tunnel, or we're
891 * switching the protocol. In both cases, we're very unlikely to
892 * understand the next protocols. We have to switch to tunnel
893 * mode, so that we transfer the request and responses then let
894 * this protocol pass unmodified. When we later implement
895 * specific parsers for such protocols, we'll want to check the
896 * Upgrade header which contains information about that protocol
897 * for responses with status 101 (eg: see RFC2817 about TLS).
898 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200899 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200900 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 +0100901 }
902 else if (h1s->flags & H1S_F_WANT_KAL) {
903 /* By default the client is in KAL mode. CLOSE mode mean
904 * it is imposed by the client itself. So only change
905 * KAL mode here. */
906 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
907 /* no length known or explicit close => close */
908 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200909 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 +0100910 }
911 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
912 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500913 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100914 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200915 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 +0100916 }
917 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200918 }
919 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100920 /* Input direction: first pass */
921 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
922 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200923 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 (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100925 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200926 }
927
928 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200929 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200930 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200931 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);
932 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200933}
934
935/* Deduce the connection mode of the client connection, depending on the
936 * configuration and the H1 message flags. This function is called twice, the
937 * first time when the request is parsed and the second time when the response
938 * is parsed.
939 */
940static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
941{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100942 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100943 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100944 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200945
Christopher Fauletf2824e62018-10-01 12:12:37 +0200946 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100947 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100949 h1s->status == 101) {
950 /* Either we've established an explicit tunnel, or we're
951 * switching the protocol. In both cases, we're very unlikely to
952 * understand the next protocols. We have to switch to tunnel
953 * mode, so that we transfer the request and responses then let
954 * this protocol pass unmodified. When we later implement
955 * specific parsers for such protocols, we'll want to check the
956 * Upgrade header which contains information about that protocol
957 * for responses with status 101 (eg: see RFC2817 about TLS).
958 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200960 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 +0100961 }
962 else if (h1s->flags & H1S_F_WANT_KAL) {
963 /* By default the server is in KAL mode. CLOSE mode mean
964 * it is imposed by haproxy itself. So only change KAL
965 * mode here. */
966 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
967 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
968 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
969 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200970 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 +0100971 }
972 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200973 }
Christopher Faulet70033782018-12-05 13:50:11 +0100974 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100975 /* Output direction: first pass */
976 if (h1m->flags & H1_MF_CONN_CLO) {
977 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100978 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200979 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 +0100980 }
981 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
982 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
983 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
984 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
985 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
986 /* no explicit keep-alive option httpclose/server-close => close */
987 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200988 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 +0100989 }
Christopher Faulet70033782018-12-05 13:50:11 +0100990 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991
992 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200993 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200994 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200995 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);
996 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200997}
998
Christopher Fauletb992af02019-03-28 15:42:24 +0100999static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001000{
1001 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001002
1003 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1004 * token is found
1005 */
1006 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001007 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001008
1009 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 if (!(h1m->flags & H1_MF_VER_11)) {
1011 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 +01001012 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001013 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 }
1015 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001016 if (h1m->flags & H1_MF_VER_11) {
1017 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001018 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001019 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001020 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021}
1022
Christopher Fauletb992af02019-03-28 15:42:24 +01001023static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001024{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001025 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1026 * token is found
1027 */
1028 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001029 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030
1031 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001032 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001033 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1034 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 +01001035 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001036 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037 }
1038 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001039 if (h1m->flags & H1_MF_VER_11) {
1040 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001041 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001042 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001043 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001044}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001045
Christopher Fauletb992af02019-03-28 15:42:24 +01001046static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001047{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001048 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001049 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001050 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001051 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001052}
1053
Christopher Fauletb992af02019-03-28 15:42:24 +01001054static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1055{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001056 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001057 h1_set_cli_conn_mode(h1s, h1m);
1058 else
1059 h1_set_srv_conn_mode(h1s, h1m);
1060
1061 if (!(h1m->flags & H1_MF_RESP))
1062 h1_update_req_conn_value(h1s, h1m, conn_val);
1063 else
1064 h1_update_res_conn_value(h1s, h1m, conn_val);
1065}
Christopher Faulete44769b2018-11-29 23:01:45 +01001066
Christopher Faulet98fbe952019-07-22 16:18:24 +02001067/* Try to adjust the case of the message header name using the global map
1068 * <hdrs_map>.
1069 */
1070static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1071{
1072 struct ebpt_node *node;
1073 struct h1_hdr_entry *entry;
1074
1075 /* No entry in the map, do nothing */
1076 if (eb_is_empty(&hdrs_map.map))
1077 return;
1078
1079 /* No conversion fo the request headers */
1080 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1081 return;
1082
1083 /* No conversion fo the response headers */
1084 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1085 return;
1086
1087 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1088 if (!node)
1089 return;
1090 entry = container_of(node, struct h1_hdr_entry, node);
1091 name->ptr = entry->name.ptr;
1092 name->len = entry->name.len;
1093}
1094
Christopher Faulete44769b2018-11-29 23:01:45 +01001095/* Append the description of what is present in error snapshot <es> into <out>.
1096 * The description must be small enough to always fit in a buffer. The output
1097 * buffer may be the trash so the trash must not be used inside this function.
1098 */
1099static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1100{
1101 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001102 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1103 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1104 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1105 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1106 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1107 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001108}
1109/*
1110 * Capture a bad request or response and archive it in the proxy's structure.
1111 * By default it tries to report the error position as h1m->err_pos. However if
1112 * this one is not set, it will then report h1m->next, which is the last known
1113 * parsing point. The function is able to deal with wrapping buffers. It always
1114 * displays buffers as a contiguous area starting at buf->p. The direction is
1115 * determined thanks to the h1m's flags.
1116 */
1117static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1118 struct h1m *h1m, struct buffer *buf)
1119{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001120 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001121 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001122 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001123 union error_snapshot_ctx ctx;
1124
Christopher Fauletbb8baf42020-09-29 15:18:40 +02001125 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001126 if (sess == NULL)
1127 sess = si_strm(h1s->cs->data)->sess;
1128 if (!(h1m->flags & H1_MF_RESP))
1129 other_end = si_strm(h1s->cs->data)->be;
1130 else
1131 other_end = sess->fe;
1132 } else
1133 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001134
1135 /* http-specific part now */
1136 ctx.h1.state = h1m->state;
1137 ctx.h1.c_flags = h1c->flags;
1138 ctx.h1.s_flags = h1s->flags;
1139 ctx.h1.m_flags = h1m->flags;
1140 ctx.h1.m_clen = h1m->curr_len;
1141 ctx.h1.m_blen = h1m->body_len;
1142
1143 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1144 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001145 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1146 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001147}
1148
Christopher Fauletadb22202018-12-12 10:32:09 +01001149/* Emit the chunksize followed by a CRLF in front of data of the buffer
1150 * <buf>. It goes backwards and starts with the byte before the buffer's
1151 * head. The caller is responsible for ensuring there is enough room left before
1152 * the buffer's head for the string.
1153 */
1154static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1155{
1156 char *beg, *end;
1157
1158 beg = end = b_head(buf);
1159 *--beg = '\n';
1160 *--beg = '\r';
1161 do {
1162 *--beg = hextab[chksz & 0xF];
1163 } while (chksz >>= 4);
1164 buf->head -= (end - beg);
1165 b_add(buf, end - beg);
1166}
1167
1168/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1169 * ensuring there is enough room left in the buffer for the string. */
1170static void h1_emit_chunk_crlf(struct buffer *buf)
1171{
1172 *(b_peek(buf, b_data(buf))) = '\r';
1173 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1174 b_add(buf, 2);
1175}
1176
Christopher Faulet129817b2018-09-20 16:14:40 +02001177/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001178 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001179 * CONNECT requests. On the client side, if the response is not finished, the
1180 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001181 */
1182static void h1_set_req_tunnel_mode(struct h1s *h1s)
1183{
1184 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1185 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001186 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1187
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001188 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001189 h1s->flags &= ~H1S_F_PARSING_DONE;
1190 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001191 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1192 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 +01001193 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001194 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001195 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1196 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001197 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001198 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 +01001199 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001200}
1201
1202/*
1203 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001204 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001205 * last case, this function takes care to switch the request to tunnel mode if
1206 * possible. On the server side, if the request is not finished, the mux is mark
1207 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001208 */
1209static void h1_set_res_tunnel_mode(struct h1s *h1s)
1210{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001211
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001212 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1213 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001214 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1215
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001216 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001217 h1s->flags &= ~H1S_F_PARSING_DONE;
1218 /* On protocol switching, switch the request to tunnel mode if it is in
1219 * DONE state. Otherwise we will wait the end of the request to switch
1220 * it in tunnel mode.
1221 */
1222 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001223 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1224 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 +01001225 }
1226 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1227 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1228 h1s->req.state = H1_MSG_TUNNEL;
1229 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1230 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001231 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001232 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1233 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001234 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001235 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 +01001236 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001237}
1238
1239/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001240 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001241 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001242 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001243 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001244static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001245 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001246{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001247 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001248 int ret = 0;
1249
Willy Tarreau022e5e52020-09-10 09:33:15 +02001250 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 +02001251
Christopher Faulet89aed322020-06-02 17:33:56 +02001252 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1253 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1254 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001255 /* Try to match H2 preface before parsing the request headers. */
1256 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001257 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001258 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001259 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001260 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001261 else {
1262 if (h1s->meth == HTTP_METH_CONNECT)
1263 h1m->flags |= H1_MF_METH_CONNECT;
1264 if (h1s->meth == HTTP_METH_HEAD)
1265 h1m->flags |= H1_MF_METH_HEAD;
1266 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001267
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001268 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1269 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001270 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 +02001271 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001272 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001273 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 +02001274 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1275 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001276 goto end;
1277 }
1278
Christopher Faulet486498c2019-10-11 14:22:00 +02001279 if (h1m->err_pos >= 0) {
1280 /* Maybe we found an error during the parsing while we were
1281 * configured not to block on that, so we have to capture it
1282 * now.
1283 */
1284 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1285 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1286 }
1287
Christopher Faulet129817b2018-09-20 16:14:40 +02001288 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001289 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001290 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001291 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001292 }
1293 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001294 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001295 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001296 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001297 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001298 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001300
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001302 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 +02001303 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001304
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001305 h2c_upgrade:
1306 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Fauleta583af62020-09-24 15:35:37 +02001307 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001308 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001309 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1310 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001311}
1312
1313/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001314 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001315 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1316 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001317 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001318static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001319 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001320 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001321{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001322 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001323
Willy Tarreau022e5e52020-09-10 09:33:15 +02001324 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 +02001325 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001326 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001327 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 +01001328 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001329 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001330 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 +02001331 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001332 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001333 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001334 }
1335
Christopher Faulet02a02532019-11-15 09:36:28 +01001336 *ofs += ret;
1337
1338 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001339 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 +02001340 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001341}
1342
1343/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001344 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1345 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1346 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1347 * responsible to update the parser state <h1m>.
1348 */
1349static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1350 struct buffer *buf, size_t *ofs, size_t max)
1351{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001352 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001353
Willy Tarreau022e5e52020-09-10 09:33:15 +02001354 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 +02001355 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001356 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001357 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 +01001358 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001359 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001360 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 +02001361 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1362 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001363 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001364 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001365
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001366 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001367
1368 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001369 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 +02001370 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001371}
1372
1373/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001374 * 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 +02001375 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001376 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001377static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1378 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001379{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001380 int ret;
1381
Willy Tarreau022e5e52020-09-10 09:33:15 +02001382 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 +01001383 ret = h1_parse_msg_eom(h1m, htx, max);
1384 if (!ret) {
1385 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1386 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001387 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001388 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 +01001389 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1390 }
1391 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001392 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001393
Christopher Faulet76014fd2019-12-10 11:47:22 +01001394 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001395 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001396 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 +01001397 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001398}
1399
1400/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001401 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001402 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1403 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001404 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001405static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001406{
Christopher Faulet539e0292018-11-19 10:40:09 +01001407 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001408 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001410 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001411 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001412
Christopher Faulet539e0292018-11-19 10:40:09 +01001413 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001414 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001415
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001416 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001417
Christopher Faulet74027762019-02-26 14:45:05 +01001418 data = htx->data;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001419 if (h1s->flags & H1S_F_PARSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001420 goto end;
1421
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001422 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001423 size_t used = htx_used_space(htx);
1424
Christopher Faulet129817b2018-09-20 16:14:40 +02001425 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001426 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001427 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001428 if (!ret)
1429 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001430
1431 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1432 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1433
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001434 if ((h1m->flags & H1_MF_RESP) &&
1435 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1436 h1m_init_res(&h1s->res);
1437 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001438 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001439 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001440 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001441 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001442 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001443 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001444 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001445 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001446
1447 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1448 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001449 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001450 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001451 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1452 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1453 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001454 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001455
Christopher Faulet76014fd2019-12-10 11:47:22 +01001456 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1457 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001458 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001459 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001460 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1461 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1462 break;
1463
1464 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1465 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1466 }
1467
Christopher Fauletf3158e92019-11-15 11:14:23 +01001468 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1469 h1_set_req_tunnel_mode(h1s);
1470 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001471 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1472 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 +02001473 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001474 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001475 else
1476 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001477 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001478 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001479 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001480 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001481 if (!ret)
1482 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001483
1484 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1485 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001486 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001487 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001488 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001489 break;
1490 }
1491
Christopher Faulet30db3d72019-05-17 15:35:33 +02001492 count -= htx_used_space(htx) - used;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001493 } while (!(h1s->flags & H1S_F_PARSING_ERROR));
Christopher Faulet129817b2018-09-20 16:14:40 +02001494
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001495 if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001496 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001497 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001498 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001499
Christopher Faulet539e0292018-11-19 10:40:09 +01001500 b_del(&h1c->ibuf, total);
1501
1502 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001503 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001504 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001505 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001506 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001507 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 +01001508 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001509 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001510
Christopher Fauleta583af62020-09-24 15:35:37 +02001511 if (!(h1m->flags & H1_MF_CHNK) &&
1512 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1513 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1514 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1515 }
1516 else {
1517 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1518 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1519 }
1520
1521 if (h1s->flags & H1S_F_PARSING_DONE)
1522 h1s->cs->flags |= CS_FL_EOI;
1523
Christopher Fauletcf56b992018-12-11 16:12:31 +01001524 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1525
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001526 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001527 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001528 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001529 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001530 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001531 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001532 if (h1m->state == H1_MSG_TUNNEL)
1533 h1s->cs->flags |= CS_FL_EOI;
1534 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001535 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001536 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001537
Christopher Faulet6b81df72019-10-01 22:08:43 +02001538 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001539 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001540
1541 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001542 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001543 htx_to_buf(htx, buf);
Christopher Fauleta583af62020-09-24 15:35:37 +02001544 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001545 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001546 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001547}
1548
Christopher Faulet129817b2018-09-20 16:14:40 +02001549/*
1550 * Process outgoing data. It parses data and transfer them from the channel buffer into
1551 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1552 * 0 if it couldn't proceed.
1553 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001554static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1555{
Christopher Faulet129817b2018-09-20 16:14:40 +02001556 struct h1s *h1s = h1c->h1s;
1557 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001558 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001560 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001561 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001562
Christopher Faulet47365272018-10-31 17:40:50 +01001563 if (!count)
1564 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001565
Christopher Faulet94b2c762019-05-24 15:28:57 +02001566 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001567 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1568
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001569 if (htx_is_empty(chn_htx))
1570 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001571
Christopher Faulet51dbc942018-09-13 09:05:15 +02001572 if (!h1_get_buf(h1c, &h1c->obuf)) {
1573 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001574 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 +02001575 goto end;
1576 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001577
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001578 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001579
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001580 if (h1s->flags & H1S_F_PROCESSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001581 goto end;
1582
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001583 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001584 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001585
Willy Tarreau3815b222018-12-11 19:50:43 +01001586 /* Perform some optimizations to reduce the number of buffer copies.
1587 * First, if the mux's buffer is empty and the htx area contains
1588 * exactly one data block of the same size as the requested count,
1589 * then it's possible to simply swap the caller's buffer with the
1590 * mux's output buffer and adjust offsets and length to match the
1591 * entire DATA HTX block in the middle. In this case we perform a
1592 * true zero-copy operation from end-to-end. This is the situation
1593 * that happens all the time with large files. Second, if this is not
1594 * possible, but the mux's output buffer is empty, we still have an
1595 * opportunity to avoid the copy to the intermediary buffer, by making
1596 * the intermediary buffer's area point to the output buffer's area.
1597 * In this case we want to skip the HTX header to make sure that copies
1598 * remain aligned and that this operation remains possible all the
1599 * time. This goes for headers, data blocks and any data extracted from
1600 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001601 */
1602 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001603 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001604 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001605 htx_get_blk_value(chn_htx, blk).len == count) {
1606 void *old_area = h1c->obuf.area;
1607
Christopher Faulet6b81df72019-10-01 22:08:43 +02001608 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 +01001609 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001610 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001611 h1c->obuf.data = count;
1612
1613 buf->area = old_area;
1614 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001615
Christopher Faulet6b81df72019-10-01 22:08:43 +02001616 chn_htx = (struct htx *)buf->area;
1617 htx_reset(chn_htx);
1618
Christopher Fauletadb22202018-12-12 10:32:09 +01001619 /* The message is chunked. We need to emit the chunk
1620 * size. We have at least the size of the struct htx to
1621 * write the chunk envelope. It should be enough.
1622 */
1623 if (h1m->flags & H1_MF_CHNK) {
1624 h1_emit_chunk_size(&h1c->obuf, count);
1625 h1_emit_chunk_crlf(&h1c->obuf);
1626 }
1627
Willy Tarreau3815b222018-12-11 19:50:43 +01001628 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001629 if (h1m->state == H1_MSG_DATA)
1630 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001631 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001632 else
1633 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001634 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001635 goto out;
1636 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001637 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001638 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001639 else
1640 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001641
Christopher Fauletc2518a52019-06-25 21:41:02 +02001642 tmp.data = 0;
1643 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001644 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001645 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001646 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001647 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001649 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001650
Christopher Fauletb2e84162018-12-06 11:39:49 +01001651 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001652 if (type != HTX_BLK_DATA && vlen > count)
1653 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001654
Christopher Faulet94b2c762019-05-24 15:28:57 +02001655 if (type == HTX_BLK_UNUSED)
1656 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001657
Christopher Faulet94b2c762019-05-24 15:28:57 +02001658 switch (h1m->state) {
1659 case H1_MSG_RQBEFORE:
1660 if (type != HTX_BLK_REQ_SL)
1661 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001662 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 +02001663 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001664 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001665 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001666 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001667 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001668 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001669 if (sl->flags & HTX_SL_F_BODYLESS)
1670 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001672 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1673 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1674 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1675 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1676 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001677 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001678
Christopher Faulet94b2c762019-05-24 15:28:57 +02001679 case H1_MSG_RPBEFORE:
1680 if (type != HTX_BLK_RES_SL)
1681 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001682 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 +02001683 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001684 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001686 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001687 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001688 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001690 if (sl->info.res.status < 200 &&
1691 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001692 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001693 h1m->state = H1_MSG_HDR_FIRST;
1694 break;
1695
Christopher Faulet94b2c762019-05-24 15:28:57 +02001696 case H1_MSG_HDR_FIRST:
1697 case H1_MSG_HDR_NAME:
1698 case H1_MSG_HDR_L2_LWS:
1699 if (type == HTX_BLK_EOH)
1700 goto last_lf;
1701 if (type != HTX_BLK_HDR)
1702 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001703
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 h1m->state = H1_MSG_HDR_NAME;
1705 n = htx_get_blk_name(chn_htx, blk);
1706 v = htx_get_blk_value(chn_htx, blk);
1707
Christopher Faulet86d144c2019-08-14 16:32:25 +02001708 /* Skip all pseudo-headers */
1709 if (*(n.ptr) == ':')
1710 goto skip_hdr;
1711
Christopher Fauletb045bb22020-02-28 10:42:20 +01001712 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001713 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001714 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001715 /* Only skip C-L header with invalid value. */
1716 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001717 goto skip_hdr;
1718 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001719 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001720 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001721 if (!v.len)
1722 goto skip_hdr;
1723 }
1724
Christopher Faulet67d58092019-10-02 10:51:38 +02001725 /* Skip header if same name is used to add the server name */
1726 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1727 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1728 goto skip_hdr;
1729
Christopher Faulet98fbe952019-07-22 16:18:24 +02001730 /* Try to adjust the case of the header name */
1731 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1732 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001733 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001734 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001735 skip_hdr:
1736 h1m->state = H1_MSG_HDR_L2_LWS;
1737 break;
1738
Christopher Faulet94b2c762019-05-24 15:28:57 +02001739 case H1_MSG_LAST_LF:
1740 if (type != HTX_BLK_EOH)
1741 goto error;
1742 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001743 h1m->state = H1_MSG_LAST_LF;
1744 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001745 /* If the reply comes from haproxy while the request is
1746 * not finished, we force the connection close. */
1747 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1748 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1749 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1750 }
1751
1752 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001753 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001754 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001755 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001756 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001757 /* Try to adjust the case of the header name */
1758 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1759 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001760 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001761 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001762 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001763 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001764 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001765
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001766 if ((h1s->meth != HTTP_METH_CONNECT &&
1767 (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 +01001768 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1769 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1770 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1771 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1772 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001773 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001774 n = ist("transfer-encoding");
1775 v = ist("chunked");
1776 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1777 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001778 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001779 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001780 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001781 h1m->flags |= H1_MF_CHNK;
1782 }
1783
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001784 /* Now add the server name to a header (if requested) */
1785 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1786 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1787 struct server *srv = objt_server(h1c->conn->target);
1788
1789 if (srv) {
1790 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1791 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001792
1793 /* Try to adjust the case of the header name */
1794 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1795 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001796 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001797 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001798 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001799 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001800 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1801 }
1802
Christopher Fauletc2518a52019-06-25 21:41:02 +02001803 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001804 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001805
Christopher Faulet6b81df72019-10-01 22:08:43 +02001806 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1807 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1808
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001809 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1810 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1811 h1_set_req_tunnel_mode(h1s);
1812 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001813 else if ((h1m->flags & H1_MF_RESP) &&
1814 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001815 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001816 * to the client. Switch the response to tunnel mode.
1817 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001818 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001819 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 +01001820 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001821 else if ((h1m->flags & H1_MF_RESP) &&
1822 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1823 h1m_init_res(&h1s->res);
1824 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001825 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001826 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001827 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001828 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001829 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001830 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1831 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001832 else
1833 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001834 break;
1835
Christopher Faulet94b2c762019-05-24 15:28:57 +02001836 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001837 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001838 if (type == HTX_BLK_EOM) {
1839 /* Chunked message without explicit trailers */
1840 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001841 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001842 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001843 }
1844 goto done;
1845 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001846 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001847 /* If the message is not chunked, never
1848 * add the last chunk. */
1849 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001850 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001851 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 +02001852 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001853 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001854 else if (type != HTX_BLK_DATA)
1855 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001856
1857 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 +02001858
1859
1860 if (vlen > count) {
1861 /* Get the maximum amount of data we can xferred */
1862 vlen = count;
1863 }
1864
1865 chklen = 0;
1866 if (h1m->flags & H1_MF_CHNK) {
1867 chklen = b_room(&tmp);
1868 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1869 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1870 (chklen < 1048576) ? 5 : 8);
1871 chklen += 4; /* 2 x CRLF */
1872 }
1873
1874 if (vlen + chklen > b_room(&tmp)) {
1875 /* too large for the buffer */
1876 if (chklen >= b_room(&tmp))
1877 goto full;
1878 vlen = b_room(&tmp) - chklen;
1879 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001880 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001881 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001882 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001883 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001884
1885 if (h1m->state == H1_MSG_DATA)
1886 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001887 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001888 else
1889 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001890 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001891 break;
1892
Christopher Faulet94b2c762019-05-24 15:28:57 +02001893 case H1_MSG_TRAILERS:
1894 if (type == HTX_BLK_EOM)
1895 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001896 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001897 goto error;
1898 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001899 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001900 /* If the message is not chunked, ignore
1901 * trailers. It may happen with H2 messages. */
1902 if (!(h1m->flags & H1_MF_CHNK))
1903 break;
1904
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001905 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001906 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001907 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001908 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1909 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001910 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001911 else { // HTX_BLK_TLR
1912 n = htx_get_blk_name(chn_htx, blk);
1913 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001914
1915 /* Try to adjust the case of the header name */
1916 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1917 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001918 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001919 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001920 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001921 break;
1922
Christopher Faulet94b2c762019-05-24 15:28:57 +02001923 case H1_MSG_DONE:
1924 if (type != HTX_BLK_EOM)
1925 goto error;
1926 done:
1927 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001928 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1929 h1_set_req_tunnel_mode(h1s);
1930 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1931 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001932 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1933 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001934 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02001935 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 +02001936 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001937
1938 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1939 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001940 break;
1941
Christopher Faulet9768c262018-10-22 09:34:31 +02001942 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001943 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001944 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001945 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001946 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001947 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001948 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001949 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1950 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001951 break;
1952 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001953
1954 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001955 total += vlen;
1956 count -= vlen;
1957 if (sz == vlen)
1958 blk = htx_remove_blk(chn_htx, blk);
1959 else {
1960 htx_cut_data_blk(chn_htx, blk, vlen);
1961 break;
1962 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001963 }
1964
Christopher Faulet9768c262018-10-22 09:34:31 +02001965 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001966 /* when the output buffer is empty, tmp shares the same area so that we
1967 * only have to update pointers and lengths.
1968 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001969 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1970 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001971 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001972 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001973
Willy Tarreau3815b222018-12-11 19:50:43 +01001974 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 out:
1976 if (!buf_room_for_htx_data(&h1c->obuf)) {
1977 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001978 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001979 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001980 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001981 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001982 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001983
1984 full:
1985 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1986 h1c->flags |= H1C_F_OUT_FULL;
1987 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001988}
1989
Christopher Faulet51dbc942018-09-13 09:05:15 +02001990/*********************************************************/
1991/* functions below are I/O callbacks from the connection */
1992/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001993static void h1_wake_stream_for_recv(struct h1s *h1s)
1994{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001995 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001996 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001997 tasklet_wakeup(h1s->subs->tasklet);
1998 h1s->subs->events &= ~SUB_RETRY_RECV;
1999 if (!h1s->subs->events)
2000 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002001 }
2002}
2003static void h1_wake_stream_for_send(struct h1s *h1s)
2004{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002005 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002006 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002007 tasklet_wakeup(h1s->subs->tasklet);
2008 h1s->subs->events &= ~SUB_RETRY_SEND;
2009 if (!h1s->subs->events)
2010 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002011 }
2012}
2013
Christopher Faulet51dbc942018-09-13 09:05:15 +02002014/*
2015 * Attempt to read data, and subscribe if none available
2016 */
2017static int h1_recv(struct h1c *h1c)
2018{
2019 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002020 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002021 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002022 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002023 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002024
Christopher Faulet6b81df72019-10-01 22:08:43 +02002025 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2026
2027 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2028 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002029 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002030 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031
Christopher Fauletae635762020-09-21 11:47:16 +02002032 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2033 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002034 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002035 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002036 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002037
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002038 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2039 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002040 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002041 goto end;
2042 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002043
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002044 /*
2045 * If we only have a small amount of data, realign it,
2046 * it's probably cheaper than doing 2 recv() calls.
2047 */
2048 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2049 b_slow_realign(&h1c->ibuf, trash.area, 0);
2050
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002051 /* avoid useless reads after first responses */
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002052 if (h1s && ((!(h1c->flags & H1C_F_IS_BACK) && h1s->req.state == H1_MSG_RQBEFORE) ||
2053 ((h1c->flags & H1C_F_IS_BACK) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002054 flags |= CO_RFL_READ_ONCE;
2055
Willy Tarreau45f2b892018-12-05 07:59:27 +01002056 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002057 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002058 if (h1c->flags & H1C_F_IN_FULL) {
2059 h1c->flags &= ~H1C_F_IN_FULL;
2060 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2061 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002062
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002063 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002064 if (!b_data(&h1c->ibuf)) {
2065 /* try to pre-align the buffer like the rxbufs will be
2066 * to optimize memory copies.
2067 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002068 h1c->ibuf.head = sizeof(struct htx);
2069 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002070 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002071 }
Christopher Faulet47365272018-10-31 17:40:50 +01002072 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002073 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002074 rcvd = 1;
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002075 if (h1c->flags & H1C_F_CS_ATTACHED)
Christopher Faulet37e36072018-12-04 15:54:12 +01002076 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Faulet47365272018-10-31 17:40:50 +01002077 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002078
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002079 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002080 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002081 goto end;
2082 }
2083
Christopher Faulet6b81df72019-10-01 22:08:43 +02002084 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002085 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002086
Christopher Faulet9768c262018-10-22 09:34:31 +02002087 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002088 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2089 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002090
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002091 if (conn_xprt_read0_pending(conn) && h1s) {
2092 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002094 rcvd = 1;
2095 }
2096
Christopher Faulet51dbc942018-09-13 09:05:15 +02002097 if (!b_data(&h1c->ibuf))
2098 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002099 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002100 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002101 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2102 }
2103
2104 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002105 return rcvd;
2106}
2107
2108
2109/*
2110 * Try to send data if possible
2111 */
2112static int h1_send(struct h1c *h1c)
2113{
2114 struct connection *conn = h1c->conn;
2115 unsigned int flags = 0;
2116 size_t ret;
2117 int sent = 0;
2118
Christopher Faulet6b81df72019-10-01 22:08:43 +02002119 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2120
2121 if (conn->flags & CO_FL_ERROR) {
2122 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002123 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002124 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002125
2126 if (!b_data(&h1c->obuf))
2127 goto end;
2128
Christopher Faulet46230362019-10-17 16:04:20 +02002129 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002130 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002131 if (h1c->flags & H1C_F_CO_STREAMER)
2132 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002134 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002135 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002136 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002137 if (h1c->flags & H1C_F_OUT_FULL) {
2138 h1c->flags &= ~H1C_F_OUT_FULL;
2139 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2140 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141 b_del(&h1c->obuf, ret);
2142 sent = 1;
2143 }
2144
Christopher Faulet145aa472018-12-06 10:56:20 +01002145 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002146 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002147 /* error or output closed, nothing to send, clear the buffer to release it */
2148 b_reset(&h1c->obuf);
2149 }
2150
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002152 if (!(h1c->flags & H1C_F_OUT_FULL))
2153 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002154
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155 /* We're done, no more to send */
2156 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002157 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002159 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2160 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002161 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002162 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002163 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002164 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2165 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002166 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002167 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002168
Christopher Faulet6b81df72019-10-01 22:08:43 +02002169 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002170 return sent;
2171}
2172
Christopher Faulet51dbc942018-09-13 09:05:15 +02002173
2174/* callback called on any event by the connection handler.
2175 * It applies changes and returns zero, or < 0 if it wants immediate
2176 * destruction of the connection.
2177 */
2178static int h1_process(struct h1c * h1c)
2179{
2180 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002181 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002182
Christopher Faulet6b81df72019-10-01 22:08:43 +02002183 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2184
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002185 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002186 return -1;
2187
Christopher Fauletfeb11742018-11-29 15:12:34 +01002188 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002189 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002190 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002191 goto release;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002192 if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002193 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002194 if (!h1c_frt_stream_new(h1c))
Christopher Faulet539e0292018-11-19 10:40:09 +01002195 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002197 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002198 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002199 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002200 }
2201
Christopher Fauletae635762020-09-21 11:47:16 +02002202 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2203 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2204 h1_wake_stream_for_recv(h1s);
2205 }
2206
Christopher Faulet4e741552020-09-30 13:47:09 +02002207 if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1)
2208 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002209
Christopher Faulet6b81df72019-10-01 22:08:43 +02002210 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002211 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002212 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2213 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002214
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002215 if (!h1s_data_pending(h1s) && h1s && (h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002216 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002217 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002218 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002219 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002220 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002221 h1s->cs->data_cb->wake(h1s->cs);
2222 }
Christopher Faulet47365272018-10-31 17:40:50 +01002223 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002224 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002225 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002226 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002227
2228 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002229 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002230 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002231 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002232}
2233
2234static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2235{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002236 struct connection *conn;
2237 struct tasklet *tl = (struct tasklet *)t;
2238 int conn_in_list;
2239 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002240 int ret = 0;
2241
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002242
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002243 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002244 if (tl->context == NULL) {
2245 /* The connection has been taken over by another thread,
2246 * we're no longer responsible for it, so just free the
2247 * tasklet, and do nothing.
2248 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002249 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002250 tasklet_free(tl);
2251 return NULL;
2252 }
2253 h1c = ctx;
2254 conn = h1c->conn;
2255
2256 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2257
2258 /* Remove the connection from the list, to be sure nobody attempts
2259 * to use it while we handle the I/O events
2260 */
2261 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2262 if (conn_in_list)
2263 MT_LIST_DEL(&conn->list);
2264
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002265 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002267 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002268 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002269 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002270 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002271 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002272 ret = h1_process(h1c);
2273 /* If we were in an idle list, we want to add it back into it,
2274 * unless h1_process() returned -1, which mean it has destroyed
2275 * the connection (testing !ret is enough, if h1_process() wasn't
2276 * called then ret will be 0 anyway.
2277 */
2278 if (!ret && conn_in_list) {
2279 struct server *srv = objt_server(conn->target);
2280
2281 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002282 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002283 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002284 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002285 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286 return NULL;
2287}
2288
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002289static void h1_reset(struct connection *conn)
2290{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002291
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002292}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002293
2294static int h1_wake(struct connection *conn)
2295{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002296 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002297 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002298
Christopher Faulet6b81df72019-10-01 22:08:43 +02002299 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2300
Christopher Faulet539e0292018-11-19 10:40:09 +01002301 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002302 ret = h1_process(h1c);
2303 if (ret == 0) {
2304 struct h1s *h1s = h1c->h1s;
2305
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002306 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002307 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002308 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002309 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002310 }
2311 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002312}
2313
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002314/* Connection timeout management. The principle is that if there's no receipt
2315 * nor sending for a certain amount of time, the connection is closed.
2316 */
2317static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2318{
2319 struct h1c *h1c = context;
2320 int expired = tick_is_expired(t->expire, now_ms);
2321
Christopher Faulet6b81df72019-10-01 22:08:43 +02002322 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2323
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002324 if (h1c) {
2325 if (!expired) {
2326 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
2327 return t;
2328 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002329
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002330 /* We're about to destroy the connection, so make sure nobody attempts
2331 * to steal it from us.
2332 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002333 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002334
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002335 /* Somebody already stole the connection from us, so we should not
2336 * free it, we just have to free the task.
2337 */
2338 if (!t->context)
2339 h1c = NULL;
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002340 else if (h1c->conn->flags & CO_FL_LIST_MASK)
2341 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002342
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002343 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002344 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002345
Olivier Houchard3f795f72019-04-17 22:51:06 +02002346 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002347
2348 if (!h1c) {
2349 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002350 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002351 return NULL;
2352 }
2353
2354 h1c->task = NULL;
2355 /* If a stream is still attached to the mux, just set an error and wait
2356 * for the stream's timeout. Otherwise, release the mux. This is only ok
2357 * because same timeouts are used.
2358 */
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002359 if (h1c->flags & H1C_F_CS_ATTACHED) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002360 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002361 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2362 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002363 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002364 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002365
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002366 return NULL;
2367}
2368
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369/*******************************************/
2370/* functions below are used by the streams */
2371/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002372
Christopher Faulet51dbc942018-09-13 09:05:15 +02002373/*
2374 * Attach a new stream to a connection
2375 * (Used for outgoing connections)
2376 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002377static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002378{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002379 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002380 struct conn_stream *cs = NULL;
2381 struct h1s *h1s;
2382
Christopher Faulet6b81df72019-10-01 22:08:43 +02002383 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2384 if (h1c->flags & H1C_F_CS_ERROR) {
2385 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 +02002386 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002387 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002388
Christopher Faulet236c93b2020-07-02 09:19:54 +02002389 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002390 if (!cs) {
2391 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 +02002392 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002393 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002394
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002395 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002396 if (h1s == NULL) {
2397 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 +02002398 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002399 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002400
Christopher Faulet6b81df72019-10-01 22:08:43 +02002401 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002402 return cs;
2403 end:
2404 cs_free(cs);
2405 return NULL;
2406}
2407
2408/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2409 * this mux, it's easy as we can only store a single conn_stream.
2410 */
2411static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2412{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002413 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002414 struct h1s *h1s = h1c->h1s;
2415
2416 if (h1s)
2417 return h1s->cs;
2418
2419 return NULL;
2420}
2421
Christopher Faulet73c12072019-04-08 11:23:22 +02002422static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002423{
Christopher Faulet73c12072019-04-08 11:23:22 +02002424 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002425
Christopher Faulet6b81df72019-10-01 22:08:43 +02002426 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002427 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002428 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002429}
2430
2431/*
2432 * Detach the stream from the connection and possibly release the connection.
2433 */
2434static void h1_detach(struct conn_stream *cs)
2435{
2436 struct h1s *h1s = cs->ctx;
2437 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002438 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002439 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002440
Christopher Faulet6b81df72019-10-01 22:08:43 +02002441 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2442
Christopher Faulet51dbc942018-09-13 09:05:15 +02002443 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002444 if (!h1s) {
2445 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002446 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002447 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002448
Olivier Houchardf502aca2018-12-14 19:42:40 +01002449 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002450 h1c = h1s->h1c;
2451 h1s->cs = NULL;
2452
Christopher Faulet42849b02020-10-05 11:35:17 +02002453 sess->accept_date = date;
2454 sess->tv_accept = now;
2455 sess->t_handshake = 0;
2456 sess->t_idle = -1;
2457
Olivier Houchard8a786902018-12-15 16:05:40 +01002458 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2459 h1s_destroy(h1s);
2460
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002461 if ((h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002462 /* If there are any excess server data in the input buffer,
2463 * release it and close the connection ASAP (some data may
2464 * remain in the output buffer). This happens if a server sends
2465 * invalid responses. So in such case, we don't want to reuse
2466 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002467 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002468 if (b_data(&h1c->ibuf)) {
2469 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002470 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002471 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002472 goto release;
2473 }
Christopher Faulet03627242019-07-19 11:34:08 +02002474
Christopher Faulet08016ab2020-07-01 16:10:06 +02002475 if (h1c->conn->flags & CO_FL_PRIVATE) {
2476 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002477 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2478 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002479 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002480 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002481 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002482 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002483 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002484 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002485 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2486 goto end;
2487 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002488 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002489 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002490 if (h1c->conn->owner == sess)
2491 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002492 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002493 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002494 /* The server doesn't want it, let's kill the connection right away */
2495 h1c->conn->mux->destroy(h1c);
2496 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2497 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002498 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002499 /* At this point, the connection has been added to the
2500 * server idle list, so another thread may already have
2501 * hijacked it, so we can't do anything with it.
2502 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002503 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002504 }
2505 }
2506
Christopher Fauletf1204b82019-07-19 14:51:06 +02002507 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002508 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002509 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2510 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2511 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002512 !h1c->conn->owner) {
2513 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002514 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002515 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002516 else {
Olivier Houchard69664412020-03-25 12:24:11 +01002517 /* If we have a new request, process it immediately */
Olivier Houchardc3500c32020-03-25 17:05:21 +01002518 if (unlikely(b_data(&h1c->ibuf)))
Olivier Houchard69664412020-03-25 12:24:11 +01002519 h1_process(h1c);
2520 else
2521 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002522 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002523 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002524 end:
2525 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002526}
2527
2528
2529static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2530{
2531 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002532 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002533
2534 if (!h1s)
2535 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002536 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002537
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2539
2540 if (cs->flags & CS_FL_KILL_CONN) {
2541 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2542 goto do_shutr;
2543 }
2544 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2545 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002546 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002547 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002548
Christopher Faulet6b81df72019-10-01 22:08:43 +02002549 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2550 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2551 goto end;
2552 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553
Christopher Faulet7f366362019-04-08 10:51:20 +02002554 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002555 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2556 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002557 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002558 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002559 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002560 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002561 end:
2562 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002563}
2564
2565static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2566{
2567 struct h1s *h1s = cs->ctx;
2568 struct h1c *h1c;
2569
2570 if (!h1s)
2571 return;
2572 h1c = h1s->h1c;
2573
Christopher Faulet6b81df72019-10-01 22:08:43 +02002574 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2575
2576 if (cs->flags & CS_FL_KILL_CONN) {
2577 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002578 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002579 }
2580 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2581 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2582 goto do_shutw;
2583 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002584
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002585 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002586 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2587 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2588 goto end;
2589 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590
Christopher Faulet7f366362019-04-08 10:51:20 +02002591 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002592 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2593 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002594 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002595 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002596 end:
2597 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002598}
2599
Christopher Faulet666a0c42019-01-08 11:12:04 +01002600static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002601{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002602 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002603
Christopher Faulet6b81df72019-10-01 22:08:43 +02002604 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002605 conn_xprt_shutw(conn);
2606 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002607 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002608 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002609}
2610
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002611/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2612 * The <es> pointer is not allowed to differ from the one passed to the
2613 * subscribe() call. It always returns zero.
2614 */
2615static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002616{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002617 struct h1s *h1s = cs->ctx;
2618
2619 if (!h1s)
2620 return 0;
2621
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002622 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002623 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002624
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002625 es->events &= ~event_type;
2626 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002627 h1s->subs = NULL;
2628
2629 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002630 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002631
2632 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002633 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002634
Christopher Faulet51dbc942018-09-13 09:05:15 +02002635 return 0;
2636}
2637
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002638/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2639 * event subscriber <es> is not allowed to change from a previous call as long
2640 * as at least one event is still subscribed. The <event_type> must only be a
2641 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2642 * the conn_stream <cs> was already detached, in which case it will return -1.
2643 */
2644static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002645{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002646 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002647 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002648
2649 if (!h1s)
2650 return -1;
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 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002657
2658 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002659 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002660
2661
Christopher Faulet6b81df72019-10-01 22:08:43 +02002662 if (event_type & SUB_RETRY_SEND) {
2663 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002664 /*
2665 * If the conn_stream attempt to subscribe, and the
2666 * mux isn't subscribed to the connection, then it
2667 * probably means the connection wasn't established
2668 * yet, so we have to subscribe.
2669 */
2670 h1c = h1s->h1c;
2671 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2672 h1c->conn->xprt->subscribe(h1c->conn,
2673 h1c->conn->xprt_ctx,
2674 SUB_RETRY_SEND,
2675 &h1c->wait_event);
2676 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002677 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002678}
2679
2680/* Called from the upper layer, to receive data */
2681static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2682{
2683 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002684 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002685 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002686 size_t ret = 0;
2687
Willy Tarreau022e5e52020-09-10 09:33:15 +02002688 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002689 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002690 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002691 else
2692 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002693
Christopher Faulete18777b2019-04-16 16:46:36 +02002694 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002695 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02002696 h1c->flags |= H1C_F_WANT_SPLICE;
2697 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 +02002698 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002699 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002700 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002701 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002702 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002703 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002704 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002705 return ret;
2706}
2707
2708
2709/* Called from the upper layer, to send data */
2710static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2711{
2712 struct h1s *h1s = cs->ctx;
2713 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002714 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002715
2716 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002717 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002718 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002719
Willy Tarreau022e5e52020-09-10 09:33:15 +02002720 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002721
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002722 /* If we're not connected yet, or we're waiting for a handshake, stop
2723 * now, as we don't want to remove everything from the channel buffer
2724 * before we're sure we can send it.
2725 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002726 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002728 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002729 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002730
Christopher Faulet46230362019-10-17 16:04:20 +02002731 /* Inherit some flags from the upper layer */
2732 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2733 if (flags & CO_SFL_MSG_MORE)
2734 h1c->flags |= H1C_F_CO_MSG_MORE;
2735 if (flags & CO_SFL_STREAMER)
2736 h1c->flags |= H1C_F_CO_STREAMER;
2737
Christopher Fauletc31872f2019-06-04 22:09:36 +02002738 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002739 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002740
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002741 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2742 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002743 else
2744 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002745
2746 if ((count - ret) > 0)
2747 h1c->flags |= H1C_F_CO_MSG_MORE;
2748
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002749 if (!ret)
2750 break;
2751 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002752 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002753 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002754 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002755 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002756 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002757 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002758 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002759}
2760
Willy Tarreaue5733232019-05-22 19:24:06 +02002761#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002762/* Send and get, using splicing */
2763static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2764{
2765 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002766 struct h1c *h1c = h1s->h1c;
2767 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002768 int ret = 0;
2769
Willy Tarreau022e5e52020-09-10 09:33:15 +02002770 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002771
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002772 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002773 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002774 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 +02002775 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002776 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002777 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002778 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002779 goto end;
2780 }
2781
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002782 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
2783 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002784 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2785 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002786 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002787 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002788 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002789 }
2790
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002791 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02002792 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2793 goto end;
2794 }
2795
Christopher Faulet1be55f92018-10-02 15:59:23 +02002796 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2797 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002798 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002799 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002800 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002801 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002802 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002803 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002804 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002805 }
2806
Christopher Faulet1be55f92018-10-02 15:59:23 +02002807 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002808 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002809 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002810 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002811 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002812 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002813
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002814 if ((h1s->flags & H1S_F_REOS) ||
2815 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002816 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002817 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002818 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002819 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002820
Willy Tarreau022e5e52020-09-10 09:33:15 +02002821 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002822 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002823}
2824
2825static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2826{
2827 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002828 int ret = 0;
2829
Willy Tarreau022e5e52020-09-10 09:33:15 +02002830 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002831
Christopher Faulet1be55f92018-10-02 15:59:23 +02002832 if (b_data(&h1s->h1c->obuf))
2833 goto end;
2834
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002835 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002836 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002837 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002838 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2839 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002840 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002841 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002842 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002843
Willy Tarreau022e5e52020-09-10 09:33:15 +02002844 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002845 return ret;
2846}
2847#endif
2848
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002849static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2850{
2851 int ret = 0;
2852 switch (mux_ctl) {
2853 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002854 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002855 ret |= MUX_STATUS_READY;
2856 return ret;
2857 default:
2858 return -1;
2859 }
2860}
2861
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002862/* for debugging with CLI's "show fd" command */
2863static void h1_show_fd(struct buffer *msg, struct connection *conn)
2864{
2865 struct h1c *h1c = conn->ctx;
2866 struct h1s *h1s = h1c->h1s;
2867
Christopher Fauletf376a312019-01-04 15:16:06 +01002868 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2869 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002870 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2871 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2872 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2873 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2874
2875 if (h1s) {
2876 char *method;
2877
2878 if (h1s->meth < HTTP_METH_OTHER)
2879 method = http_known_methods[h1s->meth].ptr;
2880 else
2881 method = "UNKNOWN";
2882 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2883 " .meth=%s status=%d",
2884 h1s, h1s->flags,
2885 h1m_state_str(h1s->req.state),
2886 h1m_state_str(h1s->res.state), method, h1s->status);
2887 if (h1s->cs)
2888 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2889 h1s->cs->flags, h1s->cs->data);
2890 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002891}
2892
2893
2894/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2895static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2896{
2897 struct h1_hdr_entry *entry;
2898
2899 /* Be sure there is a non-empty <to> */
2900 if (!strlen(to)) {
2901 memprintf(err, "expect <to>");
2902 return -1;
2903 }
2904
2905 /* Be sure only the case differs between <from> and <to> */
2906 if (strcasecmp(from, to)) {
2907 memprintf(err, "<from> and <to> must not differ execpt the case");
2908 return -1;
2909 }
2910
2911 /* Be sure <from> does not already existsin the tree */
2912 if (ebis_lookup(&hdrs_map.map, from)) {
2913 memprintf(err, "duplicate entry '%s'", from);
2914 return -1;
2915 }
2916
2917 /* Create the entry and insert it in the tree */
2918 entry = malloc(sizeof(*entry));
2919 if (!entry) {
2920 memprintf(err, "out of memory");
2921 return -1;
2922 }
2923
2924 entry->node.key = strdup(from);
2925 entry->name.ptr = strdup(to);
2926 entry->name.len = strlen(to);
2927 if (!entry->node.key || !entry->name.ptr) {
2928 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002929 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002930 free(entry);
2931 memprintf(err, "out of memory");
2932 return -1;
2933 }
2934 ebis_insert(&hdrs_map.map, &entry->node);
2935 return 0;
2936}
2937
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002938/* Migrate the the connection to the current thread.
2939 * Return 0 if successful, non-zero otherwise.
2940 * Expected to be called with the old thread lock held.
2941 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002942static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002943{
2944 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002945 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002946
2947 if (fd_takeover(conn->handle.fd, conn) != 0)
2948 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02002949
2950 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
2951 /* We failed to takeover the xprt, even if the connection may
2952 * still be valid, flag it as error'd, as we have already
2953 * taken over the fd, and wake the tasklet, so that it will
2954 * destroy it.
2955 */
2956 conn->flags |= CO_FL_ERROR;
2957 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
2958 return -1;
2959 }
2960
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002961 if (h1c->wait_event.events)
2962 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
2963 h1c->wait_event.events, &h1c->wait_event);
2964 /* To let the tasklet know it should free itself, and do nothing else,
2965 * set its context to NULL.
2966 */
2967 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002968 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002969
2970 task = h1c->task;
2971 if (task) {
2972 task->context = NULL;
2973 h1c->task = NULL;
2974 __ha_barrier_store();
2975 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002976
2977 h1c->task = task_new(tid_bit);
2978 if (!h1c->task) {
2979 h1_release(h1c);
2980 return -1;
2981 }
2982 h1c->task->process = h1_timeout_task;
2983 h1c->task->context = h1c;
2984 }
2985 h1c->wait_event.tasklet = tasklet_new();
2986 if (!h1c->wait_event.tasklet) {
2987 h1_release(h1c);
2988 return -1;
2989 }
2990 h1c->wait_event.tasklet->process = h1_io_cb;
2991 h1c->wait_event.tasklet->context = h1c;
2992 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
2993 SUB_RETRY_RECV, &h1c->wait_event);
2994
2995 return 0;
2996}
2997
2998
Christopher Faulet98fbe952019-07-22 16:18:24 +02002999static void h1_hdeaders_case_adjust_deinit()
3000{
3001 struct ebpt_node *node, *next;
3002 struct h1_hdr_entry *entry;
3003
3004 node = ebpt_first(&hdrs_map.map);
3005 while (node) {
3006 next = ebpt_next(node);
3007 ebpt_delete(node);
3008 entry = container_of(node, struct h1_hdr_entry, node);
3009 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003010 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003011 free(entry);
3012 node = next;
3013 }
3014 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003015}
3016
Christopher Faulet98fbe952019-07-22 16:18:24 +02003017static int cfg_h1_headers_case_adjust_postparser()
3018{
3019 FILE *file = NULL;
3020 char *c, *key_beg, *key_end, *value_beg, *value_end;
3021 char *err;
3022 int rc, line = 0, err_code = 0;
3023
3024 if (!hdrs_map.name)
3025 goto end;
3026
3027 file = fopen(hdrs_map.name, "r");
3028 if (!file) {
3029 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3030 hdrs_map.name);
3031 err_code |= ERR_ALERT | ERR_FATAL;
3032 goto end;
3033 }
3034
3035 /* now parse all lines. The file may contain only two header name per
3036 * line, separated by spaces. All heading and trailing spaces will be
3037 * ignored. Lines starting with a # are ignored.
3038 */
3039 while (fgets(trash.area, trash.size, file) != NULL) {
3040 line++;
3041 c = trash.area;
3042
3043 /* strip leading spaces and tabs */
3044 while (*c == ' ' || *c == '\t')
3045 c++;
3046
3047 /* ignore emptu lines, or lines beginning with a dash */
3048 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3049 continue;
3050
3051 /* look for the end of the key */
3052 key_beg = c;
3053 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3054 c++;
3055 key_end = c;
3056
3057 /* strip middle spaces and tabs */
3058 while (*c == ' ' || *c == '\t')
3059 c++;
3060
3061 /* look for the end of the value, it is the end of the line */
3062 value_beg = c;
3063 while (*c && *c != '\n' && *c != '\r')
3064 c++;
3065 value_end = c;
3066
3067 /* trim possibly trailing spaces and tabs */
3068 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3069 value_end--;
3070
3071 /* set final \0 and check entries */
3072 *key_end = '\0';
3073 *value_end = '\0';
3074
3075 err = NULL;
3076 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3077 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003078 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3079 hdrs_map.name, err, line);
3080 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003081 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003082 goto end;
3083 }
3084 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003085 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3086 hdrs_map.name, err, line);
3087 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003088 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003089 }
3090 }
3091
3092 end:
3093 if (file)
3094 fclose(file);
3095 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3096 return err_code;
3097}
3098
3099
3100/* config parser for global "h1-outgoing-header-case-adjust" */
3101static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3102 struct proxy *defpx, const char *file, int line,
3103 char **err)
3104{
3105 if (too_many_args(2, args, err, NULL))
3106 return -1;
3107 if (!*(args[1]) || !*(args[2])) {
3108 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3109 return -1;
3110 }
3111 return add_hdr_case_adjust(args[1], args[2], err);
3112}
3113
3114/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3115static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3116 struct proxy *defpx, const char *file, int line,
3117 char **err)
3118{
3119 if (too_many_args(1, args, err, NULL))
3120 return -1;
3121 if (!*(args[1])) {
3122 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3123 return -1;
3124 }
3125 free(hdrs_map.name);
3126 hdrs_map.name = strdup(args[1]);
3127 return 0;
3128}
3129
3130
3131/* config keyword parsers */
3132static struct cfg_kw_list cfg_kws = {{ }, {
3133 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3134 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3135 { 0, NULL, NULL },
3136 }
3137};
3138
3139INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3140REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3141
3142
Christopher Faulet51dbc942018-09-13 09:05:15 +02003143/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003144/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003145/****************************************/
3146
3147/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003148static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003149 .init = h1_init,
3150 .wake = h1_wake,
3151 .attach = h1_attach,
3152 .get_first_cs = h1_get_first_cs,
3153 .detach = h1_detach,
3154 .destroy = h1_destroy,
3155 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003156 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003157 .rcv_buf = h1_rcv_buf,
3158 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003159#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003160 .rcv_pipe = h1_rcv_pipe,
3161 .snd_pipe = h1_snd_pipe,
3162#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003163 .subscribe = h1_subscribe,
3164 .unsubscribe = h1_unsubscribe,
3165 .shutr = h1_shutr,
3166 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003167 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003168 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003169 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003170 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003171 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003172 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003173};
3174
3175
3176/* this mux registers default HTX proto */
3177static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003178{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003179
Willy Tarreau0108d902018-11-25 19:14:37 +01003180INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3181
Christopher Faulet51dbc942018-09-13 09:05:15 +02003182/*
3183 * Local variables:
3184 * c-indent-level: 8
3185 * c-basic-offset: 8
3186 * End:
3187 */