blob: 7b474d88fbc8bbea5ca3ff994d05459eb094d510 [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 Faulet51dbc942018-09-13 09:05:15 +020063/*
64 * H1 Stream flags (32 bits)
65 */
Christopher Faulet129817b2018-09-20 16:14:40 +020066#define H1S_F_NONE 0x00000000
Christopher Faulet60ef12c2020-09-24 10:05:44 +020067/* 0x00000001..0x00000004 unsued */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020068#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020069#define H1S_F_WANT_KAL 0x00000010
70#define H1S_F_WANT_TUN 0x00000020
71#define H1S_F_WANT_CLO 0x00000040
72#define H1S_F_WANT_MSK 0x00000070
73#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020074
Christopher Faulet76014fd2019-12-10 11:47:22 +010075#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020076#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
77#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
78#define H1S_F_ERROR 0x00001800 /* stream error mask */
79
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020080#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 +020081#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020082
83/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020084struct h1c {
85 struct connection *conn;
86 struct proxy *px;
87 uint32_t flags; /* Connection flags: H1C_F_* */
88
89 struct buffer ibuf; /* Input buffer to store data before parsing */
90 struct buffer obuf; /* Output buffer to store data after reformatting */
91
92 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
93 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
94
95 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010096 struct task *task; /* timeout management task */
97 int timeout; /* idle timeout duration in ticks */
98 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099};
100
101/* H1 stream descriptor */
102struct h1s {
103 struct h1c *h1c;
104 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100105 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200106
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100107 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200108
Olivier Houchardf502aca2018-12-14 19:42:40 +0100109 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200110 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200111 struct h1m req;
112 struct h1m res;
113
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500114 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200115 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200116};
117
Christopher Faulet98fbe952019-07-22 16:18:24 +0200118/* Map of headers used to convert outgoing headers */
119struct h1_hdrs_map {
120 char *name;
121 struct eb_root map;
122};
123
124/* An entry in a headers map */
125struct h1_hdr_entry {
126 struct ist name;
127 struct ebpt_node node;
128};
129
130/* Declare the headers map */
131static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
132
133
Christopher Faulet6b81df72019-10-01 22:08:43 +0200134/* trace source and events */
135static void h1_trace(enum trace_level level, uint64_t mask,
136 const struct trace_source *src,
137 const struct ist where, const struct ist func,
138 const void *a1, const void *a2, const void *a3, const void *a4);
139
140/* The event representation is split like this :
141 * h1c - internal H1 connection
142 * h1s - internal H1 stream
143 * strm - application layer
144 * rx - data receipt
145 * tx - data transmission
146 *
147 */
148static const struct trace_event h1_trace_events[] = {
149#define H1_EV_H1C_NEW (1ULL << 0)
150 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
151#define H1_EV_H1C_RECV (1ULL << 1)
152 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
153#define H1_EV_H1C_SEND (1ULL << 2)
154 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
155#define H1_EV_H1C_BLK (1ULL << 3)
156 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
157#define H1_EV_H1C_WAKE (1ULL << 4)
158 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
159#define H1_EV_H1C_END (1ULL << 5)
160 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
161#define H1_EV_H1C_ERR (1ULL << 6)
162 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
163
164#define H1_EV_RX_DATA (1ULL << 7)
165 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
166#define H1_EV_RX_EOI (1ULL << 8)
167 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
168#define H1_EV_RX_HDRS (1ULL << 9)
169 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
170#define H1_EV_RX_BODY (1ULL << 10)
171 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
172#define H1_EV_RX_TLRS (1ULL << 11)
173 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
174
175#define H1_EV_TX_DATA (1ULL << 12)
176 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
177#define H1_EV_TX_EOI (1ULL << 13)
178 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
179#define H1_EV_TX_HDRS (1ULL << 14)
180 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
181#define H1_EV_TX_BODY (1ULL << 15)
182 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
183#define H1_EV_TX_TLRS (1ULL << 16)
184 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
185
186#define H1_EV_H1S_NEW (1ULL << 17)
187 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
188#define H1_EV_H1S_BLK (1ULL << 18)
189 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
190#define H1_EV_H1S_END (1ULL << 19)
191 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
192#define H1_EV_H1S_ERR (1ULL << 20)
193 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
194
195#define H1_EV_STRM_NEW (1ULL << 21)
196 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
197#define H1_EV_STRM_RECV (1ULL << 22)
198 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
199#define H1_EV_STRM_SEND (1ULL << 23)
200 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
201#define H1_EV_STRM_WAKE (1ULL << 24)
202 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
203#define H1_EV_STRM_SHUT (1ULL << 25)
204 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
205#define H1_EV_STRM_END (1ULL << 26)
206 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
207#define H1_EV_STRM_ERR (1ULL << 27)
208 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
209
210 { }
211};
212
213static const struct name_desc h1_trace_lockon_args[4] = {
214 /* arg1 */ { /* already used by the connection */ },
215 /* arg2 */ { .name="h1s", .desc="H1 stream" },
216 /* arg3 */ { },
217 /* arg4 */ { }
218};
219
220static const struct name_desc h1_trace_decoding[] = {
221#define H1_VERB_CLEAN 1
222 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
223#define H1_VERB_MINIMAL 2
224 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
225#define H1_VERB_SIMPLE 3
226 { .name="simple", .desc="add request/response status line or htx info when available" },
227#define H1_VERB_ADVANCED 4
228 { .name="advanced", .desc="add header fields or frame decoding when available" },
229#define H1_VERB_COMPLETE 5
230 { .name="complete", .desc="add full data dump when available" },
231 { /* end */ }
232};
233
234static struct trace_source trace_h1 = {
235 .name = IST("h1"),
236 .desc = "HTTP/1 multiplexer",
237 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
238 .default_cb = h1_trace,
239 .known_events = h1_trace_events,
240 .lockon_args = h1_trace_lockon_args,
241 .decoding = h1_trace_decoding,
242 .report_events = ~0, // report everything by default
243};
244
245#define TRACE_SOURCE &trace_h1
246INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
247
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100249DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
250DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200251
Christopher Faulet51dbc942018-09-13 09:05:15 +0200252static int h1_recv(struct h1c *h1c);
253static int h1_send(struct h1c *h1c);
254static int h1_process(struct h1c *h1c);
255static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100256static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100257static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200258static void h1_wake_stream_for_recv(struct h1s *h1s);
259static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200260
Christopher Faulet6b81df72019-10-01 22:08:43 +0200261/* the H1 traces always expect that arg1, if non-null, is of type connection
262 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
263 * that arg3, if non-null, is a htx for rx/tx headers.
264 */
265static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
266 const struct ist where, const struct ist func,
267 const void *a1, const void *a2, const void *a3, const void *a4)
268{
269 const struct connection *conn = a1;
270 const struct h1c *h1c = conn ? conn->ctx : NULL;
271 const struct h1s *h1s = a2;
272 const struct htx *htx = a3;
273 const size_t *val = a4;
274
275 if (!h1c)
276 h1c = (h1s ? h1s->h1c : NULL);
277
278 if (!h1c || src->verbosity < H1_VERB_CLEAN)
279 return;
280
281 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200282 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200283
284 /* Display request and response states if h1s is defined */
285 if (h1s)
286 chunk_appendf(&trace_buf, " [%s, %s]",
287 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
288
289 if (src->verbosity == H1_VERB_CLEAN)
290 return;
291
292 /* Display the value to the 4th argument (level > STATE) */
293 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100294 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200295
296 /* Display status-line if possible (verbosity > MINIMAL) */
297 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
298 const struct htx_blk *blk = htx_get_head_blk(htx);
299 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
300 enum htx_blk_type type = htx_get_blk_type(blk);
301
302 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
303 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
304 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
305 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
306 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
307 }
308
309 /* Display h1c info and, if defined, h1s info (pointer + flags) */
310 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
311 if (h1s)
312 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
313
314 if (src->verbosity == H1_VERB_MINIMAL)
315 return;
316
317 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
318 if (src->level > TRACE_LEVEL_USER) {
319 if (src->verbosity == H1_VERB_COMPLETE ||
320 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
321 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
322 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
323 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
324 if (src->verbosity == H1_VERB_COMPLETE ||
325 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
326 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
327 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
328 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
329 }
330
331 /* Display htx info if defined (level > USER) */
332 if (src->level > TRACE_LEVEL_USER && htx) {
333 int full = 0;
334
335 /* Full htx info (level > STATE && verbosity > SIMPLE) */
336 if (src->level > TRACE_LEVEL_STATE) {
337 if (src->verbosity == H1_VERB_COMPLETE)
338 full = 1;
339 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
340 full = 1;
341 }
342
343 chunk_memcat(&trace_buf, "\n\t", 2);
344 htx_dump(&trace_buf, htx, full);
345 }
346}
347
348
Christopher Faulet51dbc942018-09-13 09:05:15 +0200349/*****************************************************/
350/* functions below are for dynamic buffer management */
351/*****************************************************/
352/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100353 * Indicates whether or not we may receive data. The rules are the following :
354 * - if an error or a shutdown for reads was detected on the connection we
355 must not attempt to receive
356 * - if the input buffer failed to be allocated or is full , we must not try
357 * to receive
358 * - if he input processing is busy waiting for the output side, we may
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500359 * attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200360 * - otherwise must may not attempt to receive
361 */
362static inline int h1_recv_allowed(const struct h1c *h1c)
363{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100364 if (h1c->flags & H1C_F_CS_ERROR) {
365 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 +0200366 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200367 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368
Willy Tarreau2febb842020-07-31 09:15:43 +0200369 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
370 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 +0100371 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200372 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100373
Christopher Faulet089acd52020-09-21 10:57:52 +0200374 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
375 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200376 return 0;
377 }
378
Christopher Fauletd17ad822020-09-24 15:14:29 +0200379 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200380 return 1;
381
Christopher Faulet6b81df72019-10-01 22:08:43 +0200382 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 +0200383 return 0;
384}
385
386/*
387 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
388 * flags are used to figure what buffer was requested. It returns 1 if the
389 * allocation succeeds, in which case the connection is woken up, or 0 if it's
390 * impossible to wake up and we prefer to be woken up later.
391 */
392static int h1_buf_available(void *target)
393{
394 struct h1c *h1c = target;
395
396 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200397 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 +0200398 h1c->flags &= ~H1C_F_IN_ALLOC;
399 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200400 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200401 return 1;
402 }
403
404 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200405 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 +0200406 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200407 if (h1c->h1s)
408 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 return 1;
410 }
411
Christopher Fauletd17ad822020-09-24 15:14:29 +0200412 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
413 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
414 h1c->flags &= ~H1C_F_IN_SALLOC;
415 tasklet_wakeup(h1c->wait_event.tasklet);
416 return 1;
417 }
418
Christopher Faulet51dbc942018-09-13 09:05:15 +0200419 return 0;
420}
421
422/*
423 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
424 */
425static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
426{
427 struct buffer *buf = NULL;
428
Willy Tarreau21046592020-02-26 10:39:36 +0100429 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200430 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
431 h1c->buf_wait.target = h1c;
432 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200433 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200434 }
435 return buf;
436}
437
438/*
439 * Release a buffer, if any, and try to wake up entities waiting in the buffer
440 * wait queue.
441 */
442static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
443{
444 if (bptr->size) {
445 b_free(bptr);
446 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
447 }
448}
449
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100450/* returns the number of streams in use on a connection to figure if it's idle
451 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
452 * not. This flag is only set when no H1S is attached and when the previous
453 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100454 */
455static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100457 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200458
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100459 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200460}
461
Willy Tarreau00f18a32019-01-26 12:19:01 +0100462/* returns the number of streams still available on a connection */
463static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100464{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100465 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100466}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200467
Christopher Faulet7a145d62020-08-05 11:31:16 +0200468/* Refresh the h1c task timeout if necessary */
469static void h1_refresh_timeout(struct h1c *h1c)
470{
471 if (h1c->task) {
472 h1c->task->expire = TICK_ETERNITY;
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200473 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
474 /* half-closed connections switch to clientfin/serverfin
475 * timeouts so that we don't hang too long on clients
476 * that have gone away (especially in tunnel mode).
477 */
478 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
479 task_queue(h1c->task);
480 TRACE_DEVEL("refreshing connection's timeout (half-closed)", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet268c92e2020-12-01 11:42:53 +0100481 } else if (b_data(&h1c->obuf)) {
482 /* any connection with pending data, need a timeout (server or client).
Christopher Faulet7a145d62020-08-05 11:31:16 +0200483 */
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200484 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet7a145d62020-08-05 11:31:16 +0200485 ? h1c->shut_timeout
486 : h1c->timeout));
487 task_queue(h1c->task);
488 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200489 } 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 +0100490 /* front connections waiting for a stream need a timeout. client timeout by
491 * default but http-keep-alive if defined
492 */
493 int timeout = h1c->timeout;
494
495 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
496 timeout = tick_first(timeout, h1c->px->timeout.httpka);
497
498 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
499 ? h1c->shut_timeout
500 : timeout));
501 task_queue(h1c->task);
502 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200503 }
504 }
505}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200506/*****************************************************************/
507/* functions below are dedicated to the mux setup and management */
508/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200509
510/* returns non-zero if there are input data pending for stream h1s. */
511static inline size_t h1s_data_pending(const struct h1s *h1s)
512{
513 const struct h1m *h1m;
514
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200515 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200516 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100517 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200518
519 return b_data(&h1s->h1c->ibuf);
520}
521
Christopher Faulet26256f82020-09-14 11:40:13 +0200522static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100523{
524 struct conn_stream *cs;
525
Christopher Faulet6b81df72019-10-01 22:08:43 +0200526 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200527 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200528 if (!cs) {
529 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 +0100530 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200531 }
Christopher Faulet47365272018-10-31 17:40:50 +0100532 h1s->cs = cs;
533 cs->ctx = h1s;
534
535 if (h1s->flags & H1S_F_NOT_FIRST)
536 cs->flags |= CS_FL_NOT_FIRST;
537
Christopher Faulet27182292020-07-03 15:08:49 +0200538 if (global.tune.options & GTUNE_USE_SPLICE) {
539 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100540 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200541 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100542
Christopher Faulet26256f82020-09-14 11:40:13 +0200543 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200544 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 +0100545 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200546 }
Christopher Faulet26256f82020-09-14 11:40:13 +0200547 *input = BUF_NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200548
549 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100550 return cs;
551
552 err:
553 cs_free(cs);
554 h1s->cs = NULL;
555 return NULL;
556}
557
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200558static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200559{
560 struct h1s *h1s;
561
Christopher Faulet6b81df72019-10-01 22:08:43 +0200562 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
563
Christopher Faulet51dbc942018-09-13 09:05:15 +0200564 h1s = pool_alloc(pool_head_h1s);
565 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100566 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200567 h1s->h1c = h1c;
568 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200569 h1s->sess = NULL;
570 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100571 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100572 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200573 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200574
575 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100576 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200577
Christopher Faulet129817b2018-09-20 16:14:40 +0200578 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100579 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200580
581 h1s->status = 0;
582 h1s->meth = HTTP_METH_OTHER;
583
Christopher Faulet47365272018-10-31 17:40:50 +0100584 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
585 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100586 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100587
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200588 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
589 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200590
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200591 fail:
592 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
593 return NULL;
594}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100595
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200596static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
597{
598 struct session *sess = h1c->conn->owner;
599 struct h1s *h1s;
600
601 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
602
603 h1s = h1s_new(h1c);
604 if (!h1s)
605 goto fail;
606
607 h1s->sess = sess;
608
609 if (h1c->px->options2 & PR_O2_REQBUG_OK)
610 h1s->req.err_pos = -1;
611
612 if (!h1s_new_cs(h1s, &BUF_NULL))
613 goto fail_cs;
614
Christopher Faulet6b81df72019-10-01 22:08:43 +0200615 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200616 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100617
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200618 fail_cs:
Christopher Faulet47365272018-10-31 17:40:50 +0100619 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200620 fail:
621 sess_log(sess);
622 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
623 return NULL;
624}
625
626static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
627{
628 struct h1s *h1s;
629
630 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
631
632 h1s = h1s_new(h1c);
633 if (!h1s)
634 goto fail;
635
636 h1s->cs = cs;
637 h1s->sess = sess;
638 cs->ctx = h1s;
639
640
641 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
642 h1s->res.err_pos = -1;
643
644 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
645 return h1s;
646
647 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200648 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 +0100649 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200650}
651
652static void h1s_destroy(struct h1s *h1s)
653{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200654 if (h1s) {
655 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200656
Christopher Faulet6b81df72019-10-01 22:08:43 +0200657 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200658 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200659
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100660 if (h1s->subs)
661 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200662
Christopher Fauletd17ad822020-09-24 15:14:29 +0200663 h1_release_buf(h1c, &h1s->rxbuf);
664
Christopher Faulet089acd52020-09-21 10:57:52 +0200665 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200666 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet47365272018-10-31 17:40:50 +0100667 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200668 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
669 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100670
671 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
672 !(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 +0100673 (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 +0100674 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
675 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
676 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
677 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200678 pool_free(pool_head_h1s, h1s);
679 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200680}
681
682/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200683 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500684 * to the existing conn_stream (for outgoing connections or for incoming ones
685 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200686 * establishment). <input> is always used as Input buffer and may contain
687 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
688 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200689 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200690static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
691 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200692{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200693 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100694 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200695 void *conn_ctx = conn->ctx;
696
697 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200698
699 h1c = pool_alloc(pool_head_h1c);
700 if (!h1c)
701 goto fail_h1c;
702 h1c->conn = conn;
703 h1c->px = proxy;
704
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100705 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200706 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200707 h1c->obuf = BUF_NULL;
708 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200709 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200710
Willy Tarreau21046592020-02-26 10:39:36 +0100711 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200712 h1c->wait_event.tasklet = tasklet_new();
713 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200714 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200715 h1c->wait_event.tasklet->process = h1_io_cb;
716 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100717 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200718
Christopher Faulete9b70722019-04-08 10:46:02 +0200719 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200720 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100721 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
722 if (tick_isset(proxy->timeout.serverfin))
723 h1c->shut_timeout = proxy->timeout.serverfin;
724 } else {
725 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
726 if (tick_isset(proxy->timeout.clientfin))
727 h1c->shut_timeout = proxy->timeout.clientfin;
728 }
729 if (tick_isset(h1c->timeout)) {
730 t = task_new(tid_bit);
731 if (!t)
732 goto fail;
733
734 h1c->task = t;
735 t->process = h1_timeout_task;
736 t->context = h1c;
737 t->expire = tick_add(now_ms, h1c->timeout);
738 }
739
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100740 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200741
Christopher Faulet6b81df72019-10-01 22:08:43 +0200742 /* Always Create a new H1S */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200743 if (!(h1c->flags & H1C_F_IS_BACK)) {
744 if (!h1c_frt_stream_new(h1c))
745 goto fail;
746 }
747 else {
748 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
749 goto fail;
750 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100751
752 if (t)
753 task_queue(t);
754
Christopher Faulet51dbc942018-09-13 09:05:15 +0200755 /* Try to read, if nothing is available yet we'll just subscribe */
Christopher Faulet089acd52020-09-21 10:57:52 +0200756 if (!h1_recv_allowed(h1c))
757 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200758
759 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200760 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200761 return 0;
762
763 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200764 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200765 if (h1c->wait_event.tasklet)
766 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200767 pool_free(pool_head_h1c, h1c);
768 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200769 conn->ctx = conn_ctx; // restore saved context
770 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200771 return -1;
772}
773
Christopher Faulet73c12072019-04-08 11:23:22 +0200774/* release function. This one should be called to free all resources allocated
775 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200776 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200777static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200778{
Christopher Faulet61840e72019-04-15 09:33:32 +0200779 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200780
Christopher Faulet6b81df72019-10-01 22:08:43 +0200781 TRACE_POINT(H1_EV_H1C_END);
782
Christopher Faulet51dbc942018-09-13 09:05:15 +0200783 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200784 /* The connection must be aattached to this mux to be released */
785 if (h1c->conn && h1c->conn->ctx == h1c)
786 conn = h1c->conn;
787
Christopher Faulet6b81df72019-10-01 22:08:43 +0200788 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
789
Christopher Faulet61840e72019-04-15 09:33:32 +0200790 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200791 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200792 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200793 /* Make sure we're no longer subscribed to anything */
794 if (h1c->wait_event.events)
795 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
796 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200797 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200798 /* connection successfully upgraded to H2, this
799 * mux was already released */
800 return;
801 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200802 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200803 sess_log(conn->owner); /* Log if the upgrade failed */
804 }
805
Olivier Houchard45c44372019-06-11 14:06:23 +0200806
Willy Tarreau21046592020-02-26 10:39:36 +0100807 if (MT_LIST_ADDED(&h1c->buf_wait.list))
808 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200809
810 h1_release_buf(h1c, &h1c->ibuf);
811 h1_release_buf(h1c, &h1c->obuf);
812
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100813 if (h1c->task) {
814 h1c->task->context = NULL;
815 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
816 h1c->task = NULL;
817 }
818
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200819 if (h1c->wait_event.tasklet)
820 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200821
Christopher Fauletf2824e62018-10-01 12:12:37 +0200822 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200823 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100824 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200825 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200826 pool_free(pool_head_h1c, h1c);
827 }
828
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200829 if (conn) {
830 conn->mux = NULL;
831 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200832 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200833
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200834 conn_stop_tracking(conn);
835 conn_full_close(conn);
836 if (conn->destroy_cb)
837 conn->destroy_cb(conn);
838 conn_free(conn);
839 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200840}
841
842/******************************************************/
843/* functions below are for the H1 protocol processing */
844/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200845/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
846 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200847 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100848static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200849{
Christopher Faulet570d1612018-11-26 11:13:57 +0100850 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200851
Christopher Faulet570d1612018-11-26 11:13:57 +0100852 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200853 (*(p + 5) > '1' ||
854 (*(p + 5) == '1' && *(p + 7) >= '1')))
855 h1m->flags |= H1_MF_VER_11;
856}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200857
Christopher Faulet9768c262018-10-22 09:34:31 +0200858/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
859 * greater or equal to 1.1
860 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100861static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200862{
Christopher Faulet570d1612018-11-26 11:13:57 +0100863 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200864
Christopher Faulet570d1612018-11-26 11:13:57 +0100865 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200866 (*(p + 5) > '1' ||
867 (*(p + 5) == '1' && *(p + 7) >= '1')))
868 h1m->flags |= H1_MF_VER_11;
869}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200870
Christopher Fauletf2824e62018-10-01 12:12:37 +0200871/* Deduce the connection mode of the client connection, depending on the
872 * configuration and the H1 message flags. This function is called twice, the
873 * first time when the request is parsed and the second time when the response
874 * is parsed.
875 */
876static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
877{
878 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200879
880 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100881 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200882 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100883 h1s->status == 101) {
884 /* Either we've established an explicit tunnel, or we're
885 * switching the protocol. In both cases, we're very unlikely to
886 * understand the next protocols. We have to switch to tunnel
887 * mode, so that we transfer the request and responses then let
888 * this protocol pass unmodified. When we later implement
889 * specific parsers for such protocols, we'll want to check the
890 * Upgrade header which contains information about that protocol
891 * for responses with status 101 (eg: see RFC2817 about TLS).
892 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200893 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200894 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 +0100895 }
896 else if (h1s->flags & H1S_F_WANT_KAL) {
897 /* By default the client is in KAL mode. CLOSE mode mean
898 * it is imposed by the client itself. So only change
899 * KAL mode here. */
900 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
901 /* no length known or explicit close => close */
902 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200903 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 +0100904 }
905 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
906 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500907 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100908 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200909 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 +0100910 }
911 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200912 }
913 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100914 /* Input direction: first pass */
915 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
916 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200917 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200918 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 +0100919 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920 }
921
922 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200923 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200924 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200925 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);
926 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200927}
928
929/* Deduce the connection mode of the client connection, depending on the
930 * configuration and the H1 message flags. This function is called twice, the
931 * first time when the request is parsed and the second time when the response
932 * is parsed.
933 */
934static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
935{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100936 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100937 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100938 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200939
Christopher Fauletf2824e62018-10-01 12:12:37 +0200940 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100941 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200942 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100943 h1s->status == 101) {
944 /* Either we've established an explicit tunnel, or we're
945 * switching the protocol. In both cases, we're very unlikely to
946 * understand the next protocols. We have to switch to tunnel
947 * mode, so that we transfer the request and responses then let
948 * this protocol pass unmodified. When we later implement
949 * specific parsers for such protocols, we'll want to check the
950 * Upgrade header which contains information about that protocol
951 * for responses with status 101 (eg: see RFC2817 about TLS).
952 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200953 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200954 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 +0100955 }
956 else if (h1s->flags & H1S_F_WANT_KAL) {
957 /* By default the server is in KAL mode. CLOSE mode mean
958 * it is imposed by haproxy itself. So only change KAL
959 * mode here. */
960 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
961 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
962 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
963 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200964 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 +0100965 }
966 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200967 }
Christopher Faulet70033782018-12-05 13:50:11 +0100968 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100969 /* Output direction: first pass */
970 if (h1m->flags & H1_MF_CONN_CLO) {
971 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100972 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200973 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 +0100974 }
975 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
976 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
977 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
978 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
979 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
980 /* no explicit keep-alive option httpclose/server-close => close */
981 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200982 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 +0100983 }
Christopher Faulet70033782018-12-05 13:50:11 +0100984 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200985
986 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200987 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200988 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200989 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);
990 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200991}
992
Christopher Fauletb992af02019-03-28 15:42:24 +0100993static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200994{
995 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200996
997 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
998 * token is found
999 */
1000 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001001 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001002
1003 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001004 if (!(h1m->flags & H1_MF_VER_11)) {
1005 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 +01001006 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001007 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001008 }
1009 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001010 if (h1m->flags & H1_MF_VER_11) {
1011 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001012 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001013 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001015}
1016
Christopher Fauletb992af02019-03-28 15:42:24 +01001017static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001019 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1020 * token is found
1021 */
1022 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001023 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001024
1025 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001026 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001027 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1028 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 +01001029 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001030 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001031 }
1032 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001033 if (h1m->flags & H1_MF_VER_11) {
1034 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001035 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001036 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001038}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001039
Christopher Fauletb992af02019-03-28 15:42:24 +01001040static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001041{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001042 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001043 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001044 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001045 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001046}
1047
Christopher Fauletb992af02019-03-28 15:42:24 +01001048static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1049{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001050 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001051 h1_set_cli_conn_mode(h1s, h1m);
1052 else
1053 h1_set_srv_conn_mode(h1s, h1m);
1054
1055 if (!(h1m->flags & H1_MF_RESP))
1056 h1_update_req_conn_value(h1s, h1m, conn_val);
1057 else
1058 h1_update_res_conn_value(h1s, h1m, conn_val);
1059}
Christopher Faulete44769b2018-11-29 23:01:45 +01001060
Christopher Faulet98fbe952019-07-22 16:18:24 +02001061/* Try to adjust the case of the message header name using the global map
1062 * <hdrs_map>.
1063 */
1064static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1065{
1066 struct ebpt_node *node;
1067 struct h1_hdr_entry *entry;
1068
1069 /* No entry in the map, do nothing */
1070 if (eb_is_empty(&hdrs_map.map))
1071 return;
1072
1073 /* No conversion fo the request headers */
1074 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1075 return;
1076
1077 /* No conversion fo the response headers */
1078 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1079 return;
1080
1081 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1082 if (!node)
1083 return;
1084 entry = container_of(node, struct h1_hdr_entry, node);
1085 name->ptr = entry->name.ptr;
1086 name->len = entry->name.len;
1087}
1088
Christopher Faulete44769b2018-11-29 23:01:45 +01001089/* Append the description of what is present in error snapshot <es> into <out>.
1090 * The description must be small enough to always fit in a buffer. The output
1091 * buffer may be the trash so the trash must not be used inside this function.
1092 */
1093static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1094{
1095 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001096 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1097 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1098 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1099 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1100 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1101 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001102}
1103/*
1104 * Capture a bad request or response and archive it in the proxy's structure.
1105 * By default it tries to report the error position as h1m->err_pos. However if
1106 * this one is not set, it will then report h1m->next, which is the last known
1107 * parsing point. The function is able to deal with wrapping buffers. It always
1108 * displays buffers as a contiguous area starting at buf->p. The direction is
1109 * determined thanks to the h1m's flags.
1110 */
1111static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1112 struct h1m *h1m, struct buffer *buf)
1113{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001114 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001115 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001116 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001117 union error_snapshot_ctx ctx;
1118
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001119 if (h1s->cs && h1s->cs->data) {
1120 if (sess == NULL)
1121 sess = si_strm(h1s->cs->data)->sess;
1122 if (!(h1m->flags & H1_MF_RESP))
1123 other_end = si_strm(h1s->cs->data)->be;
1124 else
1125 other_end = sess->fe;
1126 } else
1127 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001128
1129 /* http-specific part now */
1130 ctx.h1.state = h1m->state;
1131 ctx.h1.c_flags = h1c->flags;
1132 ctx.h1.s_flags = h1s->flags;
1133 ctx.h1.m_flags = h1m->flags;
1134 ctx.h1.m_clen = h1m->curr_len;
1135 ctx.h1.m_blen = h1m->body_len;
1136
1137 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1138 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001139 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1140 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001141}
1142
Christopher Fauletadb22202018-12-12 10:32:09 +01001143/* Emit the chunksize followed by a CRLF in front of data of the buffer
1144 * <buf>. It goes backwards and starts with the byte before the buffer's
1145 * head. The caller is responsible for ensuring there is enough room left before
1146 * the buffer's head for the string.
1147 */
1148static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1149{
1150 char *beg, *end;
1151
1152 beg = end = b_head(buf);
1153 *--beg = '\n';
1154 *--beg = '\r';
1155 do {
1156 *--beg = hextab[chksz & 0xF];
1157 } while (chksz >>= 4);
1158 buf->head -= (end - beg);
1159 b_add(buf, end - beg);
1160}
1161
1162/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1163 * ensuring there is enough room left in the buffer for the string. */
1164static void h1_emit_chunk_crlf(struct buffer *buf)
1165{
1166 *(b_peek(buf, b_data(buf))) = '\r';
1167 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1168 b_add(buf, 2);
1169}
1170
Christopher Faulet129817b2018-09-20 16:14:40 +02001171/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001172 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001173 * CONNECT requests. On the client side, if the response is not finished, the
1174 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001175 */
1176static void h1_set_req_tunnel_mode(struct h1s *h1s)
1177{
1178 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1179 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001180 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1181
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001182 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001183 h1s->flags &= ~H1S_F_PARSING_DONE;
1184 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001185 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1186 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 +01001187 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001188 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001189 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1190 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001191 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001192 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 +01001193 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001194}
1195
1196/*
1197 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001198 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001199 * last case, this function takes care to switch the request to tunnel mode if
1200 * possible. On the server side, if the request is not finished, the mux is mark
1201 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001202 */
1203static void h1_set_res_tunnel_mode(struct h1s *h1s)
1204{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001205
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001206 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1207 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001208 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1209
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001210 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001211 h1s->flags &= ~H1S_F_PARSING_DONE;
1212 /* On protocol switching, switch the request to tunnel mode if it is in
1213 * DONE state. Otherwise we will wait the end of the request to switch
1214 * it in tunnel mode.
1215 */
1216 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001217 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1218 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 +01001219 }
1220 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1221 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1222 h1s->req.state = H1_MSG_TUNNEL;
1223 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1224 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001225 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001226 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1227 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001228 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001229 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 +01001230 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001231}
1232
1233/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001234 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001235 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001236 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001237 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001238static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001239 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001240{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001241 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001242 int ret = 0;
1243
Willy Tarreau022e5e52020-09-10 09:33:15 +02001244 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 +02001245
Christopher Faulet89aed322020-06-02 17:33:56 +02001246 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1247 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1248 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001249 /* Try to match H2 preface before parsing the request headers. */
1250 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001251 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001252 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001253 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001254 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001255 else {
1256 if (h1s->meth == HTTP_METH_CONNECT)
1257 h1m->flags |= H1_MF_METH_CONNECT;
1258 if (h1s->meth == HTTP_METH_HEAD)
1259 h1m->flags |= H1_MF_METH_HEAD;
1260 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001261
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001262 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1263 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001264 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 +02001265 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001266 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001267 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001268 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 +02001269 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1270 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001271 goto end;
1272 }
1273
Christopher Faulet486498c2019-10-11 14:22:00 +02001274 if (h1m->err_pos >= 0) {
1275 /* Maybe we found an error during the parsing while we were
1276 * configured not to block on that, so we have to capture it
1277 * now.
1278 */
1279 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1280 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1281 }
1282
Christopher Faulet129817b2018-09-20 16:14:40 +02001283 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001284 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001285 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001286 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001287 }
1288 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001289 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001290 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001291 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001292 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001293 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001295
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001297 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 +02001298 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001299
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001300 h2c_upgrade:
1301 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001302 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001303 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001304 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1305 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001306}
1307
1308/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001309 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001310 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1311 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001312 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001313static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001314 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001315 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001316{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001317 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001318
Willy Tarreau022e5e52020-09-10 09:33:15 +02001319 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 +02001320 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001321 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001322 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 +01001323 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001324 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001325 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001326 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 +02001327 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001329 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001330 }
1331
Christopher Faulet2eaf3092020-07-03 14:51:15 +02001332 if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
Christopher Faulet27182292020-07-03 15:08:49 +02001333 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1334 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001335 h1s->cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001336 }
1337 else if (h1s->cs) {
1338 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001339 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001340 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001341
Christopher Faulet02a02532019-11-15 09:36:28 +01001342 *ofs += ret;
1343
1344 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001345 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 +02001346 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001347}
1348
1349/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001350 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1351 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1352 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1353 * responsible to update the parser state <h1m>.
1354 */
1355static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1356 struct buffer *buf, size_t *ofs, size_t max)
1357{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001358 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001359
Willy Tarreau022e5e52020-09-10 09:33:15 +02001360 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 +02001361 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001362 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001363 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 +01001364 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001365 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001366 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001367 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 +02001368 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1369 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001370 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001371 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001372
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001373 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001374
1375 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001376 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 +02001377 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001378}
1379
1380/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001381 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1382 * proceed. This functions is responsible to update the parser state <h1m>. It
1383 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001384 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001385static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1386 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001387{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001388 int ret;
1389
Willy Tarreau022e5e52020-09-10 09:33:15 +02001390 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 +01001391 ret = h1_parse_msg_eom(h1m, htx, max);
1392 if (!ret) {
1393 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1394 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001395 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001396 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001397 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 +01001398 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1399 }
1400 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001401 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001402
Christopher Faulet76014fd2019-12-10 11:47:22 +01001403 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001404 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001405 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001406 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 +01001407 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001408}
1409
1410/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001411 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001412 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1413 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001414 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001415static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001416{
Christopher Faulet539e0292018-11-19 10:40:09 +01001417 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001418 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001419 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001420 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001421 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001422
Christopher Faulet539e0292018-11-19 10:40:09 +01001423 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001424 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001425
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001426 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001427
Christopher Faulet74027762019-02-26 14:45:05 +01001428 data = htx->data;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001429 if (h1s->flags & H1S_F_PARSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001430 goto end;
1431
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001432 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001433 size_t used = htx_used_space(htx);
1434
Christopher Faulet129817b2018-09-20 16:14:40 +02001435 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001436 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001437 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001438 if (!ret)
1439 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001440
1441 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1442 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1443
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001444 if ((h1m->flags & H1_MF_RESP) &&
1445 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1446 h1m_init_res(&h1s->res);
1447 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001448 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001449 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001450 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001451 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001452 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001453 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001454 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001456
1457 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1458 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001459 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001460 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001461 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1462 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1463 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001464 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001465
Christopher Faulet76014fd2019-12-10 11:47:22 +01001466 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1467 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001468 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001469 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001470 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1471 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1472 break;
1473
1474 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1475 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1476 }
1477
Christopher Fauletf3158e92019-11-15 11:14:23 +01001478 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1479 h1_set_req_tunnel_mode(h1s);
1480 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001481 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1482 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 +02001483 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001484 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001485 else
1486 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001487 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001488 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001489 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001490 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001491 if (!ret)
1492 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001493
1494 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1495 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001496 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001497 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001498 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001499 break;
1500 }
1501
Christopher Faulet30db3d72019-05-17 15:35:33 +02001502 count -= htx_used_space(htx) - used;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001503 } while (!(h1s->flags & H1S_F_PARSING_ERROR));
Christopher Faulet129817b2018-09-20 16:14:40 +02001504
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001505 if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001506 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001507 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001508 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001509
Christopher Faulet539e0292018-11-19 10:40:09 +01001510 b_del(&h1c->ibuf, total);
1511
1512 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001513 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001514 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001515 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001516 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001517 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 +01001518 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001519 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001520
Christopher Fauletcf56b992018-12-11 16:12:31 +01001521 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1522
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001523 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001524 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001525 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001526 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001527 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001528 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001529 if (h1m->state == H1_MSG_TUNNEL)
1530 h1s->cs->flags |= CS_FL_EOI;
1531 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001532 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001533 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001534
Christopher Faulet6b81df72019-10-01 22:08:43 +02001535 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001536 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001537
1538 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001539 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001540 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001541 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001542 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001543}
1544
Christopher Faulet129817b2018-09-20 16:14:40 +02001545/*
1546 * Process outgoing data. It parses data and transfer them from the channel buffer into
1547 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1548 * 0 if it couldn't proceed.
1549 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001550static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1551{
Christopher Faulet129817b2018-09-20 16:14:40 +02001552 struct h1s *h1s = h1c->h1s;
1553 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001554 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001556 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001557 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001558
Christopher Faulet47365272018-10-31 17:40:50 +01001559 if (!count)
1560 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001561
Christopher Faulet94b2c762019-05-24 15:28:57 +02001562 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001563 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1564
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001565 if (htx_is_empty(chn_htx))
1566 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001567
Christopher Faulet51dbc942018-09-13 09:05:15 +02001568 if (!h1_get_buf(h1c, &h1c->obuf)) {
1569 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001570 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 +02001571 goto end;
1572 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001573
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001574 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001575
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001576 if (h1s->flags & H1S_F_PROCESSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001577 goto end;
1578
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001579 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001580 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001581
Willy Tarreau3815b222018-12-11 19:50:43 +01001582 /* Perform some optimizations to reduce the number of buffer copies.
1583 * First, if the mux's buffer is empty and the htx area contains
1584 * exactly one data block of the same size as the requested count,
1585 * then it's possible to simply swap the caller's buffer with the
1586 * mux's output buffer and adjust offsets and length to match the
1587 * entire DATA HTX block in the middle. In this case we perform a
1588 * true zero-copy operation from end-to-end. This is the situation
1589 * that happens all the time with large files. Second, if this is not
1590 * possible, but the mux's output buffer is empty, we still have an
1591 * opportunity to avoid the copy to the intermediary buffer, by making
1592 * the intermediary buffer's area point to the output buffer's area.
1593 * In this case we want to skip the HTX header to make sure that copies
1594 * remain aligned and that this operation remains possible all the
1595 * time. This goes for headers, data blocks and any data extracted from
1596 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001597 */
1598 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001599 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001600 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001601 htx_get_blk_value(chn_htx, blk).len == count) {
1602 void *old_area = h1c->obuf.area;
1603
Christopher Faulet6b81df72019-10-01 22:08:43 +02001604 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 +01001605 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001606 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001607 h1c->obuf.data = count;
1608
1609 buf->area = old_area;
1610 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001611
Christopher Faulet6b81df72019-10-01 22:08:43 +02001612 chn_htx = (struct htx *)buf->area;
1613 htx_reset(chn_htx);
1614
Christopher Fauletadb22202018-12-12 10:32:09 +01001615 /* The message is chunked. We need to emit the chunk
1616 * size. We have at least the size of the struct htx to
1617 * write the chunk envelope. It should be enough.
1618 */
1619 if (h1m->flags & H1_MF_CHNK) {
1620 h1_emit_chunk_size(&h1c->obuf, count);
1621 h1_emit_chunk_crlf(&h1c->obuf);
1622 }
1623
Willy Tarreau3815b222018-12-11 19:50:43 +01001624 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001625 if (h1m->state == H1_MSG_DATA)
1626 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001627 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001628 else
1629 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001630 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001631 goto out;
1632 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001633 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001634 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001635 else
1636 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001637
Christopher Fauletc2518a52019-06-25 21:41:02 +02001638 tmp.data = 0;
1639 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001640 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001641 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001643 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001645 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001646
Christopher Fauletb2e84162018-12-06 11:39:49 +01001647 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001648 if (type != HTX_BLK_DATA && vlen > count)
1649 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001650
Christopher Faulet94b2c762019-05-24 15:28:57 +02001651 if (type == HTX_BLK_UNUSED)
1652 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001653
Christopher Faulet94b2c762019-05-24 15:28:57 +02001654 switch (h1m->state) {
1655 case H1_MSG_RQBEFORE:
1656 if (type != HTX_BLK_REQ_SL)
1657 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001658 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 +02001659 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001660 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001662 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001663 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001664 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001665 if (sl->flags & HTX_SL_F_BODYLESS)
1666 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001667 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001668 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1669 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1670 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1671 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1672 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001673 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001674
Christopher Faulet94b2c762019-05-24 15:28:57 +02001675 case H1_MSG_RPBEFORE:
1676 if (type != HTX_BLK_RES_SL)
1677 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001678 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 +02001679 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001680 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001682 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001683 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001684 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001686 if (sl->info.res.status < 200 &&
1687 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001688 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 h1m->state = H1_MSG_HDR_FIRST;
1690 break;
1691
Christopher Faulet94b2c762019-05-24 15:28:57 +02001692 case H1_MSG_HDR_FIRST:
1693 case H1_MSG_HDR_NAME:
1694 case H1_MSG_HDR_L2_LWS:
1695 if (type == HTX_BLK_EOH)
1696 goto last_lf;
1697 if (type != HTX_BLK_HDR)
1698 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001699
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 h1m->state = H1_MSG_HDR_NAME;
1701 n = htx_get_blk_name(chn_htx, blk);
1702 v = htx_get_blk_value(chn_htx, blk);
1703
Christopher Faulet86d144c2019-08-14 16:32:25 +02001704 /* Skip all pseudo-headers */
1705 if (*(n.ptr) == ':')
1706 goto skip_hdr;
1707
Christopher Fauletb045bb22020-02-28 10:42:20 +01001708 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001709 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001710 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001711 /* Only skip C-L header with invalid value. */
1712 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001713 goto skip_hdr;
1714 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001715 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001716 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 if (!v.len)
1718 goto skip_hdr;
1719 }
1720
Christopher Faulet67d58092019-10-02 10:51:38 +02001721 /* Skip header if same name is used to add the server name */
1722 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1723 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1724 goto skip_hdr;
1725
Christopher Faulet98fbe952019-07-22 16:18:24 +02001726 /* Try to adjust the case of the header name */
1727 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1728 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001729 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001730 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001731 skip_hdr:
1732 h1m->state = H1_MSG_HDR_L2_LWS;
1733 break;
1734
Christopher Faulet94b2c762019-05-24 15:28:57 +02001735 case H1_MSG_LAST_LF:
1736 if (type != HTX_BLK_EOH)
1737 goto error;
1738 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001739 h1m->state = H1_MSG_LAST_LF;
1740 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001741 /* If the reply comes from haproxy while the request is
1742 * not finished, we force the connection close. */
1743 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1744 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1745 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1746 }
1747
1748 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001749 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001750 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001751 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001752 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001753 /* Try to adjust the case of the header name */
1754 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1755 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001756 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001757 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001758 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001759 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001760 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001761
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001762 if ((h1s->meth != HTTP_METH_CONNECT &&
1763 (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 +01001764 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1765 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1766 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1767 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1768 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001769 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001770 n = ist("transfer-encoding");
1771 v = ist("chunked");
1772 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1773 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001774 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001775 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001776 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001777 h1m->flags |= H1_MF_CHNK;
1778 }
1779
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001780 /* Now add the server name to a header (if requested) */
1781 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1782 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1783 struct server *srv = objt_server(h1c->conn->target);
1784
1785 if (srv) {
1786 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1787 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001788
1789 /* Try to adjust the case of the header name */
1790 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1791 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001792 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001793 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001794 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001795 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001796 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1797 }
1798
Christopher Fauletc2518a52019-06-25 21:41:02 +02001799 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001800 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001801
Christopher Faulet6b81df72019-10-01 22:08:43 +02001802 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1803 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1804
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001805 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1806 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1807 h1_set_req_tunnel_mode(h1s);
1808 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001809 else if ((h1m->flags & H1_MF_RESP) &&
1810 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001811 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001812 * to the client. Switch the response to tunnel mode.
1813 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001814 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001815 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 +01001816 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001817 else if ((h1m->flags & H1_MF_RESP) &&
1818 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1819 h1m_init_res(&h1s->res);
1820 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001821 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001822 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001823 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001824 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001825 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001826 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1827 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001828 else
1829 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001830 break;
1831
Christopher Faulet94b2c762019-05-24 15:28:57 +02001832 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001833 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001834 if (type == HTX_BLK_EOM) {
1835 /* Chunked message without explicit trailers */
1836 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001837 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001838 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001839 }
1840 goto done;
1841 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001842 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001843 /* If the message is not chunked, never
1844 * add the last chunk. */
1845 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001846 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001847 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 +02001848 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001849 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001850 else if (type != HTX_BLK_DATA)
1851 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001852
1853 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 +02001854
1855
1856 if (vlen > count) {
1857 /* Get the maximum amount of data we can xferred */
1858 vlen = count;
1859 }
1860
1861 chklen = 0;
1862 if (h1m->flags & H1_MF_CHNK) {
1863 chklen = b_room(&tmp);
1864 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1865 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1866 (chklen < 1048576) ? 5 : 8);
1867 chklen += 4; /* 2 x CRLF */
1868 }
1869
1870 if (vlen + chklen > b_room(&tmp)) {
1871 /* too large for the buffer */
1872 if (chklen >= b_room(&tmp))
1873 goto full;
1874 vlen = b_room(&tmp) - chklen;
1875 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001876 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001877 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001878 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001879 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001880
1881 if (h1m->state == H1_MSG_DATA)
1882 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001883 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001884 else
1885 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001886 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001887 break;
1888
Christopher Faulet94b2c762019-05-24 15:28:57 +02001889 case H1_MSG_TRAILERS:
1890 if (type == HTX_BLK_EOM)
1891 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001892 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001893 goto error;
1894 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001895 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001896 /* If the message is not chunked, ignore
1897 * trailers. It may happen with H2 messages. */
1898 if (!(h1m->flags & H1_MF_CHNK))
1899 break;
1900
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001901 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001902 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001903 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1905 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001906 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001907 else { // HTX_BLK_TLR
1908 n = htx_get_blk_name(chn_htx, blk);
1909 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001910
1911 /* Try to adjust the case of the header name */
1912 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1913 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001914 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001915 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001916 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001917 break;
1918
Christopher Faulet94b2c762019-05-24 15:28:57 +02001919 case H1_MSG_DONE:
1920 if (type != HTX_BLK_EOM)
1921 goto error;
1922 done:
1923 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001924 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1925 h1_set_req_tunnel_mode(h1s);
1926 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1927 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001928 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1929 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001930 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02001931 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 +02001932 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001933
1934 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1935 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001936 break;
1937
Christopher Faulet9768c262018-10-22 09:34:31 +02001938 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001939 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001940 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001941 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001942 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001943 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001944 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001945 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1946 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001947 break;
1948 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001949
1950 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001951 total += vlen;
1952 count -= vlen;
1953 if (sz == vlen)
1954 blk = htx_remove_blk(chn_htx, blk);
1955 else {
1956 htx_cut_data_blk(chn_htx, blk, vlen);
1957 break;
1958 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001959 }
1960
Christopher Faulet9768c262018-10-22 09:34:31 +02001961 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01001962 /* when the output buffer is empty, tmp shares the same area so that we
1963 * only have to update pointers and lengths.
1964 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02001965 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
1966 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001967 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02001968 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001969
Willy Tarreau3815b222018-12-11 19:50:43 +01001970 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001971 out:
1972 if (!buf_room_for_htx_data(&h1c->obuf)) {
1973 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001974 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001976 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001977 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02001978 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02001979
1980 full:
1981 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1982 h1c->flags |= H1C_F_OUT_FULL;
1983 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001984}
1985
Christopher Faulet51dbc942018-09-13 09:05:15 +02001986/*********************************************************/
1987/* functions below are I/O callbacks from the connection */
1988/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001989static void h1_wake_stream_for_recv(struct h1s *h1s)
1990{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001991 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001992 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01001993 tasklet_wakeup(h1s->subs->tasklet);
1994 h1s->subs->events &= ~SUB_RETRY_RECV;
1995 if (!h1s->subs->events)
1996 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01001997 }
1998}
1999static void h1_wake_stream_for_send(struct h1s *h1s)
2000{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002001 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002002 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002003 tasklet_wakeup(h1s->subs->tasklet);
2004 h1s->subs->events &= ~SUB_RETRY_SEND;
2005 if (!h1s->subs->events)
2006 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002007 }
2008}
2009
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010/*
2011 * Attempt to read data, and subscribe if none available
2012 */
2013static int h1_recv(struct h1c *h1c)
2014{
2015 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002016 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002017 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002018 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002019 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002020
Christopher Faulet6b81df72019-10-01 22:08:43 +02002021 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2022
2023 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2024 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002025 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002026 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002027
Christopher Fauletae635762020-09-21 11:47:16 +02002028 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2029 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002030 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002031 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002032 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002033
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002034 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2035 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002036 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002037 goto end;
2038 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002039
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002040 /*
2041 * If we only have a small amount of data, realign it,
2042 * it's probably cheaper than doing 2 recv() calls.
2043 */
2044 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2045 b_slow_realign(&h1c->ibuf, trash.area, 0);
2046
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002047 /* avoid useless reads after first responses */
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002048 if (h1s && ((!(h1c->flags & H1C_F_IS_BACK) && h1s->req.state == H1_MSG_RQBEFORE) ||
2049 ((h1c->flags & H1C_F_IS_BACK) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002050 flags |= CO_RFL_READ_ONCE;
2051
Willy Tarreau45f2b892018-12-05 07:59:27 +01002052 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002053 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002054 if (h1c->flags & H1C_F_IN_FULL) {
2055 h1c->flags &= ~H1C_F_IN_FULL;
2056 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2057 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002058
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002059 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002060 if (!b_data(&h1c->ibuf)) {
2061 /* try to pre-align the buffer like the rxbufs will be
2062 * to optimize memory copies.
2063 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002064 h1c->ibuf.head = sizeof(struct htx);
2065 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002066 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067 }
Christopher Faulet47365272018-10-31 17:40:50 +01002068 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002069 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002070 rcvd = 1;
Christopher Faulet3b536a32020-09-30 14:06:23 +02002071 if (h1s && h1s->cs)
Christopher Faulet37e36072018-12-04 15:54:12 +01002072 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Faulet47365272018-10-31 17:40:50 +01002073 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002074
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002075 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002076 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002077 goto end;
2078 }
2079
Christopher Faulet6b81df72019-10-01 22:08:43 +02002080 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002081 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002082
Christopher Faulet9768c262018-10-22 09:34:31 +02002083 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002084 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2085 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002086
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002087 if (conn_xprt_read0_pending(conn) && h1s) {
2088 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002089 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002090 rcvd = 1;
2091 }
2092
Christopher Faulet51dbc942018-09-13 09:05:15 +02002093 if (!b_data(&h1c->ibuf))
2094 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002095 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002096 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002097 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2098 }
2099
2100 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002101 return rcvd;
2102}
2103
2104
2105/*
2106 * Try to send data if possible
2107 */
2108static int h1_send(struct h1c *h1c)
2109{
2110 struct connection *conn = h1c->conn;
2111 unsigned int flags = 0;
2112 size_t ret;
2113 int sent = 0;
2114
Christopher Faulet6b81df72019-10-01 22:08:43 +02002115 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2116
2117 if (conn->flags & CO_FL_ERROR) {
2118 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002119 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002120 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002121
2122 if (!b_data(&h1c->obuf))
2123 goto end;
2124
Christopher Faulet46230362019-10-17 16:04:20 +02002125 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002126 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002127 if (h1c->flags & H1C_F_CO_STREAMER)
2128 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002129
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002130 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002132 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002133 if (h1c->flags & H1C_F_OUT_FULL) {
2134 h1c->flags &= ~H1C_F_OUT_FULL;
2135 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2136 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002137 b_del(&h1c->obuf, ret);
2138 sent = 1;
2139 }
2140
Christopher Faulet145aa472018-12-06 10:56:20 +01002141 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002142 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002143 /* error or output closed, nothing to send, clear the buffer to release it */
2144 b_reset(&h1c->obuf);
2145 }
2146
Christopher Faulet51dbc942018-09-13 09:05:15 +02002147 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002148 if (!(h1c->flags & H1C_F_OUT_FULL))
2149 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002150
Christopher Faulet51dbc942018-09-13 09:05:15 +02002151 /* We're done, no more to send */
2152 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002153 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002154 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002155 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2156 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002157 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002158 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002159 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002160 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2161 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002162 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002163 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002164
Christopher Faulet6b81df72019-10-01 22:08:43 +02002165 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002166 return sent;
2167}
2168
Christopher Faulet51dbc942018-09-13 09:05:15 +02002169
2170/* callback called on any event by the connection handler.
2171 * It applies changes and returns zero, or < 0 if it wants immediate
2172 * destruction of the connection.
2173 */
2174static int h1_process(struct h1c * h1c)
2175{
2176 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002177 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178
Christopher Faulet6b81df72019-10-01 22:08:43 +02002179 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2180
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002181 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002182 return -1;
2183
Christopher Fauletfeb11742018-11-29 15:12:34 +01002184 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002185 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002186 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002187 goto release;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002188 if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002189 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002190 if (!h1c_frt_stream_new(h1c))
Christopher Faulet539e0292018-11-19 10:40:09 +01002191 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002192 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002193 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002194 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002195 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196 }
2197
Christopher Fauletae635762020-09-21 11:47:16 +02002198 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2199 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2200 h1_wake_stream_for_recv(h1s);
2201 }
2202
Christopher Faulet4e741552020-09-30 13:47:09 +02002203 if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1)
2204 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002205
Christopher Faulet6b81df72019-10-01 22:08:43 +02002206 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002207 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002208 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2209 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002210
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002211 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002212 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002213 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002214 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002215 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002216 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002217 h1s->cs->data_cb->wake(h1s->cs);
2218 }
Christopher Faulet47365272018-10-31 17:40:50 +01002219 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002220 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002221 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002222 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002223
2224 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002225 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002226 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002227 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002228}
2229
2230static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2231{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002232 struct connection *conn;
2233 struct tasklet *tl = (struct tasklet *)t;
2234 int conn_in_list;
2235 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002236 int ret = 0;
2237
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002238
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002239 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002240 if (tl->context == NULL) {
2241 /* The connection has been taken over by another thread,
2242 * we're no longer responsible for it, so just free the
2243 * tasklet, and do nothing.
2244 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002245 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002246 tasklet_free(tl);
2247 return NULL;
2248 }
2249 h1c = ctx;
2250 conn = h1c->conn;
2251
2252 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2253
2254 /* Remove the connection from the list, to be sure nobody attempts
2255 * to use it while we handle the I/O events
2256 */
2257 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2258 if (conn_in_list)
2259 MT_LIST_DEL(&conn->list);
2260
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002261 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002262
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002263 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002264 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002265 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002266 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002267 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002268 ret = h1_process(h1c);
2269 /* If we were in an idle list, we want to add it back into it,
2270 * unless h1_process() returned -1, which mean it has destroyed
2271 * the connection (testing !ret is enough, if h1_process() wasn't
2272 * called then ret will be 0 anyway.
2273 */
2274 if (!ret && conn_in_list) {
2275 struct server *srv = objt_server(conn->target);
2276
2277 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002278 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002279 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002280 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002281 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002282 return NULL;
2283}
2284
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002285static void h1_reset(struct connection *conn)
2286{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002287
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002288}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002289
2290static int h1_wake(struct connection *conn)
2291{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002292 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002293 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294
Christopher Faulet6b81df72019-10-01 22:08:43 +02002295 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2296
Christopher Faulet539e0292018-11-19 10:40:09 +01002297 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002298 ret = h1_process(h1c);
2299 if (ret == 0) {
2300 struct h1s *h1s = h1c->h1s;
2301
Christopher Faulet6b81df72019-10-01 22:08:43 +02002302 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2303 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002304 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002305 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002306 }
2307 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002308}
2309
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002310/* Connection timeout management. The principle is that if there's no receipt
2311 * nor sending for a certain amount of time, the connection is closed.
2312 */
2313static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2314{
2315 struct h1c *h1c = context;
2316 int expired = tick_is_expired(t->expire, now_ms);
2317
Christopher Faulet6b81df72019-10-01 22:08:43 +02002318 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2319
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002320 if (h1c) {
2321 if (!expired) {
2322 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
2323 return t;
2324 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002325
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002326 /* We're about to destroy the connection, so make sure nobody attempts
2327 * to steal it from us.
2328 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002329 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002330
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002331 /* Somebody already stole the connection from us, so we should not
2332 * free it, we just have to free the task.
2333 */
2334 if (!t->context)
2335 h1c = NULL;
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002336 else if (h1c->conn->flags & CO_FL_LIST_MASK)
2337 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002338
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002339 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002340 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002341
Olivier Houchard3f795f72019-04-17 22:51:06 +02002342 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002343
2344 if (!h1c) {
2345 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002346 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002347 return NULL;
2348 }
2349
2350 h1c->task = NULL;
2351 /* If a stream is still attached to the mux, just set an error and wait
2352 * for the stream's timeout. Otherwise, release the mux. This is only ok
2353 * because same timeouts are used.
2354 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002355 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002356 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002357 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2358 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002359 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002360 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002361
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002362 return NULL;
2363}
2364
Christopher Faulet51dbc942018-09-13 09:05:15 +02002365/*******************************************/
2366/* functions below are used by the streams */
2367/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002368
Christopher Faulet51dbc942018-09-13 09:05:15 +02002369/*
2370 * Attach a new stream to a connection
2371 * (Used for outgoing connections)
2372 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002373static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002374{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002375 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002376 struct conn_stream *cs = NULL;
2377 struct h1s *h1s;
2378
Christopher Faulet6b81df72019-10-01 22:08:43 +02002379 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2380 if (h1c->flags & H1C_F_CS_ERROR) {
2381 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 +02002382 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002383 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002384
Christopher Faulet236c93b2020-07-02 09:19:54 +02002385 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002386 if (!cs) {
2387 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 +02002388 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002389 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002390
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002391 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002392 if (h1s == NULL) {
2393 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 +02002394 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002395 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002396
Christopher Faulet6b81df72019-10-01 22:08:43 +02002397 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002398 return cs;
2399 end:
2400 cs_free(cs);
2401 return NULL;
2402}
2403
2404/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2405 * this mux, it's easy as we can only store a single conn_stream.
2406 */
2407static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2408{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002409 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002410 struct h1s *h1s = h1c->h1s;
2411
2412 if (h1s)
2413 return h1s->cs;
2414
2415 return NULL;
2416}
2417
Christopher Faulet73c12072019-04-08 11:23:22 +02002418static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002419{
Christopher Faulet73c12072019-04-08 11:23:22 +02002420 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002421
Christopher Faulet6b81df72019-10-01 22:08:43 +02002422 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002423 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002424 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002425}
2426
2427/*
2428 * Detach the stream from the connection and possibly release the connection.
2429 */
2430static void h1_detach(struct conn_stream *cs)
2431{
2432 struct h1s *h1s = cs->ctx;
2433 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002434 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002435 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002436
Christopher Faulet6b81df72019-10-01 22:08:43 +02002437 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2438
Christopher Faulet51dbc942018-09-13 09:05:15 +02002439 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002440 if (!h1s) {
2441 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002442 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002443 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002444
Olivier Houchardf502aca2018-12-14 19:42:40 +01002445 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002446 h1c = h1s->h1c;
2447 h1s->cs = NULL;
2448
Christopher Faulet42849b02020-10-05 11:35:17 +02002449 sess->accept_date = date;
2450 sess->tv_accept = now;
2451 sess->t_handshake = 0;
2452 sess->t_idle = -1;
2453
Olivier Houchard8a786902018-12-15 16:05:40 +01002454 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2455 h1s_destroy(h1s);
2456
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002457 if ((h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002458 /* If there are any excess server data in the input buffer,
2459 * release it and close the connection ASAP (some data may
2460 * remain in the output buffer). This happens if a server sends
2461 * invalid responses. So in such case, we don't want to reuse
2462 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002463 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002464 if (b_data(&h1c->ibuf)) {
2465 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002466 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002467 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002468 goto release;
2469 }
Christopher Faulet03627242019-07-19 11:34:08 +02002470
Christopher Faulet08016ab2020-07-01 16:10:06 +02002471 if (h1c->conn->flags & CO_FL_PRIVATE) {
2472 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002473 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2474 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002475 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002476 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002477 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002478 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002479 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002480 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002481 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2482 goto end;
2483 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002484 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002485 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002486 if (h1c->conn->owner == sess)
2487 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002488 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002489 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002490 /* The server doesn't want it, let's kill the connection right away */
2491 h1c->conn->mux->destroy(h1c);
2492 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2493 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002494 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002495 /* At this point, the connection has been added to the
2496 * server idle list, so another thread may already have
2497 * hijacked it, so we can't do anything with it.
2498 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002499 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002500 }
2501 }
2502
Christopher Fauletf1204b82019-07-19 14:51:06 +02002503 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002504 /* 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 +02002505 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2506 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2507 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002508 !h1c->conn->owner) {
2509 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002510 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002511 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002512 else {
Olivier Houchard69664412020-03-25 12:24:11 +01002513 /* If we have a new request, process it immediately */
Olivier Houchardc3500c32020-03-25 17:05:21 +01002514 if (unlikely(b_data(&h1c->ibuf)))
Olivier Houchard69664412020-03-25 12:24:11 +01002515 h1_process(h1c);
2516 else
2517 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002518 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002519 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002520 end:
2521 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002522}
2523
2524
2525static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2526{
2527 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002528 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002529
2530 if (!h1s)
2531 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002532 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002533
Christopher Faulet6b81df72019-10-01 22:08:43 +02002534 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2535
2536 if (cs->flags & CS_FL_KILL_CONN) {
2537 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2538 goto do_shutr;
2539 }
2540 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2541 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002542 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002543 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002544
Christopher Faulet6b81df72019-10-01 22:08:43 +02002545 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2546 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2547 goto end;
2548 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002549
Christopher Faulet7f366362019-04-08 10:51:20 +02002550 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002551 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2552 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002553 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002554 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002555 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002556 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002557 end:
2558 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002559}
2560
2561static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2562{
2563 struct h1s *h1s = cs->ctx;
2564 struct h1c *h1c;
2565
2566 if (!h1s)
2567 return;
2568 h1c = h1s->h1c;
2569
Christopher Faulet6b81df72019-10-01 22:08:43 +02002570 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2571
2572 if (cs->flags & CS_FL_KILL_CONN) {
2573 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002574 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002575 }
2576 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2577 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2578 goto do_shutw;
2579 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002580
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002581 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002582 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2583 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2584 goto end;
2585 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586
Christopher Faulet7f366362019-04-08 10:51:20 +02002587 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002588 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2589 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002590 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002591 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002592 end:
2593 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002594}
2595
Christopher Faulet666a0c42019-01-08 11:12:04 +01002596static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002597{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002598 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002599
Christopher Faulet6b81df72019-10-01 22:08:43 +02002600 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002601 conn_xprt_shutw(conn);
2602 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002603 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002604 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002605}
2606
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002607/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2608 * The <es> pointer is not allowed to differ from the one passed to the
2609 * subscribe() call. It always returns zero.
2610 */
2611static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002612{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002613 struct h1s *h1s = cs->ctx;
2614
2615 if (!h1s)
2616 return 0;
2617
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002618 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002619 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002620
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002621 es->events &= ~event_type;
2622 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002623 h1s->subs = NULL;
2624
2625 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002626 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002627
2628 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002629 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002630
Christopher Faulet51dbc942018-09-13 09:05:15 +02002631 return 0;
2632}
2633
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002634/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2635 * event subscriber <es> is not allowed to change from a previous call as long
2636 * as at least one event is still subscribed. The <event_type> must only be a
2637 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2638 * the conn_stream <cs> was already detached, in which case it will return -1.
2639 */
2640static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002641{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002642 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002643 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002644
2645 if (!h1s)
2646 return -1;
2647
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002648 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002649 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002650
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002651 es->events |= event_type;
2652 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002653
2654 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002655 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002656
2657
Christopher Faulet6b81df72019-10-01 22:08:43 +02002658 if (event_type & SUB_RETRY_SEND) {
2659 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002660 /*
2661 * If the conn_stream attempt to subscribe, and the
2662 * mux isn't subscribed to the connection, then it
2663 * probably means the connection wasn't established
2664 * yet, so we have to subscribe.
2665 */
2666 h1c = h1s->h1c;
2667 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2668 h1c->conn->xprt->subscribe(h1c->conn,
2669 h1c->conn->xprt_ctx,
2670 SUB_RETRY_SEND,
2671 &h1c->wait_event);
2672 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002673 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002674}
2675
2676/* Called from the upper layer, to receive data */
2677static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2678{
2679 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002680 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002681 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002682 size_t ret = 0;
2683
Willy Tarreau022e5e52020-09-10 09:33:15 +02002684 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002685 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002686 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002687 else
2688 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002689
Christopher Faulete18777b2019-04-16 16:46:36 +02002690 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002691 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02002692 h1c->flags |= H1C_F_WANT_SPLICE;
2693 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 +02002694 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002695 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002696 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002697 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002698 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002699 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002700 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002701 return ret;
2702}
2703
2704
2705/* Called from the upper layer, to send data */
2706static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2707{
2708 struct h1s *h1s = cs->ctx;
2709 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002710 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002711
2712 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002713 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002714 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002715
Willy Tarreau022e5e52020-09-10 09:33:15 +02002716 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002717
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002718 /* If we're not connected yet, or we're waiting for a handshake, stop
2719 * now, as we don't want to remove everything from the channel buffer
2720 * before we're sure we can send it.
2721 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002722 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002723 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002724 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002725 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002726
Christopher Faulet46230362019-10-17 16:04:20 +02002727 /* Inherit some flags from the upper layer */
2728 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2729 if (flags & CO_SFL_MSG_MORE)
2730 h1c->flags |= H1C_F_CO_MSG_MORE;
2731 if (flags & CO_SFL_STREAMER)
2732 h1c->flags |= H1C_F_CO_STREAMER;
2733
Christopher Fauletc31872f2019-06-04 22:09:36 +02002734 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002735 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002736
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002737 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2738 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002739 else
2740 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002741
2742 if ((count - ret) > 0)
2743 h1c->flags |= H1C_F_CO_MSG_MORE;
2744
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002745 if (!ret)
2746 break;
2747 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002748 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002749 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002750 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002751 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002752 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002753 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002754 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002755}
2756
Willy Tarreaue5733232019-05-22 19:24:06 +02002757#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002758/* Send and get, using splicing */
2759static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2760{
2761 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002762 struct h1c *h1c = h1s->h1c;
2763 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002764 int ret = 0;
2765
Willy Tarreau022e5e52020-09-10 09:33:15 +02002766 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002767
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002768 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002769 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002770 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 +02002771 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002772 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002773 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002774 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002775 goto end;
2776 }
2777
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002778 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
2779 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002780 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2781 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002782 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002783 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002784 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002785 }
2786
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002787 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02002788 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2789 goto end;
2790 }
2791
Christopher Faulet1be55f92018-10-02 15:59:23 +02002792 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2793 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002794 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002795 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002796 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002797 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002798 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002799 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002800 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002801 }
2802
Christopher Faulet1be55f92018-10-02 15:59:23 +02002803 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002804 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002805 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002806 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002807 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002808 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002809
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002810 if ((h1s->flags & H1S_F_REOS) ||
2811 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002812 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002813 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002814 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002815 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002816
Willy Tarreau022e5e52020-09-10 09:33:15 +02002817 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002818 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002819}
2820
2821static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2822{
2823 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002824 int ret = 0;
2825
Willy Tarreau022e5e52020-09-10 09:33:15 +02002826 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002827
Christopher Faulet1be55f92018-10-02 15:59:23 +02002828 if (b_data(&h1s->h1c->obuf))
2829 goto end;
2830
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002831 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002832 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002833 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002834 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2835 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002836 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002837 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002838 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002839
Willy Tarreau022e5e52020-09-10 09:33:15 +02002840 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002841 return ret;
2842}
2843#endif
2844
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002845static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2846{
2847 int ret = 0;
2848 switch (mux_ctl) {
2849 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002850 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002851 ret |= MUX_STATUS_READY;
2852 return ret;
2853 default:
2854 return -1;
2855 }
2856}
2857
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002858/* for debugging with CLI's "show fd" command */
2859static void h1_show_fd(struct buffer *msg, struct connection *conn)
2860{
2861 struct h1c *h1c = conn->ctx;
2862 struct h1s *h1s = h1c->h1s;
2863
Christopher Fauletf376a312019-01-04 15:16:06 +01002864 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2865 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002866 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2867 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2868 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2869 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2870
2871 if (h1s) {
2872 char *method;
2873
2874 if (h1s->meth < HTTP_METH_OTHER)
2875 method = http_known_methods[h1s->meth].ptr;
2876 else
2877 method = "UNKNOWN";
2878 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2879 " .meth=%s status=%d",
2880 h1s, h1s->flags,
2881 h1m_state_str(h1s->req.state),
2882 h1m_state_str(h1s->res.state), method, h1s->status);
2883 if (h1s->cs)
2884 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2885 h1s->cs->flags, h1s->cs->data);
2886 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002887}
2888
2889
2890/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2891static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2892{
2893 struct h1_hdr_entry *entry;
2894
2895 /* Be sure there is a non-empty <to> */
2896 if (!strlen(to)) {
2897 memprintf(err, "expect <to>");
2898 return -1;
2899 }
2900
2901 /* Be sure only the case differs between <from> and <to> */
2902 if (strcasecmp(from, to)) {
2903 memprintf(err, "<from> and <to> must not differ execpt the case");
2904 return -1;
2905 }
2906
2907 /* Be sure <from> does not already existsin the tree */
2908 if (ebis_lookup(&hdrs_map.map, from)) {
2909 memprintf(err, "duplicate entry '%s'", from);
2910 return -1;
2911 }
2912
2913 /* Create the entry and insert it in the tree */
2914 entry = malloc(sizeof(*entry));
2915 if (!entry) {
2916 memprintf(err, "out of memory");
2917 return -1;
2918 }
2919
2920 entry->node.key = strdup(from);
2921 entry->name.ptr = strdup(to);
2922 entry->name.len = strlen(to);
2923 if (!entry->node.key || !entry->name.ptr) {
2924 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002925 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002926 free(entry);
2927 memprintf(err, "out of memory");
2928 return -1;
2929 }
2930 ebis_insert(&hdrs_map.map, &entry->node);
2931 return 0;
2932}
2933
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002934/* Migrate the the connection to the current thread.
2935 * Return 0 if successful, non-zero otherwise.
2936 * Expected to be called with the old thread lock held.
2937 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002938static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002939{
2940 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002941 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002942
2943 if (fd_takeover(conn->handle.fd, conn) != 0)
2944 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02002945
2946 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
2947 /* We failed to takeover the xprt, even if the connection may
2948 * still be valid, flag it as error'd, as we have already
2949 * taken over the fd, and wake the tasklet, so that it will
2950 * destroy it.
2951 */
2952 conn->flags |= CO_FL_ERROR;
2953 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
2954 return -1;
2955 }
2956
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002957 if (h1c->wait_event.events)
2958 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
2959 h1c->wait_event.events, &h1c->wait_event);
2960 /* To let the tasklet know it should free itself, and do nothing else,
2961 * set its context to NULL.
2962 */
2963 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02002964 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02002965
2966 task = h1c->task;
2967 if (task) {
2968 task->context = NULL;
2969 h1c->task = NULL;
2970 __ha_barrier_store();
2971 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002972
2973 h1c->task = task_new(tid_bit);
2974 if (!h1c->task) {
2975 h1_release(h1c);
2976 return -1;
2977 }
2978 h1c->task->process = h1_timeout_task;
2979 h1c->task->context = h1c;
2980 }
2981 h1c->wait_event.tasklet = tasklet_new();
2982 if (!h1c->wait_event.tasklet) {
2983 h1_release(h1c);
2984 return -1;
2985 }
2986 h1c->wait_event.tasklet->process = h1_io_cb;
2987 h1c->wait_event.tasklet->context = h1c;
2988 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
2989 SUB_RETRY_RECV, &h1c->wait_event);
2990
2991 return 0;
2992}
2993
2994
Christopher Faulet98fbe952019-07-22 16:18:24 +02002995static void h1_hdeaders_case_adjust_deinit()
2996{
2997 struct ebpt_node *node, *next;
2998 struct h1_hdr_entry *entry;
2999
3000 node = ebpt_first(&hdrs_map.map);
3001 while (node) {
3002 next = ebpt_next(node);
3003 ebpt_delete(node);
3004 entry = container_of(node, struct h1_hdr_entry, node);
3005 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003006 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003007 free(entry);
3008 node = next;
3009 }
3010 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003011}
3012
Christopher Faulet98fbe952019-07-22 16:18:24 +02003013static int cfg_h1_headers_case_adjust_postparser()
3014{
3015 FILE *file = NULL;
3016 char *c, *key_beg, *key_end, *value_beg, *value_end;
3017 char *err;
3018 int rc, line = 0, err_code = 0;
3019
3020 if (!hdrs_map.name)
3021 goto end;
3022
3023 file = fopen(hdrs_map.name, "r");
3024 if (!file) {
3025 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3026 hdrs_map.name);
3027 err_code |= ERR_ALERT | ERR_FATAL;
3028 goto end;
3029 }
3030
3031 /* now parse all lines. The file may contain only two header name per
3032 * line, separated by spaces. All heading and trailing spaces will be
3033 * ignored. Lines starting with a # are ignored.
3034 */
3035 while (fgets(trash.area, trash.size, file) != NULL) {
3036 line++;
3037 c = trash.area;
3038
3039 /* strip leading spaces and tabs */
3040 while (*c == ' ' || *c == '\t')
3041 c++;
3042
3043 /* ignore emptu lines, or lines beginning with a dash */
3044 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3045 continue;
3046
3047 /* look for the end of the key */
3048 key_beg = c;
3049 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3050 c++;
3051 key_end = c;
3052
3053 /* strip middle spaces and tabs */
3054 while (*c == ' ' || *c == '\t')
3055 c++;
3056
3057 /* look for the end of the value, it is the end of the line */
3058 value_beg = c;
3059 while (*c && *c != '\n' && *c != '\r')
3060 c++;
3061 value_end = c;
3062
3063 /* trim possibly trailing spaces and tabs */
3064 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3065 value_end--;
3066
3067 /* set final \0 and check entries */
3068 *key_end = '\0';
3069 *value_end = '\0';
3070
3071 err = NULL;
3072 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3073 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003074 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3075 hdrs_map.name, err, line);
3076 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003077 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003078 goto end;
3079 }
3080 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003081 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3082 hdrs_map.name, err, line);
3083 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003084 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003085 }
3086 }
3087
3088 end:
3089 if (file)
3090 fclose(file);
3091 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3092 return err_code;
3093}
3094
3095
3096/* config parser for global "h1-outgoing-header-case-adjust" */
3097static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3098 struct proxy *defpx, const char *file, int line,
3099 char **err)
3100{
3101 if (too_many_args(2, args, err, NULL))
3102 return -1;
3103 if (!*(args[1]) || !*(args[2])) {
3104 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3105 return -1;
3106 }
3107 return add_hdr_case_adjust(args[1], args[2], err);
3108}
3109
3110/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3111static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3112 struct proxy *defpx, const char *file, int line,
3113 char **err)
3114{
3115 if (too_many_args(1, args, err, NULL))
3116 return -1;
3117 if (!*(args[1])) {
3118 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3119 return -1;
3120 }
3121 free(hdrs_map.name);
3122 hdrs_map.name = strdup(args[1]);
3123 return 0;
3124}
3125
3126
3127/* config keyword parsers */
3128static struct cfg_kw_list cfg_kws = {{ }, {
3129 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3130 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3131 { 0, NULL, NULL },
3132 }
3133};
3134
3135INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3136REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3137
3138
Christopher Faulet51dbc942018-09-13 09:05:15 +02003139/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003140/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003141/****************************************/
3142
3143/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003144static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003145 .init = h1_init,
3146 .wake = h1_wake,
3147 .attach = h1_attach,
3148 .get_first_cs = h1_get_first_cs,
3149 .detach = h1_detach,
3150 .destroy = h1_destroy,
3151 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003152 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003153 .rcv_buf = h1_rcv_buf,
3154 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003155#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003156 .rcv_pipe = h1_rcv_pipe,
3157 .snd_pipe = h1_snd_pipe,
3158#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003159 .subscribe = h1_subscribe,
3160 .unsubscribe = h1_unsubscribe,
3161 .shutr = h1_shutr,
3162 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003163 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003164 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003165 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003166 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003167 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003168 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003169};
3170
3171
3172/* this mux registers default HTX proto */
3173static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003174{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003175
Willy Tarreau0108d902018-11-25 19:14:37 +01003176INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3177
Christopher Faulet51dbc942018-09-13 09:05:15 +02003178/*
3179 * Local variables:
3180 * c-indent-level: 8
3181 * c-basic-offset: 8
3182 * End:
3183 */