blob: 89817df6215457ab8a7309cd1cde90bfd76a3a25 [file] [log] [blame]
Christopher Faulet51dbc942018-09-13 09:05:15 +02001/*
2 * HTT/1 mux-demux for connections
3 *
4 * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Willy Tarreaub2551052020-06-09 09:07:15 +020012#include <import/ebistree.h>
13
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020016#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020017#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020018#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020021#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/istbuf.h>
23#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020024#include <haproxy/pipe-t.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020030
31/*
32 * H1 Connection flags (32 bits)
33 */
34#define H1C_F_NONE 0x00000000
35
36/* Flags indicating why writing output data are blocked */
37#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
38#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
39/* 0x00000004 - 0x00000008 unused */
40
41/* Flags indicating why reading input data are blocked. */
42#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
43#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020044#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010045/* 0x00000080 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020046
Christopher Faulet7b109f22019-12-05 11:18:31 +010047/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010048#define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no conn-stream is attached to the connection */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010049#define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a conn-stream is attached to the connection (may be not READY) */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010050#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
51 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
52#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
53#define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010054#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
55 * a TCP>H1 upgrade is in progress Thus this flag is only set if ATTACHED is also set */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010056#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Faulet39c7b6b2021-01-21 17:44:35 +010057/* 0x00004000 - 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020058
Christopher Fauletb385b502021-01-13 18:47:57 +010059#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
60#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
61#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
62#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
63#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
64#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
65#define H1C_F_WAIT_OUTPUT 0x00400000 /* Don't read more data for now, waiting sync with output side */
66#define H1C_F_WAIT_INPUT 0x00800000 /* Don't send more data for now, waiting sync with input side */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Christopher Fauletb385b502021-01-13 18:47:57 +010068/* 0x01000000 - 0x40000000 unusued*/
Christopher Faulet3ced1d12020-11-27 16:44:01 +010069#define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */
Christopher Faulet46230362019-10-17 16:04:20 +020070
Christopher Faulet51dbc942018-09-13 09:05:15 +020071/*
72 * H1 Stream flags (32 bits)
73 */
Christopher Faulet129817b2018-09-20 16:14:40 +020074#define H1S_F_NONE 0x00000000
Ilya Shipitsinf38a0182020-12-21 01:16:17 +050075/* 0x00000001..0x00000004 unused */
Willy Tarreauc493c9c2019-06-03 14:18:22 +020076#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020077#define H1S_F_WANT_KAL 0x00000010
78#define H1S_F_WANT_TUN 0x00000020
79#define H1S_F_WANT_CLO 0x00000040
80#define H1S_F_WANT_MSK 0x00000070
81#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010082#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020083
Ilya Shipitsinacf84592021-02-06 22:29:08 +050084/* 0x00000200 unused */
Christopher Faulet2eed8002020-12-07 11:26:13 +010085#define H1S_F_NOT_IMPL_ERROR 0x00000400 /* Set when a feature is not implemented during the message parsing */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020086#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
87#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
88#define H1S_F_ERROR 0x00001800 /* stream error mask */
89
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020090#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 +020091#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020092
93/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020094struct h1c {
95 struct connection *conn;
96 struct proxy *px;
97 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +020098 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +020099 struct buffer ibuf; /* Input buffer to store data before parsing */
100 struct buffer obuf; /* Output buffer to store data after reformatting */
101
102 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
103 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
104
105 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100106 struct task *task; /* timeout management task */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200107 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
108 int timeout; /* client/server timeout duration */
109 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200110};
111
112/* H1 stream descriptor */
113struct h1s {
114 struct h1c *h1c;
115 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100116 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200117
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100118 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200119
Olivier Houchardf502aca2018-12-14 19:42:40 +0100120 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200121 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200122 struct h1m req;
123 struct h1m res;
124
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500125 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200126 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +0100127
128 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200129};
130
Christopher Faulet98fbe952019-07-22 16:18:24 +0200131/* Map of headers used to convert outgoing headers */
132struct h1_hdrs_map {
133 char *name;
134 struct eb_root map;
135};
136
137/* An entry in a headers map */
138struct h1_hdr_entry {
139 struct ist name;
140 struct ebpt_node node;
141};
142
143/* Declare the headers map */
144static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
145
146
Christopher Faulet6b81df72019-10-01 22:08:43 +0200147/* trace source and events */
148static void h1_trace(enum trace_level level, uint64_t mask,
149 const struct trace_source *src,
150 const struct ist where, const struct ist func,
151 const void *a1, const void *a2, const void *a3, const void *a4);
152
153/* The event representation is split like this :
154 * h1c - internal H1 connection
155 * h1s - internal H1 stream
156 * strm - application layer
157 * rx - data receipt
158 * tx - data transmission
159 *
160 */
161static const struct trace_event h1_trace_events[] = {
162#define H1_EV_H1C_NEW (1ULL << 0)
163 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
164#define H1_EV_H1C_RECV (1ULL << 1)
165 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
166#define H1_EV_H1C_SEND (1ULL << 2)
167 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
168#define H1_EV_H1C_BLK (1ULL << 3)
169 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
170#define H1_EV_H1C_WAKE (1ULL << 4)
171 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
172#define H1_EV_H1C_END (1ULL << 5)
173 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
174#define H1_EV_H1C_ERR (1ULL << 6)
175 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
176
177#define H1_EV_RX_DATA (1ULL << 7)
178 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
179#define H1_EV_RX_EOI (1ULL << 8)
180 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
181#define H1_EV_RX_HDRS (1ULL << 9)
182 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
183#define H1_EV_RX_BODY (1ULL << 10)
184 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
185#define H1_EV_RX_TLRS (1ULL << 11)
186 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
187
188#define H1_EV_TX_DATA (1ULL << 12)
189 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
190#define H1_EV_TX_EOI (1ULL << 13)
191 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
192#define H1_EV_TX_HDRS (1ULL << 14)
193 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
194#define H1_EV_TX_BODY (1ULL << 15)
195 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
196#define H1_EV_TX_TLRS (1ULL << 16)
197 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
198
199#define H1_EV_H1S_NEW (1ULL << 17)
200 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
201#define H1_EV_H1S_BLK (1ULL << 18)
202 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
203#define H1_EV_H1S_END (1ULL << 19)
204 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
205#define H1_EV_H1S_ERR (1ULL << 20)
206 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
207
208#define H1_EV_STRM_NEW (1ULL << 21)
209 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
210#define H1_EV_STRM_RECV (1ULL << 22)
211 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
212#define H1_EV_STRM_SEND (1ULL << 23)
213 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
214#define H1_EV_STRM_WAKE (1ULL << 24)
215 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
216#define H1_EV_STRM_SHUT (1ULL << 25)
217 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
218#define H1_EV_STRM_END (1ULL << 26)
219 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
220#define H1_EV_STRM_ERR (1ULL << 27)
221 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
222
223 { }
224};
225
226static const struct name_desc h1_trace_lockon_args[4] = {
227 /* arg1 */ { /* already used by the connection */ },
228 /* arg2 */ { .name="h1s", .desc="H1 stream" },
229 /* arg3 */ { },
230 /* arg4 */ { }
231};
232
233static const struct name_desc h1_trace_decoding[] = {
234#define H1_VERB_CLEAN 1
235 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
236#define H1_VERB_MINIMAL 2
237 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
238#define H1_VERB_SIMPLE 3
239 { .name="simple", .desc="add request/response status line or htx info when available" },
240#define H1_VERB_ADVANCED 4
241 { .name="advanced", .desc="add header fields or frame decoding when available" },
242#define H1_VERB_COMPLETE 5
243 { .name="complete", .desc="add full data dump when available" },
244 { /* end */ }
245};
246
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200247static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200248 .name = IST("h1"),
249 .desc = "HTTP/1 multiplexer",
250 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
251 .default_cb = h1_trace,
252 .known_events = h1_trace_events,
253 .lockon_args = h1_trace_lockon_args,
254 .decoding = h1_trace_decoding,
255 .report_events = ~0, // report everything by default
256};
257
258#define TRACE_SOURCE &trace_h1
259INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
260
Christopher Faulet51dbc942018-09-13 09:05:15 +0200261/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100262DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
263DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200264
Christopher Faulet51dbc942018-09-13 09:05:15 +0200265static int h1_recv(struct h1c *h1c);
266static int h1_send(struct h1c *h1c);
267static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100268/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100269struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
270struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200271static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200272static void h1_wake_stream_for_recv(struct h1s *h1s);
273static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200274
Christopher Faulet6b81df72019-10-01 22:08:43 +0200275/* the H1 traces always expect that arg1, if non-null, is of type connection
276 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
277 * that arg3, if non-null, is a htx for rx/tx headers.
278 */
279static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
280 const struct ist where, const struct ist func,
281 const void *a1, const void *a2, const void *a3, const void *a4)
282{
283 const struct connection *conn = a1;
284 const struct h1c *h1c = conn ? conn->ctx : NULL;
285 const struct h1s *h1s = a2;
286 const struct htx *htx = a3;
287 const size_t *val = a4;
288
289 if (!h1c)
290 h1c = (h1s ? h1s->h1c : NULL);
291
292 if (!h1c || src->verbosity < H1_VERB_CLEAN)
293 return;
294
295 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200296 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200297
298 /* Display request and response states if h1s is defined */
299 if (h1s)
300 chunk_appendf(&trace_buf, " [%s, %s]",
301 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
302
303 if (src->verbosity == H1_VERB_CLEAN)
304 return;
305
306 /* Display the value to the 4th argument (level > STATE) */
307 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100308 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200309
310 /* Display status-line if possible (verbosity > MINIMAL) */
311 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
312 const struct htx_blk *blk = htx_get_head_blk(htx);
313 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
314 enum htx_blk_type type = htx_get_blk_type(blk);
315
316 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
317 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
318 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
319 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
320 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
321 }
322
323 /* Display h1c info and, if defined, h1s info (pointer + flags) */
324 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
325 if (h1s)
326 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
327
328 if (src->verbosity == H1_VERB_MINIMAL)
329 return;
330
331 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
332 if (src->level > TRACE_LEVEL_USER) {
333 if (src->verbosity == H1_VERB_COMPLETE ||
334 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
335 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
336 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
337 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
338 if (src->verbosity == H1_VERB_COMPLETE ||
339 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
340 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
341 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
342 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
343 }
344
345 /* Display htx info if defined (level > USER) */
346 if (src->level > TRACE_LEVEL_USER && htx) {
347 int full = 0;
348
349 /* Full htx info (level > STATE && verbosity > SIMPLE) */
350 if (src->level > TRACE_LEVEL_STATE) {
351 if (src->verbosity == H1_VERB_COMPLETE)
352 full = 1;
353 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
354 full = 1;
355 }
356
357 chunk_memcat(&trace_buf, "\n\t", 2);
358 htx_dump(&trace_buf, htx, full);
359 }
360}
361
362
Christopher Faulet51dbc942018-09-13 09:05:15 +0200363/*****************************************************/
364/* functions below are for dynamic buffer management */
365/*****************************************************/
366/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100367 * Indicates whether or not we may receive data. The rules are the following :
368 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200369 * must not attempt to receive
370 * - if we are waiting for the connection establishment, we must not attempt
371 * to receive
372 * - if an error was detected on the stream we must not attempt to receive
373 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100374 * - if the input buffer failed to be allocated or is full , we must not try
375 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200376 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200377 * - otherwise must may not attempt to receive
378 */
379static inline int h1_recv_allowed(const struct h1c *h1c)
380{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100381 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100382 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 +0200383 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200384 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200385
Willy Tarreau2febb842020-07-31 09:15:43 +0200386 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
387 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 +0100388 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200389 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100390
Christopher Faulet119ac872020-09-30 17:33:22 +0200391 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
392 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
393 return 0;
394 }
395
Christopher Fauletd17ad822020-09-24 15:14:29 +0200396 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200397 return 1;
398
Christopher Faulet6b81df72019-10-01 22:08:43 +0200399 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 +0200400 return 0;
401}
402
403/*
404 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
405 * flags are used to figure what buffer was requested. It returns 1 if the
406 * allocation succeeds, in which case the connection is woken up, or 0 if it's
407 * impossible to wake up and we prefer to be woken up later.
408 */
409static int h1_buf_available(void *target)
410{
411 struct h1c *h1c = target;
412
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100413 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200414 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 +0200415 h1c->flags &= ~H1C_F_IN_ALLOC;
416 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200417 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200418 return 1;
419 }
420
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100421 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200422 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 +0200423 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200424 if (h1c->h1s)
425 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200426 return 1;
427 }
428
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100429 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200430 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
431 h1c->flags &= ~H1C_F_IN_SALLOC;
432 tasklet_wakeup(h1c->wait_event.tasklet);
433 return 1;
434 }
435
Christopher Faulet51dbc942018-09-13 09:05:15 +0200436 return 0;
437}
438
439/*
440 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
441 */
442static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
443{
444 struct buffer *buf = NULL;
445
Willy Tarreau2b718102021-04-21 07:32:39 +0200446 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100447 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200448 h1c->buf_wait.target = h1c;
449 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau2b718102021-04-21 07:32:39 +0200450 LIST_APPEND(&ti->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451 }
452 return buf;
453}
454
455/*
456 * Release a buffer, if any, and try to wake up entities waiting in the buffer
457 * wait queue.
458 */
459static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
460{
461 if (bptr->size) {
462 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100463 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200464 }
465}
466
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100467/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100468 * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100469 * not. This flag is only set when no H1S is attached and when the previous
470 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100471 */
472static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200473{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100474 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200475
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100476 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200477}
478
Willy Tarreau00f18a32019-01-26 12:19:01 +0100479/* returns the number of streams still available on a connection */
480static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100481{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100482 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100483}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200484
Christopher Faulet7a145d62020-08-05 11:31:16 +0200485/* Refresh the h1c task timeout if necessary */
486static void h1_refresh_timeout(struct h1c *h1c)
487{
488 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100489 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200490 /* half-closed or dead connections : switch to clientfin/serverfin
491 * timeouts so that we don't hang too long on clients that have
492 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200493 */
494 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200495 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
496 }
497 else if (b_data(&h1c->obuf)) {
498 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200499 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200500 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
501 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100502 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
503 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200504 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100505 TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200506 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200507 else {
508 /* alive back connections of front connections with a conn-stream attached */
509 h1c->task->expire = TICK_ETERNITY;
510 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
511 }
512
Christopher Fauletdbe57792020-10-05 17:50:58 +0200513 /* Finally set the idle expiration date if shorter */
514 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200515 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
516 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200517 }
518}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200519
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200520static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200521{
522 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
523 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
524 h1c->idle_exp = TICK_ETERNITY;
525 return;
526 }
527
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100528 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200529 if (!tick_isset(h1c->idle_exp)) {
530 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
531 !b_data(&h1c->ibuf) && /* No input data */
532 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
533 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
534 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
535 }
536 else {
537 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
538 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
539 }
540 }
541 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100542 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200543 if (!tick_isset(h1c->idle_exp)) {
544 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
545 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
546 }
547 }
548 else { // CS_ATTACHED or SHUTDOWN
549 h1c->idle_exp = TICK_ETERNITY;
550 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
551 }
552}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200553/*****************************************************************/
554/* functions below are dedicated to the mux setup and management */
555/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200556
557/* returns non-zero if there are input data pending for stream h1s. */
558static inline size_t h1s_data_pending(const struct h1s *h1s)
559{
560 const struct h1m *h1m;
561
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200562 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100563 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200564}
565
Christopher Faulet16df1782020-12-04 16:47:41 +0100566/* Creates a new conn-stream and the associate stream. <input> is used as input
567 * buffer for the stream. On success, it is transferred to the stream and the
568 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
569 * mux must still take care of it. However, there is nothing special to do
570 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
571 * b_free() on it is always safe. This function returns the conn-stream on
572 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200573static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100574{
575 struct conn_stream *cs;
576
Christopher Faulet6b81df72019-10-01 22:08:43 +0200577 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200578 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200579 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100580 TRACE_ERROR("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 +0100581 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 }
Christopher Faulet47365272018-10-31 17:40:50 +0100583 h1s->cs = cs;
584 cs->ctx = h1s;
585
586 if (h1s->flags & H1S_F_NOT_FIRST)
587 cs->flags |= CS_FL_NOT_FIRST;
588
Christopher Faulet27182292020-07-03 15:08:49 +0200589 if (global.tune.options & GTUNE_USE_SPLICE) {
590 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100591 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200592 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100593
Christopher Faulet26256f82020-09-14 11:40:13 +0200594 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200595 TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100596 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200597 }
598
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100599 h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200600 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100601 return cs;
602
603 err:
604 cs_free(cs);
605 h1s->cs = NULL;
Christopher Faulet26a26432021-01-27 11:27:50 +0100606 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100607 return NULL;
608}
609
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100610static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
611{
612 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
613
614 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100615 TRACE_ERROR("stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100616 goto err;
617 }
618
619 if (global.tune.options & GTUNE_USE_SPLICE) {
620 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
621 h1s->cs->flags |= CS_FL_MAY_SPLICE;
622 }
623
624 h1s->h1c->flags |= H1C_F_ST_READY;
625 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
626 return h1s->cs;
627
628 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100629 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100630 return NULL;
631}
632
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200633static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200634{
635 struct h1s *h1s;
636
Christopher Faulet6b81df72019-10-01 22:08:43 +0200637 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
638
Christopher Faulet51dbc942018-09-13 09:05:15 +0200639 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100640 if (!h1s) {
641 TRACE_ERROR("H1S allocation failure", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100642 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100643 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200644 h1s->h1c = h1c;
645 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200646 h1s->sess = NULL;
647 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100648 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100649 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200650 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100651 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200652
653 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100654 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200655
Christopher Faulet129817b2018-09-20 16:14:40 +0200656 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100657 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200658
659 h1s->status = 0;
660 h1s->meth = HTTP_METH_OTHER;
661
Christopher Faulet47365272018-10-31 17:40:50 +0100662 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
663 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100664 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100665
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200666 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
667 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200668
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200669 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100670 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200671 return NULL;
672}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100673
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200674static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
675{
676 struct session *sess = h1c->conn->owner;
677 struct h1s *h1s;
678
679 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
680
681 h1s = h1s_new(h1c);
682 if (!h1s)
683 goto fail;
684
685 h1s->sess = sess;
686
687 if (h1c->px->options2 & PR_O2_REQBUG_OK)
688 h1s->req.err_pos = -1;
689
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200690 h1c->idle_exp = TICK_ETERNITY;
691 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200692 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200693 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100694
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200695 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100696 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200697 return NULL;
698}
699
700static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
701{
702 struct h1s *h1s;
703
704 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
705
706 h1s = h1s_new(h1c);
707 if (!h1s)
708 goto fail;
709
710 h1s->cs = cs;
711 h1s->sess = sess;
712 cs->ctx = h1s;
713
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100714 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200715
716 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
717 h1s->res.err_pos = -1;
718
719 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
720 return h1s;
721
722 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100723 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100724 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200725}
726
727static void h1s_destroy(struct h1s *h1s)
728{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200729 if (h1s) {
730 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200731
Christopher Faulet6b81df72019-10-01 22:08:43 +0200732 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200733 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200734
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100735 if (h1s->subs)
736 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200737
Christopher Fauletd17ad822020-09-24 15:14:29 +0200738 h1_release_buf(h1c, &h1s->rxbuf);
739
Christopher Fauletb385b502021-01-13 18:47:57 +0100740 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
741 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200742 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
743 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200744 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100745 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100746 TRACE_ERROR("h1s on error, set error on h1c", H1_EV_H1S_END|H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200747 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100748
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100749 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100750 !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100751 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100752 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100753 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100754 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
755 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200756 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100757 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100758 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200759 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200760 pool_free(pool_head_h1s, h1s);
761 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200762}
763
764/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200765 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500766 * to the existing conn_stream (for outgoing connections or for incoming ones
767 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200768 * establishment). <input> is always used as Input buffer and may contain
769 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
770 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200771 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200772static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
773 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200774{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200775 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100776 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200777 void *conn_ctx = conn->ctx;
778
779 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780
781 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100782 if (!h1c) {
783 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200784 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100785 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200786 h1c->conn = conn;
787 h1c->px = proxy;
788
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100789 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200790 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200791 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200792 h1c->obuf = BUF_NULL;
793 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200794 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200795
Willy Tarreau90f366b2021-02-20 11:49:49 +0100796 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200797 h1c->wait_event.tasklet = tasklet_new();
798 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200799 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200800 h1c->wait_event.tasklet->process = h1_io_cb;
801 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100802 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200803 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200804
Christopher Faulete9b70722019-04-08 10:46:02 +0200805 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100806 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100807 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
808 if (tick_isset(proxy->timeout.serverfin))
809 h1c->shut_timeout = proxy->timeout.serverfin;
810 } else {
811 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
812 if (tick_isset(proxy->timeout.clientfin))
813 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200814
815 LIST_APPEND(&mux_stopping_data[tid].list,
816 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100817 }
818 if (tick_isset(h1c->timeout)) {
819 t = task_new(tid_bit);
Christopher Faulet26a26432021-01-27 11:27:50 +0100820 if (!t) {
821 TRACE_ERROR("H1C task allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100822 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100823 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100824
825 h1c->task = t;
826 t->process = h1_timeout_task;
827 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200828
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100829 t->expire = tick_add(now_ms, h1c->timeout);
830 }
831
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100832 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200833
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200834 if (h1c->flags & H1C_F_IS_BACK) {
835 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200836 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
837 goto fail;
838 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100839 else if (conn_ctx) {
840 /* Upgraded frontend connection (from TCP) */
841 struct conn_stream *cs = conn_ctx;
842
843 if (!h1c_frt_stream_new(h1c))
844 goto fail;
845
846 h1c->h1s->cs = cs;
847 cs->ctx = h1c->h1s;
848
849 /* Attach the CS but Not ready yet */
850 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
851 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
852 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
853 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100854
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200855 if (t) {
856 h1_set_idle_expiration(h1c);
857 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100858 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200859 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100860
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200861 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100862 if (b_data(&h1c->ibuf))
863 tasklet_wakeup(h1c->wait_event.tasklet);
864 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200865 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200866
867 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200868 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200869 return 0;
870
871 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200872 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200873 if (h1c->wait_event.tasklet)
874 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200875 pool_free(pool_head_h1c, h1c);
876 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200877 conn->ctx = conn_ctx; // restore saved context
878 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200879 return -1;
880}
881
Christopher Faulet73c12072019-04-08 11:23:22 +0200882/* release function. This one should be called to free all resources allocated
883 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200884 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200885static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200886{
Christopher Faulet61840e72019-04-15 09:33:32 +0200887 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200888
Christopher Faulet6b81df72019-10-01 22:08:43 +0200889 TRACE_POINT(H1_EV_H1C_END);
890
Christopher Faulet51dbc942018-09-13 09:05:15 +0200891 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200892 /* The connection must be aattached to this mux to be released */
893 if (h1c->conn && h1c->conn->ctx == h1c)
894 conn = h1c->conn;
895
Christopher Faulet6b81df72019-10-01 22:08:43 +0200896 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
897
Christopher Faulet61840e72019-04-15 09:33:32 +0200898 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200899 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200900 /* Make sure we're no longer subscribed to anything */
901 if (h1c->wait_event.events)
902 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
903 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200904 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200905 /* connection successfully upgraded to H2, this
906 * mux was already released */
907 return;
908 }
Christopher Faulet26a26432021-01-27 11:27:50 +0100909 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200910 sess_log(conn->owner); /* Log if the upgrade failed */
911 }
912
Olivier Houchard45c44372019-06-11 14:06:23 +0200913
Willy Tarreau2b718102021-04-21 07:32:39 +0200914 if (LIST_INLIST(&h1c->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +0100915 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200916
917 h1_release_buf(h1c, &h1c->ibuf);
918 h1_release_buf(h1c, &h1c->obuf);
919
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100920 if (h1c->task) {
921 h1c->task->context = NULL;
922 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
923 h1c->task = NULL;
924 }
925
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200926 if (h1c->wait_event.tasklet)
927 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200928
Christopher Fauletf2824e62018-10-01 12:12:37 +0200929 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200930 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100931 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200932 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200933 pool_free(pool_head_h1c, h1c);
934 }
935
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200936 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200937 if (!conn_is_back(conn))
938 LIST_DEL_INIT(&conn->stopping_list);
939
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200940 conn->mux = NULL;
941 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200942 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200943
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200944 conn_stop_tracking(conn);
945 conn_full_close(conn);
946 if (conn->destroy_cb)
947 conn->destroy_cb(conn);
948 conn_free(conn);
949 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200950}
951
952/******************************************************/
953/* functions below are for the H1 protocol processing */
954/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200955/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
956 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200957 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100958static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200959{
Christopher Faulet570d1612018-11-26 11:13:57 +0100960 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200961
Christopher Faulet570d1612018-11-26 11:13:57 +0100962 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200963 (*(p + 5) > '1' ||
964 (*(p + 5) == '1' && *(p + 7) >= '1')))
965 h1m->flags |= H1_MF_VER_11;
966}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200967
Christopher Faulet9768c262018-10-22 09:34:31 +0200968/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
969 * greater or equal to 1.1
970 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100971static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200972{
Christopher Faulet570d1612018-11-26 11:13:57 +0100973 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200974
Christopher Faulet570d1612018-11-26 11:13:57 +0100975 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200976 (*(p + 5) > '1' ||
977 (*(p + 5) == '1' && *(p + 7) >= '1')))
978 h1m->flags |= H1_MF_VER_11;
979}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980
Christopher Fauletf2824e62018-10-01 12:12:37 +0200981/* Deduce the connection mode of the client connection, depending on the
982 * configuration and the H1 message flags. This function is called twice, the
983 * first time when the request is parsed and the second time when the response
984 * is parsed.
985 */
986static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
987{
988 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200989
990 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100991 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100992 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100993 h1s->status == 101) {
994 /* Either we've established an explicit tunnel, or we're
995 * switching the protocol. In both cases, we're very unlikely to
996 * understand the next protocols. We have to switch to tunnel
997 * mode, so that we transfer the request and responses then let
998 * this protocol pass unmodified. When we later implement
999 * specific parsers for such protocols, we'll want to check the
1000 * Upgrade header which contains information about that protocol
1001 * for responses with status 101 (eg: see RFC2817 about TLS).
1002 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001003 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001004 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 +01001005 }
1006 else if (h1s->flags & H1S_F_WANT_KAL) {
1007 /* By default the client is in KAL mode. CLOSE mode mean
1008 * it is imposed by the client itself. So only change
1009 * KAL mode here. */
1010 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1011 /* no length known or explicit close => close */
1012 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001013 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 +01001014 }
1015 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1016 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001017 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001018 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001019 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 +01001020 }
1021 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001022 }
1023 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001024 /* Input direction: first pass */
1025 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1026 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001027 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001028 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 +01001029 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001030 }
1031
1032 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001033 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001034 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001035 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);
1036 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001037}
1038
1039/* Deduce the connection mode of the client connection, depending on the
1040 * configuration and the H1 message flags. This function is called twice, the
1041 * first time when the request is parsed and the second time when the response
1042 * is parsed.
1043 */
1044static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1045{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001046 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001047 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001048 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049
Christopher Fauletf2824e62018-10-01 12:12:37 +02001050 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001051 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001052 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001053 h1s->status == 101) {
1054 /* Either we've established an explicit tunnel, or we're
1055 * switching the protocol. In both cases, we're very unlikely to
1056 * understand the next protocols. We have to switch to tunnel
1057 * mode, so that we transfer the request and responses then let
1058 * this protocol pass unmodified. When we later implement
1059 * specific parsers for such protocols, we'll want to check the
1060 * Upgrade header which contains information about that protocol
1061 * for responses with status 101 (eg: see RFC2817 about TLS).
1062 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001063 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001064 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 +01001065 }
1066 else if (h1s->flags & H1S_F_WANT_KAL) {
1067 /* By default the server is in KAL mode. CLOSE mode mean
1068 * it is imposed by haproxy itself. So only change KAL
1069 * mode here. */
1070 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1071 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1072 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1073 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001074 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 +01001075 }
1076 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001077 }
Christopher Faulet70033782018-12-05 13:50:11 +01001078 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001079 /* Output direction: first pass */
1080 if (h1m->flags & H1_MF_CONN_CLO) {
1081 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001082 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001083 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 +01001084 }
1085 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1086 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1087 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1088 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1089 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1090 /* no explicit keep-alive option httpclose/server-close => close */
1091 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001092 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 +01001093 }
Christopher Faulet70033782018-12-05 13:50:11 +01001094 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001095
1096 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001097 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001098 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001099 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);
1100 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001101}
1102
Christopher Fauletb992af02019-03-28 15:42:24 +01001103static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001104{
1105 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001106
1107 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1108 * token is found
1109 */
1110 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001111 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001112
1113 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001114 if (!(h1m->flags & H1_MF_VER_11)) {
1115 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 +01001116 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001117 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001118 }
1119 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001120 if (h1m->flags & H1_MF_VER_11) {
1121 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001122 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001123 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001124 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001125}
1126
Christopher Fauletb992af02019-03-28 15:42:24 +01001127static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001129 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1130 * token is found
1131 */
1132 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001133 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001134
1135 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001136 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001137 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1138 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 +01001139 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001140 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001141 }
1142 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001143 if (h1m->flags & H1_MF_VER_11) {
1144 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001145 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001146 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001147 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001148}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001149
Christopher Fauletb992af02019-03-28 15:42:24 +01001150static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001151{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001152 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001153 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001154 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001156}
1157
Christopher Fauletb992af02019-03-28 15:42:24 +01001158static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1159{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001160 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001161 h1_set_cli_conn_mode(h1s, h1m);
1162 else
1163 h1_set_srv_conn_mode(h1s, h1m);
1164
1165 if (!(h1m->flags & H1_MF_RESP))
1166 h1_update_req_conn_value(h1s, h1m, conn_val);
1167 else
1168 h1_update_res_conn_value(h1s, h1m, conn_val);
1169}
Christopher Faulete44769b2018-11-29 23:01:45 +01001170
Christopher Faulet98fbe952019-07-22 16:18:24 +02001171/* Try to adjust the case of the message header name using the global map
1172 * <hdrs_map>.
1173 */
1174static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1175{
1176 struct ebpt_node *node;
1177 struct h1_hdr_entry *entry;
1178
1179 /* No entry in the map, do nothing */
1180 if (eb_is_empty(&hdrs_map.map))
1181 return;
1182
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001183 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001184 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1185 return;
1186
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001187 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001188 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1189 return;
1190
1191 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1192 if (!node)
1193 return;
1194 entry = container_of(node, struct h1_hdr_entry, node);
1195 name->ptr = entry->name.ptr;
1196 name->len = entry->name.len;
1197}
1198
Christopher Faulete44769b2018-11-29 23:01:45 +01001199/* Append the description of what is present in error snapshot <es> into <out>.
1200 * The description must be small enough to always fit in a buffer. The output
1201 * buffer may be the trash so the trash must not be used inside this function.
1202 */
1203static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1204{
1205 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001206 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1207 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1208 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1209 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1210 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1211 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001212}
1213/*
1214 * Capture a bad request or response and archive it in the proxy's structure.
1215 * By default it tries to report the error position as h1m->err_pos. However if
1216 * this one is not set, it will then report h1m->next, which is the last known
1217 * parsing point. The function is able to deal with wrapping buffers. It always
1218 * displays buffers as a contiguous area starting at buf->p. The direction is
1219 * determined thanks to the h1m's flags.
1220 */
1221static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1222 struct h1m *h1m, struct buffer *buf)
1223{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001224 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001225 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001226 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001227 union error_snapshot_ctx ctx;
1228
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001229 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001230 if (sess == NULL)
1231 sess = si_strm(h1s->cs->data)->sess;
1232 if (!(h1m->flags & H1_MF_RESP))
1233 other_end = si_strm(h1s->cs->data)->be;
1234 else
1235 other_end = sess->fe;
1236 } else
1237 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001238
1239 /* http-specific part now */
1240 ctx.h1.state = h1m->state;
1241 ctx.h1.c_flags = h1c->flags;
1242 ctx.h1.s_flags = h1s->flags;
1243 ctx.h1.m_flags = h1m->flags;
1244 ctx.h1.m_clen = h1m->curr_len;
1245 ctx.h1.m_blen = h1m->body_len;
1246
1247 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1248 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001249 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1250 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001251}
1252
Christopher Fauletadb22202018-12-12 10:32:09 +01001253/* Emit the chunksize followed by a CRLF in front of data of the buffer
1254 * <buf>. It goes backwards and starts with the byte before the buffer's
1255 * head. The caller is responsible for ensuring there is enough room left before
1256 * the buffer's head for the string.
1257 */
1258static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1259{
1260 char *beg, *end;
1261
1262 beg = end = b_head(buf);
1263 *--beg = '\n';
1264 *--beg = '\r';
1265 do {
1266 *--beg = hextab[chksz & 0xF];
1267 } while (chksz >>= 4);
1268 buf->head -= (end - beg);
1269 b_add(buf, end - beg);
1270}
1271
1272/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1273 * ensuring there is enough room left in the buffer for the string. */
1274static void h1_emit_chunk_crlf(struct buffer *buf)
1275{
1276 *(b_peek(buf, b_data(buf))) = '\r';
1277 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1278 b_add(buf, 2);
1279}
1280
Christopher Faulet129817b2018-09-20 16:14:40 +02001281/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001282 * Switch the stream to tunnel mode. This function must only be called on 2xx
1283 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001284 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001285static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001286{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001287 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001288
Christopher Faulet5be651d2021-01-22 15:28:03 +01001289 h1s->req.state = H1_MSG_TUNNEL;
1290 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001291
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001292 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001293 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001294
Christopher Faulet5be651d2021-01-22 15:28:03 +01001295 TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01001296
Christopher Faulet5be651d2021-01-22 15:28:03 +01001297 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1298 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1299 if (b_data(&h1c->ibuf))
1300 h1_wake_stream_for_recv(h1s);
1301 tasklet_wakeup(h1c->wait_event.tasklet);
1302 TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001303 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001304 if (h1c->flags & H1C_F_WAIT_INPUT) {
1305 h1c->flags &= ~H1C_F_WAIT_INPUT;
1306 h1_wake_stream_for_send(h1s);
1307 if (b_data(&h1c->obuf))
1308 tasklet_wakeup(h1c->wait_event.tasklet);
1309 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001310 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001311}
1312
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001313/* Search for a websocket key header. The message should have been identified
1314 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001315 *
1316 * On the request side, if found the key is stored in the session. It might be
1317 * needed to calculate response key if the server side is using http/2.
1318 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001319 * On the response side, the key might be verified if haproxy has been
1320 * responsible for the generation of a key. This happens when a h2 client is
1321 * interfaced with a h1 server.
1322 *
1323 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001324 */
1325static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1326{
1327 struct htx_blk *blk;
1328 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001329 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001330 int ws_key_found = 0, idx;
1331
1332 idx = htx_get_head(htx); // returns the SL that we skip
1333 while ((idx = htx_get_next(htx, idx)) != -1) {
1334 blk = htx_get_blk(htx, idx);
1335 type = htx_get_blk_type(blk);
1336
1337 if (type == HTX_BLK_UNUSED)
1338 continue;
1339
1340 if (type != HTX_BLK_HDR)
1341 break;
1342
1343 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001344 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001345
Amaury Denoyellec1938232020-12-11 17:53:03 +01001346 /* Websocket key is base64 encoded of 16 bytes */
1347 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001348 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001349 /* Copy the key on request side
1350 * we might need it if the server is using h2 and does
1351 * not provide the response
1352 */
1353 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001354 ws_key_found = 1;
1355 break;
1356 }
1357 else if (isteqi(n, ist("sec-websocket-accept")) &&
1358 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001359 /* Need to verify the response key if the input was
1360 * generated by haproxy
1361 */
1362 if (h1s->ws_key[0]) {
1363 char key[29];
1364 h1_calculate_ws_output_key(h1s->ws_key, key);
1365 if (!isteqi(ist(key), v))
1366 break;
1367 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001368 ws_key_found = 1;
1369 break;
1370 }
1371 }
1372
1373 /* missing websocket key, reject the message */
1374 if (!ws_key_found) {
1375 htx->flags |= HTX_FL_PARSING_ERROR;
1376 return 0;
1377 }
1378
1379 return 1;
1380}
1381
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001382/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001383 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001384 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001385 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001386 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001387static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001388 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001389{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001390 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001391 int ret = 0;
1392
Willy Tarreau022e5e52020-09-10 09:33:15 +02001393 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 +02001394
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001395 if (h1s->meth == HTTP_METH_CONNECT)
1396 h1m->flags |= H1_MF_METH_CONNECT;
1397 if (h1s->meth == HTTP_METH_HEAD)
1398 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001399
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001400 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1401 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001402 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 +02001403 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001404 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001405 TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001406 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1407 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001408 goto end;
1409 }
1410
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001411 /* If websocket handshake, search for the websocket key */
1412 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1413 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1414 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1415 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001416 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001417 TRACE_ERROR("missing/invalid websocket key, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001418 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1419
1420 ret = 0;
1421 goto end;
1422 }
1423 }
1424
Christopher Faulet486498c2019-10-11 14:22:00 +02001425 if (h1m->err_pos >= 0) {
1426 /* Maybe we found an error during the parsing while we were
1427 * configured not to block on that, so we have to capture it
1428 * now.
1429 */
1430 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1431 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1432 }
1433
Christopher Faulet5696f542020-12-02 16:08:38 +01001434 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001435 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001436 if (h1s->meth == HTTP_METH_HEAD)
1437 h1s->flags |= H1S_F_BODYLESS_RESP;
1438 }
1439 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001440 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001441 if (h1s->status == 204 || h1s->status == 304)
1442 h1s->flags |= H1S_F_BODYLESS_RESP;
1443 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001444 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001445 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001446
Christopher Faulet129817b2018-09-20 16:14:40 +02001447 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001448 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 +02001449 return ret;
1450}
1451
1452/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001453 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001454 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1455 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001456 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001457static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001458 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001459 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001460{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001461 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001462
Willy Tarreau022e5e52020-09-10 09:33:15 +02001463 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 +02001464 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001465 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001466 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 +01001467 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001468 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001469 TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001470 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001472 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 }
1474
Christopher Faulet02a02532019-11-15 09:36:28 +01001475 *ofs += ret;
1476
1477 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001478 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 +02001479 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001480}
1481
1482/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001483 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1484 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1485 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1486 * responsible to update the parser state <h1m>.
1487 */
1488static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1489 struct buffer *buf, size_t *ofs, size_t max)
1490{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001491 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001492
Willy Tarreau022e5e52020-09-10 09:33:15 +02001493 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 +02001494 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001495 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001496 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 +01001497 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001498 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001499 TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001500 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1501 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001502 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001503 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001504
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001505 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001506
1507 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001508 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 +02001509 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001510}
1511
1512/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001513 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001514 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1515 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001516 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001517static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001518{
Christopher Faulet539e0292018-11-19 10:40:09 +01001519 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001520 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001521 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001522 size_t data;
1523 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001524 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001525
Christopher Faulet539e0292018-11-19 10:40:09 +01001526 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001527 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001528
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001529 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001530 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001531
Christopher Faulet2eed8002020-12-07 11:26:13 +01001532 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001533 goto end;
1534
Christopher Faulet5be651d2021-01-22 15:28:03 +01001535 if (h1c->flags & H1C_F_WAIT_OUTPUT)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001536 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001537
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001538 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001539 size_t used = htx_used_space(htx);
1540
Christopher Faulet129817b2018-09-20 16:14:40 +02001541 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001542 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001543 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001544 if (!ret)
1545 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001546
1547 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1548 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1549
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001550 /* Reject Protocol upgrade request with payload */
1551 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1552 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001553 TRACE_ERROR("Upgrade with body not implemented, reject H1 message",
1554 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001555 break;
1556 }
1557
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001558 if ((h1m->flags & H1_MF_RESP) &&
1559 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1560 h1m_init_res(&h1s->res);
1561 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001562 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001563 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001564 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001565 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001566 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001567 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001568 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001569 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001570
1571 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1572 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001573 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001574 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001575 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1576 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1577 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001578 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001579
Christopher Faulet76014fd2019-12-10 11:47:22 +01001580 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1581 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001582 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001583 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001584 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1585 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001586
Christopher Faulet5be651d2021-01-22 15:28:03 +01001587 if ((h1m->flags & H1_MF_RESP) &&
1588 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1589 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001590 else {
1591 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1592 /* Unfinished transaction: block this input side waiting the end of the output side */
1593 h1c->flags |= H1C_F_WAIT_OUTPUT;
1594 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1595 }
1596 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1597 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1598 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1599 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1600 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001601 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001602 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001603 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001604 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001605 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001606 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001607 if (!ret)
1608 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001609
1610 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1611 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001612 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001613 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001614 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001615 break;
1616 }
1617
Christopher Faulet30db3d72019-05-17 15:35:33 +02001618 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001619 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1620
Christopher Faulet129817b2018-09-20 16:14:40 +02001621
Christopher Faulet2eed8002020-12-07 11:26:13 +01001622 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001623 TRACE_ERROR("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001624 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001625 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001626
Christopher Faulet539e0292018-11-19 10:40:09 +01001627 b_del(&h1c->ibuf, total);
1628
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001629 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001630 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1631
Christopher Faulet30db3d72019-05-17 15:35:33 +02001632 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001633 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001634 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001635 TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01001636 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001637 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001638
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001639 if (!b_data(&h1c->ibuf))
1640 h1_release_buf(h1c, &h1c->ibuf);
1641
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001642 if (!(h1c->flags & H1C_F_ST_READY)) {
1643 /* The H1 connection is not ready. Most of time, there is no CS
1644 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1645 * cases, it is only possible on the client side.
1646 */
1647 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1648
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001649 if (h1m->state <= H1_MSG_LAST_LF) {
1650 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1651 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1652 goto end;
1653 }
1654
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001655 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1656 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1657 BUG_ON(h1s->cs);
1658 if (!h1s_new_cs(h1s, buf)) {
1659 h1c->flags |= H1C_F_ST_ERROR;
1660 goto err;
1661 }
1662 }
1663 else {
1664 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1665 BUG_ON(h1s->cs == NULL);
1666 if (!h1s_upgrade_cs(h1s, buf)) {
1667 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001668 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001669 goto err;
1670 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001671 }
1672 }
1673
1674 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001675 if (!(h1m->flags & H1_MF_CHNK) &&
1676 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1677 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1678 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1679 }
1680 else {
1681 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1682 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1683 }
1684
Christopher Fauleta22782b2021-02-08 17:18:01 +01001685 /* Set EOI on conn-stream in DONE state iff:
1686 * - it is a response
1687 * - it is a request but no a protocol upgrade nor a CONNECT
1688 *
1689 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001690 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001691 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001692 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1693 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Christopher Fauleta583af62020-09-24 15:35:37 +02001694 h1s->cs->flags |= CS_FL_EOI;
1695
Christopher Fauletec4207c2021-04-08 18:34:30 +02001696 out:
Christopher Faulet6716cc22019-12-20 17:33:24 +01001697 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001698 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001699 else {
1700 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1701 if (h1s->flags & H1S_F_REOS) {
1702 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001703 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1704 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1705 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001706 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001707 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001708 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001709 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001710 TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1711 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001712
1713 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1714 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1715 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1716 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1717 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001718 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001719 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001720
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001721 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001722 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001723 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001724
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001725 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001726 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001727 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001728 if (h1s->cs)
1729 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001730 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001731 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001732}
1733
Christopher Faulet129817b2018-09-20 16:14:40 +02001734/*
1735 * Process outgoing data. It parses data and transfer them from the channel buffer into
1736 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1737 * 0 if it couldn't proceed.
1738 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001739static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1740{
Christopher Faulet129817b2018-09-20 16:14:40 +02001741 struct h1s *h1s = h1c->h1s;
1742 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001743 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001744 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001745 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001746 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001747 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001748 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001749
Christopher Faulet94b2c762019-05-24 15:28:57 +02001750 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001751 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1752
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001753 if (htx_is_empty(chn_htx))
1754 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001755
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001756 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1757 goto end;
1758
Christopher Fauletdea24742021-01-22 15:12:30 +01001759 if (h1c->flags & H1C_F_WAIT_INPUT)
1760 goto end;
1761
Christopher Faulet51dbc942018-09-13 09:05:15 +02001762 if (!h1_get_buf(h1c, &h1c->obuf)) {
1763 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001764 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 +02001765 goto end;
1766 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001767
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001768 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001769
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001770 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001771 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001772
Willy Tarreau3815b222018-12-11 19:50:43 +01001773 /* Perform some optimizations to reduce the number of buffer copies.
1774 * First, if the mux's buffer is empty and the htx area contains
1775 * exactly one data block of the same size as the requested count,
1776 * then it's possible to simply swap the caller's buffer with the
1777 * mux's output buffer and adjust offsets and length to match the
1778 * entire DATA HTX block in the middle. In this case we perform a
1779 * true zero-copy operation from end-to-end. This is the situation
1780 * that happens all the time with large files. Second, if this is not
1781 * possible, but the mux's output buffer is empty, we still have an
1782 * opportunity to avoid the copy to the intermediary buffer, by making
1783 * the intermediary buffer's area point to the output buffer's area.
1784 * In this case we want to skip the HTX header to make sure that copies
1785 * remain aligned and that this operation remains possible all the
1786 * time. This goes for headers, data blocks and any data extracted from
1787 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001788 */
1789 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001790 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001791 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001792 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001793 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001794 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001795 void *old_area;
1796
Christopher Faulet6b81df72019-10-01 22:08:43 +02001797 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001798 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1799 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1800 last_data = 1;
1801 }
1802
Christopher Faulete5596bf2020-12-02 16:13:22 +01001803 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001804 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001805 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001806 h1c->obuf.data = count;
1807
1808 buf->area = old_area;
1809 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001810
Christopher Faulet6b81df72019-10-01 22:08:43 +02001811 chn_htx = (struct htx *)buf->area;
1812 htx_reset(chn_htx);
1813
Christopher Fauletadb22202018-12-12 10:32:09 +01001814 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001815 * size and eventually the last chunk. We have at least
1816 * the size of the struct htx to write the chunk
1817 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001818 */
1819 if (h1m->flags & H1_MF_CHNK) {
1820 h1_emit_chunk_size(&h1c->obuf, count);
1821 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001822 if (last_data) {
1823 /* Emit the last chunk too at the buffer's end */
1824 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1825 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001826 }
1827
Christopher Faulet6b81df72019-10-01 22:08:43 +02001828 if (h1m->state == H1_MSG_DATA)
1829 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001830 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001831 else
1832 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001833 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001834
Christopher Faulete5596bf2020-12-02 16:13:22 +01001835 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001836 if (last_data) {
1837 h1m->state = H1_MSG_DONE;
1838 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1839 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1840 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1841 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1842 }
1843
1844 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1845 H1_EV_TX_DATA, h1c->conn, h1s);
1846 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001847 goto out;
1848 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001849 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001850 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001851 else
1852 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001853
Christopher Fauletc2518a52019-06-25 21:41:02 +02001854 tmp.data = 0;
1855 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001856 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001857 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001858 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001859 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001860 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001861 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001862
Christopher Fauletb2e84162018-12-06 11:39:49 +01001863 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001864 if (type != HTX_BLK_DATA && vlen > count)
1865 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001866
Christopher Faulet94b2c762019-05-24 15:28:57 +02001867 if (type == HTX_BLK_UNUSED)
1868 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001869
Christopher Faulet94b2c762019-05-24 15:28:57 +02001870 switch (h1m->state) {
1871 case H1_MSG_RQBEFORE:
1872 if (type != HTX_BLK_REQ_SL)
1873 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001874 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 +02001875 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001876 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001877 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001878 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001879 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001880 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001881 if (sl->flags & HTX_SL_F_BODYLESS)
1882 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001883 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001884 if (h1s->meth == HTTP_METH_HEAD)
1885 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Fauletb385b502021-01-13 18:47:57 +01001886 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1887 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001888 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1889 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1890 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001891 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001892
Christopher Faulet94b2c762019-05-24 15:28:57 +02001893 case H1_MSG_RPBEFORE:
1894 if (type != HTX_BLK_RES_SL)
1895 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001896 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 +02001897 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001898 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001899 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001900 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001901 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001902 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001903 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01001904 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02001905 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001906 else if (h1s->status == 204 || h1s->status == 304)
1907 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001908 h1m->state = H1_MSG_HDR_FIRST;
1909 break;
1910
Christopher Faulet94b2c762019-05-24 15:28:57 +02001911 case H1_MSG_HDR_FIRST:
1912 case H1_MSG_HDR_NAME:
1913 case H1_MSG_HDR_L2_LWS:
1914 if (type == HTX_BLK_EOH)
1915 goto last_lf;
1916 if (type != HTX_BLK_HDR)
1917 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001918
Christopher Faulet9768c262018-10-22 09:34:31 +02001919 h1m->state = H1_MSG_HDR_NAME;
1920 n = htx_get_blk_name(chn_htx, blk);
1921 v = htx_get_blk_value(chn_htx, blk);
1922
Christopher Faulet86d144c2019-08-14 16:32:25 +02001923 /* Skip all pseudo-headers */
1924 if (*(n.ptr) == ':')
1925 goto skip_hdr;
1926
Christopher Faulet91fcf212020-12-02 16:17:15 +01001927 if (isteq(n, ist("transfer-encoding"))) {
1928 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1929 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02001930 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01001931 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001932 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01001933 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1934 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01001935 /* Only skip C-L header with invalid value. */
1936 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001937 goto skip_hdr;
1938 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001939 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001940 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001941 if (!v.len)
1942 goto skip_hdr;
1943 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01001944 else if (isteq(n, ist("upgrade"))) {
1945 h1_parse_upgrade_header(h1m, v);
1946 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001947 else if ((isteq(n, ist("sec-websocket-accept")) &&
1948 h1m->flags & H1_MF_RESP) ||
1949 (isteq(n, ist("sec-websocket-key")) &&
1950 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001951 ws_key_found = 1;
1952 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001953
Christopher Faulet67d58092019-10-02 10:51:38 +02001954 /* Skip header if same name is used to add the server name */
1955 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1956 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1957 goto skip_hdr;
1958
Christopher Faulet98fbe952019-07-22 16:18:24 +02001959 /* Try to adjust the case of the header name */
1960 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1961 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001962 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001963 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001964 skip_hdr:
1965 h1m->state = H1_MSG_HDR_L2_LWS;
1966 break;
1967
Christopher Faulet94b2c762019-05-24 15:28:57 +02001968 case H1_MSG_LAST_LF:
1969 if (type != HTX_BLK_EOH)
1970 goto error;
1971 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001972 h1m->state = H1_MSG_LAST_LF;
1973 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001974 /* If the reply comes from haproxy while the request is
1975 * not finished, we force the connection close. */
1976 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1977 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1978 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1979 }
1980
1981 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001982 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001983 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001984 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001985 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001986 /* Try to adjust the case of the header name */
1987 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1988 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001989 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001990 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001991 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001992 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001993 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001994
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001995 if ((h1s->meth != HTTP_METH_CONNECT &&
1996 (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 +01001997 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01001998 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001999 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002000 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2001 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002002 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002003 n = ist("transfer-encoding");
2004 v = ist("chunked");
2005 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2006 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002007 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002008 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002009 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002010 h1m->flags |= H1_MF_CHNK;
2011 }
2012
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002013 /* Now add the server name to a header (if requested) */
2014 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
2015 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
2016 struct server *srv = objt_server(h1c->conn->target);
2017
2018 if (srv) {
2019 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
2020 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002021
2022 /* Try to adjust the case of the header name */
2023 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2024 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002025 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002026 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002027 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002028 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002029 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2030 }
2031
Amaury Denoyellec1938232020-12-11 17:53:03 +01002032 /* Add websocket handshake key if needed */
2033 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2034 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002035 if (!(h1m->flags & H1_MF_RESP)) {
2036 /* generate a random websocket key
2037 * stored in the session to
2038 * verify it on the response side
2039 */
2040 h1_generate_random_ws_input_key(h1s->ws_key);
2041
2042 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2043 ist(h1s->ws_key),
2044 &tmp)) {
2045 goto full;
2046 }
2047 }
2048 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002049 /* add the response header key */
2050 char key[29];
2051 h1_calculate_ws_output_key(h1s->ws_key, key);
2052 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2053 ist(key),
2054 &tmp)) {
2055 goto full;
2056 }
2057 }
2058 }
2059
Christopher Faulet6b81df72019-10-01 22:08:43 +02002060 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2061 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2062
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002063 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002064 if (!chunk_memcat(&tmp, "\r\n", 2))
2065 goto full;
2066 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002067 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002068 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002069 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002070 if (!chunk_memcat(&tmp, "\r\n", 2))
2071 goto full;
2072 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002073 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002074 else if ((h1m->flags & H1_MF_RESP) &&
2075 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002076 if (!chunk_memcat(&tmp, "\r\n", 2))
2077 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002078 h1m_init_res(&h1s->res);
2079 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002080 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002081 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002082 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002083 else {
2084 /* EOM flag is set and it is the last block */
2085 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002086 if (h1m->flags & H1_MF_CHNK) {
2087 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2088 goto full;
2089 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002090 else if (!chunk_memcat(&tmp, "\r\n", 2))
2091 goto full;
2092 goto done;
2093 }
2094 else if (!chunk_memcat(&tmp, "\r\n", 2))
2095 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002096 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002097 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002098 break;
2099
Christopher Faulet94b2c762019-05-24 15:28:57 +02002100 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002101 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002102 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002103 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2104 goto trailers;
2105
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002106 /* If the message is not chunked, never
2107 * add the last chunk. */
2108 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002109 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002110 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 +02002111 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002112 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002113 else if (type != HTX_BLK_DATA)
2114 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002115
2116 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 +02002117
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002118 /* It is the last block of this message. After this one,
2119 * only tunneled data may be forwarded. */
2120 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2121 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2122 last_data = 1;
2123 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002124
2125 if (vlen > count) {
2126 /* Get the maximum amount of data we can xferred */
2127 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002128 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002129 }
2130
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002131 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2132 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2133 goto skip_data;
2134 }
2135
Christopher Fauletd9233f02019-10-14 14:01:24 +02002136 chklen = 0;
2137 if (h1m->flags & H1_MF_CHNK) {
2138 chklen = b_room(&tmp);
2139 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2140 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2141 (chklen < 1048576) ? 5 : 8);
2142 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002143
2144 /* If it is the end of the chunked message (without EOT), reserve the
2145 * last chunk size */
2146 if (last_data)
2147 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002148 }
2149
2150 if (vlen + chklen > b_room(&tmp)) {
2151 /* too large for the buffer */
2152 if (chklen >= b_room(&tmp))
2153 goto full;
2154 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002155 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002156 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002157 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002158 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002159 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002160 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002161
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002162 /* Space already reserved, so it must succeed */
2163 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2164 goto error;
2165
Christopher Faulet6b81df72019-10-01 22:08:43 +02002166 if (h1m->state == H1_MSG_DATA)
2167 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002168 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002169 else
2170 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002171 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002172
2173 skip_data:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002174 if (last_data)
2175 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002176 break;
2177
Christopher Faulet94b2c762019-05-24 15:28:57 +02002178 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002179 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002180 goto error;
2181 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002183
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002184 /* If the message is not chunked, ignore
2185 * trailers. It may happen with H2 messages. */
Christopher Faulet0a916d22021-02-10 08:48:19 +01002186 if (!(h1m->flags & H1_MF_CHNK)) {
2187 if (type == HTX_BLK_EOT)
2188 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002189 break;
Christopher Faulet0a916d22021-02-10 08:48:19 +01002190 }
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002191
Christopher Faulete5596bf2020-12-02 16:13:22 +01002192 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2193 TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002194 if (type == HTX_BLK_EOT)
2195 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002196 break;
2197 }
2198
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002199 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002200 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002201 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002202 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2203 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002204 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002205 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002206 else { // HTX_BLK_TLR
2207 n = htx_get_blk_name(chn_htx, blk);
2208 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002209
2210 /* Try to adjust the case of the header name */
2211 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2212 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002213 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002214 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002215 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002216 break;
2217
Christopher Faulet94b2c762019-05-24 15:28:57 +02002218 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002219 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2220 goto error; /* For now return an error */
2221
Christopher Faulet94b2c762019-05-24 15:28:57 +02002222 done:
Christopher Faulet36893672021-02-10 09:52:07 +01002223 if (!(chn_htx->flags & HTX_FL_EOM)) {
2224 TRACE_STATE("No EOM flags in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2225 goto error; /* For now return an error */
2226 }
2227
Christopher Faulet94b2c762019-05-24 15:28:57 +02002228 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002229 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2230 h1c->flags |= H1C_F_WAIT_INPUT;
2231 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2232 }
2233 else if ((h1m->flags & H1_MF_RESP) &&
2234 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2235 /* a successful reply to a CONNECT or a protocol switching is sent
2236 * to the client. Switch the response to tunnel mode.
2237 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002238 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002239 TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletf3158e92019-11-15 11:14:23 +01002240 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002241
2242 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002243 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002244 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002245 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02002246 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002247
2248 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2249 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002250 break;
2251
Christopher Faulet9768c262018-10-22 09:34:31 +02002252 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002253 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002254 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002255 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002256 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002257 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002258 TRACE_ERROR("processing output error, set error on h1c/h1s",
2259 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002260 break;
2261 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002262
2263 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002264 total += vlen;
2265 count -= vlen;
2266 if (sz == vlen)
2267 blk = htx_remove_blk(chn_htx, blk);
2268 else {
2269 htx_cut_data_blk(chn_htx, blk, vlen);
2270 break;
2271 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002272 }
2273
Christopher Faulet9768c262018-10-22 09:34:31 +02002274 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002275 /* when the output buffer is empty, tmp shares the same area so that we
2276 * only have to update pointers and lengths.
2277 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002278 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2279 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002280 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002281 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002282
Willy Tarreau3815b222018-12-11 19:50:43 +01002283 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002284 out:
2285 if (!buf_room_for_htx_data(&h1c->obuf)) {
2286 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002287 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002288 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002289 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002290 /* Both the request and the response reached the DONE state. So set EOI
2291 * flag on the conn-stream. Most of time, the flag will already be set,
2292 * except for protocol upgrades. Report an error if data remains blocked
2293 * in the output buffer.
2294 */
2295 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2296 if (!htx_is_empty(chn_htx)) {
2297 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002298 TRACE_ERROR("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002299 }
2300 h1s->cs->flags |= CS_FL_EOI;
2301 }
2302
Christopher Faulet6b81df72019-10-01 22:08:43 +02002303 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002304 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002305
2306 full:
2307 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2308 h1c->flags |= H1C_F_OUT_FULL;
2309 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002310}
2311
Christopher Faulet51dbc942018-09-13 09:05:15 +02002312/*********************************************************/
2313/* functions below are I/O callbacks from the connection */
2314/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002315static void h1_wake_stream_for_recv(struct h1s *h1s)
2316{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002317 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002318 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002319 tasklet_wakeup(h1s->subs->tasklet);
2320 h1s->subs->events &= ~SUB_RETRY_RECV;
2321 if (!h1s->subs->events)
2322 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002323 }
2324}
2325static void h1_wake_stream_for_send(struct h1s *h1s)
2326{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002327 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002328 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002329 tasklet_wakeup(h1s->subs->tasklet);
2330 h1s->subs->events &= ~SUB_RETRY_SEND;
2331 if (!h1s->subs->events)
2332 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002333 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002334}
2335
Christopher Fauletad4daf62021-01-21 17:49:01 +01002336/* alerts the data layer following this sequence :
2337 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2338 * - if its subscribed to send, then it's woken up for send
2339 * - if it was subscribed to neither, its ->wake() callback is called
2340 */
2341static void h1_alert(struct h1s *h1s)
2342{
2343 if (h1s->subs) {
2344 h1_wake_stream_for_recv(h1s);
2345 h1_wake_stream_for_send(h1s);
2346 }
2347 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2348 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2349 h1s->cs->data_cb->wake(h1s->cs);
2350 }
2351}
2352
Christopher Fauletc18fc232020-10-06 17:41:36 +02002353/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2354 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2355 * retryable errors (allocation error or buffer full). On success, the error is
2356 * copied in the output buffer.
2357*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002358static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002359{
2360 int rc = http_get_status_idx(h1c->errcode);
2361 int ret = 0;
2362
2363 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2364
2365 /* Verify if the error is mapped on /dev/null or any empty file */
2366 /// XXX: do a function !
2367 if (h1c->px->replies[rc] &&
2368 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2369 h1c->px->replies[rc]->body.errmsg &&
2370 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2371 /* Empty error, so claim a success */
2372 ret = 1;
2373 goto out;
2374 }
2375
2376 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2377 h1c->flags |= H1C_F_ERR_PENDING;
2378 goto out;
2379 }
2380
2381 if (!h1_get_buf(h1c, &h1c->obuf)) {
2382 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2383 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2384 goto out;
2385 }
2386 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2387 if (unlikely(ret <= 0)) {
2388 if (!ret) {
2389 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2390 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2391 goto out;
2392 }
2393 else {
2394 /* we cannot report this error, so claim a success */
2395 ret = 1;
2396 }
2397 }
2398 h1c->flags &= ~H1C_F_ERR_PENDING;
2399 out:
2400 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2401 return ret;
2402}
2403
2404/* Try to send a 500 internal error. It relies on h1_send_error to send the
2405 * error. This function takes care of incrementing stats and tracked counters.
2406 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002407static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002408{
2409 struct session *sess = h1c->conn->owner;
2410 int ret = 1;
2411
2412 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002413 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002414 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2415 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002416 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002417 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002418
2419 h1c->errcode = 500;
2420 ret = h1_send_error(h1c);
2421 sess_log(sess);
2422 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002423}
2424
Christopher Fauletc18fc232020-10-06 17:41:36 +02002425/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2426 * error. This function takes care of incrementing stats and tracked counters.
2427 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002428static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002429{
2430 struct session *sess = h1c->conn->owner;
2431 int ret = 1;
2432
2433 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2434 goto end;
2435
2436 session_inc_http_req_ctr(sess);
2437 session_inc_http_err_ctr(sess);
2438 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002439 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2440 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002441 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002442 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002443
2444 h1c->errcode = 400;
2445 ret = h1_send_error(h1c);
2446 sess_log(sess);
2447
2448 end:
2449 return ret;
2450}
2451
Christopher Faulet2eed8002020-12-07 11:26:13 +01002452/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2453 * the error. This function takes care of incrementing stats and tracked
2454 * counters.
2455 */
2456static int h1_handle_not_impl_err(struct h1c *h1c)
2457{
2458 struct session *sess = h1c->conn->owner;
2459 int ret = 1;
2460
2461 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2462 goto end;
2463
2464 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002465 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002466 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2467 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002468 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002469 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002470
2471 h1c->errcode = 501;
2472 ret = h1_send_error(h1c);
2473 sess_log(sess);
2474
2475 end:
2476 return ret;
2477}
2478
Christopher Fauletc18fc232020-10-06 17:41:36 +02002479/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2480 * error. This function takes care of incrementing stats and tracked counters.
2481 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002482static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002483{
2484 struct session *sess = h1c->conn->owner;
2485 int ret = 1;
2486
2487 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2488 goto end;
2489
2490 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002491 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002492 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2493 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002494 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002495 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002496
2497 h1c->errcode = 408;
2498 ret = h1_send_error(h1c);
2499 sess_log(sess);
2500 end:
2501 return ret;
2502}
2503
2504
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505/*
2506 * Attempt to read data, and subscribe if none available
2507 */
2508static int h1_recv(struct h1c *h1c)
2509{
2510 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002511 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002512 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002513
Christopher Faulet6b81df72019-10-01 22:08:43 +02002514 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2515
2516 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2517 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002518 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002519 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002520
Christopher Fauletae635762020-09-21 11:47:16 +02002521 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2522 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002523 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002524 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002525
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002526 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2527 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002528 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002529 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002530 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002531
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002532 /*
2533 * If we only have a small amount of data, realign it,
2534 * it's probably cheaper than doing 2 recv() calls.
2535 */
2536 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2537 b_slow_realign(&h1c->ibuf, trash.area, 0);
2538
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002539 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002540 if (!h1c->h1s ||
2541 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2542 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002543 flags |= CO_RFL_READ_ONCE;
2544
Willy Tarreau45f2b892018-12-05 07:59:27 +01002545 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002546 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002547 if (h1c->flags & H1C_F_IN_FULL) {
2548 h1c->flags &= ~H1C_F_IN_FULL;
2549 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2550 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002551
2552 if (!b_data(&h1c->ibuf)) {
2553 /* try to pre-align the buffer like the rxbufs will be
2554 * to optimize memory copies.
2555 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002556 h1c->ibuf.head = sizeof(struct htx);
2557 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002558 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002559 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002560 if (max && !ret && h1_recv_allowed(h1c)) {
2561 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2562 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002563 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002564 else {
2565 h1_wake_stream_for_recv(h1c->h1s);
2566 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002567 }
2568
Christopher Faulet51dbc942018-09-13 09:05:15 +02002569 if (!b_data(&h1c->ibuf))
2570 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002571 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002572 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002573 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2574 }
2575
2576 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002577 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002578}
2579
2580
2581/*
2582 * Try to send data if possible
2583 */
2584static int h1_send(struct h1c *h1c)
2585{
2586 struct connection *conn = h1c->conn;
2587 unsigned int flags = 0;
2588 size_t ret;
2589 int sent = 0;
2590
Christopher Faulet6b81df72019-10-01 22:08:43 +02002591 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2592
2593 if (conn->flags & CO_FL_ERROR) {
2594 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002595 b_reset(&h1c->obuf);
2596 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002597 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002598
2599 if (!b_data(&h1c->obuf))
2600 goto end;
2601
Christopher Faulet46230362019-10-17 16:04:20 +02002602 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002603 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002604 if (h1c->flags & H1C_F_CO_STREAMER)
2605 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002606
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002607 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002608 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002609 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002610 if (h1c->flags & H1C_F_OUT_FULL) {
2611 h1c->flags &= ~H1C_F_OUT_FULL;
2612 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2613 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002614 b_del(&h1c->obuf, ret);
2615 sent = 1;
2616 }
2617
Christopher Faulet145aa472018-12-06 10:56:20 +01002618 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002619 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002620 /* error or output closed, nothing to send, clear the buffer to release it */
2621 b_reset(&h1c->obuf);
2622 }
2623
Christopher Faulet51dbc942018-09-13 09:05:15 +02002624 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002625 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002626 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002627
Christopher Faulet51dbc942018-09-13 09:05:15 +02002628 /* We're done, no more to send */
2629 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002630 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002631 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002632 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002633 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002634 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002635 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002636 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002637 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2638 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002639 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002640 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002641
Christopher Faulet6b81df72019-10-01 22:08:43 +02002642 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002643 return sent;
2644}
2645
Christopher Faulet51dbc942018-09-13 09:05:15 +02002646/* callback called on any event by the connection handler.
2647 * It applies changes and returns zero, or < 0 if it wants immediate
2648 * destruction of the connection.
2649 */
2650static int h1_process(struct h1c * h1c)
2651{
2652 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002653 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002654
Christopher Faulet6b81df72019-10-01 22:08:43 +02002655 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2656
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002657 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002658 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2659 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2660 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002661 struct buffer *buf;
2662 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002663
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002664 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2665 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002666 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002667
2668 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002669 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002670 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2671 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002672 /* Try to match H2 preface before parsing the request headers. */
2673 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2674 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002675 if (h1c->flags & H1C_F_ST_ATTACHED) {
2676 /* Force the REOS here to be sure to release the CS.
2677 Here ATTACHED implies !READY, and h1s defined
2678 */
2679 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2680 h1s->flags |= H1S_F_REOS;
2681 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002682 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002683 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002684 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002685 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002686
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002687 /* Create the H1 stream if not already there */
2688 if (!h1s) {
2689 h1s = h1c_frt_stream_new(h1c);
2690 if (!h1s) {
2691 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002692 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002693 goto no_parsing;
2694 }
2695 }
2696
2697 if (h1s->sess->t_idle == -1)
2698 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2699
2700 /* Get the stream rxbuf */
2701 buf = h1_get_buf(h1c, &h1s->rxbuf);
2702 if (!buf) {
2703 h1c->flags |= H1C_F_IN_SALLOC;
2704 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2705 return 0;
2706 }
2707
2708 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2709 h1_process_input(h1c, buf, count);
2710 h1_release_buf(h1c, &h1s->rxbuf);
2711 h1_set_idle_expiration(h1c);
2712
2713 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002714 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002715 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002716 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002717 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002718 }
2719 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2720 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002721 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002722 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002723 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002724 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2725 h1_handle_not_impl_err(h1c);
2726 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002727 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002728 }
Christopher Fauletae635762020-09-21 11:47:16 +02002729 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002730 h1_send(h1c);
2731
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002732 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002733 if (!(h1c->flags & H1C_F_ST_READY)) {
2734 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002735 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002736 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002737 if (h1_handle_bad_req(h1c))
2738 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002739 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002740 }
2741
2742 /* Handle pending error, if any (only possible on frontend connection) */
2743 if (h1c->flags & H1C_F_ERR_PENDING) {
2744 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2745 if (h1_send_error(h1c))
2746 h1_send(h1c);
2747 }
Christopher Fauletae635762020-09-21 11:47:16 +02002748
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002749 /* If there is some pending outgoing data or error, just wait */
2750 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2751 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002752
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002753 /* Otherwise we can release the H1 connection */
2754 goto release;
2755 }
2756 else {
2757 /* Here there is still a H1 stream with a conn-stream.
2758 * Report the connection state at the stream level
2759 */
2760 if (conn_xprt_read0_pending(conn)) {
2761 h1s->flags |= H1S_F_REOS;
2762 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2763 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002764 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002765 h1s->cs->flags |= CS_FL_ERROR;
2766 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002767 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002768 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002769 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002770
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002771 if (!b_data(&h1c->ibuf))
2772 h1_release_buf(h1c, &h1c->ibuf);
2773
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02002774 /* Check if a soft-stop is in progress.
2775 * Release idling front connection if this is the case.
2776 */
2777 if (!(h1c->flags & H1C_F_IS_BACK)) {
2778 if (unlikely(h1c->px->disabled)) {
2779 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
2780 goto release;
2781 }
2782 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002783
2784 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2785 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2786 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002787 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002788
Christopher Faulet47365272018-10-31 17:40:50 +01002789 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002790 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002791 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002792 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002793
2794 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002795 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05002796 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002797 * attached CS first. Here, the H1C must not be READY */
2798 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2799
2800 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2801 h1s->cs->flags |= CS_FL_EOS;
2802 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2803 h1s->cs->flags |= CS_FL_ERROR;
2804 h1_alert(h1s);
2805 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2806 }
2807 else {
2808 h1_release(h1c);
2809 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2810 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002811 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002812}
2813
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002814struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002815{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002816 struct connection *conn;
2817 struct tasklet *tl = (struct tasklet *)t;
2818 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002819 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002820 int ret = 0;
2821
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002822 if (state & TASK_F_USR1) {
2823 /* the tasklet was idling on an idle connection, it might have
2824 * been stolen, let's be careful!
2825 */
2826 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2827 if (tl->context == NULL) {
2828 /* The connection has been taken over by another thread,
2829 * we're no longer responsible for it, so just free the
2830 * tasklet, and do nothing.
2831 */
2832 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
2833 tasklet_free(tl);
2834 return NULL;
2835 }
2836 conn = h1c->conn;
2837 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002838
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002839 /* Remove the connection from the list, to be sure nobody attempts
2840 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002841 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002842 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2843 if (conn_in_list)
2844 conn_delete_from_tree(&conn->hash_node->node);
2845
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002846 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01002847 } else {
2848 /* we're certain the connection was not in an idle list */
2849 conn = h1c->conn;
2850 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2851 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002852 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002853
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002854 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002855 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002856 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002857 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002858 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002859 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01002860
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002861 /* If we were in an idle list, we want to add it back into it,
2862 * unless h1_process() returned -1, which mean it has destroyed
2863 * the connection (testing !ret is enough, if h1_process() wasn't
2864 * called then ret will be 0 anyway.
2865 */
Willy Tarreau74163142021-03-13 11:30:19 +01002866 if (ret < 0)
2867 t = NULL;
2868
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002869 if (!ret && conn_in_list) {
2870 struct server *srv = objt_server(conn->target);
2871
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002872 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002873 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01002874 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002875 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01002876 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002877 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002878 }
Willy Tarreau74163142021-03-13 11:30:19 +01002879 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002880}
2881
Christopher Faulet51dbc942018-09-13 09:05:15 +02002882static int h1_wake(struct connection *conn)
2883{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002884 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002885 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002886
Christopher Faulet6b81df72019-10-01 22:08:43 +02002887 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2888
Christopher Faulet539e0292018-11-19 10:40:09 +01002889 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002890 ret = h1_process(h1c);
2891 if (ret == 0) {
2892 struct h1s *h1s = h1c->h1s;
2893
Christopher Fauletad4daf62021-01-21 17:49:01 +01002894 if (h1c->flags & H1C_F_ST_ATTACHED)
2895 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002896 }
2897 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002898}
2899
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002900/* Connection timeout management. The principle is that if there's no receipt
2901 * nor sending for a certain amount of time, the connection is closed.
2902 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002903struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002904{
2905 struct h1c *h1c = context;
2906 int expired = tick_is_expired(t->expire, now_ms);
2907
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002908 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002909
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002910 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002911 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002912 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002913
2914 /* Somebody already stole the connection from us, so we should not
2915 * free it, we just have to free the task.
2916 */
2917 if (!t->context) {
2918 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002919 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002920 goto do_leave;
2921 }
2922
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002923 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002924 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002925 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002926 return t;
2927 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002928
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002929 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002930 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002931 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002932 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002933 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002934 t->expire = TICK_ETERNITY;
2935 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2936 return t;
2937 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002938
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002939 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002940 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2941 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002942 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002943 if (h1_handle_req_tout(h1c))
2944 h1_send(h1c);
2945 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2946 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002947 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002948 return t;
2949 }
2950 }
2951
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002952 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05002953 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002954 * attached CS first. Here, the H1C must not be READY */
2955 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2956 h1_alert(h1c->h1s);
2957 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002958 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002959 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2960 return t;
2961 }
2962
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002963 /* We're about to destroy the connection, so make sure nobody attempts
2964 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002965 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002966 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01002967 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002968
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01002969 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002970 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002971
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002972 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002973 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002974
2975 if (!h1c) {
2976 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002977 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002978 return NULL;
2979 }
2980
2981 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002982 h1_release(h1c);
2983 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002984 return NULL;
2985}
2986
Christopher Faulet51dbc942018-09-13 09:05:15 +02002987/*******************************************/
2988/* functions below are used by the streams */
2989/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002990
Christopher Faulet51dbc942018-09-13 09:05:15 +02002991/*
2992 * Attach a new stream to a connection
2993 * (Used for outgoing connections)
2994 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002995static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002996{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002997 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002998 struct conn_stream *cs = NULL;
2999 struct h1s *h1s;
3000
Christopher Faulet6b81df72019-10-01 22:08:43 +02003001 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003002 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003003 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3004 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003005 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003006
Christopher Faulet236c93b2020-07-02 09:19:54 +02003007 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003008 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003009 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3010 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003011 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003012
Christopher Faulet2f0ec662020-09-24 10:30:15 +02003013 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003014 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003015 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3016 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003017 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003018
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003019 /* the connection is not idle anymore, let's mark this */
3020 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003021 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003022
Christopher Faulet6b81df72019-10-01 22:08:43 +02003023 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003024 return cs;
Christopher Faulet26a26432021-01-27 11:27:50 +01003025 err:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003026 cs_free(cs);
Christopher Faulet26a26432021-01-27 11:27:50 +01003027 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003028 return NULL;
3029}
3030
3031/* Retrieves a valid conn_stream from this connection, or returns NULL. For
3032 * this mux, it's easy as we can only store a single conn_stream.
3033 */
3034static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
3035{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003036 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003037 struct h1s *h1s = h1c->h1s;
3038
3039 if (h1s)
3040 return h1s->cs;
3041
3042 return NULL;
3043}
3044
Christopher Faulet73c12072019-04-08 11:23:22 +02003045static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003046{
Christopher Faulet73c12072019-04-08 11:23:22 +02003047 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003048
Christopher Faulet6b81df72019-10-01 22:08:43 +02003049 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003050 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003051 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003052}
3053
3054/*
3055 * Detach the stream from the connection and possibly release the connection.
3056 */
3057static void h1_detach(struct conn_stream *cs)
3058{
3059 struct h1s *h1s = cs->ctx;
3060 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003061 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003062 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003063
Christopher Faulet6b81df72019-10-01 22:08:43 +02003064 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3065
Christopher Faulet51dbc942018-09-13 09:05:15 +02003066 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003067 if (!h1s) {
3068 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003069 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003070 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003071
Olivier Houchardf502aca2018-12-14 19:42:40 +01003072 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003073 h1c = h1s->h1c;
3074 h1s->cs = NULL;
3075
Christopher Faulet42849b02020-10-05 11:35:17 +02003076 sess->accept_date = date;
3077 sess->tv_accept = now;
3078 sess->t_handshake = 0;
3079 sess->t_idle = -1;
3080
Olivier Houchard8a786902018-12-15 16:05:40 +01003081 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3082 h1s_destroy(h1s);
3083
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003084 if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) {
Christopher Fauletf1204b82019-07-19 14:51:06 +02003085 /* If there are any excess server data in the input buffer,
3086 * release it and close the connection ASAP (some data may
3087 * remain in the output buffer). This happens if a server sends
3088 * invalid responses. So in such case, we don't want to reuse
3089 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003090 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003091 if (b_data(&h1c->ibuf)) {
3092 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003093 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003094 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003095 goto release;
3096 }
Christopher Faulet03627242019-07-19 11:34:08 +02003097
Christopher Faulet08016ab2020-07-01 16:10:06 +02003098 if (h1c->conn->flags & CO_FL_PRIVATE) {
3099 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003100 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3101 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003102 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003103 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003104 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003105 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003106 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003107 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003108 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3109 goto end;
3110 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003111 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003112 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003113 if (h1c->conn->owner == sess)
3114 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003115
3116 /* mark that the tasklet may lose its context to another thread and
3117 * that the handler needs to check it under the idle conns lock.
3118 */
3119 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003120 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003121 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3122
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003123 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003124 /* The server doesn't want it, let's kill the connection right away */
3125 h1c->conn->mux->destroy(h1c);
3126 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3127 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003128 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003129 /* At this point, the connection has been added to the
3130 * server idle list, so another thread may already have
3131 * hijacked it, so we can't do anything with it.
3132 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003133 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003134 }
3135 }
3136
Christopher Fauletf1204b82019-07-19 14:51:06 +02003137 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003138 /* We don't want to close right now unless the connection is in error or shut down for writes */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003139 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003140 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003141 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003142 !h1c->conn->owner) {
3143 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003144 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003145 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003146 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003147 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003148 /* If we have a new request, process it immediately or
3149 * subscribe for reads waiting for new data
3150 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003151 if (unlikely(b_data(&h1c->ibuf))) {
3152 if (h1_process(h1c) == -1)
3153 goto end;
3154 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003155 else
3156 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3157 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003158 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003159 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003160 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003161 end:
3162 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003163}
3164
3165
3166static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3167{
3168 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02003169 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003170
3171 if (!h1s)
3172 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003173 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003174
Christopher Faulet6b81df72019-10-01 22:08:43 +02003175 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3176
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003177 if (cs->flags & CS_FL_SHR)
3178 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003179 if (cs->flags & CS_FL_KILL_CONN) {
3180 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3181 goto do_shutr;
3182 }
3183 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3184 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003185 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003186 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003187
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003188 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3189 /* Here attached is implicit because there is CS */
3190 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3191 goto end;
3192 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003193 if (h1s->flags & H1S_F_WANT_KAL) {
3194 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003195 goto end;
3196 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003197
Christopher Faulet7f366362019-04-08 10:51:20 +02003198 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003199 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3200 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003201 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003202 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003203 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003204 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003205 end:
3206 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003207}
3208
3209static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3210{
3211 struct h1s *h1s = cs->ctx;
3212 struct h1c *h1c;
3213
3214 if (!h1s)
3215 return;
3216 h1c = h1s->h1c;
3217
Christopher Faulet6b81df72019-10-01 22:08:43 +02003218 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3219
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003220 if (cs->flags & CS_FL_SHW)
3221 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003222 if (cs->flags & CS_FL_KILL_CONN) {
3223 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003224 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003225 }
3226 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3227 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3228 goto do_shutw;
3229 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003230
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003231 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3232 /* Here attached is implicit because there is CS */
3233 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3234 goto end;
3235 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003236 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3237 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003238 goto end;
3239 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003240
Christopher Faulet7f366362019-04-08 10:51:20 +02003241 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003242 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003243 if (!b_data(&h1c->obuf))
3244 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003245 end:
3246 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003247}
3248
Christopher Faulet666a0c42019-01-08 11:12:04 +01003249static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003250{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003251 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003252
Willy Tarreau62592ad2021-03-26 09:09:42 +01003253 if (conn->flags & CO_FL_SOCK_WR_SH)
3254 return;
3255
Christopher Faulet6b81df72019-10-01 22:08:43 +02003256 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003257 conn_xprt_shutw(conn);
3258 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003259 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003260}
3261
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003262/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3263 * The <es> pointer is not allowed to differ from the one passed to the
3264 * subscribe() call. It always returns zero.
3265 */
3266static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003267{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003268 struct h1s *h1s = cs->ctx;
3269
3270 if (!h1s)
3271 return 0;
3272
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003273 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003274 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003275
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003276 es->events &= ~event_type;
3277 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003278 h1s->subs = NULL;
3279
3280 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003281 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003282
3283 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003284 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003285
Christopher Faulet51dbc942018-09-13 09:05:15 +02003286 return 0;
3287}
3288
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003289/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3290 * event subscriber <es> is not allowed to change from a previous call as long
3291 * as at least one event is still subscribed. The <event_type> must only be a
3292 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3293 * the conn_stream <cs> was already detached, in which case it will return -1.
3294 */
3295static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003296{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003297 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003298 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003299
3300 if (!h1s)
3301 return -1;
3302
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003303 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003304 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003305
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003306 es->events |= event_type;
3307 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003308
3309 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003310 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003311
3312
Christopher Faulet6b81df72019-10-01 22:08:43 +02003313 if (event_type & SUB_RETRY_SEND) {
3314 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003315 /*
3316 * If the conn_stream attempt to subscribe, and the
3317 * mux isn't subscribed to the connection, then it
3318 * probably means the connection wasn't established
3319 * yet, so we have to subscribe.
3320 */
3321 h1c = h1s->h1c;
3322 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3323 h1c->conn->xprt->subscribe(h1c->conn,
3324 h1c->conn->xprt_ctx,
3325 SUB_RETRY_SEND,
3326 &h1c->wait_event);
3327 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003328 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003329}
3330
3331/* Called from the upper layer, to receive data */
3332static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3333{
3334 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003335 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003336 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003337 size_t ret = 0;
3338
Willy Tarreau022e5e52020-09-10 09:33:15 +02003339 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003340
3341 /* Do nothing for now if not READY */
3342 if (!(h1c->flags & H1C_F_ST_READY)) {
3343 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3344 goto end;
3345 }
3346
Christopher Faulet539e0292018-11-19 10:40:09 +01003347 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003348 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003349 else
3350 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003351
Christopher Faulete18777b2019-04-16 16:46:36 +02003352 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003353 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003354 h1c->flags |= H1C_F_WANT_SPLICE;
3355 TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003356 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003357 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003358 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003359 if (((flags & CO_RFL_KEEP_RECV) || (h1m->state != H1_MSG_DONE)) && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003360 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003361 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003362
3363 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003364 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003365 return ret;
3366}
3367
3368
3369/* Called from the upper layer, to send data */
3370static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3371{
3372 struct h1s *h1s = cs->ctx;
3373 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003374 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003375
3376 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003377 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003378 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003379
Willy Tarreau022e5e52020-09-10 09:33:15 +02003380 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003381
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003382 /* If we're not connected yet, or we're waiting for a handshake, stop
3383 * now, as we don't want to remove everything from the channel buffer
3384 * before we're sure we can send it.
3385 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003386 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003387 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003388 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003389 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003390
Christopher Fauletdea24742021-01-22 15:12:30 +01003391 if (h1c->flags & H1C_F_ST_ERROR) {
3392 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003393 TRACE_ERROR("H1C on error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01003394 return 0;
3395 }
3396
Christopher Faulet46230362019-10-17 16:04:20 +02003397 /* Inherit some flags from the upper layer */
3398 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3399 if (flags & CO_SFL_MSG_MORE)
3400 h1c->flags |= H1C_F_CO_MSG_MORE;
3401 if (flags & CO_SFL_STREAMER)
3402 h1c->flags |= H1C_F_CO_STREAMER;
3403
Christopher Fauletc31872f2019-06-04 22:09:36 +02003404 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003405 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003406
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003407 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3408 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003409 else
3410 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003411
3412 if ((count - ret) > 0)
3413 h1c->flags |= H1C_F_CO_MSG_MORE;
3414
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003415 if (!ret)
3416 break;
3417 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003418 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003419 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003420 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003421 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003422
3423 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletdea24742021-01-22 15:12:30 +01003424 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003425 TRACE_ERROR("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01003426 }
3427
Christopher Faulet7a145d62020-08-05 11:31:16 +02003428 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003429 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003430 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003431}
3432
Willy Tarreaue5733232019-05-22 19:24:06 +02003433#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003434/* Send and get, using splicing */
3435static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3436{
3437 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003438 struct h1c *h1c = h1s->h1c;
3439 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003440 int ret = 0;
3441
Willy Tarreau022e5e52020-09-10 09:33:15 +02003442 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003443
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003444 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003445 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003446 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003447 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003448 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003449 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003450 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003451 goto end;
3452 }
3453
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003454 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3455 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003456 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3457 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003458 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003459 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003460 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003461 }
3462
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003463 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003464 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3465 goto end;
3466 }
3467
Christopher Faulet1be55f92018-10-02 15:59:23 +02003468 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3469 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003470 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003471 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003472 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003473 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003474 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003475 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003476 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003477 }
3478
Christopher Faulet1be55f92018-10-02 15:59:23 +02003479 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003480 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003481 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003482 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003483 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003484 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003485
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003486 if ((h1s->flags & H1S_F_REOS) ||
3487 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003488 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003489 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003490 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003491 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003492
Willy Tarreau022e5e52020-09-10 09:33:15 +02003493 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003494 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003495}
3496
3497static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3498{
3499 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003500 int ret = 0;
3501
Willy Tarreau022e5e52020-09-10 09:33:15 +02003502 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003503
Christopher Faulet1be55f92018-10-02 15:59:23 +02003504 if (b_data(&h1s->h1c->obuf))
3505 goto end;
3506
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003507 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003508 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003509 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003510 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3511 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003512 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003513 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003514 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003515
Willy Tarreau022e5e52020-09-10 09:33:15 +02003516 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003517 return ret;
3518}
3519#endif
3520
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003521static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3522{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003523 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003524 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003525
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003526 switch (mux_ctl) {
3527 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003528 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003529 ret |= MUX_STATUS_READY;
3530 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003531 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003532 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3533 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003534 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3535 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3536 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003537 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003538 default:
3539 return -1;
3540 }
3541}
3542
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003543/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003544static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003545{
3546 struct h1c *h1c = conn->ctx;
3547 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003548 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003549
Christopher Fauletf376a312019-01-04 15:16:06 +01003550 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3551 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003552 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3553 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3554 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3555 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3556
3557 if (h1s) {
3558 char *method;
3559
3560 if (h1s->meth < HTTP_METH_OTHER)
3561 method = http_known_methods[h1s->meth].ptr;
3562 else
3563 method = "UNKNOWN";
3564 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3565 " .meth=%s status=%d",
3566 h1s, h1s->flags,
3567 h1m_state_str(h1s->req.state),
3568 h1m_state_str(h1s->res.state), method, h1s->status);
3569 if (h1s->cs)
3570 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3571 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003572
3573 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3574 if (h1s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01003575 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3576 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3577 h1s->subs->tasklet->calls,
3578 h1s->subs->tasklet->context);
3579 if (h1s->subs->tasklet->calls >= 1000000)
3580 ret = 1;
3581 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3582 chunk_appendf(&trash, ")");
Willy Tarreau150c4f82021-01-20 17:05:58 +01003583 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003584 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003585 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003586}
3587
3588
3589/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3590static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3591{
3592 struct h1_hdr_entry *entry;
3593
3594 /* Be sure there is a non-empty <to> */
3595 if (!strlen(to)) {
3596 memprintf(err, "expect <to>");
3597 return -1;
3598 }
3599
3600 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003601 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003602 memprintf(err, "<from> and <to> must not differ execpt the case");
3603 return -1;
3604 }
3605
3606 /* Be sure <from> does not already existsin the tree */
3607 if (ebis_lookup(&hdrs_map.map, from)) {
3608 memprintf(err, "duplicate entry '%s'", from);
3609 return -1;
3610 }
3611
3612 /* Create the entry and insert it in the tree */
3613 entry = malloc(sizeof(*entry));
3614 if (!entry) {
3615 memprintf(err, "out of memory");
3616 return -1;
3617 }
3618
3619 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003620 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003621 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003622 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003623 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003624 free(entry);
3625 memprintf(err, "out of memory");
3626 return -1;
3627 }
3628 ebis_insert(&hdrs_map.map, &entry->node);
3629 return 0;
3630}
3631
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003632/* Migrate the the connection to the current thread.
3633 * Return 0 if successful, non-zero otherwise.
3634 * Expected to be called with the old thread lock held.
3635 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003636static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003637{
3638 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003639 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003640
3641 if (fd_takeover(conn->handle.fd, conn) != 0)
3642 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003643
3644 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3645 /* We failed to takeover the xprt, even if the connection may
3646 * still be valid, flag it as error'd, as we have already
3647 * taken over the fd, and wake the tasklet, so that it will
3648 * destroy it.
3649 */
3650 conn->flags |= CO_FL_ERROR;
3651 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3652 return -1;
3653 }
3654
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003655 if (h1c->wait_event.events)
3656 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3657 h1c->wait_event.events, &h1c->wait_event);
3658 /* To let the tasklet know it should free itself, and do nothing else,
3659 * set its context to NULL.
3660 */
3661 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003662 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003663
3664 task = h1c->task;
3665 if (task) {
3666 task->context = NULL;
3667 h1c->task = NULL;
3668 __ha_barrier_store();
3669 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003670
3671 h1c->task = task_new(tid_bit);
3672 if (!h1c->task) {
3673 h1_release(h1c);
3674 return -1;
3675 }
3676 h1c->task->process = h1_timeout_task;
3677 h1c->task->context = h1c;
3678 }
3679 h1c->wait_event.tasklet = tasklet_new();
3680 if (!h1c->wait_event.tasklet) {
3681 h1_release(h1c);
3682 return -1;
3683 }
3684 h1c->wait_event.tasklet->process = h1_io_cb;
3685 h1c->wait_event.tasklet->context = h1c;
3686 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3687 SUB_RETRY_RECV, &h1c->wait_event);
3688
3689 return 0;
3690}
3691
3692
Christopher Faulet98fbe952019-07-22 16:18:24 +02003693static void h1_hdeaders_case_adjust_deinit()
3694{
3695 struct ebpt_node *node, *next;
3696 struct h1_hdr_entry *entry;
3697
3698 node = ebpt_first(&hdrs_map.map);
3699 while (node) {
3700 next = ebpt_next(node);
3701 ebpt_delete(node);
3702 entry = container_of(node, struct h1_hdr_entry, node);
3703 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003704 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003705 free(entry);
3706 node = next;
3707 }
3708 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003709}
3710
Christopher Faulet98fbe952019-07-22 16:18:24 +02003711static int cfg_h1_headers_case_adjust_postparser()
3712{
3713 FILE *file = NULL;
3714 char *c, *key_beg, *key_end, *value_beg, *value_end;
3715 char *err;
3716 int rc, line = 0, err_code = 0;
3717
3718 if (!hdrs_map.name)
3719 goto end;
3720
3721 file = fopen(hdrs_map.name, "r");
3722 if (!file) {
3723 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3724 hdrs_map.name);
3725 err_code |= ERR_ALERT | ERR_FATAL;
3726 goto end;
3727 }
3728
3729 /* now parse all lines. The file may contain only two header name per
3730 * line, separated by spaces. All heading and trailing spaces will be
3731 * ignored. Lines starting with a # are ignored.
3732 */
3733 while (fgets(trash.area, trash.size, file) != NULL) {
3734 line++;
3735 c = trash.area;
3736
3737 /* strip leading spaces and tabs */
3738 while (*c == ' ' || *c == '\t')
3739 c++;
3740
3741 /* ignore emptu lines, or lines beginning with a dash */
3742 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3743 continue;
3744
3745 /* look for the end of the key */
3746 key_beg = c;
3747 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3748 c++;
3749 key_end = c;
3750
3751 /* strip middle spaces and tabs */
3752 while (*c == ' ' || *c == '\t')
3753 c++;
3754
3755 /* look for the end of the value, it is the end of the line */
3756 value_beg = c;
3757 while (*c && *c != '\n' && *c != '\r')
3758 c++;
3759 value_end = c;
3760
3761 /* trim possibly trailing spaces and tabs */
3762 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3763 value_end--;
3764
3765 /* set final \0 and check entries */
3766 *key_end = '\0';
3767 *value_end = '\0';
3768
3769 err = NULL;
3770 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3771 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003772 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3773 hdrs_map.name, err, line);
3774 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003775 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003776 goto end;
3777 }
3778 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003779 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3780 hdrs_map.name, err, line);
3781 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003782 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003783 }
3784 }
3785
3786 end:
3787 if (file)
3788 fclose(file);
3789 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3790 return err_code;
3791}
3792
3793
3794/* config parser for global "h1-outgoing-header-case-adjust" */
3795static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01003796 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02003797 char **err)
3798{
3799 if (too_many_args(2, args, err, NULL))
3800 return -1;
3801 if (!*(args[1]) || !*(args[2])) {
3802 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3803 return -1;
3804 }
3805 return add_hdr_case_adjust(args[1], args[2], err);
3806}
3807
3808/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3809static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01003810 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02003811 char **err)
3812{
3813 if (too_many_args(1, args, err, NULL))
3814 return -1;
3815 if (!*(args[1])) {
3816 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3817 return -1;
3818 }
3819 free(hdrs_map.name);
3820 hdrs_map.name = strdup(args[1]);
3821 return 0;
3822}
3823
3824
3825/* config keyword parsers */
3826static struct cfg_kw_list cfg_kws = {{ }, {
3827 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3828 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3829 { 0, NULL, NULL },
3830 }
3831};
3832
3833INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3834REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3835
3836
Christopher Faulet51dbc942018-09-13 09:05:15 +02003837/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003838/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003839/****************************************/
3840
3841/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01003842static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003843 .init = h1_init,
3844 .wake = h1_wake,
3845 .attach = h1_attach,
3846 .get_first_cs = h1_get_first_cs,
3847 .detach = h1_detach,
3848 .destroy = h1_destroy,
3849 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003850 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003851 .rcv_buf = h1_rcv_buf,
3852 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003853#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003854 .rcv_pipe = h1_rcv_pipe,
3855 .snd_pipe = h1_snd_pipe,
3856#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003857 .subscribe = h1_subscribe,
3858 .unsubscribe = h1_unsubscribe,
3859 .shutr = h1_shutr,
3860 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003861 .show_fd = h1_show_fd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003862 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003863 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003864 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003865 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003866};
3867
Christopher Faulet3f612f72021-02-05 16:44:21 +01003868static const struct mux_ops mux_h1_ops = {
3869 .init = h1_init,
3870 .wake = h1_wake,
3871 .attach = h1_attach,
3872 .get_first_cs = h1_get_first_cs,
3873 .detach = h1_detach,
3874 .destroy = h1_destroy,
3875 .avail_streams = h1_avail_streams,
3876 .used_streams = h1_used_streams,
3877 .rcv_buf = h1_rcv_buf,
3878 .snd_buf = h1_snd_buf,
3879#if defined(USE_LINUX_SPLICE)
3880 .rcv_pipe = h1_rcv_pipe,
3881 .snd_pipe = h1_snd_pipe,
3882#endif
3883 .subscribe = h1_subscribe,
3884 .unsubscribe = h1_unsubscribe,
3885 .shutr = h1_shutr,
3886 .shutw = h1_shutw,
3887 .show_fd = h1_show_fd,
3888 .ctl = h1_ctl,
3889 .takeover = h1_takeover,
3890 .flags = MX_FL_HTX|MX_FL_NO_UPG,
3891 .name = "H1",
3892};
Christopher Faulet51dbc942018-09-13 09:05:15 +02003893
Christopher Faulet3f612f72021-02-05 16:44:21 +01003894/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
3895static struct mux_proto_list mux_proto_h1 =
3896 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
3897static struct mux_proto_list mux_proto_http =
3898 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003899
Christopher Faulet3f612f72021-02-05 16:44:21 +01003900INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
3901INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01003902
Christopher Faulet51dbc942018-09-13 09:05:15 +02003903/*
3904 * Local variables:
3905 * c-indent-level: 8
3906 * c-basic-offset: 8
3907 * End:
3908 */