blob: d2bafffb57172c395384e9a4bfa176935aa702d5 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Willy Tarreaub2551052020-06-09 09:07:15 +020012#include <import/ebistree.h>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
45/* 0x00000080 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +020048#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
49#define H1C_F_CS_SHUTDOWN 0x00002000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
50/* 0x00000040 unused */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010051#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
52 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020055#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020056
Christopher Faulet46230362019-10-17 16:04:20 +020057#define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
58#define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
59
Christopher Faulet089acd52020-09-21 10:57:52 +020060#define H1C_F_WAIT_OPPOSITE 0x00100000 /* Don't read more data for now, waiting sync with opposite side */
Christopher Fauletae635762020-09-21 11:47:16 +020061#define H1C_F_WANT_SPLICE 0x00200000 /* Don't read into a bufffer because we want to use or we are using splicing */
Christopher Faulet0a799aa2020-09-24 09:52:52 +020062#define H1C_F_IS_BACK 0x00400000 /* Set on outgoing connection */
Christopher Fauletbb8baf42020-09-29 15:18:40 +020063
64#define H1C_F_CS_EMBRYONIC 0x01000000 /* Set when a H1 stream with no conn-stream is attached to the connection */
65#define H1C_F_CS_ATTACHED 0x02000000 /* Set when a H1 stream with a conn-stream is attached to the connection */
66#define H1C_F_CS_ALIVE (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED)
Christopher Faulet51dbc942018-09-13 09:05:15 +020067/*
68 * H1 Stream flags (32 bits)
69 */
Christopher Faulet129817b2018-09-20 16:14:40 +020070#define H1S_F_NONE 0x00000000
Christopher Faulet60ef12c2020-09-24 10:05:44 +020071/* 0x00000001..0x00000004 unsued */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020072#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020073#define H1S_F_WANT_KAL 0x00000010
74#define H1S_F_WANT_TUN 0x00000020
75#define H1S_F_WANT_CLO 0x00000040
76#define H1S_F_WANT_MSK 0x00000070
77#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020078
Christopher Faulet76014fd2019-12-10 11:47:22 +010079#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020080#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
81#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
82#define H1S_F_ERROR 0x00001800 /* stream error mask */
83
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020084#define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */
Christopher Fauletada34b62019-05-24 16:36:43 +020085#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020086
87/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020088struct h1c {
89 struct connection *conn;
90 struct proxy *px;
91 uint32_t flags; /* Connection flags: H1C_F_* */
92
93 struct buffer ibuf; /* Input buffer to store data before parsing */
94 struct buffer obuf; /* Output buffer to store data after reformatting */
95
96 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
97 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
98
99 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100100 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200101 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
102 int timeout; /* client/server timeout duration */
103 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104};
105
106/* H1 stream descriptor */
107struct h1s {
108 struct h1c *h1c;
109 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100110 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200111
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100112 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200113
Olivier Houchardf502aca2018-12-14 19:42:40 +0100114 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200115 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200116 struct h1m req;
117 struct h1m res;
118
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500119 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200120 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200121};
122
Christopher Faulet98fbe952019-07-22 16:18:24 +0200123/* Map of headers used to convert outgoing headers */
124struct h1_hdrs_map {
125 char *name;
126 struct eb_root map;
127};
128
129/* An entry in a headers map */
130struct h1_hdr_entry {
131 struct ist name;
132 struct ebpt_node node;
133};
134
135/* Declare the headers map */
136static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
137
138
Christopher Faulet6b81df72019-10-01 22:08:43 +0200139/* trace source and events */
140static void h1_trace(enum trace_level level, uint64_t mask,
141 const struct trace_source *src,
142 const struct ist where, const struct ist func,
143 const void *a1, const void *a2, const void *a3, const void *a4);
144
145/* The event representation is split like this :
146 * h1c - internal H1 connection
147 * h1s - internal H1 stream
148 * strm - application layer
149 * rx - data receipt
150 * tx - data transmission
151 *
152 */
153static const struct trace_event h1_trace_events[] = {
154#define H1_EV_H1C_NEW (1ULL << 0)
155 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
156#define H1_EV_H1C_RECV (1ULL << 1)
157 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
158#define H1_EV_H1C_SEND (1ULL << 2)
159 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
160#define H1_EV_H1C_BLK (1ULL << 3)
161 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
162#define H1_EV_H1C_WAKE (1ULL << 4)
163 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
164#define H1_EV_H1C_END (1ULL << 5)
165 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
166#define H1_EV_H1C_ERR (1ULL << 6)
167 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
168
169#define H1_EV_RX_DATA (1ULL << 7)
170 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
171#define H1_EV_RX_EOI (1ULL << 8)
172 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
173#define H1_EV_RX_HDRS (1ULL << 9)
174 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
175#define H1_EV_RX_BODY (1ULL << 10)
176 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
177#define H1_EV_RX_TLRS (1ULL << 11)
178 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
179
180#define H1_EV_TX_DATA (1ULL << 12)
181 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
182#define H1_EV_TX_EOI (1ULL << 13)
183 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
184#define H1_EV_TX_HDRS (1ULL << 14)
185 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
186#define H1_EV_TX_BODY (1ULL << 15)
187 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
188#define H1_EV_TX_TLRS (1ULL << 16)
189 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
190
191#define H1_EV_H1S_NEW (1ULL << 17)
192 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
193#define H1_EV_H1S_BLK (1ULL << 18)
194 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
195#define H1_EV_H1S_END (1ULL << 19)
196 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
197#define H1_EV_H1S_ERR (1ULL << 20)
198 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
199
200#define H1_EV_STRM_NEW (1ULL << 21)
201 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
202#define H1_EV_STRM_RECV (1ULL << 22)
203 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
204#define H1_EV_STRM_SEND (1ULL << 23)
205 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
206#define H1_EV_STRM_WAKE (1ULL << 24)
207 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
208#define H1_EV_STRM_SHUT (1ULL << 25)
209 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
210#define H1_EV_STRM_END (1ULL << 26)
211 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
212#define H1_EV_STRM_ERR (1ULL << 27)
213 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
214
215 { }
216};
217
218static const struct name_desc h1_trace_lockon_args[4] = {
219 /* arg1 */ { /* already used by the connection */ },
220 /* arg2 */ { .name="h1s", .desc="H1 stream" },
221 /* arg3 */ { },
222 /* arg4 */ { }
223};
224
225static const struct name_desc h1_trace_decoding[] = {
226#define H1_VERB_CLEAN 1
227 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
228#define H1_VERB_MINIMAL 2
229 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
230#define H1_VERB_SIMPLE 3
231 { .name="simple", .desc="add request/response status line or htx info when available" },
232#define H1_VERB_ADVANCED 4
233 { .name="advanced", .desc="add header fields or frame decoding when available" },
234#define H1_VERB_COMPLETE 5
235 { .name="complete", .desc="add full data dump when available" },
236 { /* end */ }
237};
238
239static struct trace_source trace_h1 = {
240 .name = IST("h1"),
241 .desc = "HTTP/1 multiplexer",
242 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
243 .default_cb = h1_trace,
244 .known_events = h1_trace_events,
245 .lockon_args = h1_trace_lockon_args,
246 .decoding = h1_trace_decoding,
247 .report_events = ~0, // report everything by default
248};
249
250#define TRACE_SOURCE &trace_h1
251INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
252
Christopher Faulet51dbc942018-09-13 09:05:15 +0200253/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100254DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
255DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200256
Christopher Faulet51dbc942018-09-13 09:05:15 +0200257static int h1_recv(struct h1c *h1c);
258static int h1_send(struct h1c *h1c);
259static int h1_process(struct h1c *h1c);
260static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100261static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200262static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200263static void h1_wake_stream_for_recv(struct h1s *h1s);
264static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200265
Christopher Faulet6b81df72019-10-01 22:08:43 +0200266/* the H1 traces always expect that arg1, if non-null, is of type connection
267 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
268 * that arg3, if non-null, is a htx for rx/tx headers.
269 */
270static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
271 const struct ist where, const struct ist func,
272 const void *a1, const void *a2, const void *a3, const void *a4)
273{
274 const struct connection *conn = a1;
275 const struct h1c *h1c = conn ? conn->ctx : NULL;
276 const struct h1s *h1s = a2;
277 const struct htx *htx = a3;
278 const size_t *val = a4;
279
280 if (!h1c)
281 h1c = (h1s ? h1s->h1c : NULL);
282
283 if (!h1c || src->verbosity < H1_VERB_CLEAN)
284 return;
285
286 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200287 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200288
289 /* Display request and response states if h1s is defined */
290 if (h1s)
291 chunk_appendf(&trace_buf, " [%s, %s]",
292 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
293
294 if (src->verbosity == H1_VERB_CLEAN)
295 return;
296
297 /* Display the value to the 4th argument (level > STATE) */
298 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100299 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200300
301 /* Display status-line if possible (verbosity > MINIMAL) */
302 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
303 const struct htx_blk *blk = htx_get_head_blk(htx);
304 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
305 enum htx_blk_type type = htx_get_blk_type(blk);
306
307 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
308 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
309 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
310 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
311 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
312 }
313
314 /* Display h1c info and, if defined, h1s info (pointer + flags) */
315 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
316 if (h1s)
317 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
318
319 if (src->verbosity == H1_VERB_MINIMAL)
320 return;
321
322 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
323 if (src->level > TRACE_LEVEL_USER) {
324 if (src->verbosity == H1_VERB_COMPLETE ||
325 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
326 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
327 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
328 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
329 if (src->verbosity == H1_VERB_COMPLETE ||
330 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
331 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
332 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
333 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
334 }
335
336 /* Display htx info if defined (level > USER) */
337 if (src->level > TRACE_LEVEL_USER && htx) {
338 int full = 0;
339
340 /* Full htx info (level > STATE && verbosity > SIMPLE) */
341 if (src->level > TRACE_LEVEL_STATE) {
342 if (src->verbosity == H1_VERB_COMPLETE)
343 full = 1;
344 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
345 full = 1;
346 }
347
348 chunk_memcat(&trace_buf, "\n\t", 2);
349 htx_dump(&trace_buf, htx, full);
350 }
351}
352
353
Christopher Faulet51dbc942018-09-13 09:05:15 +0200354/*****************************************************/
355/* functions below are for dynamic buffer management */
356/*****************************************************/
357/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100358 * Indicates whether or not we may receive data. The rules are the following :
359 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200360 * must not attempt to receive
361 * - if we are waiting for the connection establishment, we must not attempt
362 * to receive
363 * - if an error was detected on the stream we must not attempt to receive
364 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100365 * - if the input buffer failed to be allocated or is full , we must not try
366 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200367 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200368 * - otherwise must may not attempt to receive
369 */
370static inline int h1_recv_allowed(const struct h1c *h1c)
371{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100372 if (h1c->flags & H1C_F_CS_ERROR) {
373 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 +0200374 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200375 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200376
Willy Tarreau2febb842020-07-31 09:15:43 +0200377 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
378 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 +0100379 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200380 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100381
Christopher Faulet119ac872020-09-30 17:33:22 +0200382 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
383 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
384 return 0;
385 }
386
Christopher Faulet089acd52020-09-21 10:57:52 +0200387 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
388 TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200389 return 0;
390 }
391
Christopher Fauletd17ad822020-09-24 15:14:29 +0200392 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200393 return 1;
394
Christopher Faulet6b81df72019-10-01 22:08:43 +0200395 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 +0200396 return 0;
397}
398
399/*
400 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
401 * flags are used to figure what buffer was requested. It returns 1 if the
402 * allocation succeeds, in which case the connection is woken up, or 0 if it's
403 * impossible to wake up and we prefer to be woken up later.
404 */
405static int h1_buf_available(void *target)
406{
407 struct h1c *h1c = target;
408
409 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200410 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 +0200411 h1c->flags &= ~H1C_F_IN_ALLOC;
412 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200413 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200414 return 1;
415 }
416
417 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200418 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 +0200419 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200420 if (h1c->h1s)
421 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200422 return 1;
423 }
424
Christopher Fauletd17ad822020-09-24 15:14:29 +0200425 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
426 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
427 h1c->flags &= ~H1C_F_IN_SALLOC;
428 tasklet_wakeup(h1c->wait_event.tasklet);
429 return 1;
430 }
431
Christopher Faulet51dbc942018-09-13 09:05:15 +0200432 return 0;
433}
434
435/*
436 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
437 */
438static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
439{
440 struct buffer *buf = NULL;
441
Willy Tarreau21046592020-02-26 10:39:36 +0100442 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200443 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
444 h1c->buf_wait.target = h1c;
445 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200446 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200447 }
448 return buf;
449}
450
451/*
452 * Release a buffer, if any, and try to wake up entities waiting in the buffer
453 * wait queue.
454 */
455static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
456{
457 if (bptr->size) {
458 b_free(bptr);
459 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
460 }
461}
462
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100463/* returns the number of streams in use on a connection to figure if it's idle
464 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
465 * not. This flag is only set when no H1S is attached and when the previous
466 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100467 */
468static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200469{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100470 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100472 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473}
474
Willy Tarreau00f18a32019-01-26 12:19:01 +0100475/* returns the number of streams still available on a connection */
476static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100477{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100478 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100479}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480
Christopher Faulet7a145d62020-08-05 11:31:16 +0200481/* Refresh the h1c task timeout if necessary */
482static void h1_refresh_timeout(struct h1c *h1c)
483{
484 if (h1c->task) {
Christopher Fauletadcd7892020-10-05 17:13:05 +0200485 if (!(h1c->flags & H1C_F_CS_ALIVE) || (h1c->flags & H1C_F_CS_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200486 /* half-closed or dead connections : switch to clientfin/serverfin
487 * timeouts so that we don't hang too long on clients that have
488 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200489 */
490 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200491 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
492 }
493 else if (b_data(&h1c->obuf)) {
494 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200495 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200496 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
497 }
498 else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC))) {
Christopher Faulet268c92e2020-12-01 11:42:53 +0100499 /* front connections waiting for a stream need a timeout. client timeout by
500 * default but http-keep-alive if defined
501 */
502 int timeout = h1c->timeout;
503
504 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
505 timeout = tick_first(timeout, h1c->px->timeout.httpka);
506
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200507 h1c->task->expire = tick_add(now_ms, timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200508 TRACE_DEVEL("refreshing connection's timeout (alive front h1c without a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200509 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200510 else {
511 /* alive back connections of front connections with a conn-stream attached */
512 h1c->task->expire = TICK_ETERNITY;
513 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
514 }
515
Christopher Fauletdbe57792020-10-05 17:50:58 +0200516 /* Finally set the idle expiration date if shorter */
517 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200518 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
519 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200520 }
521}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200522
523static __maybe_unused void h1_set_idle_expiration(struct h1c *h1c)
524{
525 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
526 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
527 h1c->idle_exp = TICK_ETERNITY;
528 return;
529 }
530
531 if (h1c->flags & H1C_F_CS_IDLE) {
532 if (!tick_isset(h1c->idle_exp)) {
533 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
534 !b_data(&h1c->ibuf) && /* No input data */
535 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
536 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
537 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
538 }
539 else {
540 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
541 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
542 }
543 }
544 }
545 else if (h1c->flags & H1C_F_CS_EMBRYONIC) {
546 if (!tick_isset(h1c->idle_exp)) {
547 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
548 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
549 }
550 }
551 else { // CS_ATTACHED or SHUTDOWN
552 h1c->idle_exp = TICK_ETERNITY;
553 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
554 }
555}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200556/*****************************************************************/
557/* functions below are dedicated to the mux setup and management */
558/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200559
560/* returns non-zero if there are input data pending for stream h1s. */
561static inline size_t h1s_data_pending(const struct h1s *h1s)
562{
563 const struct h1m *h1m;
564
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200565 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200566 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100567 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200568
569 return b_data(&h1s->h1c->ibuf);
570}
571
Christopher Faulet26256f82020-09-14 11:40:13 +0200572static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100573{
574 struct conn_stream *cs;
575
Christopher Faulet6b81df72019-10-01 22:08:43 +0200576 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200577 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200578 if (!cs) {
579 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 +0100580 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200581 }
Christopher Faulet47365272018-10-31 17:40:50 +0100582 h1s->cs = cs;
583 cs->ctx = h1s;
584
585 if (h1s->flags & H1S_F_NOT_FIRST)
586 cs->flags |= CS_FL_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200587 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet47365272018-10-31 17:40:50 +0100588
Christopher Faulet27182292020-07-03 15:08:49 +0200589 if (global.tune.options & GTUNE_USE_SPLICE) {
590 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100591 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200592 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100593
Christopher Faulet26256f82020-09-14 11:40:13 +0200594 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200595 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 +0100596 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200597 }
Christopher Faulet26256f82020-09-14 11:40:13 +0200598 *input = BUF_NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200599
600 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100601 return cs;
602
603 err:
604 cs_free(cs);
605 h1s->cs = NULL;
606 return NULL;
607}
608
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200609static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200610{
611 struct h1s *h1s;
612
Christopher Faulet6b81df72019-10-01 22:08:43 +0200613 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
614
Christopher Faulet51dbc942018-09-13 09:05:15 +0200615 h1s = pool_alloc(pool_head_h1s);
616 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100617 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200618 h1s->h1c = h1c;
619 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200620 h1s->sess = NULL;
621 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100622 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100623 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200624 h1s->rxbuf = BUF_NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200625
626 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100627 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200628
Christopher Faulet129817b2018-09-20 16:14:40 +0200629 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100630 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200631
632 h1s->status = 0;
633 h1s->meth = HTTP_METH_OTHER;
634
Christopher Faulet47365272018-10-31 17:40:50 +0100635 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
636 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200637 h1c->flags = (h1c->flags & ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_CS_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100638
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200639 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
640 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200641
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200642 fail:
643 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
644 return NULL;
645}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100646
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200647static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
648{
649 struct session *sess = h1c->conn->owner;
650 struct h1s *h1s;
651
652 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
653
654 h1s = h1s_new(h1c);
655 if (!h1s)
656 goto fail;
657
658 h1s->sess = sess;
659
660 if (h1c->px->options2 & PR_O2_REQBUG_OK)
661 h1s->req.err_pos = -1;
662
663 if (!h1s_new_cs(h1s, &BUF_NULL))
664 goto fail_cs;
665
Christopher Faulet6b81df72019-10-01 22:08:43 +0200666 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200667 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100668
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200669 fail_cs:
Christopher Faulet47365272018-10-31 17:40:50 +0100670 pool_free(pool_head_h1s, h1s);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200671 fail:
672 sess_log(sess);
673 TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
674 return NULL;
675}
676
677static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
678{
679 struct h1s *h1s;
680
681 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
682
683 h1s = h1s_new(h1c);
684 if (!h1s)
685 goto fail;
686
687 h1s->cs = cs;
688 h1s->sess = sess;
689 cs->ctx = h1s;
690
Christopher Fauletbb8baf42020-09-29 15:18:40 +0200691 h1c->flags = (h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200692
693 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
694 h1s->res.err_pos = -1;
695
696 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
697 return h1s;
698
699 fail:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200700 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 +0100701 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200702}
703
704static void h1s_destroy(struct h1s *h1s)
705{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200706 if (h1s) {
707 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200708
Christopher Faulet6b81df72019-10-01 22:08:43 +0200709 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200710 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200711
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100712 if (h1s->subs)
713 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200714
Christopher Fauletd17ad822020-09-24 15:14:29 +0200715 h1_release_buf(h1c, &h1s->rxbuf);
716
Christopher Faulet295b8d12020-09-30 17:14:55 +0200717 h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_WANT_SPLICE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED|
718 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
719 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200720 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet47365272018-10-31 17:40:50 +0100721 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200722 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
723 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100724
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200725 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100726 !(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 +0100727 (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 +0100728 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
729 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
730 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
731 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200732 else {
733 TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
734 h1c->flags |= H1C_F_CS_SHUTDOWN;
735 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200736 pool_free(pool_head_h1s, h1s);
737 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200738}
739
740/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200741 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500742 * to the existing conn_stream (for outgoing connections or for incoming ones
743 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200744 * establishment). <input> is always used as Input buffer and may contain
745 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
746 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200747 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200748static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
749 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200750{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200751 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100752 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200753 void *conn_ctx = conn->ctx;
754
755 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200756
757 h1c = pool_alloc(pool_head_h1c);
758 if (!h1c)
759 goto fail_h1c;
760 h1c->conn = conn;
761 h1c->px = proxy;
762
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100763 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200764 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200765 h1c->obuf = BUF_NULL;
766 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200767 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200768
Willy Tarreau21046592020-02-26 10:39:36 +0100769 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200770 h1c->wait_event.tasklet = tasklet_new();
771 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200772 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200773 h1c->wait_event.tasklet->process = h1_io_cb;
774 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100775 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200776 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200777
Christopher Faulete9b70722019-04-08 10:46:02 +0200778 if (conn_is_back(conn)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200779 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100780 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
781 if (tick_isset(proxy->timeout.serverfin))
782 h1c->shut_timeout = proxy->timeout.serverfin;
783 } else {
784 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
785 if (tick_isset(proxy->timeout.clientfin))
786 h1c->shut_timeout = proxy->timeout.clientfin;
787 }
788 if (tick_isset(h1c->timeout)) {
789 t = task_new(tid_bit);
790 if (!t)
791 goto fail;
792
793 h1c->task = t;
794 t->process = h1_timeout_task;
795 t->context = h1c;
796 t->expire = tick_add(now_ms, h1c->timeout);
797 }
798
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100799 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200800
Christopher Faulet6b81df72019-10-01 22:08:43 +0200801 /* Always Create a new H1S */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200802 if (!(h1c->flags & H1C_F_IS_BACK)) {
803 if (!h1c_frt_stream_new(h1c))
804 goto fail;
805 }
806 else {
807 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
808 goto fail;
809 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100810
811 if (t)
812 task_queue(t);
813
Christopher Faulet51dbc942018-09-13 09:05:15 +0200814 /* Try to read, if nothing is available yet we'll just subscribe */
Christopher Faulet089acd52020-09-21 10:57:52 +0200815 if (!h1_recv_allowed(h1c))
816 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200817
818 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200819 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200820 return 0;
821
822 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200823 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200824 if (h1c->wait_event.tasklet)
825 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200826 pool_free(pool_head_h1c, h1c);
827 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200828 conn->ctx = conn_ctx; // restore saved context
829 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200830 return -1;
831}
832
Christopher Faulet73c12072019-04-08 11:23:22 +0200833/* release function. This one should be called to free all resources allocated
834 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200835 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200836static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200837{
Christopher Faulet61840e72019-04-15 09:33:32 +0200838 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200839
Christopher Faulet6b81df72019-10-01 22:08:43 +0200840 TRACE_POINT(H1_EV_H1C_END);
841
Christopher Faulet51dbc942018-09-13 09:05:15 +0200842 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200843 /* The connection must be aattached to this mux to be released */
844 if (h1c->conn && h1c->conn->ctx == h1c)
845 conn = h1c->conn;
846
Christopher Faulet6b81df72019-10-01 22:08:43 +0200847 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
848
Christopher Faulet61840e72019-04-15 09:33:32 +0200849 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200850 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200851 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200852 /* Make sure we're no longer subscribed to anything */
853 if (h1c->wait_event.events)
854 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
855 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200856 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200857 /* connection successfully upgraded to H2, this
858 * mux was already released */
859 return;
860 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200861 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200862 sess_log(conn->owner); /* Log if the upgrade failed */
863 }
864
Olivier Houchard45c44372019-06-11 14:06:23 +0200865
Willy Tarreau21046592020-02-26 10:39:36 +0100866 if (MT_LIST_ADDED(&h1c->buf_wait.list))
867 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200868
869 h1_release_buf(h1c, &h1c->ibuf);
870 h1_release_buf(h1c, &h1c->obuf);
871
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100872 if (h1c->task) {
873 h1c->task->context = NULL;
874 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
875 h1c->task = NULL;
876 }
877
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200878 if (h1c->wait_event.tasklet)
879 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200880
Christopher Fauletf2824e62018-10-01 12:12:37 +0200881 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200882 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100883 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200884 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200885 pool_free(pool_head_h1c, h1c);
886 }
887
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200888 if (conn) {
889 conn->mux = NULL;
890 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200891 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200892
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200893 conn_stop_tracking(conn);
894 conn_full_close(conn);
895 if (conn->destroy_cb)
896 conn->destroy_cb(conn);
897 conn_free(conn);
898 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200899}
900
901/******************************************************/
902/* functions below are for the H1 protocol processing */
903/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200904/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
905 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200906 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100907static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200908{
Christopher Faulet570d1612018-11-26 11:13:57 +0100909 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200910
Christopher Faulet570d1612018-11-26 11:13:57 +0100911 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200912 (*(p + 5) > '1' ||
913 (*(p + 5) == '1' && *(p + 7) >= '1')))
914 h1m->flags |= H1_MF_VER_11;
915}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200916
Christopher Faulet9768c262018-10-22 09:34:31 +0200917/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
918 * greater or equal to 1.1
919 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100920static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200921{
Christopher Faulet570d1612018-11-26 11:13:57 +0100922 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200923
Christopher Faulet570d1612018-11-26 11:13:57 +0100924 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200925 (*(p + 5) > '1' ||
926 (*(p + 5) == '1' && *(p + 7) >= '1')))
927 h1m->flags |= H1_MF_VER_11;
928}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200929
Christopher Fauletf2824e62018-10-01 12:12:37 +0200930/* Deduce the connection mode of the client connection, depending on the
931 * configuration and the H1 message flags. This function is called twice, the
932 * first time when the request is parsed and the second time when the response
933 * is parsed.
934 */
935static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
936{
937 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200938
939 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100940 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200941 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100942 h1s->status == 101) {
943 /* Either we've established an explicit tunnel, or we're
944 * switching the protocol. In both cases, we're very unlikely to
945 * understand the next protocols. We have to switch to tunnel
946 * mode, so that we transfer the request and responses then let
947 * this protocol pass unmodified. When we later implement
948 * specific parsers for such protocols, we'll want to check the
949 * Upgrade header which contains information about that protocol
950 * for responses with status 101 (eg: see RFC2817 about TLS).
951 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200952 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200953 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 +0100954 }
955 else if (h1s->flags & H1S_F_WANT_KAL) {
956 /* By default the client is in KAL mode. CLOSE mode mean
957 * it is imposed by the client itself. So only change
958 * KAL mode here. */
959 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
960 /* no length known or explicit close => close */
961 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200962 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 +0100963 }
964 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
965 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500966 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100967 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200968 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 +0100969 }
970 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200971 }
972 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100973 /* Input direction: first pass */
974 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
975 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200976 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200977 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 +0100978 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200979 }
980
981 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200982 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200984 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);
985 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200986}
987
988/* Deduce the connection mode of the client connection, depending on the
989 * configuration and the H1 message flags. This function is called twice, the
990 * first time when the request is parsed and the second time when the response
991 * is parsed.
992 */
993static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
994{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100995 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100996 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100997 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200998
Christopher Fauletf2824e62018-10-01 12:12:37 +0200999 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001000 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001001 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001002 h1s->status == 101) {
1003 /* Either we've established an explicit tunnel, or we're
1004 * switching the protocol. In both cases, we're very unlikely to
1005 * understand the next protocols. We have to switch to tunnel
1006 * mode, so that we transfer the request and responses then let
1007 * this protocol pass unmodified. When we later implement
1008 * specific parsers for such protocols, we'll want to check the
1009 * Upgrade header which contains information about that protocol
1010 * for responses with status 101 (eg: see RFC2817 about TLS).
1011 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001012 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001013 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 +01001014 }
1015 else if (h1s->flags & H1S_F_WANT_KAL) {
1016 /* By default the server is in KAL mode. CLOSE mode mean
1017 * it is imposed by haproxy itself. So only change KAL
1018 * mode here. */
1019 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1020 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1021 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1022 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001023 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 +01001024 }
1025 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001026 }
Christopher Faulet70033782018-12-05 13:50:11 +01001027 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001028 /* Output direction: first pass */
1029 if (h1m->flags & H1_MF_CONN_CLO) {
1030 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001031 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001032 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 +01001033 }
1034 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1035 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1036 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1037 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1038 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1039 /* no explicit keep-alive option httpclose/server-close => close */
1040 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001041 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 +01001042 }
Christopher Faulet70033782018-12-05 13:50:11 +01001043 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001044
1045 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001046 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001048 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);
1049 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050}
1051
Christopher Fauletb992af02019-03-28 15:42:24 +01001052static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001053{
1054 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001055
1056 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1057 * token is found
1058 */
1059 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001060 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001061
1062 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001063 if (!(h1m->flags & H1_MF_VER_11)) {
1064 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 +01001065 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001066 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001067 }
1068 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001069 if (h1m->flags & H1_MF_VER_11) {
1070 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001071 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001072 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001073 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001074}
1075
Christopher Fauletb992af02019-03-28 15:42:24 +01001076static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001078 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1079 * token is found
1080 */
1081 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001082 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001083
1084 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001085 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001086 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1087 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 +01001088 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001089 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001090 }
1091 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001092 if (h1m->flags & H1_MF_VER_11) {
1093 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001094 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001095 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001096 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001097}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001098
Christopher Fauletb992af02019-03-28 15:42:24 +01001099static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001100{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001101 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001102 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001103 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001104 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001105}
1106
Christopher Fauletb992af02019-03-28 15:42:24 +01001107static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1108{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001109 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001110 h1_set_cli_conn_mode(h1s, h1m);
1111 else
1112 h1_set_srv_conn_mode(h1s, h1m);
1113
1114 if (!(h1m->flags & H1_MF_RESP))
1115 h1_update_req_conn_value(h1s, h1m, conn_val);
1116 else
1117 h1_update_res_conn_value(h1s, h1m, conn_val);
1118}
Christopher Faulete44769b2018-11-29 23:01:45 +01001119
Christopher Faulet98fbe952019-07-22 16:18:24 +02001120/* Try to adjust the case of the message header name using the global map
1121 * <hdrs_map>.
1122 */
1123static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1124{
1125 struct ebpt_node *node;
1126 struct h1_hdr_entry *entry;
1127
1128 /* No entry in the map, do nothing */
1129 if (eb_is_empty(&hdrs_map.map))
1130 return;
1131
1132 /* No conversion fo the request headers */
1133 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1134 return;
1135
1136 /* No conversion fo the response headers */
1137 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1138 return;
1139
1140 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1141 if (!node)
1142 return;
1143 entry = container_of(node, struct h1_hdr_entry, node);
1144 name->ptr = entry->name.ptr;
1145 name->len = entry->name.len;
1146}
1147
Christopher Faulete44769b2018-11-29 23:01:45 +01001148/* Append the description of what is present in error snapshot <es> into <out>.
1149 * The description must be small enough to always fit in a buffer. The output
1150 * buffer may be the trash so the trash must not be used inside this function.
1151 */
1152static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1153{
1154 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001155 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1156 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1157 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1158 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1159 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1160 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001161}
1162/*
1163 * Capture a bad request or response and archive it in the proxy's structure.
1164 * By default it tries to report the error position as h1m->err_pos. However if
1165 * this one is not set, it will then report h1m->next, which is the last known
1166 * parsing point. The function is able to deal with wrapping buffers. It always
1167 * displays buffers as a contiguous area starting at buf->p. The direction is
1168 * determined thanks to the h1m's flags.
1169 */
1170static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1171 struct h1m *h1m, struct buffer *buf)
1172{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001173 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001174 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001175 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001176 union error_snapshot_ctx ctx;
1177
Christopher Fauletbb8baf42020-09-29 15:18:40 +02001178 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001179 if (sess == NULL)
1180 sess = si_strm(h1s->cs->data)->sess;
1181 if (!(h1m->flags & H1_MF_RESP))
1182 other_end = si_strm(h1s->cs->data)->be;
1183 else
1184 other_end = sess->fe;
1185 } else
1186 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001187
1188 /* http-specific part now */
1189 ctx.h1.state = h1m->state;
1190 ctx.h1.c_flags = h1c->flags;
1191 ctx.h1.s_flags = h1s->flags;
1192 ctx.h1.m_flags = h1m->flags;
1193 ctx.h1.m_clen = h1m->curr_len;
1194 ctx.h1.m_blen = h1m->body_len;
1195
1196 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1197 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001198 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1199 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001200}
1201
Christopher Fauletadb22202018-12-12 10:32:09 +01001202/* Emit the chunksize followed by a CRLF in front of data of the buffer
1203 * <buf>. It goes backwards and starts with the byte before the buffer's
1204 * head. The caller is responsible for ensuring there is enough room left before
1205 * the buffer's head for the string.
1206 */
1207static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1208{
1209 char *beg, *end;
1210
1211 beg = end = b_head(buf);
1212 *--beg = '\n';
1213 *--beg = '\r';
1214 do {
1215 *--beg = hextab[chksz & 0xF];
1216 } while (chksz >>= 4);
1217 buf->head -= (end - beg);
1218 b_add(buf, end - beg);
1219}
1220
1221/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1222 * ensuring there is enough room left in the buffer for the string. */
1223static void h1_emit_chunk_crlf(struct buffer *buf)
1224{
1225 *(b_peek(buf, b_data(buf))) = '\r';
1226 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1227 b_add(buf, 2);
1228}
1229
Christopher Faulet129817b2018-09-20 16:14:40 +02001230/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001231 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001232 * CONNECT requests. On the client side, if the response is not finished, the
1233 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001234 */
1235static void h1_set_req_tunnel_mode(struct h1s *h1s)
1236{
1237 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1238 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001239 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1240
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001241 if (!(h1s->h1c->flags & H1C_F_IS_BACK)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001242 h1s->flags &= ~H1S_F_PARSING_DONE;
1243 if (h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001244 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1245 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 +01001246 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001247 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001248 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1249 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001250 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001251 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 +01001252 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001253}
1254
1255/*
1256 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001257 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001258 * last case, this function takes care to switch the request to tunnel mode if
1259 * possible. On the server side, if the request is not finished, the mux is mark
1260 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001261 */
1262static void h1_set_res_tunnel_mode(struct h1s *h1s)
1263{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001264
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001265 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1266 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001267 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1268
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001269 if (h1s->h1c->flags & H1C_F_IS_BACK) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001270 h1s->flags &= ~H1S_F_PARSING_DONE;
1271 /* On protocol switching, switch the request to tunnel mode if it is in
1272 * DONE state. Otherwise we will wait the end of the request to switch
1273 * it in tunnel mode.
1274 */
1275 if (h1s->req.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001276 h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE;
1277 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 +01001278 }
1279 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1280 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1281 h1s->req.state = H1_MSG_TUNNEL;
1282 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1283 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001284 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001285 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1286 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001287 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
Christopher Faulet089acd52020-09-21 10:57:52 +02001288 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 +01001289 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001290}
1291
1292/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001293 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001294 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001295 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001296 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001297static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001298 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001299{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001300 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001301 int ret = 0;
1302
Willy Tarreau022e5e52020-09-10 09:33:15 +02001303 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 +02001304
Christopher Faulet89aed322020-06-02 17:33:56 +02001305 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1306 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1307 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001308 /* Try to match H2 preface before parsing the request headers. */
1309 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001310 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001311 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001312 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001313 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001314 else {
1315 if (h1s->meth == HTTP_METH_CONNECT)
1316 h1m->flags |= H1_MF_METH_CONNECT;
1317 if (h1s->meth == HTTP_METH_HEAD)
1318 h1m->flags |= H1_MF_METH_HEAD;
1319 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001320
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001321 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1322 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001323 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 +02001324 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001325 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001326 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 +02001327 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1328 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001329 goto end;
1330 }
1331
Christopher Faulet486498c2019-10-11 14:22:00 +02001332 if (h1m->err_pos >= 0) {
1333 /* Maybe we found an error during the parsing while we were
1334 * configured not to block on that, so we have to capture it
1335 * now.
1336 */
1337 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1338 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1339 }
1340
Christopher Faulet129817b2018-09-20 16:14:40 +02001341 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001342 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001343 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001344 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001345 }
1346 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001347 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001348 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001349 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001350 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001351 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001353
Christopher Faulet129817b2018-09-20 16:14:40 +02001354 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001355 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 +02001356 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001357
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001358 h2c_upgrade:
1359 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Fauleta583af62020-09-24 15:35:37 +02001360 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001361 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001362 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1363 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001364}
1365
1366/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001367 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001368 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1369 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001370 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001371static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001372 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001373 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001374{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001375 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001376
Willy Tarreau022e5e52020-09-10 09:33:15 +02001377 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 +02001378 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001379 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001380 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 +01001381 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001382 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001383 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 +02001384 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001385 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001386 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001387 }
1388
Christopher Faulet02a02532019-11-15 09:36:28 +01001389 *ofs += ret;
1390
1391 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001392 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 +02001393 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001394}
1395
1396/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1398 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1399 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1400 * responsible to update the parser state <h1m>.
1401 */
1402static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1403 struct buffer *buf, size_t *ofs, size_t max)
1404{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001405 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001406
Willy Tarreau022e5e52020-09-10 09:33:15 +02001407 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 +02001408 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001409 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001410 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 +01001411 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001412 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001413 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 +02001414 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1415 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001416 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001417 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001418
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001419 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001420
1421 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001422 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 +02001423 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001424}
1425
1426/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001427 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
Christopher Fauleta583af62020-09-24 15:35:37 +02001428 * proceed. This functions is responsible to update the parser state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001429 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001430static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1431 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001432{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001433 int ret;
1434
Willy Tarreau022e5e52020-09-10 09:33:15 +02001435 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 +01001436 ret = h1_parse_msg_eom(h1m, htx, max);
1437 if (!ret) {
1438 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1439 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001440 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001441 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 +01001442 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1443 }
1444 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001445 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001446
Christopher Faulet76014fd2019-12-10 11:47:22 +01001447 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001448 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001449 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 +01001450 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001451}
1452
1453/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001454 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001455 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1456 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001457 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001458static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001459{
Christopher Faulet539e0292018-11-19 10:40:09 +01001460 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001461 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001463 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001464 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001465
Christopher Faulet539e0292018-11-19 10:40:09 +01001466 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001467 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001468
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001469 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet9768c262018-10-22 09:34:31 +02001470
Christopher Faulet74027762019-02-26 14:45:05 +01001471 data = htx->data;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001472 if (h1s->flags & H1S_F_PARSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001473 goto end;
1474
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001475 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001476 size_t used = htx_used_space(htx);
1477
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001479 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001480 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001481 if (!ret)
1482 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001483
1484 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1485 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1486
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001487 if ((h1m->flags & H1_MF_RESP) &&
1488 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1489 h1m_init_res(&h1s->res);
1490 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001491 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001492 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001493 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001494 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001495 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001496 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001497 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001498 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001499
1500 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1501 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001502 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001503 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001504 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1505 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1506 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001507 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001508
Christopher Faulet76014fd2019-12-10 11:47:22 +01001509 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1510 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001511 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001512 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001513 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1514 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1515 break;
1516
1517 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1518 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1519 }
1520
Christopher Fauletf3158e92019-11-15 11:14:23 +01001521 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1522 h1_set_req_tunnel_mode(h1s);
1523 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet089acd52020-09-21 10:57:52 +02001524 h1c->flags |= H1C_F_WAIT_OPPOSITE;
1525 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 +02001526 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001527 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001528 else
1529 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001530 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001531 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001532 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001533 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001534 if (!ret)
1535 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001536
1537 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1538 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001539 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001540 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001541 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001542 break;
1543 }
1544
Christopher Faulet30db3d72019-05-17 15:35:33 +02001545 count -= htx_used_space(htx) - used;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001546 } while (!(h1s->flags & H1S_F_PARSING_ERROR));
Christopher Faulet129817b2018-09-20 16:14:40 +02001547
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001548 if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001549 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001550 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001551 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001552
Christopher Faulet539e0292018-11-19 10:40:09 +01001553 b_del(&h1c->ibuf, total);
1554
1555 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001556 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001557 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001558 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001559 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001560 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 +01001561 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001562 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001563
Christopher Fauleta583af62020-09-24 15:35:37 +02001564 if (!(h1m->flags & H1_MF_CHNK) &&
1565 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1566 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1567 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1568 }
1569 else {
1570 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1571 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1572 }
1573
1574 if (h1s->flags & H1S_F_PARSING_DONE)
1575 h1s->cs->flags |= CS_FL_EOI;
1576
Christopher Fauletcf56b992018-12-11 16:12:31 +01001577 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1578
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001579 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001580 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001581 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001582 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001583 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001584 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet466080d2019-11-15 09:50:22 +01001585 if (h1m->state == H1_MSG_TUNNEL)
1586 h1s->cs->flags |= CS_FL_EOI;
1587 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001588 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001589 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001590
Christopher Faulet6b81df72019-10-01 22:08:43 +02001591 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001592 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001593
1594 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001595 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001596 htx_to_buf(htx, buf);
Christopher Fauleta583af62020-09-24 15:35:37 +02001597 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001598 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001599 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001600}
1601
Christopher Faulet129817b2018-09-20 16:14:40 +02001602/*
1603 * Process outgoing data. It parses data and transfer them from the channel buffer into
1604 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1605 * 0 if it couldn't proceed.
1606 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001607static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1608{
Christopher Faulet129817b2018-09-20 16:14:40 +02001609 struct h1s *h1s = h1c->h1s;
1610 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001611 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001612 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001613 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001614 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001615
Christopher Faulet47365272018-10-31 17:40:50 +01001616 if (!count)
1617 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001618
Christopher Faulet94b2c762019-05-24 15:28:57 +02001619 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001620 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1621
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001622 if (htx_is_empty(chn_htx))
1623 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001624
Christopher Faulet51dbc942018-09-13 09:05:15 +02001625 if (!h1_get_buf(h1c, &h1c->obuf)) {
1626 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001627 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 +02001628 goto end;
1629 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001630
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001631 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001632
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001633 if (h1s->flags & H1S_F_PROCESSING_ERROR)
Christopher Faulet0e54d542019-07-04 21:22:34 +02001634 goto end;
1635
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001636 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001637 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001638
Willy Tarreau3815b222018-12-11 19:50:43 +01001639 /* Perform some optimizations to reduce the number of buffer copies.
1640 * First, if the mux's buffer is empty and the htx area contains
1641 * exactly one data block of the same size as the requested count,
1642 * then it's possible to simply swap the caller's buffer with the
1643 * mux's output buffer and adjust offsets and length to match the
1644 * entire DATA HTX block in the middle. In this case we perform a
1645 * true zero-copy operation from end-to-end. This is the situation
1646 * that happens all the time with large files. Second, if this is not
1647 * possible, but the mux's output buffer is empty, we still have an
1648 * opportunity to avoid the copy to the intermediary buffer, by making
1649 * the intermediary buffer's area point to the output buffer's area.
1650 * In this case we want to skip the HTX header to make sure that copies
1651 * remain aligned and that this operation remains possible all the
1652 * time. This goes for headers, data blocks and any data extracted from
1653 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001654 */
1655 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001656 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001657 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001658 htx_get_blk_value(chn_htx, blk).len == count) {
1659 void *old_area = h1c->obuf.area;
1660
Christopher Faulet6b81df72019-10-01 22:08:43 +02001661 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 +01001662 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001663 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001664 h1c->obuf.data = count;
1665
1666 buf->area = old_area;
1667 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001668
Christopher Faulet6b81df72019-10-01 22:08:43 +02001669 chn_htx = (struct htx *)buf->area;
1670 htx_reset(chn_htx);
1671
Christopher Fauletadb22202018-12-12 10:32:09 +01001672 /* The message is chunked. We need to emit the chunk
1673 * size. We have at least the size of the struct htx to
1674 * write the chunk envelope. It should be enough.
1675 */
1676 if (h1m->flags & H1_MF_CHNK) {
1677 h1_emit_chunk_size(&h1c->obuf, count);
1678 h1_emit_chunk_crlf(&h1c->obuf);
1679 }
1680
Willy Tarreau3815b222018-12-11 19:50:43 +01001681 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001682 if (h1m->state == H1_MSG_DATA)
1683 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001684 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001685 else
1686 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001687 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001688 goto out;
1689 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001690 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001691 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001692 else
1693 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001694
Christopher Fauletc2518a52019-06-25 21:41:02 +02001695 tmp.data = 0;
1696 tmp.size = b_room(&h1c->obuf);
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001697 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001698 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001700 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001701 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001702 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001703
Christopher Fauletb2e84162018-12-06 11:39:49 +01001704 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001705 if (type != HTX_BLK_DATA && vlen > count)
1706 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707
Christopher Faulet94b2c762019-05-24 15:28:57 +02001708 if (type == HTX_BLK_UNUSED)
1709 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001710
Christopher Faulet94b2c762019-05-24 15:28:57 +02001711 switch (h1m->state) {
1712 case H1_MSG_RQBEFORE:
1713 if (type != HTX_BLK_REQ_SL)
1714 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001715 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 +02001716 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001717 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001718 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001719 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001720 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001721 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001722 if (sl->flags & HTX_SL_F_BODYLESS)
1723 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet089acd52020-09-21 10:57:52 +02001725 if (h1c->flags & H1C_F_WAIT_OPPOSITE) {
1726 h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
1727 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1728 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1729 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001730 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001731
Christopher Faulet94b2c762019-05-24 15:28:57 +02001732 case H1_MSG_RPBEFORE:
1733 if (type != HTX_BLK_RES_SL)
1734 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001735 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 +02001736 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001737 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001738 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001739 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001740 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001741 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001742 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001743 if (sl->info.res.status < 200 &&
1744 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001745 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001746 h1m->state = H1_MSG_HDR_FIRST;
1747 break;
1748
Christopher Faulet94b2c762019-05-24 15:28:57 +02001749 case H1_MSG_HDR_FIRST:
1750 case H1_MSG_HDR_NAME:
1751 case H1_MSG_HDR_L2_LWS:
1752 if (type == HTX_BLK_EOH)
1753 goto last_lf;
1754 if (type != HTX_BLK_HDR)
1755 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001756
Christopher Faulet9768c262018-10-22 09:34:31 +02001757 h1m->state = H1_MSG_HDR_NAME;
1758 n = htx_get_blk_name(chn_htx, blk);
1759 v = htx_get_blk_value(chn_htx, blk);
1760
Christopher Faulet86d144c2019-08-14 16:32:25 +02001761 /* Skip all pseudo-headers */
1762 if (*(n.ptr) == ':')
1763 goto skip_hdr;
1764
Christopher Fauletb045bb22020-02-28 10:42:20 +01001765 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001766 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001767 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001768 /* Only skip C-L header with invalid value. */
1769 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001770 goto skip_hdr;
1771 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001772 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001773 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001774 if (!v.len)
1775 goto skip_hdr;
1776 }
1777
Christopher Faulet67d58092019-10-02 10:51:38 +02001778 /* Skip header if same name is used to add the server name */
1779 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1780 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1781 goto skip_hdr;
1782
Christopher Faulet98fbe952019-07-22 16:18:24 +02001783 /* Try to adjust the case of the header name */
1784 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1785 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001786 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001787 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001788 skip_hdr:
1789 h1m->state = H1_MSG_HDR_L2_LWS;
1790 break;
1791
Christopher Faulet94b2c762019-05-24 15:28:57 +02001792 case H1_MSG_LAST_LF:
1793 if (type != HTX_BLK_EOH)
1794 goto error;
1795 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001796 h1m->state = H1_MSG_LAST_LF;
1797 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001798 /* If the reply comes from haproxy while the request is
1799 * not finished, we force the connection close. */
1800 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1801 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1802 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1803 }
1804
1805 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001806 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001807 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001808 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001809 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001810 /* Try to adjust the case of the header name */
1811 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1812 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001813 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001814 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001815 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001816 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001817 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001818
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001819 if ((h1s->meth != HTTP_METH_CONNECT &&
1820 (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 +01001821 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1822 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1823 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1824 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1825 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001826 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001827 n = ist("transfer-encoding");
1828 v = ist("chunked");
1829 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1830 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001831 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001832 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001833 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001834 h1m->flags |= H1_MF_CHNK;
1835 }
1836
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001837 /* Now add the server name to a header (if requested) */
1838 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1839 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1840 struct server *srv = objt_server(h1c->conn->target);
1841
1842 if (srv) {
1843 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1844 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001845
1846 /* Try to adjust the case of the header name */
1847 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1848 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001849 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001850 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001851 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001852 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001853 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1854 }
1855
Christopher Fauletc2518a52019-06-25 21:41:02 +02001856 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001857 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001858
Christopher Faulet6b81df72019-10-01 22:08:43 +02001859 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1860 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1861
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001862 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1863 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1864 h1_set_req_tunnel_mode(h1s);
1865 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001866 else if ((h1m->flags & H1_MF_RESP) &&
1867 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001868 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001869 * to the client. Switch the response to tunnel mode.
1870 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001871 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001872 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 +01001873 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001874 else if ((h1m->flags & H1_MF_RESP) &&
1875 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1876 h1m_init_res(&h1s->res);
1877 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001878 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001879 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001880 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001881 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001882 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001883 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1884 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001885 else
1886 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001887 break;
1888
Christopher Faulet94b2c762019-05-24 15:28:57 +02001889 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001890 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001891 if (type == HTX_BLK_EOM) {
1892 /* Chunked message without explicit trailers */
1893 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001894 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001895 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001896 }
1897 goto done;
1898 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001899 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001900 /* If the message is not chunked, never
1901 * add the last chunk. */
1902 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001903 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001904 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 +02001905 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001906 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001907 else if (type != HTX_BLK_DATA)
1908 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001909
1910 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 +02001911
1912
1913 if (vlen > count) {
1914 /* Get the maximum amount of data we can xferred */
1915 vlen = count;
1916 }
1917
1918 chklen = 0;
1919 if (h1m->flags & H1_MF_CHNK) {
1920 chklen = b_room(&tmp);
1921 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1922 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1923 (chklen < 1048576) ? 5 : 8);
1924 chklen += 4; /* 2 x CRLF */
1925 }
1926
1927 if (vlen + chklen > b_room(&tmp)) {
1928 /* too large for the buffer */
1929 if (chklen >= b_room(&tmp))
1930 goto full;
1931 vlen = b_room(&tmp) - chklen;
1932 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001933 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001934 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001935 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001936 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001937
1938 if (h1m->state == H1_MSG_DATA)
1939 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001940 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001941 else
1942 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001943 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001944 break;
1945
Christopher Faulet94b2c762019-05-24 15:28:57 +02001946 case H1_MSG_TRAILERS:
1947 if (type == HTX_BLK_EOM)
1948 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001949 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001950 goto error;
1951 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001952 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001953 /* If the message is not chunked, ignore
1954 * trailers. It may happen with H2 messages. */
1955 if (!(h1m->flags & H1_MF_CHNK))
1956 break;
1957
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001958 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001959 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001960 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001961 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1962 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001963 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001964 else { // HTX_BLK_TLR
1965 n = htx_get_blk_name(chn_htx, blk);
1966 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001967
1968 /* Try to adjust the case of the header name */
1969 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1970 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001971 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001972 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001973 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001974 break;
1975
Christopher Faulet94b2c762019-05-24 15:28:57 +02001976 case H1_MSG_DONE:
1977 if (type != HTX_BLK_EOM)
1978 goto error;
1979 done:
1980 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001981 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1982 h1_set_req_tunnel_mode(h1s);
1983 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1984 }
Christopher Faulet089acd52020-09-21 10:57:52 +02001985 else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) {
1986 h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001987 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02001988 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 +02001989 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001990
1991 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1992 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001993 break;
1994
Christopher Faulet9768c262018-10-22 09:34:31 +02001995 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001996 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001997 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001998 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001999 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002000 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002001 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002002 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2003 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002004 break;
2005 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002006
2007 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002008 total += vlen;
2009 count -= vlen;
2010 if (sz == vlen)
2011 blk = htx_remove_blk(chn_htx, blk);
2012 else {
2013 htx_cut_data_blk(chn_htx, blk, vlen);
2014 break;
2015 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002016 }
2017
Christopher Faulet9768c262018-10-22 09:34:31 +02002018 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002019 /* when the output buffer is empty, tmp shares the same area so that we
2020 * only have to update pointers and lengths.
2021 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002022 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2023 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002024 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002025 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002026
Willy Tarreau3815b222018-12-11 19:50:43 +01002027 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002028 out:
2029 if (!buf_room_for_htx_data(&h1c->obuf)) {
2030 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002031 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002032 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002033 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002034 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002035 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002036
2037 full:
2038 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2039 h1c->flags |= H1C_F_OUT_FULL;
2040 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002041}
2042
Christopher Faulet51dbc942018-09-13 09:05:15 +02002043/*********************************************************/
2044/* functions below are I/O callbacks from the connection */
2045/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002046static void h1_wake_stream_for_recv(struct h1s *h1s)
2047{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002048 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002049 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002050 tasklet_wakeup(h1s->subs->tasklet);
2051 h1s->subs->events &= ~SUB_RETRY_RECV;
2052 if (!h1s->subs->events)
2053 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002054 }
2055}
2056static void h1_wake_stream_for_send(struct h1s *h1s)
2057{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002058 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002059 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002060 tasklet_wakeup(h1s->subs->tasklet);
2061 h1s->subs->events &= ~SUB_RETRY_SEND;
2062 if (!h1s->subs->events)
2063 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002064 }
2065}
2066
Christopher Faulet51dbc942018-09-13 09:05:15 +02002067/*
2068 * Attempt to read data, and subscribe if none available
2069 */
2070static int h1_recv(struct h1c *h1c)
2071{
2072 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002073 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002074 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002076 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002077
Christopher Faulet6b81df72019-10-01 22:08:43 +02002078 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2079
2080 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2081 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002082 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002083 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002084
Christopher Fauletae635762020-09-21 11:47:16 +02002085 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2086 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002087 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002088 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002089 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002090
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002091 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2092 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002093 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002094 goto end;
2095 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002096
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002097 /*
2098 * If we only have a small amount of data, realign it,
2099 * it's probably cheaper than doing 2 recv() calls.
2100 */
2101 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2102 b_slow_realign(&h1c->ibuf, trash.area, 0);
2103
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002104 /* avoid useless reads after first responses */
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002105 if (h1s && ((!(h1c->flags & H1C_F_IS_BACK) && h1s->req.state == H1_MSG_RQBEFORE) ||
2106 ((h1c->flags & H1C_F_IS_BACK) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002107 flags |= CO_RFL_READ_ONCE;
2108
Willy Tarreau45f2b892018-12-05 07:59:27 +01002109 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002110 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002111 if (h1c->flags & H1C_F_IN_FULL) {
2112 h1c->flags &= ~H1C_F_IN_FULL;
2113 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2114 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002115
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002116 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002117 if (!b_data(&h1c->ibuf)) {
2118 /* try to pre-align the buffer like the rxbufs will be
2119 * to optimize memory copies.
2120 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002121 h1c->ibuf.head = sizeof(struct htx);
2122 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002123 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002124 }
Christopher Faulet47365272018-10-31 17:40:50 +01002125 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002126 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002127 rcvd = 1;
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002128 if (h1c->flags & H1C_F_CS_ATTACHED)
Christopher Faulet37e36072018-12-04 15:54:12 +01002129 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Faulet47365272018-10-31 17:40:50 +01002130 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002131
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002132 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002133 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002134 goto end;
2135 }
2136
Christopher Faulet6b81df72019-10-01 22:08:43 +02002137 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002138 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002139
Christopher Faulet9768c262018-10-22 09:34:31 +02002140 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002141 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2142 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002143
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002144 if (conn_xprt_read0_pending(conn) && h1s) {
2145 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002146 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002147 rcvd = 1;
2148 }
2149
Christopher Faulet51dbc942018-09-13 09:05:15 +02002150 if (!b_data(&h1c->ibuf))
2151 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002152 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002153 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002154 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2155 }
2156
2157 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002158 return rcvd;
2159}
2160
2161
2162/*
2163 * Try to send data if possible
2164 */
2165static int h1_send(struct h1c *h1c)
2166{
2167 struct connection *conn = h1c->conn;
2168 unsigned int flags = 0;
2169 size_t ret;
2170 int sent = 0;
2171
Christopher Faulet6b81df72019-10-01 22:08:43 +02002172 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2173
2174 if (conn->flags & CO_FL_ERROR) {
2175 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002176 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002177 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178
2179 if (!b_data(&h1c->obuf))
2180 goto end;
2181
Christopher Faulet46230362019-10-17 16:04:20 +02002182 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002183 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002184 if (h1c->flags & H1C_F_CO_STREAMER)
2185 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002186
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002187 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002189 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002190 if (h1c->flags & H1C_F_OUT_FULL) {
2191 h1c->flags &= ~H1C_F_OUT_FULL;
2192 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2193 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002194 b_del(&h1c->obuf, ret);
2195 sent = 1;
2196 }
2197
Christopher Faulet145aa472018-12-06 10:56:20 +01002198 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002199 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002200 /* error or output closed, nothing to send, clear the buffer to release it */
2201 b_reset(&h1c->obuf);
2202 }
2203
Christopher Faulet51dbc942018-09-13 09:05:15 +02002204 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002205 if (!(h1c->flags & H1C_F_OUT_FULL))
2206 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002207
Christopher Faulet51dbc942018-09-13 09:05:15 +02002208 /* We're done, no more to send */
2209 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002210 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002211 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002212 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002213 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002214 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002215 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002216 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002217 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2218 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002219 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002220 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002221
Christopher Faulet6b81df72019-10-01 22:08:43 +02002222 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002223 return sent;
2224}
2225
Christopher Faulet51dbc942018-09-13 09:05:15 +02002226
2227/* callback called on any event by the connection handler.
2228 * It applies changes and returns zero, or < 0 if it wants immediate
2229 * destruction of the connection.
2230 */
2231static int h1_process(struct h1c * h1c)
2232{
2233 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002234 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002235
Christopher Faulet6b81df72019-10-01 22:08:43 +02002236 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2237
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002238 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002239 return -1;
2240
Christopher Fauletfeb11742018-11-29 15:12:34 +01002241 if (!h1s) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002242 if ((h1c->flags & H1C_F_CS_ERROR) ||
2243 ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002244 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002245 goto release;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002246 if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002247 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002248 if (!h1c_frt_stream_new(h1c))
Christopher Faulet539e0292018-11-19 10:40:09 +01002249 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002250 }
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002251 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002252 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002253 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002254 }
2255
Christopher Fauletae635762020-09-21 11:47:16 +02002256 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2257 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2258 h1_wake_stream_for_recv(h1s);
2259 }
2260
Christopher Faulet4e741552020-09-30 13:47:09 +02002261 if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1)
2262 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002263
Christopher Faulet6b81df72019-10-01 22:08:43 +02002264 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002265 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2267 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002268
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002269 if (!h1s_data_pending(h1s) && h1s && (h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002270 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002271 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002272 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002273 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002274 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002275 h1s->cs->data_cb->wake(h1s->cs);
2276 }
Christopher Faulet47365272018-10-31 17:40:50 +01002277 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002278 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002279 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002280 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002281
2282 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002283 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002284 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002285 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002286}
2287
2288static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
2289{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002290 struct connection *conn;
2291 struct tasklet *tl = (struct tasklet *)t;
2292 int conn_in_list;
2293 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294 int ret = 0;
2295
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002296
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002297 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002298 if (tl->context == NULL) {
2299 /* The connection has been taken over by another thread,
2300 * we're no longer responsible for it, so just free the
2301 * tasklet, and do nothing.
2302 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002303 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002304 tasklet_free(tl);
2305 return NULL;
2306 }
2307 h1c = ctx;
2308 conn = h1c->conn;
2309
2310 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2311
2312 /* Remove the connection from the list, to be sure nobody attempts
2313 * to use it while we handle the I/O events
2314 */
2315 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2316 if (conn_in_list)
2317 MT_LIST_DEL(&conn->list);
2318
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002319 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002320
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002321 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002322 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002323 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002324 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002325 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002326 ret = h1_process(h1c);
2327 /* If we were in an idle list, we want to add it back into it,
2328 * unless h1_process() returned -1, which mean it has destroyed
2329 * the connection (testing !ret is enough, if h1_process() wasn't
2330 * called then ret will be 0 anyway.
2331 */
2332 if (!ret && conn_in_list) {
2333 struct server *srv = objt_server(conn->target);
2334
2335 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002336 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002337 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002338 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002339 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002340 return NULL;
2341}
2342
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002343static void h1_reset(struct connection *conn)
2344{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002345
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002346}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002347
2348static int h1_wake(struct connection *conn)
2349{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002350 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002351 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352
Christopher Faulet6b81df72019-10-01 22:08:43 +02002353 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2354
Christopher Faulet539e0292018-11-19 10:40:09 +01002355 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002356 ret = h1_process(h1c);
2357 if (ret == 0) {
2358 struct h1s *h1s = h1c->h1s;
2359
Christopher Fauletbb8baf42020-09-29 15:18:40 +02002360 if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002361 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002362 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002363 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002364 }
2365 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002366}
2367
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002368/* Connection timeout management. The principle is that if there's no receipt
2369 * nor sending for a certain amount of time, the connection is closed.
2370 */
2371static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2372{
2373 struct h1c *h1c = context;
2374 int expired = tick_is_expired(t->expire, now_ms);
2375
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002376 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002377
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002378 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002379 /* Make sure nobody stole the connection from us */
2380 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2381
2382 /* Somebody already stole the connection from us, so we should not
2383 * free it, we just have to free the task.
2384 */
2385 if (!t->context) {
2386 h1c = NULL;
2387 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2388 goto do_leave;
2389 }
2390
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002391 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002392 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2393 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002394 return t;
2395 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002396
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002397 /* If a conn-stream is still attached to the mux, wait for the
2398 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002399 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002400 if (h1c->flags & H1C_F_CS_ATTACHED) {
2401 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2402 t->expire = TICK_ETERNITY;
2403 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2404 return t;
2405 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002406
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002407 /* We're about to destroy the connection, so make sure nobody attempts
2408 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002409 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002410 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002411 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002412
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002413 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002414 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002415
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002416 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002417 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002418
2419 if (!h1c) {
2420 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002421 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002422 return NULL;
2423 }
2424
2425 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002426 h1_release(h1c);
2427 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002428 return NULL;
2429}
2430
Christopher Faulet51dbc942018-09-13 09:05:15 +02002431/*******************************************/
2432/* functions below are used by the streams */
2433/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002434
Christopher Faulet51dbc942018-09-13 09:05:15 +02002435/*
2436 * Attach a new stream to a connection
2437 * (Used for outgoing connections)
2438 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002439static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002440{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002441 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002442 struct conn_stream *cs = NULL;
2443 struct h1s *h1s;
2444
Christopher Faulet6b81df72019-10-01 22:08:43 +02002445 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2446 if (h1c->flags & H1C_F_CS_ERROR) {
2447 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 +02002448 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002450
Christopher Faulet236c93b2020-07-02 09:19:54 +02002451 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002452 if (!cs) {
2453 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 +02002454 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002455 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002456
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002457 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002458 if (h1s == NULL) {
2459 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 +02002460 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002461 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002462
Christopher Faulet6b81df72019-10-01 22:08:43 +02002463 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002464 return cs;
2465 end:
2466 cs_free(cs);
2467 return NULL;
2468}
2469
2470/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2471 * this mux, it's easy as we can only store a single conn_stream.
2472 */
2473static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2474{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002475 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002476 struct h1s *h1s = h1c->h1s;
2477
2478 if (h1s)
2479 return h1s->cs;
2480
2481 return NULL;
2482}
2483
Christopher Faulet73c12072019-04-08 11:23:22 +02002484static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002485{
Christopher Faulet73c12072019-04-08 11:23:22 +02002486 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002487
Christopher Faulet6b81df72019-10-01 22:08:43 +02002488 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002489 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002490 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002491}
2492
2493/*
2494 * Detach the stream from the connection and possibly release the connection.
2495 */
2496static void h1_detach(struct conn_stream *cs)
2497{
2498 struct h1s *h1s = cs->ctx;
2499 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002500 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002501 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002502
Christopher Faulet6b81df72019-10-01 22:08:43 +02002503 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2504
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002506 if (!h1s) {
2507 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002508 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002509 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002510
Olivier Houchardf502aca2018-12-14 19:42:40 +01002511 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002512 h1c = h1s->h1c;
2513 h1s->cs = NULL;
2514
Christopher Faulet42849b02020-10-05 11:35:17 +02002515 sess->accept_date = date;
2516 sess->tv_accept = now;
2517 sess->t_handshake = 0;
2518 sess->t_idle = -1;
2519
Olivier Houchard8a786902018-12-15 16:05:40 +01002520 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2521 h1s_destroy(h1s);
2522
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002523 if ((h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002524 /* If there are any excess server data in the input buffer,
2525 * release it and close the connection ASAP (some data may
2526 * remain in the output buffer). This happens if a server sends
2527 * invalid responses. So in such case, we don't want to reuse
2528 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002529 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002530 if (b_data(&h1c->ibuf)) {
2531 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002532 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002533 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002534 goto release;
2535 }
Christopher Faulet03627242019-07-19 11:34:08 +02002536
Christopher Faulet08016ab2020-07-01 16:10:06 +02002537 if (h1c->conn->flags & CO_FL_PRIVATE) {
2538 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002539 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2540 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002541 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002542 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002543 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002544 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002545 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002546 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002547 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2548 goto end;
2549 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002550 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002551 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002552 if (h1c->conn->owner == sess)
2553 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002554 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002555 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002556 /* The server doesn't want it, let's kill the connection right away */
2557 h1c->conn->mux->destroy(h1c);
2558 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2559 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002560 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002561 /* At this point, the connection has been added to the
2562 * server idle list, so another thread may already have
2563 * hijacked it, so we can't do anything with it.
2564 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002565 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002566 }
2567 }
2568
Christopher Fauletf1204b82019-07-19 14:51:06 +02002569 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002570 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002571 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_UPG_H2C)) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02002572 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002573 ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002574 !h1c->conn->owner) {
2575 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002576 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002578 else {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02002579 if (h1c->flags & H1C_F_CS_IDLE) {
2580 /* If we have a new request, process it immediately or
2581 * subscribe for reads waiting for new data
2582 */
2583 if (unlikely(b_data(&h1c->ibuf)))
2584 h1_process(h1c);
2585 else
2586 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
2587 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002588 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002589 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002590 end:
2591 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002592}
2593
2594
2595static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2596{
2597 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002598 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002599
2600 if (!h1s)
2601 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002602 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002603
Christopher Faulet6b81df72019-10-01 22:08:43 +02002604 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2605
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002606 if (cs->flags & CS_FL_SHR)
2607 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002608 if (cs->flags & CS_FL_KILL_CONN) {
2609 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2610 goto do_shutr;
2611 }
2612 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2613 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002614 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002615 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002616
Christopher Faulet6b81df72019-10-01 22:08:43 +02002617 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2618 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2619 goto end;
2620 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002621
Christopher Faulet7f366362019-04-08 10:51:20 +02002622 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002623 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2624 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002625 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002626 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002627 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002628 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002629 end:
2630 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002631}
2632
2633static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2634{
2635 struct h1s *h1s = cs->ctx;
2636 struct h1c *h1c;
2637
2638 if (!h1s)
2639 return;
2640 h1c = h1s->h1c;
2641
Christopher Faulet6b81df72019-10-01 22:08:43 +02002642 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2643
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002644 if (cs->flags & CS_FL_SHW)
2645 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002646 if (cs->flags & CS_FL_KILL_CONN) {
2647 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002648 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002649 }
2650 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2651 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2652 goto do_shutw;
2653 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002654
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002655 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002656 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2657 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2658 goto end;
2659 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002660
Christopher Faulet7f366362019-04-08 10:51:20 +02002661 do_shutw:
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02002662 h1c->flags |= H1C_F_CS_SHUTDOWN;
2663 if (!b_data(&h1c->obuf))
2664 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002665 end:
2666 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002667}
2668
Christopher Faulet666a0c42019-01-08 11:12:04 +01002669static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002670{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002671 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002672
Christopher Faulet6b81df72019-10-01 22:08:43 +02002673 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002674 conn_xprt_shutw(conn);
2675 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002676 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002677}
2678
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002679/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2680 * The <es> pointer is not allowed to differ from the one passed to the
2681 * subscribe() call. It always returns zero.
2682 */
2683static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002684{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002685 struct h1s *h1s = cs->ctx;
2686
2687 if (!h1s)
2688 return 0;
2689
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002690 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002691 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002692
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002693 es->events &= ~event_type;
2694 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002695 h1s->subs = NULL;
2696
2697 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002698 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002699
2700 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002701 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002702
Christopher Faulet51dbc942018-09-13 09:05:15 +02002703 return 0;
2704}
2705
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002706/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2707 * event subscriber <es> is not allowed to change from a previous call as long
2708 * as at least one event is still subscribed. The <event_type> must only be a
2709 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2710 * the conn_stream <cs> was already detached, in which case it will return -1.
2711 */
2712static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002713{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002714 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002715 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002716
2717 if (!h1s)
2718 return -1;
2719
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002720 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002721 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002722
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002723 es->events |= event_type;
2724 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002725
2726 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002727 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002728
2729
Christopher Faulet6b81df72019-10-01 22:08:43 +02002730 if (event_type & SUB_RETRY_SEND) {
2731 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002732 /*
2733 * If the conn_stream attempt to subscribe, and the
2734 * mux isn't subscribed to the connection, then it
2735 * probably means the connection wasn't established
2736 * yet, so we have to subscribe.
2737 */
2738 h1c = h1s->h1c;
2739 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2740 h1c->conn->xprt->subscribe(h1c->conn,
2741 h1c->conn->xprt_ctx,
2742 SUB_RETRY_SEND,
2743 &h1c->wait_event);
2744 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002745 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002746}
2747
2748/* Called from the upper layer, to receive data */
2749static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2750{
2751 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002752 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002753 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002754 size_t ret = 0;
2755
Willy Tarreau022e5e52020-09-10 09:33:15 +02002756 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002757 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002758 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002759 else
2760 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002761
Christopher Faulete18777b2019-04-16 16:46:36 +02002762 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002763 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02002764 h1c->flags |= H1C_F_WANT_SPLICE;
2765 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 +02002766 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002767 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002768 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002769 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002770 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002771 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002772 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002773 return ret;
2774}
2775
2776
2777/* Called from the upper layer, to send data */
2778static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2779{
2780 struct h1s *h1s = cs->ctx;
2781 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002782 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002783
2784 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002785 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002786 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002787
Willy Tarreau022e5e52020-09-10 09:33:15 +02002788 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002789
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002790 /* If we're not connected yet, or we're waiting for a handshake, stop
2791 * now, as we don't want to remove everything from the channel buffer
2792 * before we're sure we can send it.
2793 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002794 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002795 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002796 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002797 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002798
Christopher Faulet46230362019-10-17 16:04:20 +02002799 /* Inherit some flags from the upper layer */
2800 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2801 if (flags & CO_SFL_MSG_MORE)
2802 h1c->flags |= H1C_F_CO_MSG_MORE;
2803 if (flags & CO_SFL_STREAMER)
2804 h1c->flags |= H1C_F_CO_STREAMER;
2805
Christopher Fauletc31872f2019-06-04 22:09:36 +02002806 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002807 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002808
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002809 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2810 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002811 else
2812 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002813
2814 if ((count - ret) > 0)
2815 h1c->flags |= H1C_F_CO_MSG_MORE;
2816
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002817 if (!ret)
2818 break;
2819 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002820 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002821 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002822 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002823 }
Christopher Faulet7a145d62020-08-05 11:31:16 +02002824 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002825 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002826 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002827}
2828
Willy Tarreaue5733232019-05-22 19:24:06 +02002829#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002830/* Send and get, using splicing */
2831static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2832{
2833 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002834 struct h1c *h1c = h1s->h1c;
2835 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002836 int ret = 0;
2837
Willy Tarreau022e5e52020-09-10 09:33:15 +02002838 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002839
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002840 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002841 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002842 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 +02002843 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002844 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002845 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002846 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002847 goto end;
2848 }
2849
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002850 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
2851 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002852 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2853 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002854 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002855 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002856 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002857 }
2858
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002859 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02002860 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2861 goto end;
2862 }
2863
Christopher Faulet1be55f92018-10-02 15:59:23 +02002864 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2865 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002866 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002867 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002868 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002869 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002870 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002871 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002872 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002873 }
2874
Christopher Faulet1be55f92018-10-02 15:59:23 +02002875 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002876 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002877 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002878 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02002879 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002880 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002881
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002882 if ((h1s->flags & H1S_F_REOS) ||
2883 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002884 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02002885 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002886 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002887 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002888
Willy Tarreau022e5e52020-09-10 09:33:15 +02002889 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002890 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002891}
2892
2893static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2894{
2895 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002896 int ret = 0;
2897
Willy Tarreau022e5e52020-09-10 09:33:15 +02002898 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002899
Christopher Faulet1be55f92018-10-02 15:59:23 +02002900 if (b_data(&h1s->h1c->obuf))
2901 goto end;
2902
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002903 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002904 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002905 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002906 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2907 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002908 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002909 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002910 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002911
Willy Tarreau022e5e52020-09-10 09:33:15 +02002912 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002913 return ret;
2914}
2915#endif
2916
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002917static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2918{
2919 int ret = 0;
2920 switch (mux_ctl) {
2921 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002922 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002923 ret |= MUX_STATUS_READY;
2924 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02002925 case MUX_EXIT_STATUS:
2926 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002927 default:
2928 return -1;
2929 }
2930}
2931
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002932/* for debugging with CLI's "show fd" command */
2933static void h1_show_fd(struct buffer *msg, struct connection *conn)
2934{
2935 struct h1c *h1c = conn->ctx;
2936 struct h1s *h1s = h1c->h1s;
2937
Christopher Fauletf376a312019-01-04 15:16:06 +01002938 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2939 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002940 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2941 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2942 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2943 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2944
2945 if (h1s) {
2946 char *method;
2947
2948 if (h1s->meth < HTTP_METH_OTHER)
2949 method = http_known_methods[h1s->meth].ptr;
2950 else
2951 method = "UNKNOWN";
2952 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2953 " .meth=%s status=%d",
2954 h1s, h1s->flags,
2955 h1m_state_str(h1s->req.state),
2956 h1m_state_str(h1s->res.state), method, h1s->status);
2957 if (h1s->cs)
2958 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2959 h1s->cs->flags, h1s->cs->data);
2960 }
Christopher Faulet98fbe952019-07-22 16:18:24 +02002961}
2962
2963
2964/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2965static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2966{
2967 struct h1_hdr_entry *entry;
2968
2969 /* Be sure there is a non-empty <to> */
2970 if (!strlen(to)) {
2971 memprintf(err, "expect <to>");
2972 return -1;
2973 }
2974
2975 /* Be sure only the case differs between <from> and <to> */
2976 if (strcasecmp(from, to)) {
2977 memprintf(err, "<from> and <to> must not differ execpt the case");
2978 return -1;
2979 }
2980
2981 /* Be sure <from> does not already existsin the tree */
2982 if (ebis_lookup(&hdrs_map.map, from)) {
2983 memprintf(err, "duplicate entry '%s'", from);
2984 return -1;
2985 }
2986
2987 /* Create the entry and insert it in the tree */
2988 entry = malloc(sizeof(*entry));
2989 if (!entry) {
2990 memprintf(err, "out of memory");
2991 return -1;
2992 }
2993
2994 entry->node.key = strdup(from);
2995 entry->name.ptr = strdup(to);
2996 entry->name.len = strlen(to);
2997 if (!entry->node.key || !entry->name.ptr) {
2998 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01002999 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003000 free(entry);
3001 memprintf(err, "out of memory");
3002 return -1;
3003 }
3004 ebis_insert(&hdrs_map.map, &entry->node);
3005 return 0;
3006}
3007
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003008/* Migrate the the connection to the current thread.
3009 * Return 0 if successful, non-zero otherwise.
3010 * Expected to be called with the old thread lock held.
3011 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003012static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003013{
3014 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003015 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003016
3017 if (fd_takeover(conn->handle.fd, conn) != 0)
3018 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003019
3020 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3021 /* We failed to takeover the xprt, even if the connection may
3022 * still be valid, flag it as error'd, as we have already
3023 * taken over the fd, and wake the tasklet, so that it will
3024 * destroy it.
3025 */
3026 conn->flags |= CO_FL_ERROR;
3027 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3028 return -1;
3029 }
3030
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003031 if (h1c->wait_event.events)
3032 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3033 h1c->wait_event.events, &h1c->wait_event);
3034 /* To let the tasklet know it should free itself, and do nothing else,
3035 * set its context to NULL.
3036 */
3037 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003038 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003039
3040 task = h1c->task;
3041 if (task) {
3042 task->context = NULL;
3043 h1c->task = NULL;
3044 __ha_barrier_store();
3045 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003046
3047 h1c->task = task_new(tid_bit);
3048 if (!h1c->task) {
3049 h1_release(h1c);
3050 return -1;
3051 }
3052 h1c->task->process = h1_timeout_task;
3053 h1c->task->context = h1c;
3054 }
3055 h1c->wait_event.tasklet = tasklet_new();
3056 if (!h1c->wait_event.tasklet) {
3057 h1_release(h1c);
3058 return -1;
3059 }
3060 h1c->wait_event.tasklet->process = h1_io_cb;
3061 h1c->wait_event.tasklet->context = h1c;
3062 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3063 SUB_RETRY_RECV, &h1c->wait_event);
3064
3065 return 0;
3066}
3067
3068
Christopher Faulet98fbe952019-07-22 16:18:24 +02003069static void h1_hdeaders_case_adjust_deinit()
3070{
3071 struct ebpt_node *node, *next;
3072 struct h1_hdr_entry *entry;
3073
3074 node = ebpt_first(&hdrs_map.map);
3075 while (node) {
3076 next = ebpt_next(node);
3077 ebpt_delete(node);
3078 entry = container_of(node, struct h1_hdr_entry, node);
3079 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003080 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003081 free(entry);
3082 node = next;
3083 }
3084 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003085}
3086
Christopher Faulet98fbe952019-07-22 16:18:24 +02003087static int cfg_h1_headers_case_adjust_postparser()
3088{
3089 FILE *file = NULL;
3090 char *c, *key_beg, *key_end, *value_beg, *value_end;
3091 char *err;
3092 int rc, line = 0, err_code = 0;
3093
3094 if (!hdrs_map.name)
3095 goto end;
3096
3097 file = fopen(hdrs_map.name, "r");
3098 if (!file) {
3099 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3100 hdrs_map.name);
3101 err_code |= ERR_ALERT | ERR_FATAL;
3102 goto end;
3103 }
3104
3105 /* now parse all lines. The file may contain only two header name per
3106 * line, separated by spaces. All heading and trailing spaces will be
3107 * ignored. Lines starting with a # are ignored.
3108 */
3109 while (fgets(trash.area, trash.size, file) != NULL) {
3110 line++;
3111 c = trash.area;
3112
3113 /* strip leading spaces and tabs */
3114 while (*c == ' ' || *c == '\t')
3115 c++;
3116
3117 /* ignore emptu lines, or lines beginning with a dash */
3118 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3119 continue;
3120
3121 /* look for the end of the key */
3122 key_beg = c;
3123 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3124 c++;
3125 key_end = c;
3126
3127 /* strip middle spaces and tabs */
3128 while (*c == ' ' || *c == '\t')
3129 c++;
3130
3131 /* look for the end of the value, it is the end of the line */
3132 value_beg = c;
3133 while (*c && *c != '\n' && *c != '\r')
3134 c++;
3135 value_end = c;
3136
3137 /* trim possibly trailing spaces and tabs */
3138 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3139 value_end--;
3140
3141 /* set final \0 and check entries */
3142 *key_end = '\0';
3143 *value_end = '\0';
3144
3145 err = NULL;
3146 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3147 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003148 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3149 hdrs_map.name, err, line);
3150 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003151 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003152 goto end;
3153 }
3154 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003155 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3156 hdrs_map.name, err, line);
3157 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003158 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003159 }
3160 }
3161
3162 end:
3163 if (file)
3164 fclose(file);
3165 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3166 return err_code;
3167}
3168
3169
3170/* config parser for global "h1-outgoing-header-case-adjust" */
3171static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3172 struct proxy *defpx, const char *file, int line,
3173 char **err)
3174{
3175 if (too_many_args(2, args, err, NULL))
3176 return -1;
3177 if (!*(args[1]) || !*(args[2])) {
3178 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3179 return -1;
3180 }
3181 return add_hdr_case_adjust(args[1], args[2], err);
3182}
3183
3184/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3185static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3186 struct proxy *defpx, const char *file, int line,
3187 char **err)
3188{
3189 if (too_many_args(1, args, err, NULL))
3190 return -1;
3191 if (!*(args[1])) {
3192 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3193 return -1;
3194 }
3195 free(hdrs_map.name);
3196 hdrs_map.name = strdup(args[1]);
3197 return 0;
3198}
3199
3200
3201/* config keyword parsers */
3202static struct cfg_kw_list cfg_kws = {{ }, {
3203 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3204 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3205 { 0, NULL, NULL },
3206 }
3207};
3208
3209INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3210REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3211
3212
Christopher Faulet51dbc942018-09-13 09:05:15 +02003213/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003214/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003215/****************************************/
3216
3217/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003218static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003219 .init = h1_init,
3220 .wake = h1_wake,
3221 .attach = h1_attach,
3222 .get_first_cs = h1_get_first_cs,
3223 .detach = h1_detach,
3224 .destroy = h1_destroy,
3225 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003226 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003227 .rcv_buf = h1_rcv_buf,
3228 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003229#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003230 .rcv_pipe = h1_rcv_pipe,
3231 .snd_pipe = h1_snd_pipe,
3232#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003233 .subscribe = h1_subscribe,
3234 .unsubscribe = h1_unsubscribe,
3235 .shutr = h1_shutr,
3236 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003237 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003238 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003239 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003240 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003241 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003242 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003243};
3244
3245
3246/* this mux registers default HTX proto */
3247static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003248{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003249
Willy Tarreau0108d902018-11-25 19:14:37 +01003250INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3251
Christopher Faulet51dbc942018-09-13 09:05:15 +02003252/*
3253 * Local variables:
3254 * c-indent-level: 8
3255 * c-basic-offset: 8
3256 * End:
3257 */