blob: cefba3d9dbd476cf7b61bdf65525457c3d8f39d0 [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 Faulet7b109f22019-12-05 11:18:31 +010044#define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */
Christopher Faulet539e0292018-11-19 10:40:09 +010045/* 0x00000040 - 0x00000800 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet51dbc942018-09-13 09:05:15 +020048#define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */
49#define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */
Christopher Faulet7b109f22019-12-05 11:18:31 +010050#define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +010051#define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused
52 * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */
Christopher Faulet51dbc942018-09-13 09:05:15 +020053
Christopher Fauletf2824e62018-10-01 12:12:37 +020054#define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */
Christopher Faulet0ef372a2019-04-08 10:57:20 +020055#define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */
Christopher Faulet129817b2018-09-20 16:14:40 +020056
Christopher Faulet46230362019-10-17 16:04:20 +020057#define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
58#define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
59
Christopher Faulet51dbc942018-09-13 09:05:15 +020060/*
61 * H1 Stream flags (32 bits)
62 */
Christopher Faulet129817b2018-09-20 16:14:40 +020063#define H1S_F_NONE 0x00000000
64#define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */
Christopher Fauletf2824e62018-10-01 12:12:37 +020065#define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */
66#define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020067#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020068#define H1S_F_WANT_KAL 0x00000010
69#define H1S_F_WANT_TUN 0x00000020
70#define H1S_F_WANT_CLO 0x00000040
71#define H1S_F_WANT_MSK 0x00000070
72#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet539e0292018-11-19 10:40:09 +010073#define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */
Christopher Fauletd44ad5b2018-11-19 21:52:12 +010074#define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */
Christopher Faulet76014fd2019-12-10 11:47:22 +010075#define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */
76/* 0x00000800 .. 0x00001000 unused */
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020077#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 +020078#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020079
80/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020081struct h1c {
82 struct connection *conn;
83 struct proxy *px;
84 uint32_t flags; /* Connection flags: H1C_F_* */
85
86 struct buffer ibuf; /* Input buffer to store data before parsing */
87 struct buffer obuf; /* Output buffer to store data after reformatting */
88
89 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
90 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
91
92 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +010093 struct task *task; /* timeout management task */
94 int timeout; /* idle timeout duration in ticks */
95 int shut_timeout; /* idle timeout duration in ticks after stream shutdown */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096};
97
98/* H1 stream descriptor */
99struct h1s {
100 struct h1c *h1c;
101 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100102 struct cs_info csinfo; /* CS info, only used for client connections */
103 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200104
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100105 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200106
Olivier Houchardf502aca2018-12-14 19:42:40 +0100107 struct session *sess; /* Associated session */
Christopher Faulet129817b2018-09-20 16:14:40 +0200108 struct h1m req;
109 struct h1m res;
110
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500111 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200112 uint16_t status; /* HTTP response status */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200113};
114
Christopher Faulet98fbe952019-07-22 16:18:24 +0200115/* Map of headers used to convert outgoing headers */
116struct h1_hdrs_map {
117 char *name;
118 struct eb_root map;
119};
120
121/* An entry in a headers map */
122struct h1_hdr_entry {
123 struct ist name;
124 struct ebpt_node node;
125};
126
127/* Declare the headers map */
128static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
129
130
Christopher Faulet6b81df72019-10-01 22:08:43 +0200131/* trace source and events */
132static void h1_trace(enum trace_level level, uint64_t mask,
133 const struct trace_source *src,
134 const struct ist where, const struct ist func,
135 const void *a1, const void *a2, const void *a3, const void *a4);
136
137/* The event representation is split like this :
138 * h1c - internal H1 connection
139 * h1s - internal H1 stream
140 * strm - application layer
141 * rx - data receipt
142 * tx - data transmission
143 *
144 */
145static const struct trace_event h1_trace_events[] = {
146#define H1_EV_H1C_NEW (1ULL << 0)
147 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
148#define H1_EV_H1C_RECV (1ULL << 1)
149 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
150#define H1_EV_H1C_SEND (1ULL << 2)
151 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
152#define H1_EV_H1C_BLK (1ULL << 3)
153 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
154#define H1_EV_H1C_WAKE (1ULL << 4)
155 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
156#define H1_EV_H1C_END (1ULL << 5)
157 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
158#define H1_EV_H1C_ERR (1ULL << 6)
159 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
160
161#define H1_EV_RX_DATA (1ULL << 7)
162 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
163#define H1_EV_RX_EOI (1ULL << 8)
164 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
165#define H1_EV_RX_HDRS (1ULL << 9)
166 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
167#define H1_EV_RX_BODY (1ULL << 10)
168 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
169#define H1_EV_RX_TLRS (1ULL << 11)
170 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
171
172#define H1_EV_TX_DATA (1ULL << 12)
173 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
174#define H1_EV_TX_EOI (1ULL << 13)
175 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
176#define H1_EV_TX_HDRS (1ULL << 14)
177 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
178#define H1_EV_TX_BODY (1ULL << 15)
179 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
180#define H1_EV_TX_TLRS (1ULL << 16)
181 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
182
183#define H1_EV_H1S_NEW (1ULL << 17)
184 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
185#define H1_EV_H1S_BLK (1ULL << 18)
186 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
187#define H1_EV_H1S_END (1ULL << 19)
188 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
189#define H1_EV_H1S_ERR (1ULL << 20)
190 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
191
192#define H1_EV_STRM_NEW (1ULL << 21)
193 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
194#define H1_EV_STRM_RECV (1ULL << 22)
195 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
196#define H1_EV_STRM_SEND (1ULL << 23)
197 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
198#define H1_EV_STRM_WAKE (1ULL << 24)
199 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
200#define H1_EV_STRM_SHUT (1ULL << 25)
201 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
202#define H1_EV_STRM_END (1ULL << 26)
203 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
204#define H1_EV_STRM_ERR (1ULL << 27)
205 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
206
207 { }
208};
209
210static const struct name_desc h1_trace_lockon_args[4] = {
211 /* arg1 */ { /* already used by the connection */ },
212 /* arg2 */ { .name="h1s", .desc="H1 stream" },
213 /* arg3 */ { },
214 /* arg4 */ { }
215};
216
217static const struct name_desc h1_trace_decoding[] = {
218#define H1_VERB_CLEAN 1
219 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
220#define H1_VERB_MINIMAL 2
221 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
222#define H1_VERB_SIMPLE 3
223 { .name="simple", .desc="add request/response status line or htx info when available" },
224#define H1_VERB_ADVANCED 4
225 { .name="advanced", .desc="add header fields or frame decoding when available" },
226#define H1_VERB_COMPLETE 5
227 { .name="complete", .desc="add full data dump when available" },
228 { /* end */ }
229};
230
231static struct trace_source trace_h1 = {
232 .name = IST("h1"),
233 .desc = "HTTP/1 multiplexer",
234 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
235 .default_cb = h1_trace,
236 .known_events = h1_trace_events,
237 .lockon_args = h1_trace_lockon_args,
238 .decoding = h1_trace_decoding,
239 .report_events = ~0, // report everything by default
240};
241
242#define TRACE_SOURCE &trace_h1
243INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
244
Christopher Faulet51dbc942018-09-13 09:05:15 +0200245/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100246DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
247DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200248
Christopher Faulet51dbc942018-09-13 09:05:15 +0200249static int h1_recv(struct h1c *h1c);
250static int h1_send(struct h1c *h1c);
251static int h1_process(struct h1c *h1c);
252static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Christopher Faulet666a0c42019-01-08 11:12:04 +0100253static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100254static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200255static void h1_wake_stream_for_recv(struct h1s *h1s);
256static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200257
Christopher Faulet6b81df72019-10-01 22:08:43 +0200258/* the H1 traces always expect that arg1, if non-null, is of type connection
259 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
260 * that arg3, if non-null, is a htx for rx/tx headers.
261 */
262static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
263 const struct ist where, const struct ist func,
264 const void *a1, const void *a2, const void *a3, const void *a4)
265{
266 const struct connection *conn = a1;
267 const struct h1c *h1c = conn ? conn->ctx : NULL;
268 const struct h1s *h1s = a2;
269 const struct htx *htx = a3;
270 const size_t *val = a4;
271
272 if (!h1c)
273 h1c = (h1s ? h1s->h1c : NULL);
274
275 if (!h1c || src->verbosity < H1_VERB_CLEAN)
276 return;
277
278 /* Display frontend/backend info by default */
279 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(h1c->conn) ? 'B' : 'F'));
280
281 /* Display request and response states if h1s is defined */
282 if (h1s)
283 chunk_appendf(&trace_buf, " [%s, %s]",
284 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
285
286 if (src->verbosity == H1_VERB_CLEAN)
287 return;
288
289 /* Display the value to the 4th argument (level > STATE) */
290 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100291 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200292
293 /* Display status-line if possible (verbosity > MINIMAL) */
294 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
295 const struct htx_blk *blk = htx_get_head_blk(htx);
296 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
297 enum htx_blk_type type = htx_get_blk_type(blk);
298
299 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
300 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
301 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
302 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
303 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
304 }
305
306 /* Display h1c info and, if defined, h1s info (pointer + flags) */
307 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
308 if (h1s)
309 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
310
311 if (src->verbosity == H1_VERB_MINIMAL)
312 return;
313
314 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
315 if (src->level > TRACE_LEVEL_USER) {
316 if (src->verbosity == H1_VERB_COMPLETE ||
317 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
318 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
319 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
320 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
321 if (src->verbosity == H1_VERB_COMPLETE ||
322 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
323 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
324 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
325 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
326 }
327
328 /* Display htx info if defined (level > USER) */
329 if (src->level > TRACE_LEVEL_USER && htx) {
330 int full = 0;
331
332 /* Full htx info (level > STATE && verbosity > SIMPLE) */
333 if (src->level > TRACE_LEVEL_STATE) {
334 if (src->verbosity == H1_VERB_COMPLETE)
335 full = 1;
336 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
337 full = 1;
338 }
339
340 chunk_memcat(&trace_buf, "\n\t", 2);
341 htx_dump(&trace_buf, htx, full);
342 }
343}
344
345
Christopher Faulet51dbc942018-09-13 09:05:15 +0200346/*****************************************************/
347/* functions below are for dynamic buffer management */
348/*****************************************************/
349/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100350 * Indicates whether or not we may receive data. The rules are the following :
351 * - if an error or a shutdown for reads was detected on the connection we
352 must not attempt to receive
353 * - if the input buffer failed to be allocated or is full , we must not try
354 * to receive
355 * - if he input processing is busy waiting for the output side, we may
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500356 * attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200357 * - otherwise must may not attempt to receive
358 */
359static inline int h1_recv_allowed(const struct h1c *h1c)
360{
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100361 if (h1c->flags & H1C_F_CS_ERROR) {
362 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 +0200363 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200364 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200365
Willy Tarreau2febb842020-07-31 09:15:43 +0200366 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
367 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 +0100368 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200369 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100370
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200371 if (conn_is_back(h1c->conn) && h1c->h1s && h1c->h1s->req.state == H1_MSG_RQBEFORE) {
372 TRACE_DEVEL("recv not allowed because back and request not sent yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
373 return 0;
374 }
375
Christopher Faulet65520542021-04-08 18:34:30 +0200376 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377 return 1;
378
Christopher Faulet6b81df72019-10-01 22:08:43 +0200379 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 +0200380 return 0;
381}
382
383/*
384 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
385 * flags are used to figure what buffer was requested. It returns 1 if the
386 * allocation succeeds, in which case the connection is woken up, or 0 if it's
387 * impossible to wake up and we prefer to be woken up later.
388 */
389static int h1_buf_available(void *target)
390{
391 struct h1c *h1c = target;
392
393 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200394 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 +0200395 h1c->flags &= ~H1C_F_IN_ALLOC;
396 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200397 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200398 return 1;
399 }
400
401 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200402 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 +0200403 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200404 if (h1c->h1s)
405 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200406 return 1;
407 }
408
Christopher Faulet51dbc942018-09-13 09:05:15 +0200409 return 0;
410}
411
412/*
413 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
414 */
415static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
416{
417 struct buffer *buf = NULL;
418
Willy Tarreau954827a2021-02-20 11:49:49 +0100419 if (likely(!LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200420 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
421 h1c->buf_wait.target = h1c;
422 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau954827a2021-02-20 11:49:49 +0100423 LIST_ADDQ(&ti->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200424 }
425 return buf;
426}
427
428/*
429 * Release a buffer, if any, and try to wake up entities waiting in the buffer
430 * wait queue.
431 */
432static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
433{
434 if (bptr->size) {
435 b_free(bptr);
Willy Tarreau132b3a42021-02-20 12:02:46 +0100436 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200437 }
438}
439
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100440/* returns the number of streams in use on a connection to figure if it's idle
441 * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or
442 * not. This flag is only set when no H1S is attached and when the previous
443 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100444 */
445static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200446{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100447 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100449 return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200450}
451
Willy Tarreau00f18a32019-01-26 12:19:01 +0100452/* returns the number of streams still available on a connection */
453static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100454{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100455 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100456}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200457
Christopher Faulet7a145d62020-08-05 11:31:16 +0200458/* Refresh the h1c task timeout if necessary */
459static void h1_refresh_timeout(struct h1c *h1c)
460{
461 if (h1c->task) {
462 h1c->task->expire = TICK_ETERNITY;
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200463 if (h1c->flags & H1C_F_CS_SHUTDOWN) {
464 /* half-closed connections switch to clientfin/serverfin
465 * timeouts so that we don't hang too long on clients
466 * that have gone away (especially in tunnel mode).
467 */
468 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
469 task_queue(h1c->task);
470 TRACE_DEVEL("refreshing connection's timeout (half-closed)", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletbcc51622020-12-01 11:42:53 +0100471 } else if (b_data(&h1c->obuf)) {
472 /* any connection with pending data, need a timeout (server or client).
Christopher Faulet7a145d62020-08-05 11:31:16 +0200473 */
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200474 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
Christopher Faulet7a145d62020-08-05 11:31:16 +0200475 ? h1c->shut_timeout
476 : h1c->timeout));
477 task_queue(h1c->task);
478 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletbcc51622020-12-01 11:42:53 +0100479 } else if ((h1c->flags & (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) && !conn_is_back(h1c->conn)) {
480 /* front connections waiting for a stream need a timeout. client timeout by
481 * default but http-keep-alive if defined
482 */
483 int timeout = h1c->timeout;
484
485 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
486 timeout = tick_first(timeout, h1c->px->timeout.httpka);
487
488 h1c->task->expire = tick_add(now_ms, ((h1c->flags & H1C_F_CS_SHUTW_NOW)
489 ? h1c->shut_timeout
490 : timeout));
491 task_queue(h1c->task);
492 TRACE_DEVEL("refreshing connection's timeout", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200493 }
494 }
495}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200496/*****************************************************************/
497/* functions below are dedicated to the mux setup and management */
498/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200499
500/* returns non-zero if there are input data pending for stream h1s. */
501static inline size_t h1s_data_pending(const struct h1s *h1s)
502{
503 const struct h1m *h1m;
504
505 h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req;
506 if (h1m->state == H1_MSG_DONE)
Christopher Faulet76014fd2019-12-10 11:47:22 +0100507 return !(h1s->flags & H1S_F_PARSING_DONE);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200508
509 return b_data(&h1s->h1c->ibuf);
510}
511
Christopher Faulet47365272018-10-31 17:40:50 +0100512static struct conn_stream *h1s_new_cs(struct h1s *h1s)
513{
514 struct conn_stream *cs;
515
Christopher Faulet6b81df72019-10-01 22:08:43 +0200516 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200517 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200518 if (!cs) {
519 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 +0100520 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200521 }
Christopher Faulet47365272018-10-31 17:40:50 +0100522 h1s->cs = cs;
523 cs->ctx = h1s;
524
525 if (h1s->flags & H1S_F_NOT_FIRST)
526 cs->flags |= CS_FL_NOT_FIRST;
527
Christopher Faulet27182292020-07-03 15:08:49 +0200528 if (global.tune.options & GTUNE_USE_SPLICE) {
529 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100530 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200531 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100532
Christopher Faulet6b81df72019-10-01 22:08:43 +0200533 if (stream_create_from_cs(cs) < 0) {
534 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 +0100535 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200536 }
537
538 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100539 return cs;
540
541 err:
542 cs_free(cs);
543 h1s->cs = NULL;
544 return NULL;
545}
546
Olivier Houchardf502aca2018-12-14 19:42:40 +0100547static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200548{
549 struct h1s *h1s;
550
Christopher Faulet6b81df72019-10-01 22:08:43 +0200551 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
552
Christopher Faulet51dbc942018-09-13 09:05:15 +0200553 h1s = pool_alloc(pool_head_h1s);
554 if (!h1s)
Christopher Faulet47365272018-10-31 17:40:50 +0100555 goto fail;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200556
557 h1s->h1c = h1c;
558 h1c->h1s = h1s;
559
Olivier Houchardf502aca2018-12-14 19:42:40 +0100560 h1s->sess = sess;
561
Christopher Faulet51dbc942018-09-13 09:05:15 +0200562 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100563 h1s->flags = H1S_F_WANT_KAL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200564
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100565 h1s->subs = NULL;
Christopher Faulet129817b2018-09-20 16:14:40 +0200566
567 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100568 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200569
Christopher Faulet129817b2018-09-20 16:14:40 +0200570 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100571 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200572
573 h1s->status = 0;
574 h1s->meth = HTTP_METH_OTHER;
575
Christopher Faulet47365272018-10-31 17:40:50 +0100576 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
577 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100578 h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet47365272018-10-31 17:40:50 +0100579
Christopher Faulet129817b2018-09-20 16:14:40 +0200580 if (!conn_is_back(h1c->conn)) {
581 if (h1c->px->options2 & PR_O2_REQBUG_OK)
582 h1s->req.err_pos = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200583
584 /* For frontend connections we should always have a session */
585 if (!sess)
Christopher Faulete9da9752020-09-30 15:00:13 +0200586 h1s->sess = sess = h1c->conn->owner;
Christopher Faulete9b70722019-04-08 10:46:02 +0200587
Dave Pirotte234740f2019-07-10 13:57:38 +0000588 /* Timers for subsequent sessions on the same HTTP 1.x connection
589 * measure from `now`, not from the connection accept time */
590 if (h1s->flags & H1S_F_NOT_FIRST) {
591 h1s->csinfo.create_date = date;
592 h1s->csinfo.tv_create = now;
593 h1s->csinfo.t_handshake = 0;
594 h1s->csinfo.t_idle = -1;
595 }
596 else {
597 h1s->csinfo.create_date = sess->accept_date;
598 h1s->csinfo.tv_create = sess->tv_accept;
599 h1s->csinfo.t_handshake = sess->t_handshake;
600 h1s->csinfo.t_idle = -1;
601 }
Christopher Faulet129817b2018-09-20 16:14:40 +0200602 }
603 else {
604 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
605 h1s->res.err_pos = -1;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200606
Christopher Fauletfeb11742018-11-29 15:12:34 +0100607 h1s->csinfo.create_date = date;
608 h1s->csinfo.tv_create = now;
609 h1s->csinfo.t_handshake = 0;
610 h1s->csinfo.t_idle = -1;
Christopher Faulete9b70722019-04-08 10:46:02 +0200611 }
Christopher Fauletfeb11742018-11-29 15:12:34 +0100612
Christopher Faulete9b70722019-04-08 10:46:02 +0200613 /* If a conn_stream already exists, attach it to this H1S. Otherwise we
614 * create a new one.
615 */
616 if (cs) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200617 cs->ctx = h1s;
618 h1s->cs = cs;
619 }
Christopher Faulet47365272018-10-31 17:40:50 +0100620 else {
621 cs = h1s_new_cs(h1s);
622 if (!cs)
623 goto fail;
624 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200625 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200626 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100627
628 fail:
629 pool_free(pool_head_h1s, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200630 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 +0100631 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200632}
633
634static void h1s_destroy(struct h1s *h1s)
635{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200636 if (h1s) {
637 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200638
Christopher Faulet6b81df72019-10-01 22:08:43 +0200639 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200640 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200641
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100642 if (h1s->subs)
643 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644
Christopher Fauletcb55f482018-12-10 11:56:47 +0100645 h1c->flags &= ~H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200646 if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100647 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200648 TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
649 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100650
651 if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */
652 !(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 +0100653 (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 +0100654 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
655 h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ);
656 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
657 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200658 pool_free(pool_head_h1s, h1s);
659 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200660}
661
Christopher Fauletfeb11742018-11-29 15:12:34 +0100662static const struct cs_info *h1_get_cs_info(struct conn_stream *cs)
663{
664 struct h1s *h1s = cs->ctx;
665
666 if (h1s && !conn_is_back(cs->conn))
667 return &h1s->csinfo;
668 return NULL;
669}
670
Christopher Faulet51dbc942018-09-13 09:05:15 +0200671/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200672 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500673 * to the existing conn_stream (for outgoing connections or for incoming ones
674 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200675 * establishment). <input> is always used as Input buffer and may contain
676 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
677 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200678 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200679static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
680 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200681{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200682 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100683 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200684 void *conn_ctx = conn->ctx;
685
686 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200687
688 h1c = pool_alloc(pool_head_h1c);
689 if (!h1c)
690 goto fail_h1c;
691 h1c->conn = conn;
692 h1c->px = proxy;
693
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100694 h1c->flags = H1C_F_CS_IDLE;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200695 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200696 h1c->obuf = BUF_NULL;
697 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200698 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200699
Willy Tarreau954827a2021-02-20 11:49:49 +0100700 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200701 h1c->wait_event.tasklet = tasklet_new();
702 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200703 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200704 h1c->wait_event.tasklet->process = h1_io_cb;
705 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100706 h1c->wait_event.events = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200707
Christopher Faulete9b70722019-04-08 10:46:02 +0200708 if (conn_is_back(conn)) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100709 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
710 if (tick_isset(proxy->timeout.serverfin))
711 h1c->shut_timeout = proxy->timeout.serverfin;
712 } else {
713 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
714 if (tick_isset(proxy->timeout.clientfin))
715 h1c->shut_timeout = proxy->timeout.clientfin;
716 }
717 if (tick_isset(h1c->timeout)) {
718 t = task_new(tid_bit);
719 if (!t)
720 goto fail;
721
722 h1c->task = t;
723 t->process = h1_timeout_task;
724 t->context = h1c;
725 t->expire = tick_add(now_ms, h1c->timeout);
726 }
727
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100728 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200729
Christopher Faulet6b81df72019-10-01 22:08:43 +0200730 /* Always Create a new H1S */
731 if (!h1s_create(h1c, conn_ctx, sess))
732 goto fail;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100733
734 if (t)
735 task_queue(t);
736
Christopher Faulet51dbc942018-09-13 09:05:15 +0200737 /* Try to read, if nothing is available yet we'll just subscribe */
Willy Tarreau2c1f37d2020-03-04 17:50:02 +0100738 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200739
740 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200741 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200742 return 0;
743
744 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200745 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200746 if (h1c->wait_event.tasklet)
747 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200748 pool_free(pool_head_h1c, h1c);
749 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200750 conn->ctx = conn_ctx; // restore saved context
751 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200752 return -1;
753}
754
Christopher Faulet73c12072019-04-08 11:23:22 +0200755/* release function. This one should be called to free all resources allocated
756 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200757 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200758static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200759{
Christopher Faulet61840e72019-04-15 09:33:32 +0200760 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200761
Christopher Faulet6b81df72019-10-01 22:08:43 +0200762 TRACE_POINT(H1_EV_H1C_END);
763
Christopher Faulet51dbc942018-09-13 09:05:15 +0200764 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200765 /* The connection must be aattached to this mux to be released */
766 if (h1c->conn && h1c->conn->ctx == h1c)
767 conn = h1c->conn;
768
Christopher Faulet6b81df72019-10-01 22:08:43 +0200769 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
770
Christopher Faulet61840e72019-04-15 09:33:32 +0200771 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200772 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200773 h1c->flags &= ~H1C_F_UPG_H2C;
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200774 /* Make sure we're no longer subscribed to anything */
775 if (h1c->wait_event.events)
776 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
777 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200778 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200779 /* connection successfully upgraded to H2, this
780 * mux was already released */
781 return;
782 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200783 TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200784 sess_log(conn->owner); /* Log if the upgrade failed */
785 }
786
Olivier Houchard45c44372019-06-11 14:06:23 +0200787
Willy Tarreau954827a2021-02-20 11:49:49 +0100788 if (LIST_ADDED(&h1c->buf_wait.list))
789 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200790
791 h1_release_buf(h1c, &h1c->ibuf);
792 h1_release_buf(h1c, &h1c->obuf);
793
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100794 if (h1c->task) {
795 h1c->task->context = NULL;
796 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
797 h1c->task = NULL;
798 }
799
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200800 if (h1c->wait_event.tasklet)
801 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200802
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200804 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100805 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200806 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200807 pool_free(pool_head_h1c, h1c);
808 }
809
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200810 if (conn) {
811 conn->mux = NULL;
812 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200813 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200814
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200815 conn_stop_tracking(conn);
816 conn_full_close(conn);
817 if (conn->destroy_cb)
818 conn->destroy_cb(conn);
819 conn_free(conn);
820 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200821}
822
823/******************************************************/
824/* functions below are for the H1 protocol processing */
825/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200826/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
827 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200828 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100829static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200830{
Christopher Faulet570d1612018-11-26 11:13:57 +0100831 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200832
Christopher Faulet570d1612018-11-26 11:13:57 +0100833 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200834 (*(p + 5) > '1' ||
835 (*(p + 5) == '1' && *(p + 7) >= '1')))
836 h1m->flags |= H1_MF_VER_11;
837}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200838
Christopher Faulet9768c262018-10-22 09:34:31 +0200839/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
840 * greater or equal to 1.1
841 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100842static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200843{
Christopher Faulet570d1612018-11-26 11:13:57 +0100844 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200845
Christopher Faulet570d1612018-11-26 11:13:57 +0100846 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200847 (*(p + 5) > '1' ||
848 (*(p + 5) == '1' && *(p + 7) >= '1')))
849 h1m->flags |= H1_MF_VER_11;
850}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200851
Christopher Fauletf2824e62018-10-01 12:12:37 +0200852/* Deduce the connection mode of the client connection, depending on the
853 * configuration and the H1 message flags. This function is called twice, the
854 * first time when the request is parsed and the second time when the response
855 * is parsed.
856 */
857static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
858{
859 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200860
861 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100862 /* Output direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200863 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100864 h1s->status == 101) {
865 /* Either we've established an explicit tunnel, or we're
866 * switching the protocol. In both cases, we're very unlikely to
867 * understand the next protocols. We have to switch to tunnel
868 * mode, so that we transfer the request and responses then let
869 * this protocol pass unmodified. When we later implement
870 * specific parsers for such protocols, we'll want to check the
871 * Upgrade header which contains information about that protocol
872 * for responses with status 101 (eg: see RFC2817 about TLS).
873 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200874 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200875 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 +0100876 }
877 else if (h1s->flags & H1S_F_WANT_KAL) {
878 /* By default the client is in KAL mode. CLOSE mode mean
879 * it is imposed by the client itself. So only change
880 * KAL mode here. */
881 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
882 /* no length known or explicit close => close */
883 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200884 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 +0100885 }
886 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
887 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500888 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +0100889 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200890 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 +0100891 }
892 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200893 }
894 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100895 /* Input direction: first pass */
896 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
897 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +0200898 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200899 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 +0100900 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200901 }
902
903 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200904 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200905 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200906 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);
907 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200908}
909
910/* Deduce the connection mode of the client connection, depending on the
911 * configuration and the H1 message flags. This function is called twice, the
912 * first time when the request is parsed and the second time when the response
913 * is parsed.
914 */
915static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
916{
Olivier Houchardf502aca2018-12-14 19:42:40 +0100917 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +0100918 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100919 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200920
Christopher Fauletf2824e62018-10-01 12:12:37 +0200921 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100922 /* Input direction: second pass */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200923 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100924 h1s->status == 101) {
925 /* Either we've established an explicit tunnel, or we're
926 * switching the protocol. In both cases, we're very unlikely to
927 * understand the next protocols. We have to switch to tunnel
928 * mode, so that we transfer the request and responses then let
929 * this protocol pass unmodified. When we later implement
930 * specific parsers for such protocols, we'll want to check the
931 * Upgrade header which contains information about that protocol
932 * for responses with status 101 (eg: see RFC2817 about TLS).
933 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200934 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200935 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 +0100936 }
937 else if (h1s->flags & H1S_F_WANT_KAL) {
938 /* By default the server is in KAL mode. CLOSE mode mean
939 * it is imposed by haproxy itself. So only change KAL
940 * mode here. */
941 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
942 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
943 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
944 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200945 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 +0100946 }
947 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200948 }
Christopher Faulet70033782018-12-05 13:50:11 +0100949 else {
Christopher Fauletb992af02019-03-28 15:42:24 +0100950 /* Output direction: first pass */
951 if (h1m->flags & H1_MF_CONN_CLO) {
952 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +0100953 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200954 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 +0100955 }
956 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
957 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
958 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
959 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
960 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
961 /* no explicit keep-alive option httpclose/server-close => close */
962 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200963 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 +0100964 }
Christopher Faulet70033782018-12-05 13:50:11 +0100965 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200966
967 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200968 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +0200969 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200970 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);
971 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200972}
973
Christopher Fauletb992af02019-03-28 15:42:24 +0100974static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200975{
976 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200977
978 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
979 * token is found
980 */
981 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +0200982 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200983
984 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200985 if (!(h1m->flags & H1_MF_VER_11)) {
986 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 +0100987 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200988 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200989 }
990 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200991 if (h1m->flags & H1_MF_VER_11) {
992 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +0200994 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200995 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200996}
997
Christopher Fauletb992af02019-03-28 15:42:24 +0100998static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200999{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001000 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1001 * token is found
1002 */
1003 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001004 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001005
1006 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001007 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001008 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1009 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 +01001010 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001011 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001012 }
1013 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001014 if (h1m->flags & H1_MF_VER_11) {
1015 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001016 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001017 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001018 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001019}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001020
Christopher Fauletb992af02019-03-28 15:42:24 +01001021static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001022{
Christopher Fauletb992af02019-03-28 15:42:24 +01001023 if (!conn_is_back(h1s->h1c->conn))
Christopher Faulet9768c262018-10-22 09:34:31 +02001024 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001025 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001026 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001027}
1028
Christopher Fauletb992af02019-03-28 15:42:24 +01001029static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1030{
1031 if (!conn_is_back(h1s->h1c->conn))
1032 h1_set_cli_conn_mode(h1s, h1m);
1033 else
1034 h1_set_srv_conn_mode(h1s, h1m);
1035
1036 if (!(h1m->flags & H1_MF_RESP))
1037 h1_update_req_conn_value(h1s, h1m, conn_val);
1038 else
1039 h1_update_res_conn_value(h1s, h1m, conn_val);
1040}
Christopher Faulete44769b2018-11-29 23:01:45 +01001041
Christopher Faulet98fbe952019-07-22 16:18:24 +02001042/* Try to adjust the case of the message header name using the global map
1043 * <hdrs_map>.
1044 */
1045static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1046{
1047 struct ebpt_node *node;
1048 struct h1_hdr_entry *entry;
1049
1050 /* No entry in the map, do nothing */
1051 if (eb_is_empty(&hdrs_map.map))
1052 return;
1053
1054 /* No conversion fo the request headers */
1055 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1056 return;
1057
1058 /* No conversion fo the response headers */
1059 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1060 return;
1061
1062 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1063 if (!node)
1064 return;
1065 entry = container_of(node, struct h1_hdr_entry, node);
1066 name->ptr = entry->name.ptr;
1067 name->len = entry->name.len;
1068}
1069
Christopher Faulete44769b2018-11-29 23:01:45 +01001070/* Append the description of what is present in error snapshot <es> into <out>.
1071 * The description must be small enough to always fit in a buffer. The output
1072 * buffer may be the trash so the trash must not be used inside this function.
1073 */
1074static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1075{
1076 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001077 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1078 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1079 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1080 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1081 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1082 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001083}
1084/*
1085 * Capture a bad request or response and archive it in the proxy's structure.
1086 * By default it tries to report the error position as h1m->err_pos. However if
1087 * this one is not set, it will then report h1m->next, which is the last known
1088 * parsing point. The function is able to deal with wrapping buffers. It always
1089 * displays buffers as a contiguous area starting at buf->p. The direction is
1090 * determined thanks to the h1m's flags.
1091 */
1092static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1093 struct h1m *h1m, struct buffer *buf)
1094{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001095 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001096 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001097 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001098 union error_snapshot_ctx ctx;
1099
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001100 if (h1s->cs && h1s->cs->data) {
1101 if (sess == NULL)
1102 sess = si_strm(h1s->cs->data)->sess;
1103 if (!(h1m->flags & H1_MF_RESP))
1104 other_end = si_strm(h1s->cs->data)->be;
1105 else
1106 other_end = sess->fe;
1107 } else
1108 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001109
1110 /* http-specific part now */
1111 ctx.h1.state = h1m->state;
1112 ctx.h1.c_flags = h1c->flags;
1113 ctx.h1.s_flags = h1s->flags;
1114 ctx.h1.m_flags = h1m->flags;
1115 ctx.h1.m_clen = h1m->curr_len;
1116 ctx.h1.m_blen = h1m->body_len;
1117
1118 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1119 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001120 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1121 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001122}
1123
Christopher Fauletadb22202018-12-12 10:32:09 +01001124/* Emit the chunksize followed by a CRLF in front of data of the buffer
1125 * <buf>. It goes backwards and starts with the byte before the buffer's
1126 * head. The caller is responsible for ensuring there is enough room left before
1127 * the buffer's head for the string.
1128 */
1129static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1130{
1131 char *beg, *end;
1132
1133 beg = end = b_head(buf);
1134 *--beg = '\n';
1135 *--beg = '\r';
1136 do {
1137 *--beg = hextab[chksz & 0xF];
1138 } while (chksz >>= 4);
1139 buf->head -= (end - beg);
1140 b_add(buf, end - beg);
1141}
1142
1143/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1144 * ensuring there is enough room left in the buffer for the string. */
1145static void h1_emit_chunk_crlf(struct buffer *buf)
1146{
1147 *(b_peek(buf, b_data(buf))) = '\r';
1148 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1149 b_add(buf, 2);
1150}
1151
Christopher Faulet129817b2018-09-20 16:14:40 +02001152/*
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001153 * Switch the request to tunnel mode. This function must only be called for
Christopher Fauletf3158e92019-11-15 11:14:23 +01001154 * CONNECT requests. On the client side, if the response is not finished, the
1155 * mux is mark as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001156 */
1157static void h1_set_req_tunnel_mode(struct h1s *h1s)
1158{
1159 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1160 h1s->req.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001161 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1162
Christopher Faulet76014fd2019-12-10 11:47:22 +01001163 if (!conn_is_back(h1s->h1c->conn)) {
1164 h1s->flags &= ~H1S_F_PARSING_DONE;
1165 if (h1s->res.state < H1_MSG_DONE) {
1166 h1s->h1c->flags |= H1C_F_IN_BUSY;
1167 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1168 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001169 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001170 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1171 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1172 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1173 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
1174 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001175}
1176
1177/*
1178 * Switch the response to tunnel mode. This function must only be called on
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001179 * successful replies to CONNECT requests or on protocol switching. In this
Christopher Fauletf3158e92019-11-15 11:14:23 +01001180 * last case, this function takes care to switch the request to tunnel mode if
1181 * possible. On the server side, if the request is not finished, the mux is mark
1182 * as busy on input.
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001183 */
1184static void h1_set_res_tunnel_mode(struct h1s *h1s)
1185{
Christopher Fauletf3158e92019-11-15 11:14:23 +01001186
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001187 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1188 h1s->res.state = H1_MSG_TUNNEL;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001189 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1190
Christopher Faulet76014fd2019-12-10 11:47:22 +01001191 if (conn_is_back(h1s->h1c->conn)) {
1192 h1s->flags &= ~H1S_F_PARSING_DONE;
1193 /* On protocol switching, switch the request to tunnel mode if it is in
1194 * DONE state. Otherwise we will wait the end of the request to switch
1195 * it in tunnel mode.
1196 */
1197 if (h1s->req.state < H1_MSG_DONE) {
1198 h1s->h1c->flags |= H1C_F_IN_BUSY;
1199 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s);
1200 }
1201 else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) {
1202 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
1203 h1s->req.state = H1_MSG_TUNNEL;
1204 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1205 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001206 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001207 else if (h1s->h1c->flags & H1C_F_IN_BUSY) {
1208 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
1209 tasklet_wakeup(h1s->h1c->wait_event.tasklet);
1210 TRACE_STATE("h1c no more busy", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001211 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001212}
1213
1214/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001215 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001216 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001217 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001218 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001219static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001220 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001221{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001222 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001223 int ret = 0;
1224
Willy Tarreau022e5e52020-09-10 09:33:15 +02001225 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 +02001226
Christopher Faulet89aed322020-06-02 17:33:56 +02001227 if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
1228 !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */
1229 !(h1m->flags & H1_MF_RESP)) { /* It is a request */
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001230 /* Try to match H2 preface before parsing the request headers. */
1231 ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE));
Christopher Faulet6b81df72019-10-01 22:08:43 +02001232 if (ret > 0) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001233 goto h2c_upgrade;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001234 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001235 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001236 else {
1237 if (h1s->meth == HTTP_METH_CONNECT)
1238 h1m->flags |= H1_MF_METH_CONNECT;
1239 if (h1s->meth == HTTP_METH_HEAD)
1240 h1m->flags |= H1_MF_METH_HEAD;
1241 }
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001242
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001243 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1244 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001245 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 +02001246 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001247 if (!(h1m->flags & H1_MF_RESP)) {
1248 h1s->flags |= H1S_F_REQ_ERROR;
1249 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1250 }
1251 else {
1252 h1s->flags |= H1S_F_RES_ERROR;
1253 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1254 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001255 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001256 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001257 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1258 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001259 goto end;
1260 }
1261
Christopher Faulet486498c2019-10-11 14:22:00 +02001262 if (h1m->err_pos >= 0) {
1263 /* Maybe we found an error during the parsing while we were
1264 * configured not to block on that, so we have to capture it
1265 * now.
1266 */
1267 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1268 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1269 }
1270
Christopher Faulet129817b2018-09-20 16:14:40 +02001271 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001272 h1s->meth = h1sl.rq.meth;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001273 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001274 h1_set_req_tunnel_mode(h1s);
Christopher Faulet129817b2018-09-20 16:14:40 +02001275 }
1276 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001277 h1s->status = h1sl.st.status;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001278 if (h1m->state == H1_MSG_TUNNEL)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001279 h1_set_res_tunnel_mode(h1s);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001280 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001281 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001282 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001283
Christopher Faulet129817b2018-09-20 16:14:40 +02001284 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001285 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 +02001286 return ret;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001287
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001288 h2c_upgrade:
1289 h1s->h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +02001290 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001291 htx->flags |= HTX_FL_UPGRADE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001292 TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1293 return 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001294}
1295
1296/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001297 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001298 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1299 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001300 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001301static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001302 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001303 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001304{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001305 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001306
Willy Tarreau022e5e52020-09-10 09:33:15 +02001307 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 +02001308 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001309 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001310 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 +01001311 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001312 if (!(h1m->flags & H1_MF_RESP)) {
1313 h1s->flags |= H1S_F_REQ_ERROR;
1314 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1315 }
1316 else {
1317 h1s->flags |= H1S_F_RES_ERROR;
1318 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1319 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001320 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001321 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001322 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001323 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001324 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001325 }
1326
Christopher Faulet2eaf3092020-07-03 14:51:15 +02001327 if (h1s->cs && !(h1m->flags & H1_MF_CHNK) &&
Christopher Faulet27182292020-07-03 15:08:49 +02001328 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1329 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001330 h1s->cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001331 }
1332 else if (h1s->cs) {
1333 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001334 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02001335 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01001336
Christopher Faulet02a02532019-11-15 09:36:28 +01001337 *ofs += ret;
1338
1339 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001340 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 +02001341 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001342}
1343
1344/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001345 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1346 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1347 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1348 * responsible to update the parser state <h1m>.
1349 */
1350static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1351 struct buffer *buf, size_t *ofs, size_t max)
1352{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001353 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001354
Willy Tarreau022e5e52020-09-10 09:33:15 +02001355 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 +02001356 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001357 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001358 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 +01001359 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001360 if (!(h1m->flags & H1_MF_RESP)) {
1361 h1s->flags |= H1S_F_REQ_ERROR;
1362 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1363 }
1364 else {
1365 h1s->flags |= H1S_F_RES_ERROR;
1366 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1367 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001368 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001369 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001370 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1371 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001372 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001373 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001374
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001375 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001376
1377 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001378 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 +02001379 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001380}
1381
1382/*
Christopher Faulet76014fd2019-12-10 11:47:22 +01001383 * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't
1384 * proceed. This functions is responsible to update the parser state <h1m>. It
1385 * also add the flag CS_FL_EOI on the CS.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001386 */
Christopher Faulet76014fd2019-12-10 11:47:22 +01001387static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1388 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001389{
Christopher Faulet76014fd2019-12-10 11:47:22 +01001390 int ret;
1391
Willy Tarreau022e5e52020-09-10 09:33:15 +02001392 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 +01001393 ret = h1_parse_msg_eom(h1m, htx, max);
1394 if (!ret) {
1395 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s);
1396 if (htx->flags & HTX_FL_PARSING_ERROR) {
1397 if (!(h1m->flags & H1_MF_RESP)) {
1398 h1s->flags |= H1S_F_REQ_ERROR;
1399 TRACE_USER("rejected H1 request", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1400 }
1401 else {
1402 h1s->flags |= H1S_F_RES_ERROR;
1403 TRACE_USER("rejected H1 response", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1404 }
1405 h1s->cs->flags |= CS_FL_EOI;
1406 TRACE_STATE("parsing error", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1407 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1408 }
1409 goto end;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001410 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001411
Christopher Faulet76014fd2019-12-10 11:47:22 +01001412 h1s->flags |= H1S_F_PARSING_DONE;
Christopher Fauletd50c18f2021-02-08 17:18:01 +01001413 /* Set EOI on conn-stream in DONE state iff:
1414 * - it is a response
1415 * - it is a request but no a protocol upgrade nor a CONNECT
1416 *
1417 * If not set, Wait the response to do so or not depending on the status
1418 * code.
1419 */
1420 if ((h1m->flags & H1_MF_RESP) || ((h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Christopher Fauletfe2d5b12020-12-07 18:21:27 +01001421 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001422 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001423 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 +01001424 return ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001425}
1426
1427/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001428 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001429 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1430 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001431 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001432static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001433{
Christopher Faulet539e0292018-11-19 10:40:09 +01001434 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001435 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001436 struct htx *htx;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001437 size_t ret, data;
Christopher Faulet129817b2018-09-20 16:14:40 +02001438 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001439 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001440
Christopher Faulet539e0292018-11-19 10:40:09 +01001441 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001442 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001443
Christopher Fauletf2824e62018-10-01 12:12:37 +02001444 if (!conn_is_back(h1c->conn)) {
1445 h1m = &h1s->req;
1446 errflag = H1S_F_REQ_ERROR;
1447 }
1448 else {
1449 h1m = &h1s->res;
1450 errflag = H1S_F_RES_ERROR;
1451 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001452
Christopher Faulet74027762019-02-26 14:45:05 +01001453 data = htx->data;
Christopher Faulet0e54d542019-07-04 21:22:34 +02001454 if (h1s->flags & errflag)
1455 goto end;
1456
Christopher Faulet65520542021-04-08 18:34:30 +02001457 if (h1c->flags & H1C_F_IN_BUSY)
1458 goto end;
1459
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001460 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001461 size_t used = htx_used_space(htx);
1462
Christopher Faulet129817b2018-09-20 16:14:40 +02001463 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001464 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001465 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001466 if (!ret)
1467 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001468
1469 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1470 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1471
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001472 if ((h1m->flags & H1_MF_RESP) &&
1473 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1474 h1m_init_res(&h1s->res);
1475 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001476 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001477 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001478 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001479 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001480 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001481 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001482 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001483 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001484
1485 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1486 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001487 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001488 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001489 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1490 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1491 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001492 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001493
Christopher Faulet76014fd2019-12-10 11:47:22 +01001494 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1495 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001496 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001497 else if (h1m->state == H1_MSG_DONE) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001498 if (!(h1s->flags & H1S_F_PARSING_DONE)) {
1499 if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count))
1500 break;
1501
1502 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1503 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
1504 }
1505
Christopher Fauletf3158e92019-11-15 11:14:23 +01001506 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101)
1507 h1_set_req_tunnel_mode(h1s);
Christopher Fauletecae6e92021-02-22 08:11:59 +01001508 else if ((h1m->flags & H1_MF_RESP) && h1s->req.state == H1_MSG_TUNNEL) {
1509 TRACE_STATE("switch back H1 request from tunnel mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1510 h1s->req.state = H1_MSG_DONE;
1511 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001512 else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
Christopher Faulet2f320ee2019-04-16 20:26:53 +02001513 h1c->flags |= H1C_F_IN_BUSY;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001514 TRACE_STATE("switch h1c in busy mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
Christopher Faulet23021ad2020-07-10 10:01:26 +02001515 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001516 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001517 else
1518 break;
Christopher Fauletcb55f482018-12-10 11:56:47 +01001519 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001520 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001521 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001522 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001523 if (!ret)
1524 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001525
1526 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1527 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001528 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001529 else {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001530 h1s->flags |= errflag;
Christopher Faulet129817b2018-09-20 16:14:40 +02001531 break;
1532 }
1533
Christopher Faulet30db3d72019-05-17 15:35:33 +02001534 count -= htx_used_space(htx) - used;
Christopher Faulet6b321922019-09-03 16:26:15 +02001535 } while (!(h1s->flags & errflag));
Christopher Faulet129817b2018-09-20 16:14:40 +02001536
Christopher Faulet6b81df72019-10-01 22:08:43 +02001537 if (h1s->flags & errflag) {
1538 TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +01001539 goto parsing_err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001540 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001541
Christopher Faulet539e0292018-11-19 10:40:09 +01001542 b_del(&h1c->ibuf, total);
1543
1544 end:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001545 htx_to_buf(htx, buf);
Christopher Faulet30db3d72019-05-17 15:35:33 +02001546 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001547 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001548 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001549 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 +01001550 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001551 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001552
Christopher Fauletcf56b992018-12-11 16:12:31 +01001553 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1554
Christopher Fauletcdc90e92019-03-28 13:28:46 +01001555 if (!b_data(&h1c->ibuf))
Christopher Faulet539e0292018-11-19 10:40:09 +01001556 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6716cc22019-12-20 17:33:24 +01001557 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001558 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +01001559 else if (h1s->flags & H1S_F_REOS) {
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001560 h1s->cs->flags |= CS_FL_EOS;
Christopher Fauletfe2d5b12020-12-07 18:21:27 +01001561 if (h1m->state >= H1_MSG_DONE)
Christopher Faulet466080d2019-11-15 09:50:22 +01001562 h1s->cs->flags |= CS_FL_EOI;
1563 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE)
Christopher Fauletb8d2ee02019-02-25 15:29:51 +01001564 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet539e0292018-11-19 10:40:09 +01001565 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001566
Christopher Faulet6b81df72019-10-01 22:08:43 +02001567 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001568 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001569
1570 parsing_err:
Christopher Faulet47365272018-10-31 17:40:50 +01001571 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001572 htx_to_buf(htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001573 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001574 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001575}
1576
Christopher Faulet129817b2018-09-20 16:14:40 +02001577/*
1578 * Process outgoing data. It parses data and transfer them from the channel buffer into
1579 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1580 * 0 if it couldn't proceed.
1581 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001582static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1583{
Christopher Faulet129817b2018-09-20 16:14:40 +02001584 struct h1s *h1s = h1c->h1s;
1585 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001586 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001587 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001588 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001589 size_t total = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001590 int errflag;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001591
Christopher Faulet47365272018-10-31 17:40:50 +01001592 if (!count)
1593 goto end;
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001594
Christopher Faulet94b2c762019-05-24 15:28:57 +02001595 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001596 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1597
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001598 if (htx_is_empty(chn_htx))
1599 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001600
Christopher Faulet51dbc942018-09-13 09:05:15 +02001601 if (!h1_get_buf(h1c, &h1c->obuf)) {
1602 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001603 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 +02001604 goto end;
1605 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001606
Christopher Fauletf2824e62018-10-01 12:12:37 +02001607 if (!conn_is_back(h1c->conn)) {
1608 h1m = &h1s->res;
1609 errflag = H1S_F_RES_ERROR;
1610 }
1611 else {
1612 h1m = &h1s->req;
1613 errflag = H1S_F_REQ_ERROR;
1614 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001615
Christopher Faulet0e54d542019-07-04 21:22:34 +02001616 if (h1s->flags & errflag)
1617 goto end;
1618
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001619 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001620 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001621
Willy Tarreau3815b222018-12-11 19:50:43 +01001622 /* Perform some optimizations to reduce the number of buffer copies.
1623 * First, if the mux's buffer is empty and the htx area contains
1624 * exactly one data block of the same size as the requested count,
1625 * then it's possible to simply swap the caller's buffer with the
1626 * mux's output buffer and adjust offsets and length to match the
1627 * entire DATA HTX block in the middle. In this case we perform a
1628 * true zero-copy operation from end-to-end. This is the situation
1629 * that happens all the time with large files. Second, if this is not
1630 * possible, but the mux's output buffer is empty, we still have an
1631 * opportunity to avoid the copy to the intermediary buffer, by making
1632 * the intermediary buffer's area point to the output buffer's area.
1633 * In this case we want to skip the HTX header to make sure that copies
1634 * remain aligned and that this operation remains possible all the
1635 * time. This goes for headers, data blocks and any data extracted from
1636 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001637 */
1638 if (!b_data(&h1c->obuf)) {
Christopher Faulet192c6a22019-06-11 16:32:24 +02001639 if (htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001640 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001641 htx_get_blk_value(chn_htx, blk).len == count) {
1642 void *old_area = h1c->obuf.area;
1643
Christopher Faulet6b81df72019-10-01 22:08:43 +02001644 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 +01001645 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001646 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001647 h1c->obuf.data = count;
1648
1649 buf->area = old_area;
1650 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001651
Christopher Faulet6b81df72019-10-01 22:08:43 +02001652 chn_htx = (struct htx *)buf->area;
1653 htx_reset(chn_htx);
1654
Christopher Fauletadb22202018-12-12 10:32:09 +01001655 /* The message is chunked. We need to emit the chunk
1656 * size. We have at least the size of the struct htx to
1657 * write the chunk envelope. It should be enough.
1658 */
1659 if (h1m->flags & H1_MF_CHNK) {
1660 h1_emit_chunk_size(&h1c->obuf, count);
1661 h1_emit_chunk_crlf(&h1c->obuf);
1662 }
1663
Willy Tarreau3815b222018-12-11 19:50:43 +01001664 total += count;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001665 if (h1m->state == H1_MSG_DATA)
1666 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001667 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001668 else
1669 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001670 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Willy Tarreau3815b222018-12-11 19:50:43 +01001671 goto out;
1672 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001673 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001674 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001675 else
1676 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001677
Christopher Fauletc2518a52019-06-25 21:41:02 +02001678 tmp.data = 0;
1679 tmp.size = b_room(&h1c->obuf);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001680 while (count && !(h1s->flags & errflag) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001681 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001682 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001683 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001684 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001685 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001686
Christopher Fauletb2e84162018-12-06 11:39:49 +01001687 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001688 if (type != HTX_BLK_DATA && vlen > count)
1689 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001690
Christopher Faulet94b2c762019-05-24 15:28:57 +02001691 if (type == HTX_BLK_UNUSED)
1692 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001693
Christopher Faulet94b2c762019-05-24 15:28:57 +02001694 switch (h1m->state) {
1695 case H1_MSG_RQBEFORE:
1696 if (type != HTX_BLK_REQ_SL)
1697 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001698 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 +02001699 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001700 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001701 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001702 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001703 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001705 if (sl->flags & HTX_SL_F_BODYLESS)
1706 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet129817b2018-09-20 16:14:40 +02001708 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001709
Christopher Faulet94b2c762019-05-24 15:28:57 +02001710 case H1_MSG_RPBEFORE:
1711 if (type != HTX_BLK_RES_SL)
1712 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001713 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 +02001714 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001715 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001716 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001717 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001718 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001719 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001720 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001721 if (sl->info.res.status < 200 &&
1722 (sl->info.res.status == 100 || sl->info.res.status >= 102))
Christopher Fauletada34b62019-05-24 16:36:43 +02001723 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724 h1m->state = H1_MSG_HDR_FIRST;
1725 break;
1726
Christopher Faulet94b2c762019-05-24 15:28:57 +02001727 case H1_MSG_HDR_FIRST:
1728 case H1_MSG_HDR_NAME:
1729 case H1_MSG_HDR_L2_LWS:
1730 if (type == HTX_BLK_EOH)
1731 goto last_lf;
1732 if (type != HTX_BLK_HDR)
1733 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001734
Christopher Faulet9768c262018-10-22 09:34:31 +02001735 h1m->state = H1_MSG_HDR_NAME;
1736 n = htx_get_blk_name(chn_htx, blk);
1737 v = htx_get_blk_value(chn_htx, blk);
1738
Christopher Faulet86d144c2019-08-14 16:32:25 +02001739 /* Skip all pseudo-headers */
1740 if (*(n.ptr) == ':')
1741 goto skip_hdr;
1742
Christopher Fauletb045bb22020-02-28 10:42:20 +01001743 if (isteq(n, ist("transfer-encoding")))
Christopher Faulet9768c262018-10-22 09:34:31 +02001744 h1_parse_xfer_enc_header(h1m, v);
Christopher Fauletb045bb22020-02-28 10:42:20 +01001745 else if (isteq(n, ist("content-length"))) {
Christopher Faulet5220ef22019-03-27 15:44:56 +01001746 /* Only skip C-L header with invalid value. */
1747 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001748 goto skip_hdr;
1749 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001750 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001751 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001752 if (!v.len)
1753 goto skip_hdr;
1754 }
1755
Christopher Faulet67d58092019-10-02 10:51:38 +02001756 /* Skip header if same name is used to add the server name */
1757 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1758 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1759 goto skip_hdr;
1760
Christopher Faulet98fbe952019-07-22 16:18:24 +02001761 /* Try to adjust the case of the header name */
1762 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1763 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001764 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001765 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001766 skip_hdr:
1767 h1m->state = H1_MSG_HDR_L2_LWS;
1768 break;
1769
Christopher Faulet94b2c762019-05-24 15:28:57 +02001770 case H1_MSG_LAST_LF:
1771 if (type != HTX_BLK_EOH)
1772 goto error;
1773 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001774 h1m->state = H1_MSG_LAST_LF;
1775 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001776 /* If the reply comes from haproxy while the request is
1777 * not finished, we force the connection close. */
1778 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1779 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1780 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1781 }
1782
1783 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001784 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001785 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001786 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001787 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001788 /* Try to adjust the case of the header name */
1789 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1790 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001791 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001792 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001793 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001794 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001795 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001796
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001797 if ((h1s->meth != HTTP_METH_CONNECT &&
1798 (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 +01001799 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
1800 (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 &&
1801 h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) &&
1802 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
1803 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01001804 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02001805 n = ist("transfer-encoding");
1806 v = ist("chunked");
1807 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1808 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001809 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001810 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001811 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01001812 h1m->flags |= H1_MF_CHNK;
1813 }
1814
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001815 /* Now add the server name to a header (if requested) */
1816 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
1817 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
1818 struct server *srv = objt_server(h1c->conn->target);
1819
1820 if (srv) {
1821 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
1822 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02001823
1824 /* Try to adjust the case of the header name */
1825 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1826 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001827 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001828 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001829 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001830 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02001831 h1s->flags |= H1S_F_HAVE_SRV_NAME;
1832 }
1833
Christopher Fauletc2518a52019-06-25 21:41:02 +02001834 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001835 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001836
Christopher Faulet6b81df72019-10-01 22:08:43 +02001837 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
1838 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1839
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001840 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
1841 /* a CONNECT request is sent to the server. Switch it to tunnel mode. */
1842 h1_set_req_tunnel_mode(h1s);
1843 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01001844 else if ((h1m->flags & H1_MF_RESP) &&
1845 ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001846 /* a successful reply to a CONNECT or a protocol switching is sent
Christopher Fauletf3158e92019-11-15 11:14:23 +01001847 * to the client. Switch the response to tunnel mode.
1848 */
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001849 h1_set_res_tunnel_mode(h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001850 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 +01001851 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001852 else if ((h1m->flags & H1_MF_RESP) &&
1853 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1854 h1m_init_res(&h1s->res);
1855 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02001856 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001857 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001858 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001859 else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) {
Christopher Fauletb8fc3042019-07-01 16:17:30 +02001860 h1m->state = H1_MSG_DONE;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001861 TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1862 }
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001863 else
1864 h1m->state = H1_MSG_DATA;
Christopher Faulet9768c262018-10-22 09:34:31 +02001865 break;
1866
Christopher Faulet94b2c762019-05-24 15:28:57 +02001867 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02001868 case H1_MSG_TUNNEL:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001869 if (type == HTX_BLK_EOM) {
1870 /* Chunked message without explicit trailers */
1871 if (h1m->flags & H1_MF_CHNK) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001872 if (!chunk_memcat(&tmp, "0\r\n\r\n", 5))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001873 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001874 }
1875 goto done;
1876 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001877 else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001878 /* If the message is not chunked, never
1879 * add the last chunk. */
1880 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001881 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001882 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 +02001883 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001884 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001885 else if (type != HTX_BLK_DATA)
1886 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001887
1888 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 +02001889
1890
1891 if (vlen > count) {
1892 /* Get the maximum amount of data we can xferred */
1893 vlen = count;
1894 }
1895
1896 chklen = 0;
1897 if (h1m->flags & H1_MF_CHNK) {
1898 chklen = b_room(&tmp);
1899 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
1900 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
1901 (chklen < 1048576) ? 5 : 8);
1902 chklen += 4; /* 2 x CRLF */
1903 }
1904
1905 if (vlen + chklen > b_room(&tmp)) {
1906 /* too large for the buffer */
1907 if (chklen >= b_room(&tmp))
1908 goto full;
1909 vlen = b_room(&tmp) - chklen;
1910 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001911 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01001912 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02001913 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001914 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001915
1916 if (h1m->state == H1_MSG_DATA)
1917 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001918 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001919 else
1920 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001921 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet9768c262018-10-22 09:34:31 +02001922 break;
1923
Christopher Faulet94b2c762019-05-24 15:28:57 +02001924 case H1_MSG_TRAILERS:
1925 if (type == HTX_BLK_EOM)
1926 goto done;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001927 else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02001928 goto error;
1929 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02001930 h1m->state = H1_MSG_TRAILERS;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02001931 /* If the message is not chunked, ignore
1932 * trailers. It may happen with H2 messages. */
1933 if (!(h1m->flags & H1_MF_CHNK))
1934 break;
1935
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001936 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02001937 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001938 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001939 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
1940 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Faulet32188212018-11-20 18:21:43 +01001941 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001942 else { // HTX_BLK_TLR
1943 n = htx_get_blk_name(chn_htx, blk);
1944 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02001945
1946 /* Try to adjust the case of the header name */
1947 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1948 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001949 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001950 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001951 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001952 break;
1953
Christopher Faulet94b2c762019-05-24 15:28:57 +02001954 case H1_MSG_DONE:
1955 if (type != HTX_BLK_EOM)
1956 goto error;
1957 done:
1958 h1m->state = H1_MSG_DONE;
Christopher Fauletf3158e92019-11-15 11:14:23 +01001959 if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) {
1960 h1_set_req_tunnel_mode(h1s);
1961 TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
1962 }
Christopher Fauletecae6e92021-02-22 08:11:59 +01001963 else if ((h1m->flags & H1_MF_RESP) && h1s->req.state == H1_MSG_TUNNEL) {
1964 TRACE_STATE("switch back H1 request from tunnel mode", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1965 h1s->req.state = H1_MSG_DONE;
1966 h1s->flags |= H1S_F_PARSING_DONE;
1967 }
1968
1969 if (h1s->h1c->flags & H1C_F_IN_BUSY) {
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001970 h1s->h1c->flags &= ~H1C_F_IN_BUSY;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001971 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001972 TRACE_STATE("h1c no more busy", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02001973 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001974
1975 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1976 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001977 break;
1978
Christopher Faulet9768c262018-10-22 09:34:31 +02001979 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02001980 error:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001981 TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001982 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02001983 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Fauleta2ea1582019-05-28 10:35:18 +02001984 h1s->flags |= errflag;
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02001985 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001986 TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1987 TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001988 break;
1989 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02001990
1991 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01001992 total += vlen;
1993 count -= vlen;
1994 if (sz == vlen)
1995 blk = htx_remove_blk(chn_htx, blk);
1996 else {
1997 htx_cut_data_blk(chn_htx, blk, vlen);
1998 break;
1999 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002000 }
2001
Christopher Faulet9768c262018-10-22 09:34:31 +02002002 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002003 /* when the output buffer is empty, tmp shares the same area so that we
2004 * only have to update pointers and lengths.
2005 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002006 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2007 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002008 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002009 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002010
Willy Tarreau3815b222018-12-11 19:50:43 +01002011 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002012 out:
Christopher Fauletfe2d5b12020-12-07 18:21:27 +01002013 /* Both the request and the response reached the DONE state. So set EOI
2014 * flag on the conn-stream. Most of time, the flag will already be set,
2015 * except for protocol upgrades.
2016 */
2017 if (h1s->cs && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)
2018 h1s->cs->flags |= CS_FL_EOI;
2019
Christopher Faulet6b81df72019-10-01 22:08:43 +02002020 if (!buf_room_for_htx_data(&h1c->obuf)) {
2021 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002022 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002023 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002024 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02002025 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002026 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002027
2028 full:
2029 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2030 h1c->flags |= H1C_F_OUT_FULL;
2031 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002032}
2033
Christopher Faulet51dbc942018-09-13 09:05:15 +02002034/*********************************************************/
2035/* functions below are I/O callbacks from the connection */
2036/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002037static void h1_wake_stream_for_recv(struct h1s *h1s)
2038{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002039 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002040 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002041 tasklet_wakeup(h1s->subs->tasklet);
2042 h1s->subs->events &= ~SUB_RETRY_RECV;
2043 if (!h1s->subs->events)
2044 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002045 }
2046}
2047static void h1_wake_stream_for_send(struct h1s *h1s)
2048{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002049 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002050 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002051 tasklet_wakeup(h1s->subs->tasklet);
2052 h1s->subs->events &= ~SUB_RETRY_SEND;
2053 if (!h1s->subs->events)
2054 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002055 }
2056}
2057
Christopher Faulet51dbc942018-09-13 09:05:15 +02002058/*
2059 * Attempt to read data, and subscribe if none available
2060 */
2061static int h1_recv(struct h1c *h1c)
2062{
2063 struct connection *conn = h1c->conn;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002064 struct h1s *h1s = h1c->h1s;
Olivier Houchard75159a92018-12-03 18:46:09 +01002065 size_t ret = 0, max;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002066 int rcvd = 0;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002067 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002068
Christopher Faulet6b81df72019-10-01 22:08:43 +02002069 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2070
2071 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2072 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002073 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002074 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002075
Olivier Houchard75159a92018-12-03 18:46:09 +01002076 if (!h1_recv_allowed(h1c)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002077 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_H1C_RECV, h1c->conn);
Olivier Houchard75159a92018-12-03 18:46:09 +01002078 rcvd = 1;
Christopher Faulet9768c262018-10-22 09:34:31 +02002079 goto end;
Olivier Houchard75159a92018-12-03 18:46:09 +01002080 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002081
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002082 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2083 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002084 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002085 goto end;
2086 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002087
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002088 if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) {
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002089 if (!h1s_data_pending(h1s))
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002090 h1_wake_stream_for_recv(h1s);
2091 rcvd = 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002092 TRACE_DEVEL("leaving on (buf_flush|spliced_data)", H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet9768c262018-10-22 09:34:31 +02002093 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002094 }
2095
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002096 /*
2097 * If we only have a small amount of data, realign it,
2098 * it's probably cheaper than doing 2 recv() calls.
2099 */
2100 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2101 b_slow_realign(&h1c->ibuf, trash.area, 0);
2102
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002103 /* avoid useless reads after first responses */
Christopher Faulet69f2cb82020-09-21 11:59:21 +02002104 if (h1s && ((!conn_is_back(conn) && h1s->req.state == H1_MSG_RQBEFORE) ||
2105 (conn_is_back(conn) && h1s->res.state == H1_MSG_RPBEFORE)))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002106 flags |= CO_RFL_READ_ONCE;
2107
Willy Tarreau45f2b892018-12-05 07:59:27 +01002108 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002109 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002110 if (h1c->flags & H1C_F_IN_FULL) {
2111 h1c->flags &= ~H1C_F_IN_FULL;
2112 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2113 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002114
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002115 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002116 if (!b_data(&h1c->ibuf)) {
2117 /* try to pre-align the buffer like the rxbufs will be
2118 * to optimize memory copies.
2119 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002120 h1c->ibuf.head = sizeof(struct htx);
2121 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002122 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002123 }
Christopher Faulet47365272018-10-31 17:40:50 +01002124 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002125 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002126 rcvd = 1;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002127 if (h1s && h1s->cs) {
Christopher Faulet37e36072018-12-04 15:54:12 +01002128 h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE);
Christopher Fauletfeb11742018-11-29 15:12:34 +01002129 if (h1s->csinfo.t_idle == -1)
2130 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2131 }
Christopher Faulet47365272018-10-31 17:40:50 +01002132 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002133
Olivier Houchardcc3fec82019-07-26 15:11:11 +02002134 if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet81d48432018-11-19 21:22:43 +01002135 rcvd = 1;
Christopher Fauletcf56b992018-12-11 16:12:31 +01002136 goto end;
2137 }
2138
Christopher Faulet6b81df72019-10-01 22:08:43 +02002139 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002140 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002141
Christopher Faulet9768c262018-10-22 09:34:31 +02002142 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002143 if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn))
2144 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002145
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002146 if (conn_xprt_read0_pending(conn) && h1s) {
2147 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002148 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002149 rcvd = 1;
2150 }
2151
Christopher Faulet51dbc942018-09-13 09:05:15 +02002152 if (!b_data(&h1c->ibuf))
2153 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002154 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002155 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002156 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2157 }
2158
2159 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002160 return rcvd;
2161}
2162
2163
2164/*
2165 * Try to send data if possible
2166 */
2167static int h1_send(struct h1c *h1c)
2168{
2169 struct connection *conn = h1c->conn;
2170 unsigned int flags = 0;
2171 size_t ret;
2172 int sent = 0;
2173
Christopher Faulet6b81df72019-10-01 22:08:43 +02002174 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2175
2176 if (conn->flags & CO_FL_ERROR) {
2177 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002178 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002179 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002180
2181 if (!b_data(&h1c->obuf))
2182 goto end;
2183
Christopher Faulet46230362019-10-17 16:04:20 +02002184 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002185 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002186 if (h1c->flags & H1C_F_CO_STREAMER)
2187 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002188
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002189 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002190 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002191 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002192 if (h1c->flags & H1C_F_OUT_FULL) {
2193 h1c->flags &= ~H1C_F_OUT_FULL;
2194 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2195 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002196 b_del(&h1c->obuf, ret);
2197 sent = 1;
2198 }
2199
Christopher Faulet145aa472018-12-06 10:56:20 +01002200 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002201 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002202 /* error or output closed, nothing to send, clear the buffer to release it */
2203 b_reset(&h1c->obuf);
2204 }
2205
Christopher Faulet51dbc942018-09-13 09:05:15 +02002206 end:
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002207 if (!(h1c->flags & H1C_F_OUT_FULL))
2208 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002209
Christopher Faulet51dbc942018-09-13 09:05:15 +02002210 /* We're done, no more to send */
2211 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002212 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002213 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002214 if (h1c->flags & H1C_F_CS_SHUTW_NOW) {
2215 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002216 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002217 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002218 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002219 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2220 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002221 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002222 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002223
Christopher Faulet6b81df72019-10-01 22:08:43 +02002224 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002225 return sent;
2226}
2227
Christopher Faulet51dbc942018-09-13 09:05:15 +02002228
2229/* callback called on any event by the connection handler.
2230 * It applies changes and returns zero, or < 0 if it wants immediate
2231 * destruction of the connection.
2232 */
2233static int h1_process(struct h1c * h1c)
2234{
2235 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002236 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002237
Christopher Faulet6b81df72019-10-01 22:08:43 +02002238 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2239
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002240 if (!conn->ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002241 return -1;
2242
Christopher Fauletfeb11742018-11-29 15:12:34 +01002243 if (!h1s) {
Christopher Faulet37243bc2019-07-11 15:40:25 +02002244 if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) ||
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002245 conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH))
Christopher Faulet539e0292018-11-19 10:40:09 +01002246 goto release;
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002247 if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002248 TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn);
Olivier Houchardf502aca2018-12-14 19:42:40 +01002249 if (!h1s_create(h1c, NULL, NULL))
Christopher Faulet539e0292018-11-19 10:40:09 +01002250 goto release;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002251 }
Christopher Fauletd29ab4d2021-04-19 19:57:43 +02002252 else if (conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE) && b_data(&h1c->ibuf))
2253 goto release;
Christopher Faulet1a7ad7a2018-12-04 16:10:44 +01002254 else
Olivier Houcharde7284782018-12-06 18:54:54 +01002255 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002256 h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002257 }
2258
Christopher Fauletfeb11742018-11-29 15:12:34 +01002259 if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1)
2260 h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake;
2261
Christopher Faulet6b81df72019-10-01 22:08:43 +02002262 if (conn_xprt_read0_pending(conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002263 h1s->flags |= H1S_F_REOS;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002264 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2265 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002266
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002267 if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake &&
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002268 (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR ||
Olivier Houchard32d75ed2019-01-14 17:27:23 +01002269 conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houchard75159a92018-12-03 18:46:09 +01002270 if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002271 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002272 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002273 h1s->cs->data_cb->wake(h1s->cs);
2274 }
Christopher Faulet47365272018-10-31 17:40:50 +01002275 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002276 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002277 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002278 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002279
2280 release:
Christopher Faulet73c12072019-04-08 11:23:22 +02002281 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002282 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002283 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002284}
2285
Willy Tarreau9a5a0e02021-01-20 14:55:01 +01002286struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002288 struct connection *conn;
2289 struct tasklet *tl = (struct tasklet *)t;
2290 int conn_in_list;
Willy Tarreaub360bb82021-03-02 16:51:09 +01002291 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002292 int ret = 0;
2293
Willy Tarreaub360bb82021-03-02 16:51:09 +01002294 if (status & TASK_F_USR1) {
2295 /* the tasklet was idling on an idle connection, it might have
2296 * been stolen, let's be careful!
2297 */
2298 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2299 if (tl->context == NULL) {
2300 /* The connection has been taken over by another thread,
2301 * we're no longer responsible for it, so just free the
2302 * tasklet, and do nothing.
2303 */
2304 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2305 tasklet_free(tl);
2306 return NULL;
2307 }
2308 conn = h1c->conn;
2309 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002310
Willy Tarreaub360bb82021-03-02 16:51:09 +01002311 /* Remove the connection from the list, to be sure nobody attempts
2312 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002313 */
Willy Tarreaub360bb82021-03-02 16:51:09 +01002314 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2315 if (conn_in_list)
2316 MT_LIST_DEL(&conn->list);
2317
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002318 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreaub360bb82021-03-02 16:51:09 +01002319 } else {
2320 /* we're certain the connection was not in an idle list */
2321 conn = h1c->conn;
2322 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2323 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002324 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002325
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002326 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002327 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002328 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002329 ret |= h1_recv(h1c);
Christopher Faulet81d48432018-11-19 21:22:43 +01002330 if (ret || !h1c->h1s)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002331 ret = h1_process(h1c);
2332 /* If we were in an idle list, we want to add it back into it,
2333 * unless h1_process() returned -1, which mean it has destroyed
2334 * the connection (testing !ret is enough, if h1_process() wasn't
2335 * called then ret will be 0 anyway.
2336 */
2337 if (!ret && conn_in_list) {
2338 struct server *srv = objt_server(conn->target);
2339
2340 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002341 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002342 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002343 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002344 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002345 return NULL;
2346}
2347
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002348static void h1_reset(struct connection *conn)
2349{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002350
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002351}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002352
2353static int h1_wake(struct connection *conn)
2354{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002355 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002356 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002357
Christopher Faulet6b81df72019-10-01 22:08:43 +02002358 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2359
Christopher Faulet539e0292018-11-19 10:40:09 +01002360 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002361 ret = h1_process(h1c);
2362 if (ret == 0) {
2363 struct h1s *h1s = h1c->h1s;
2364
Christopher Faulet6b81df72019-10-01 22:08:43 +02002365 if (h1s && h1s->cs && h1s->cs->data_cb->wake) {
2366 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002367 ret = h1s->cs->data_cb->wake(h1s->cs);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002368 }
Olivier Houchard75159a92018-12-03 18:46:09 +01002369 }
2370 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002371}
2372
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002373/* Connection timeout management. The principle is that if there's no receipt
2374 * nor sending for a certain amount of time, the connection is closed.
2375 */
2376static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
2377{
2378 struct h1c *h1c = context;
2379 int expired = tick_is_expired(t->expire, now_ms);
2380
Christopher Faulet6b81df72019-10-01 22:08:43 +02002381 TRACE_POINT(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
2382
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002383 if (h1c) {
2384 if (!expired) {
2385 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn);
2386 return t;
2387 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002388
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002389 /* We're about to destroy the connection, so make sure nobody attempts
2390 * to steal it from us.
2391 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002392 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002393
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002394 /* Somebody already stole the connection from us, so we should not
2395 * free it, we just have to free the task.
2396 */
2397 if (!t->context)
2398 h1c = NULL;
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002399 else if (h1c->conn->flags & CO_FL_LIST_MASK)
2400 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002401
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002402 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002403 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002404
Olivier Houchard3f795f72019-04-17 22:51:06 +02002405 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002406
2407 if (!h1c) {
2408 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002409 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002410 return NULL;
2411 }
2412
2413 h1c->task = NULL;
2414 /* If a stream is still attached to the mux, just set an error and wait
2415 * for the stream's timeout. Otherwise, release the mux. This is only ok
2416 * because same timeouts are used.
2417 */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002418 if (h1c->h1s && h1c->h1s->cs) {
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002419 h1c->flags |= H1C_F_CS_ERROR;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002420 TRACE_STATE("error on h1c, h1s still attached (expired)", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
2421 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002422 else
Christopher Faulet73c12072019-04-08 11:23:22 +02002423 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002424
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002425 return NULL;
2426}
2427
Christopher Faulet51dbc942018-09-13 09:05:15 +02002428/*******************************************/
2429/* functions below are used by the streams */
2430/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002431
Christopher Faulet51dbc942018-09-13 09:05:15 +02002432/*
2433 * Attach a new stream to a connection
2434 * (Used for outgoing connections)
2435 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002436static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002437{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002438 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002439 struct conn_stream *cs = NULL;
2440 struct h1s *h1s;
2441
Christopher Faulet6b81df72019-10-01 22:08:43 +02002442 TRACE_ENTER(H1_EV_STRM_NEW, conn);
2443 if (h1c->flags & H1C_F_CS_ERROR) {
2444 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 +02002445 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002446 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002447
Christopher Faulet236c93b2020-07-02 09:19:54 +02002448 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002449 if (!cs) {
2450 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 +02002451 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002452 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002453
Olivier Houchardf502aca2018-12-14 19:42:40 +01002454 h1s = h1s_create(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002455 if (h1s == NULL) {
2456 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 +02002457 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002458 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002459
Willy Tarreaub360bb82021-03-02 16:51:09 +01002460 /* the connection is not idle anymore, let's mark this */
2461 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01002462 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaub360bb82021-03-02 16:51:09 +01002463
Christopher Faulet6b81df72019-10-01 22:08:43 +02002464 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002465 return cs;
2466 end:
2467 cs_free(cs);
2468 return NULL;
2469}
2470
2471/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2472 * this mux, it's easy as we can only store a single conn_stream.
2473 */
2474static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2475{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002476 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002477 struct h1s *h1s = h1c->h1s;
2478
2479 if (h1s)
2480 return h1s->cs;
2481
2482 return NULL;
2483}
2484
Christopher Faulet73c12072019-04-08 11:23:22 +02002485static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002486{
Christopher Faulet73c12072019-04-08 11:23:22 +02002487 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002488
Christopher Faulet6b81df72019-10-01 22:08:43 +02002489 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02002490 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02002491 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002492}
2493
2494/*
2495 * Detach the stream from the connection and possibly release the connection.
2496 */
2497static void h1_detach(struct conn_stream *cs)
2498{
2499 struct h1s *h1s = cs->ctx;
2500 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002501 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01002502 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002503
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
2505
Christopher Faulet51dbc942018-09-13 09:05:15 +02002506 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002507 if (!h1s) {
2508 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002509 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002510 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002511
Olivier Houchardf502aca2018-12-14 19:42:40 +01002512 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513 h1c = h1s->h1c;
2514 h1s->cs = NULL;
2515
Olivier Houchard8a786902018-12-15 16:05:40 +01002516 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
2517 h1s_destroy(h1s);
2518
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002519 if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02002520 /* If there are any excess server data in the input buffer,
2521 * release it and close the connection ASAP (some data may
2522 * remain in the output buffer). This happens if a server sends
2523 * invalid responses. So in such case, we don't want to reuse
2524 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02002525 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02002526 if (b_data(&h1c->ibuf)) {
2527 h1_release_buf(h1c, &h1c->ibuf);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +01002528 h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002529 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02002530 goto release;
2531 }
Christopher Faulet03627242019-07-19 11:34:08 +02002532
Christopher Faulet08016ab2020-07-01 16:10:06 +02002533 if (h1c->conn->flags & CO_FL_PRIVATE) {
2534 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01002535 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
2536 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01002537 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002538 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01002539 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002540 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002541 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002542 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002543 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2544 goto end;
2545 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002546 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02002547 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002548 if (h1c->conn->owner == sess)
2549 h1c->conn->owner = NULL;
Willy Tarreaub360bb82021-03-02 16:51:09 +01002550
2551 /* mark that the tasklet may lose its context to another thread and
2552 * that the handler needs to check it under the idle conns lock.
2553 */
2554 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01002555 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau9a4864d2021-03-02 17:27:58 +01002556 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
2557
Olivier Houcharddc2f2752020-02-13 19:12:07 +01002558 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01002559 /* The server doesn't want it, let's kill the connection right away */
2560 h1c->conn->mux->destroy(h1c);
2561 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
2562 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01002563 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01002564 /* At this point, the connection has been added to the
2565 * server idle list, so another thread may already have
2566 * hijacked it, so we can't do anything with it.
2567 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01002568 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01002569 }
2570 }
2571
Christopher Fauletf1204b82019-07-19 14:51:06 +02002572 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02002573 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet37243bc2019-07-11 15:40:25 +02002574 if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) ||
2575 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
2576 ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 !h1c->conn->owner) {
2578 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02002579 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002580 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002581 else {
Olivier Houchard69664412020-03-25 12:24:11 +01002582 /* If we have a new request, process it immediately */
Christopher Faulet43b1cfa2020-12-18 15:13:47 +01002583 if (unlikely(b_data(&h1c->ibuf))) {
2584 if (h1_process(h1c) == -1)
2585 goto end;
2586 }
Olivier Houchard69664412020-03-25 12:24:11 +01002587 else
2588 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet7a145d62020-08-05 11:31:16 +02002589 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002590 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002591 end:
2592 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002593}
2594
2595
2596static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2597{
2598 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02002599 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002600
2601 if (!h1s)
2602 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02002603 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002604
Christopher Faulet6b81df72019-10-01 22:08:43 +02002605 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2606
2607 if (cs->flags & CS_FL_KILL_CONN) {
2608 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
2609 goto do_shutr;
2610 }
2611 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2612 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002613 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002614 }
Christopher Faulet7f366362019-04-08 10:51:20 +02002615
Christopher Faulet6b81df72019-10-01 22:08:43 +02002616 if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) {
2617 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2618 goto end;
2619 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002620
Christopher Faulet7f366362019-04-08 10:51:20 +02002621 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002622 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
2623 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002624 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002625 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002626 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02002627 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002628 end:
2629 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002630}
2631
2632static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2633{
2634 struct h1s *h1s = cs->ctx;
2635 struct h1c *h1c;
2636
2637 if (!h1s)
2638 return;
2639 h1c = h1s->h1c;
2640
Christopher Faulet6b81df72019-10-01 22:08:43 +02002641 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
2642
2643 if (cs->flags & CS_FL_KILL_CONN) {
2644 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02002645 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002646 }
2647 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
2648 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2649 goto do_shutw;
2650 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01002651
Christopher Faulet0ef372a2019-04-08 10:57:20 +02002652 if ((h1c->flags & H1C_F_UPG_H2C) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02002653 ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
2654 TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
2655 goto end;
2656 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002657
Christopher Faulet7f366362019-04-08 10:51:20 +02002658 do_shutw:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002659 h1c->flags |= H1C_F_CS_SHUTW_NOW;
2660 if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf))
Christopher Faulet6b81df72019-10-01 22:08:43 +02002661 goto end;
Christopher Faulet666a0c42019-01-08 11:12:04 +01002662 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002663 end:
2664 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002665}
2666
Christopher Faulet666a0c42019-01-08 11:12:04 +01002667static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002668{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002669 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002670
Willy Tarreauc6eedcc2021-03-26 09:09:42 +01002671 if (conn->flags & CO_FL_SOCK_WR_SH)
2672 return;
2673
Christopher Faulet6b81df72019-10-01 22:08:43 +02002674 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002675 conn_xprt_shutw(conn);
2676 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet7b109f22019-12-05 11:18:31 +01002677 h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002678 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002679}
2680
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002681/* Called from the upper layer, to unsubscribe <es> from events <event_type>
2682 * The <es> pointer is not allowed to differ from the one passed to the
2683 * subscribe() call. It always returns zero.
2684 */
2685static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002686{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002687 struct h1s *h1s = cs->ctx;
2688
2689 if (!h1s)
2690 return 0;
2691
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002692 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002693 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002694
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002695 es->events &= ~event_type;
2696 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002697 h1s->subs = NULL;
2698
2699 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002700 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002701
2702 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002703 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002704
Christopher Faulet51dbc942018-09-13 09:05:15 +02002705 return 0;
2706}
2707
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002708/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2709 * event subscriber <es> is not allowed to change from a previous call as long
2710 * as at least one event is still subscribed. The <event_type> must only be a
2711 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
2712 * the conn_stream <cs> was already detached, in which case it will return -1.
2713 */
2714static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002715{
Christopher Faulet51dbc942018-09-13 09:05:15 +02002716 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02002717 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002718
2719 if (!h1s)
2720 return -1;
2721
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002722 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002723 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002724
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01002725 es->events |= event_type;
2726 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002727
2728 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02002729 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002730
2731
Christopher Faulet6b81df72019-10-01 22:08:43 +02002732 if (event_type & SUB_RETRY_SEND) {
2733 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002734 /*
2735 * If the conn_stream attempt to subscribe, and the
2736 * mux isn't subscribed to the connection, then it
2737 * probably means the connection wasn't established
2738 * yet, so we have to subscribe.
2739 */
2740 h1c = h1s->h1c;
2741 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
2742 h1c->conn->xprt->subscribe(h1c->conn,
2743 h1c->conn->xprt_ctx,
2744 SUB_RETRY_SEND,
2745 &h1c->wait_event);
2746 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002747 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002748}
2749
2750/* Called from the upper layer, to receive data */
2751static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2752{
2753 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01002754 struct h1c *h1c = h1s->h1c;
Olivier Houcharddedd3062019-07-26 15:12:38 +02002755 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002756 size_t ret = 0;
2757
Willy Tarreau022e5e52020-09-10 09:33:15 +02002758 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet539e0292018-11-19 10:40:09 +01002759 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02002760 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002761 else
2762 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002763
Christopher Faulete18777b2019-04-16 16:46:36 +02002764 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02002765 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002766 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002767 TRACE_STATE("flush stream's buffer", H1_EV_STRM_RECV, h1c->conn, h1s);
2768 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002769 }
Olivier Houchard02bac852019-08-22 18:34:25 +02002770 else {
Christopher Faulet7b7016b2020-07-03 15:12:00 +02002771 if (ret && h1s->flags & H1S_F_SPLICED_DATA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002772 h1s->flags &= ~H1S_F_SPLICED_DATA;
2773 TRACE_STATE("disable splicing", H1_EV_STRM_RECV, h1c->conn, h1s);
2774 }
2775 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002776 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002777 }
Willy Tarreau022e5e52020-09-10 09:33:15 +02002778 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02002779 return ret;
2780}
2781
2782
2783/* Called from the upper layer, to send data */
2784static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
2785{
2786 struct h1s *h1s = cs->ctx;
2787 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002788 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002789
2790 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002791 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002792 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002793
Willy Tarreau022e5e52020-09-10 09:33:15 +02002794 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002795
Olivier Houchard305d5ab2019-07-24 18:07:06 +02002796 /* If we're not connected yet, or we're waiting for a handshake, stop
2797 * now, as we don't want to remove everything from the channel buffer
2798 * before we're sure we can send it.
2799 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01002800 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002801 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02002802 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002803 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002804
Christopher Faulet4099f992021-03-01 17:46:32 +01002805 if (h1c->flags & H1C_F_CS_ERROR) {
2806 cs->flags |= CS_FL_ERROR;
2807 TRACE_DEVEL("H1 connection is in error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
2808 return 0;
2809 }
2810
Christopher Faulet46230362019-10-17 16:04:20 +02002811 /* Inherit some flags from the upper layer */
2812 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
2813 if (flags & CO_SFL_MSG_MORE)
2814 h1c->flags |= H1C_F_CO_MSG_MORE;
2815 if (flags & CO_SFL_STREAMER)
2816 h1c->flags |= H1C_F_CO_STREAMER;
2817
Christopher Fauletc31872f2019-06-04 22:09:36 +02002818 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002819 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002821 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
2822 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002823 else
2824 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02002825
2826 if ((count - ret) > 0)
2827 h1c->flags |= H1C_F_CO_MSG_MORE;
2828
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002829 if (!ret)
2830 break;
2831 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02002832 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01002833 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002834 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002835 }
Christopher Faulet4099f992021-03-01 17:46:32 +01002836
2837 if (h1c->flags & H1C_F_CS_ERROR) {
2838 TRACE_DEVEL("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
2839 cs->flags |= CS_FL_ERROR;
2840 }
2841
Christopher Faulet7a145d62020-08-05 11:31:16 +02002842 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02002843 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01002844 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002845}
2846
Willy Tarreaue5733232019-05-22 19:24:06 +02002847#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02002848/* Send and get, using splicing */
2849static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
2850{
2851 struct h1s *h1s = cs->ctx;
2852 struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res);
2853 int ret = 0;
2854
Willy Tarreau022e5e52020-09-10 09:33:15 +02002855 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002856
Christopher Faulet9fa40c42019-11-05 16:24:27 +01002857 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002858 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002859 TRACE_STATE("disable splicing on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
2860 if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) {
2861 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1146f972019-05-29 14:35:24 +02002862 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002863 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002864 goto end;
2865 }
2866
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002867 if (h1s_data_pending(h1s)) {
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002868 h1s->flags |= H1S_F_BUF_FLUSH;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002869 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002870 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002871 }
2872
Christopher Faulet27182292020-07-03 15:08:49 +02002873 if (!(h1s->flags & H1S_F_SPLICED_DATA)) {
2874 h1s->flags &= ~H1S_F_BUF_FLUSH;
2875 h1s->flags |= H1S_F_SPLICED_DATA;
2876 TRACE_STATE("enable splicing", H1_EV_STRM_RECV, cs->conn, h1s);
2877 }
Christopher Faulet0060be92020-07-03 15:02:25 +02002878
2879 if (!h1_recv_allowed(h1s->h1c)) {
2880 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
2881 goto end;
2882 }
2883
Christopher Faulet1be55f92018-10-02 15:59:23 +02002884 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
2885 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002886 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02002887 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02002888 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002889 if (!h1m->curr_len) {
Christopher Faulete18777b2019-04-16 16:46:36 +02002890 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002891 TRACE_STATE("disable splicing on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
2892 }
Christopher Faulete18777b2019-04-16 16:46:36 +02002893 }
2894
Christopher Faulet1be55f92018-10-02 15:59:23 +02002895 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02002896 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002897 h1s->flags |= H1S_F_REOS;
Christopher Fauletf3158e92019-11-15 11:14:23 +01002898 h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002899 TRACE_STATE("read0 on connection", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02002900 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002901
Christopher Fauleta131a8f2020-07-07 10:56:40 +02002902 if ((h1s->flags & H1S_F_REOS) ||
2903 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02002904 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
2905 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002906 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02002907 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01002908
Willy Tarreau022e5e52020-09-10 09:33:15 +02002909 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002910 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002911}
2912
2913static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
2914{
2915 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02002916 int ret = 0;
2917
Willy Tarreau022e5e52020-09-10 09:33:15 +02002918 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002919
Christopher Faulet1be55f92018-10-02 15:59:23 +02002920 if (b_data(&h1s->h1c->obuf))
2921 goto end;
2922
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002923 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02002924 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002925 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002926 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
2927 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002928 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002929 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01002930 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002931
Willy Tarreau022e5e52020-09-10 09:33:15 +02002932 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02002933 return ret;
2934}
2935#endif
2936
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002937static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
2938{
2939 int ret = 0;
2940 switch (mux_ctl) {
2941 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01002942 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02002943 ret |= MUX_STATUS_READY;
2944 return ret;
2945 default:
2946 return -1;
2947 }
2948}
2949
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002950/* for debugging with CLI's "show fd" command */
Willy Tarreau6cb467e2021-01-21 08:26:06 +01002951static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002952{
2953 struct h1c *h1c = conn->ctx;
2954 struct h1s *h1s = h1c->h1s;
Willy Tarreau5fe86682021-01-21 09:13:35 +01002955 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002956
Christopher Fauletf376a312019-01-04 15:16:06 +01002957 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
2958 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002959 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
2960 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
2961 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
2962 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
2963
2964 if (h1s) {
2965 char *method;
2966
2967 if (h1s->meth < HTTP_METH_OTHER)
2968 method = http_known_methods[h1s->meth].ptr;
2969 else
2970 method = "UNKNOWN";
2971 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
2972 " .meth=%s status=%d",
2973 h1s, h1s->flags,
2974 h1m_state_str(h1s->req.state),
2975 h1m_state_str(h1s->res.state), method, h1s->status);
2976 if (h1s->cs)
2977 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
2978 h1s->cs->flags, h1s->cs->data);
Willy Tarreau1be37da2021-01-20 17:05:58 +01002979
2980 chunk_appendf(&trash, " .subs=%p", h1s->subs);
2981 if (h1s->subs) {
Christopher Faulet70da1072021-02-25 10:06:29 +01002982 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
2983 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
2984 h1s->subs->tasklet->calls,
2985 h1s->subs->tasklet->context);
2986 if (h1s->subs->tasklet->calls >= 1000000)
2987 ret = 1;
2988 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
2989 chunk_appendf(&trash, ")");
Willy Tarreau1be37da2021-01-20 17:05:58 +01002990 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01002991 }
Willy Tarreau5fe86682021-01-21 09:13:35 +01002992 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02002993}
2994
2995
2996/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
2997static int add_hdr_case_adjust(const char *from, const char *to, char **err)
2998{
2999 struct h1_hdr_entry *entry;
3000
3001 /* Be sure there is a non-empty <to> */
3002 if (!strlen(to)) {
3003 memprintf(err, "expect <to>");
3004 return -1;
3005 }
3006
3007 /* Be sure only the case differs between <from> and <to> */
3008 if (strcasecmp(from, to)) {
3009 memprintf(err, "<from> and <to> must not differ execpt the case");
3010 return -1;
3011 }
3012
3013 /* Be sure <from> does not already existsin the tree */
3014 if (ebis_lookup(&hdrs_map.map, from)) {
3015 memprintf(err, "duplicate entry '%s'", from);
3016 return -1;
3017 }
3018
3019 /* Create the entry and insert it in the tree */
3020 entry = malloc(sizeof(*entry));
3021 if (!entry) {
3022 memprintf(err, "out of memory");
3023 return -1;
3024 }
3025
3026 entry->node.key = strdup(from);
3027 entry->name.ptr = strdup(to);
3028 entry->name.len = strlen(to);
3029 if (!entry->node.key || !entry->name.ptr) {
3030 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003031 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003032 free(entry);
3033 memprintf(err, "out of memory");
3034 return -1;
3035 }
3036 ebis_insert(&hdrs_map.map, &entry->node);
3037 return 0;
3038}
3039
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003040/* Migrate the the connection to the current thread.
3041 * Return 0 if successful, non-zero otherwise.
3042 * Expected to be called with the old thread lock held.
3043 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003044static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003045{
3046 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003047 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003048
3049 if (fd_takeover(conn->handle.fd, conn) != 0)
3050 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003051
3052 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3053 /* We failed to takeover the xprt, even if the connection may
3054 * still be valid, flag it as error'd, as we have already
3055 * taken over the fd, and wake the tasklet, so that it will
3056 * destroy it.
3057 */
3058 conn->flags |= CO_FL_ERROR;
3059 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3060 return -1;
3061 }
3062
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003063 if (h1c->wait_event.events)
3064 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3065 h1c->wait_event.events, &h1c->wait_event);
3066 /* To let the tasklet know it should free itself, and do nothing else,
3067 * set its context to NULL.
3068 */
3069 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003070 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003071
3072 task = h1c->task;
3073 if (task) {
3074 task->context = NULL;
3075 h1c->task = NULL;
3076 __ha_barrier_store();
3077 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003078
3079 h1c->task = task_new(tid_bit);
3080 if (!h1c->task) {
3081 h1_release(h1c);
3082 return -1;
3083 }
3084 h1c->task->process = h1_timeout_task;
3085 h1c->task->context = h1c;
3086 }
3087 h1c->wait_event.tasklet = tasklet_new();
3088 if (!h1c->wait_event.tasklet) {
3089 h1_release(h1c);
3090 return -1;
3091 }
3092 h1c->wait_event.tasklet->process = h1_io_cb;
3093 h1c->wait_event.tasklet->context = h1c;
3094 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3095 SUB_RETRY_RECV, &h1c->wait_event);
3096
3097 return 0;
3098}
3099
3100
Christopher Faulet98fbe952019-07-22 16:18:24 +02003101static void h1_hdeaders_case_adjust_deinit()
3102{
3103 struct ebpt_node *node, *next;
3104 struct h1_hdr_entry *entry;
3105
3106 node = ebpt_first(&hdrs_map.map);
3107 while (node) {
3108 next = ebpt_next(node);
3109 ebpt_delete(node);
3110 entry = container_of(node, struct h1_hdr_entry, node);
3111 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003112 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003113 free(entry);
3114 node = next;
3115 }
3116 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003117}
3118
Christopher Faulet98fbe952019-07-22 16:18:24 +02003119static int cfg_h1_headers_case_adjust_postparser()
3120{
3121 FILE *file = NULL;
3122 char *c, *key_beg, *key_end, *value_beg, *value_end;
3123 char *err;
3124 int rc, line = 0, err_code = 0;
3125
3126 if (!hdrs_map.name)
3127 goto end;
3128
3129 file = fopen(hdrs_map.name, "r");
3130 if (!file) {
3131 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3132 hdrs_map.name);
3133 err_code |= ERR_ALERT | ERR_FATAL;
3134 goto end;
3135 }
3136
3137 /* now parse all lines. The file may contain only two header name per
3138 * line, separated by spaces. All heading and trailing spaces will be
3139 * ignored. Lines starting with a # are ignored.
3140 */
3141 while (fgets(trash.area, trash.size, file) != NULL) {
3142 line++;
3143 c = trash.area;
3144
3145 /* strip leading spaces and tabs */
3146 while (*c == ' ' || *c == '\t')
3147 c++;
3148
3149 /* ignore emptu lines, or lines beginning with a dash */
3150 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3151 continue;
3152
3153 /* look for the end of the key */
3154 key_beg = c;
3155 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3156 c++;
3157 key_end = c;
3158
3159 /* strip middle spaces and tabs */
3160 while (*c == ' ' || *c == '\t')
3161 c++;
3162
3163 /* look for the end of the value, it is the end of the line */
3164 value_beg = c;
3165 while (*c && *c != '\n' && *c != '\r')
3166 c++;
3167 value_end = c;
3168
3169 /* trim possibly trailing spaces and tabs */
3170 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3171 value_end--;
3172
3173 /* set final \0 and check entries */
3174 *key_end = '\0';
3175 *value_end = '\0';
3176
3177 err = NULL;
3178 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3179 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003180 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3181 hdrs_map.name, err, line);
3182 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003183 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003184 goto end;
3185 }
3186 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003187 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3188 hdrs_map.name, err, line);
3189 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003190 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003191 }
3192 }
3193
3194 end:
3195 if (file)
3196 fclose(file);
3197 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3198 return err_code;
3199}
3200
3201
3202/* config parser for global "h1-outgoing-header-case-adjust" */
3203static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3204 struct proxy *defpx, const char *file, int line,
3205 char **err)
3206{
3207 if (too_many_args(2, args, err, NULL))
3208 return -1;
3209 if (!*(args[1]) || !*(args[2])) {
3210 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3211 return -1;
3212 }
3213 return add_hdr_case_adjust(args[1], args[2], err);
3214}
3215
3216/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3217static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3218 struct proxy *defpx, const char *file, int line,
3219 char **err)
3220{
3221 if (too_many_args(1, args, err, NULL))
3222 return -1;
3223 if (!*(args[1])) {
3224 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3225 return -1;
3226 }
3227 free(hdrs_map.name);
3228 hdrs_map.name = strdup(args[1]);
3229 return 0;
3230}
3231
3232
3233/* config keyword parsers */
3234static struct cfg_kw_list cfg_kws = {{ }, {
3235 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3236 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3237 { 0, NULL, NULL },
3238 }
3239};
3240
3241INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3242REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3243
3244
Christopher Faulet51dbc942018-09-13 09:05:15 +02003245/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003246/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003247/****************************************/
3248
3249/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003250static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003251 .init = h1_init,
3252 .wake = h1_wake,
3253 .attach = h1_attach,
3254 .get_first_cs = h1_get_first_cs,
Christopher Fauletfeb11742018-11-29 15:12:34 +01003255 .get_cs_info = h1_get_cs_info,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003256 .detach = h1_detach,
3257 .destroy = h1_destroy,
3258 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003259 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003260 .rcv_buf = h1_rcv_buf,
3261 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003262#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003263 .rcv_pipe = h1_rcv_pipe,
3264 .snd_pipe = h1_snd_pipe,
3265#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003266 .subscribe = h1_subscribe,
3267 .unsubscribe = h1_unsubscribe,
3268 .shutr = h1_shutr,
3269 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003270 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003271 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003272 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003273 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003274 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003275 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003276};
3277
3278
3279/* this mux registers default HTX proto */
3280static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003281{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003282
Willy Tarreau0108d902018-11-25 19:14:37 +01003283INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3284
Christopher Faulet51dbc942018-09-13 09:05:15 +02003285/*
3286 * Local variables:
3287 * c-indent-level: 8
3288 * c-basic-offset: 8
3289 * End:
3290 */