blob: 3837f7a4389245524c601af26dabc8510b56ba53 [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
Christopher Fauletd1ac2b92020-12-02 19:12:22 +010084/* 0x00000200 unsued */
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
247static struct trace_source trace_h1 = {
248 .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" */
269struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreauac6322d2021-01-29 12:33:46 +0100270struct task *h1_timeout_task(struct task *t, void *context, unsigned short 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 Fauletb385b502021-01-13 18:47:57 +0100396 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
397 TRACE_DEVEL("recv not allowed (wait_output)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Willy Tarreauf5ea3a82020-07-31 09:16:23 +0200398 return 0;
399 }
400
Christopher Fauletd17ad822020-09-24 15:14:29 +0200401 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200402 return 1;
403
Christopher Faulet6b81df72019-10-01 22:08:43 +0200404 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 +0200405 return 0;
406}
407
408/*
409 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
410 * flags are used to figure what buffer was requested. It returns 1 if the
411 * allocation succeeds, in which case the connection is woken up, or 0 if it's
412 * impossible to wake up and we prefer to be woken up later.
413 */
414static int h1_buf_available(void *target)
415{
416 struct h1c *h1c = target;
417
418 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200419 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 +0200420 h1c->flags &= ~H1C_F_IN_ALLOC;
421 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200422 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200423 return 1;
424 }
425
426 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200427 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 +0200428 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200429 if (h1c->h1s)
430 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200431 return 1;
432 }
433
Christopher Fauletd17ad822020-09-24 15:14:29 +0200434 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) {
435 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
436 h1c->flags &= ~H1C_F_IN_SALLOC;
437 tasklet_wakeup(h1c->wait_event.tasklet);
438 return 1;
439 }
440
Christopher Faulet51dbc942018-09-13 09:05:15 +0200441 return 0;
442}
443
444/*
445 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
446 */
447static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
448{
449 struct buffer *buf = NULL;
450
Willy Tarreau21046592020-02-26 10:39:36 +0100451 if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) &&
Christopher Faulet51dbc942018-09-13 09:05:15 +0200452 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
453 h1c->buf_wait.target = h1c;
454 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreau86891272020-07-10 08:22:26 +0200455 MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200456 }
457 return buf;
458}
459
460/*
461 * Release a buffer, if any, and try to wake up entities waiting in the buffer
462 * wait queue.
463 */
464static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
465{
466 if (bptr->size) {
467 b_free(bptr);
468 offer_buffers(h1c->buf_wait.target, tasks_run_queue);
469 }
470}
471
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100472/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100473 * 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 +0100474 * not. This flag is only set when no H1S is attached and when the previous
475 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100476 */
477static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200478{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100479 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200480
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100481 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200482}
483
Willy Tarreau00f18a32019-01-26 12:19:01 +0100484/* returns the number of streams still available on a connection */
485static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100486{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100487 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100488}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200489
Christopher Faulet7a145d62020-08-05 11:31:16 +0200490/* Refresh the h1c task timeout if necessary */
491static void h1_refresh_timeout(struct h1c *h1c)
492{
493 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100494 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200495 /* half-closed or dead connections : switch to clientfin/serverfin
496 * timeouts so that we don't hang too long on clients that have
497 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200498 */
499 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200500 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
501 }
502 else if (b_data(&h1c->obuf)) {
503 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200504 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200505 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
506 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100507 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
508 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200509 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100510 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 +0200511 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200512 else {
513 /* alive back connections of front connections with a conn-stream attached */
514 h1c->task->expire = TICK_ETERNITY;
515 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
516 }
517
Christopher Fauletdbe57792020-10-05 17:50:58 +0200518 /* Finally set the idle expiration date if shorter */
519 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200520 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
521 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200522 }
523}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200524
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200525static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200526{
527 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
528 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
529 h1c->idle_exp = TICK_ETERNITY;
530 return;
531 }
532
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100533 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200534 if (!tick_isset(h1c->idle_exp)) {
535 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
536 !b_data(&h1c->ibuf) && /* No input data */
537 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
538 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
539 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
540 }
541 else {
542 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
543 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
544 }
545 }
546 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100547 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200548 if (!tick_isset(h1c->idle_exp)) {
549 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
550 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
551 }
552 }
553 else { // CS_ATTACHED or SHUTDOWN
554 h1c->idle_exp = TICK_ETERNITY;
555 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
556 }
557}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200558/*****************************************************************/
559/* functions below are dedicated to the mux setup and management */
560/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200561
562/* returns non-zero if there are input data pending for stream h1s. */
563static inline size_t h1s_data_pending(const struct h1s *h1s)
564{
565 const struct h1m *h1m;
566
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200567 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100568 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200569}
570
Christopher Faulet16df1782020-12-04 16:47:41 +0100571/* Creates a new conn-stream and the associate stream. <input> is used as input
572 * buffer for the stream. On success, it is transferred to the stream and the
573 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
574 * mux must still take care of it. However, there is nothing special to do
575 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
576 * b_free() on it is always safe. This function returns the conn-stream on
577 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200578static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100579{
580 struct conn_stream *cs;
581
Christopher Faulet6b81df72019-10-01 22:08:43 +0200582 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200583 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200584 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100585 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 +0100586 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200587 }
Christopher Faulet47365272018-10-31 17:40:50 +0100588 h1s->cs = cs;
589 cs->ctx = h1s;
590
591 if (h1s->flags & H1S_F_NOT_FIRST)
592 cs->flags |= CS_FL_NOT_FIRST;
593
Christopher Faulet27182292020-07-03 15:08:49 +0200594 if (global.tune.options & GTUNE_USE_SPLICE) {
595 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100596 cs->flags |= CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +0200597 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100598
Christopher Faulet26256f82020-09-14 11:40:13 +0200599 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200600 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 +0100601 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200602 }
603
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100604 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 +0200605 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100606 return cs;
607
608 err:
609 cs_free(cs);
610 h1s->cs = NULL;
Christopher Faulet26a26432021-01-27 11:27:50 +0100611 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100612 return NULL;
613}
614
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100615static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
616{
617 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
618
619 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100620 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 +0100621 goto err;
622 }
623
624 if (global.tune.options & GTUNE_USE_SPLICE) {
625 TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
626 h1s->cs->flags |= CS_FL_MAY_SPLICE;
627 }
628
629 h1s->h1c->flags |= H1C_F_ST_READY;
630 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
631 return h1s->cs;
632
633 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100634 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100635 return NULL;
636}
637
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200638static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200639{
640 struct h1s *h1s;
641
Christopher Faulet6b81df72019-10-01 22:08:43 +0200642 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
643
Christopher Faulet51dbc942018-09-13 09:05:15 +0200644 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100645 if (!h1s) {
646 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 +0100647 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100648 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200649 h1s->h1c = h1c;
650 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200651 h1s->sess = NULL;
652 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100653 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100654 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200655 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100656 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200657
658 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100659 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200660
Christopher Faulet129817b2018-09-20 16:14:40 +0200661 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100662 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200663
664 h1s->status = 0;
665 h1s->meth = HTTP_METH_OTHER;
666
Christopher Faulet47365272018-10-31 17:40:50 +0100667 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
668 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100669 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100670
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200671 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
672 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200673
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200674 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100675 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200676 return NULL;
677}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100678
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200679static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
680{
681 struct session *sess = h1c->conn->owner;
682 struct h1s *h1s;
683
684 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
685
686 h1s = h1s_new(h1c);
687 if (!h1s)
688 goto fail;
689
690 h1s->sess = sess;
691
692 if (h1c->px->options2 & PR_O2_REQBUG_OK)
693 h1s->req.err_pos = -1;
694
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200695 h1c->idle_exp = TICK_ETERNITY;
696 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200697 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200698 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100699
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200700 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100701 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200702 return NULL;
703}
704
705static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
706{
707 struct h1s *h1s;
708
709 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
710
711 h1s = h1s_new(h1c);
712 if (!h1s)
713 goto fail;
714
715 h1s->cs = cs;
716 h1s->sess = sess;
717 cs->ctx = h1s;
718
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100719 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200720
721 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
722 h1s->res.err_pos = -1;
723
724 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
725 return h1s;
726
727 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100728 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100729 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200730}
731
732static void h1s_destroy(struct h1s *h1s)
733{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200734 if (h1s) {
735 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200736
Christopher Faulet6b81df72019-10-01 22:08:43 +0200737 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200738 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200739
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100740 if (h1s->subs)
741 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200742
Christopher Fauletd17ad822020-09-24 15:14:29 +0200743 h1_release_buf(h1c, &h1s->rxbuf);
744
Christopher Fauletb385b502021-01-13 18:47:57 +0100745 h1c->flags &= ~(H1C_F_WAIT_INPUT|H1C_F_WAIT_OUTPUT|H1C_F_WANT_SPLICE|
746 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200747 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
748 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200749 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100750 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100751 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 +0200752 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100753
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100754 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100755 !(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 +0100756 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100757 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100758 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100759 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
760 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200761 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100762 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100763 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200764 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200765 pool_free(pool_head_h1s, h1s);
766 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200767}
768
769/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200770 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500771 * to the existing conn_stream (for outgoing connections or for incoming ones
772 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200773 * establishment). <input> is always used as Input buffer and may contain
774 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
775 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200776 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200777static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
778 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200779{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200780 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100781 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200782 void *conn_ctx = conn->ctx;
783
784 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200785
786 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100787 if (!h1c) {
788 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200789 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100790 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200791 h1c->conn = conn;
792 h1c->px = proxy;
793
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100794 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200795 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200796 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200797 h1c->obuf = BUF_NULL;
798 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200799 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200800
Willy Tarreau21046592020-02-26 10:39:36 +0100801 MT_LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200802 h1c->wait_event.tasklet = tasklet_new();
803 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200804 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200805 h1c->wait_event.tasklet->process = h1_io_cb;
806 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100807 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200808 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200809
Christopher Faulete9b70722019-04-08 10:46:02 +0200810 if (conn_is_back(conn)) {
Christopher Fauletb385b502021-01-13 18:47:57 +0100811 h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OUTPUT);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100812 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
813 if (tick_isset(proxy->timeout.serverfin))
814 h1c->shut_timeout = proxy->timeout.serverfin;
815 } else {
816 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
817 if (tick_isset(proxy->timeout.clientfin))
818 h1c->shut_timeout = proxy->timeout.clientfin;
819 }
820 if (tick_isset(h1c->timeout)) {
821 t = task_new(tid_bit);
Christopher Faulet26a26432021-01-27 11:27:50 +0100822 if (!t) {
823 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 +0100824 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100825 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100826
827 h1c->task = t;
828 t->process = h1_timeout_task;
829 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200830
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100831 t->expire = tick_add(now_ms, h1c->timeout);
832 }
833
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100834 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200835
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200836 if (h1c->flags & H1C_F_IS_BACK) {
837 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200838 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
839 goto fail;
840 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100841 else if (conn_ctx) {
842 /* Upgraded frontend connection (from TCP) */
843 struct conn_stream *cs = conn_ctx;
844
845 if (!h1c_frt_stream_new(h1c))
846 goto fail;
847
848 h1c->h1s->cs = cs;
849 cs->ctx = h1c->h1s;
850
851 /* Attach the CS but Not ready yet */
852 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
853 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
854 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
855 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100856
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200857 if (t) {
858 h1_set_idle_expiration(h1c);
859 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100860 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200861 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100862
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200863 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100864 if (b_data(&h1c->ibuf))
865 tasklet_wakeup(h1c->wait_event.tasklet);
866 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200867 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200868
869 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200870 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200871 return 0;
872
873 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200874 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200875 if (h1c->wait_event.tasklet)
876 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200877 pool_free(pool_head_h1c, h1c);
878 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200879 conn->ctx = conn_ctx; // restore saved context
880 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200881 return -1;
882}
883
Christopher Faulet73c12072019-04-08 11:23:22 +0200884/* release function. This one should be called to free all resources allocated
885 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200886 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200887static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200888{
Christopher Faulet61840e72019-04-15 09:33:32 +0200889 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200890
Christopher Faulet6b81df72019-10-01 22:08:43 +0200891 TRACE_POINT(H1_EV_H1C_END);
892
Christopher Faulet51dbc942018-09-13 09:05:15 +0200893 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200894 /* The connection must be aattached to this mux to be released */
895 if (h1c->conn && h1c->conn->ctx == h1c)
896 conn = h1c->conn;
897
Christopher Faulet6b81df72019-10-01 22:08:43 +0200898 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
899
Christopher Faulet61840e72019-04-15 09:33:32 +0200900 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200901 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200902 /* Make sure we're no longer subscribed to anything */
903 if (h1c->wait_event.events)
904 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
905 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200906 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200907 /* connection successfully upgraded to H2, this
908 * mux was already released */
909 return;
910 }
Christopher Faulet26a26432021-01-27 11:27:50 +0100911 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200912 sess_log(conn->owner); /* Log if the upgrade failed */
913 }
914
Olivier Houchard45c44372019-06-11 14:06:23 +0200915
Willy Tarreau21046592020-02-26 10:39:36 +0100916 if (MT_LIST_ADDED(&h1c->buf_wait.list))
917 MT_LIST_DEL(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200918
919 h1_release_buf(h1c, &h1c->ibuf);
920 h1_release_buf(h1c, &h1c->obuf);
921
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100922 if (h1c->task) {
923 h1c->task->context = NULL;
924 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
925 h1c->task = NULL;
926 }
927
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200928 if (h1c->wait_event.tasklet)
929 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200930
Christopher Fauletf2824e62018-10-01 12:12:37 +0200931 h1s_destroy(h1c->h1s);
Olivier Houchard0e079372019-04-15 17:51:16 +0200932 if (conn && h1c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100933 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200934 &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200935 pool_free(pool_head_h1c, h1c);
936 }
937
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200938 if (conn) {
939 conn->mux = NULL;
940 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200941 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200942
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200943 conn_stop_tracking(conn);
944 conn_full_close(conn);
945 if (conn->destroy_cb)
946 conn->destroy_cb(conn);
947 conn_free(conn);
948 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200949}
950
951/******************************************************/
952/* functions below are for the H1 protocol processing */
953/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +0200954/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
955 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +0200956 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100957static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +0200958{
Christopher Faulet570d1612018-11-26 11:13:57 +0100959 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200960
Christopher Faulet570d1612018-11-26 11:13:57 +0100961 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200962 (*(p + 5) > '1' ||
963 (*(p + 5) == '1' && *(p + 7) >= '1')))
964 h1m->flags |= H1_MF_VER_11;
965}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200966
Christopher Faulet9768c262018-10-22 09:34:31 +0200967/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
968 * greater or equal to 1.1
969 */
Christopher Faulet570d1612018-11-26 11:13:57 +0100970static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +0200971{
Christopher Faulet570d1612018-11-26 11:13:57 +0100972 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200973
Christopher Faulet570d1612018-11-26 11:13:57 +0100974 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +0200975 (*(p + 5) > '1' ||
976 (*(p + 5) == '1' && *(p + 7) >= '1')))
977 h1m->flags |= H1_MF_VER_11;
978}
Christopher Fauletf2824e62018-10-01 12:12:37 +0200979
Christopher Fauletf2824e62018-10-01 12:12:37 +0200980/* Deduce the connection mode of the client connection, depending on the
981 * configuration and the H1 message flags. This function is called twice, the
982 * first time when the request is parsed and the second time when the response
983 * is parsed.
984 */
985static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
986{
987 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200988
989 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +0100990 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +0100991 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +0100992 h1s->status == 101) {
993 /* Either we've established an explicit tunnel, or we're
994 * switching the protocol. In both cases, we're very unlikely to
995 * understand the next protocols. We have to switch to tunnel
996 * mode, so that we transfer the request and responses then let
997 * this protocol pass unmodified. When we later implement
998 * specific parsers for such protocols, we'll want to check the
999 * Upgrade header which contains information about that protocol
1000 * for responses with status 101 (eg: see RFC2817 about TLS).
1001 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001002 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001003 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 +01001004 }
1005 else if (h1s->flags & H1S_F_WANT_KAL) {
1006 /* By default the client is in KAL mode. CLOSE mode mean
1007 * it is imposed by the client itself. So only change
1008 * KAL mode here. */
1009 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1010 /* no length known or explicit close => close */
1011 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001012 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 +01001013 }
1014 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1015 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001016 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001017 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001018 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 +01001019 }
1020 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001021 }
1022 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001023 /* Input direction: first pass */
1024 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1025 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001026 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001027 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 +01001028 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001029 }
1030
1031 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001032 if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001033 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001034 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);
1035 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001036}
1037
1038/* Deduce the connection mode of the client connection, depending on the
1039 * configuration and the H1 message flags. This function is called twice, the
1040 * first time when the request is parsed and the second time when the response
1041 * is parsed.
1042 */
1043static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1044{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001045 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001046 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001047 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001048
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001050 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001051 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001052 h1s->status == 101) {
1053 /* Either we've established an explicit tunnel, or we're
1054 * switching the protocol. In both cases, we're very unlikely to
1055 * understand the next protocols. We have to switch to tunnel
1056 * mode, so that we transfer the request and responses then let
1057 * this protocol pass unmodified. When we later implement
1058 * specific parsers for such protocols, we'll want to check the
1059 * Upgrade header which contains information about that protocol
1060 * for responses with status 101 (eg: see RFC2817 about TLS).
1061 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001062 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001063 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 +01001064 }
1065 else if (h1s->flags & H1S_F_WANT_KAL) {
1066 /* By default the server is in KAL mode. CLOSE mode mean
1067 * it is imposed by haproxy itself. So only change KAL
1068 * mode here. */
1069 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1070 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1071 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1072 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001073 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 +01001074 }
1075 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001076 }
Christopher Faulet70033782018-12-05 13:50:11 +01001077 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001078 /* Output direction: first pass */
1079 if (h1m->flags & H1_MF_CONN_CLO) {
1080 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001081 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001082 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 +01001083 }
1084 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1085 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1086 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1087 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1088 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1089 /* no explicit keep-alive option httpclose/server-close => close */
1090 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001091 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 +01001092 }
Christopher Faulet70033782018-12-05 13:50:11 +01001093 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001094
1095 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001096 if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001097 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001098 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);
1099 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001100}
1101
Christopher Fauletb992af02019-03-28 15:42:24 +01001102static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001103{
1104 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001105
1106 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1107 * token is found
1108 */
1109 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001110 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001111
1112 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001113 if (!(h1m->flags & H1_MF_VER_11)) {
1114 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 +01001115 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001116 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 }
1118 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001119 if (h1m->flags & H1_MF_VER_11) {
1120 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001121 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001122 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001123 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001124}
1125
Christopher Fauletb992af02019-03-28 15:42:24 +01001126static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001127{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001128 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1129 * token is found
1130 */
1131 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001132 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001133
1134 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001135 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001136 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1137 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 +01001138 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001139 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001140 }
1141 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001142 if (h1m->flags & H1_MF_VER_11) {
1143 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001144 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001145 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001146 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001147}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001148
Christopher Fauletb992af02019-03-28 15:42:24 +01001149static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001150{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001151 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001152 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001153 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001154 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001155}
1156
Christopher Fauletb992af02019-03-28 15:42:24 +01001157static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1158{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001159 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001160 h1_set_cli_conn_mode(h1s, h1m);
1161 else
1162 h1_set_srv_conn_mode(h1s, h1m);
1163
1164 if (!(h1m->flags & H1_MF_RESP))
1165 h1_update_req_conn_value(h1s, h1m, conn_val);
1166 else
1167 h1_update_res_conn_value(h1s, h1m, conn_val);
1168}
Christopher Faulete44769b2018-11-29 23:01:45 +01001169
Christopher Faulet98fbe952019-07-22 16:18:24 +02001170/* Try to adjust the case of the message header name using the global map
1171 * <hdrs_map>.
1172 */
1173static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1174{
1175 struct ebpt_node *node;
1176 struct h1_hdr_entry *entry;
1177
1178 /* No entry in the map, do nothing */
1179 if (eb_is_empty(&hdrs_map.map))
1180 return;
1181
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001182 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001183 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1184 return;
1185
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001186 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001187 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1188 return;
1189
1190 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1191 if (!node)
1192 return;
1193 entry = container_of(node, struct h1_hdr_entry, node);
1194 name->ptr = entry->name.ptr;
1195 name->len = entry->name.len;
1196}
1197
Christopher Faulete44769b2018-11-29 23:01:45 +01001198/* Append the description of what is present in error snapshot <es> into <out>.
1199 * The description must be small enough to always fit in a buffer. The output
1200 * buffer may be the trash so the trash must not be used inside this function.
1201 */
1202static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1203{
1204 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001205 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1206 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1207 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1208 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1209 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1210 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001211}
1212/*
1213 * Capture a bad request or response and archive it in the proxy's structure.
1214 * By default it tries to report the error position as h1m->err_pos. However if
1215 * this one is not set, it will then report h1m->next, which is the last known
1216 * parsing point. The function is able to deal with wrapping buffers. It always
1217 * displays buffers as a contiguous area starting at buf->p. The direction is
1218 * determined thanks to the h1m's flags.
1219 */
1220static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1221 struct h1m *h1m, struct buffer *buf)
1222{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001223 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001224 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001225 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001226 union error_snapshot_ctx ctx;
1227
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001228 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001229 if (sess == NULL)
1230 sess = si_strm(h1s->cs->data)->sess;
1231 if (!(h1m->flags & H1_MF_RESP))
1232 other_end = si_strm(h1s->cs->data)->be;
1233 else
1234 other_end = sess->fe;
1235 } else
1236 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001237
1238 /* http-specific part now */
1239 ctx.h1.state = h1m->state;
1240 ctx.h1.c_flags = h1c->flags;
1241 ctx.h1.s_flags = h1s->flags;
1242 ctx.h1.m_flags = h1m->flags;
1243 ctx.h1.m_clen = h1m->curr_len;
1244 ctx.h1.m_blen = h1m->body_len;
1245
1246 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1247 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001248 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1249 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001250}
1251
Christopher Fauletadb22202018-12-12 10:32:09 +01001252/* Emit the chunksize followed by a CRLF in front of data of the buffer
1253 * <buf>. It goes backwards and starts with the byte before the buffer's
1254 * head. The caller is responsible for ensuring there is enough room left before
1255 * the buffer's head for the string.
1256 */
1257static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1258{
1259 char *beg, *end;
1260
1261 beg = end = b_head(buf);
1262 *--beg = '\n';
1263 *--beg = '\r';
1264 do {
1265 *--beg = hextab[chksz & 0xF];
1266 } while (chksz >>= 4);
1267 buf->head -= (end - beg);
1268 b_add(buf, end - beg);
1269}
1270
1271/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1272 * ensuring there is enough room left in the buffer for the string. */
1273static void h1_emit_chunk_crlf(struct buffer *buf)
1274{
1275 *(b_peek(buf, b_data(buf))) = '\r';
1276 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1277 b_add(buf, 2);
1278}
1279
Christopher Faulet129817b2018-09-20 16:14:40 +02001280/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001281 * Switch the stream to tunnel mode. This function must only be called on 2xx
1282 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001283 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001284static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001285{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001286 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001287
Christopher Faulet5be651d2021-01-22 15:28:03 +01001288 h1s->req.state = H1_MSG_TUNNEL;
1289 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001290
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001291 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001292 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001293
Christopher Faulet5be651d2021-01-22 15:28:03 +01001294 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 +01001295
Christopher Faulet5be651d2021-01-22 15:28:03 +01001296 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1297 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1298 if (b_data(&h1c->ibuf))
1299 h1_wake_stream_for_recv(h1s);
1300 tasklet_wakeup(h1c->wait_event.tasklet);
1301 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 +02001302 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01001303 if (h1c->flags & H1C_F_WAIT_INPUT) {
1304 h1c->flags &= ~H1C_F_WAIT_INPUT;
1305 h1_wake_stream_for_send(h1s);
1306 if (b_data(&h1c->obuf))
1307 tasklet_wakeup(h1c->wait_event.tasklet);
1308 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 +01001309 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001310}
1311
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001312/* Search for a websocket key header. The message should have been identified
1313 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001314 *
1315 * On the request side, if found the key is stored in the session. It might be
1316 * needed to calculate response key if the server side is using http/2.
1317 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001318 * On the response side, the key might be verified if haproxy has been
1319 * responsible for the generation of a key. This happens when a h2 client is
1320 * interfaced with a h1 server.
1321 *
1322 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001323 */
1324static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1325{
1326 struct htx_blk *blk;
1327 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001328 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001329 int ws_key_found = 0, idx;
1330
1331 idx = htx_get_head(htx); // returns the SL that we skip
1332 while ((idx = htx_get_next(htx, idx)) != -1) {
1333 blk = htx_get_blk(htx, idx);
1334 type = htx_get_blk_type(blk);
1335
1336 if (type == HTX_BLK_UNUSED)
1337 continue;
1338
1339 if (type != HTX_BLK_HDR)
1340 break;
1341
1342 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001343 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001344
Amaury Denoyellec1938232020-12-11 17:53:03 +01001345 /* Websocket key is base64 encoded of 16 bytes */
1346 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001347 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001348 /* Copy the key on request side
1349 * we might need it if the server is using h2 and does
1350 * not provide the response
1351 */
1352 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001353 ws_key_found = 1;
1354 break;
1355 }
1356 else if (isteqi(n, ist("sec-websocket-accept")) &&
1357 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001358 /* Need to verify the response key if the input was
1359 * generated by haproxy
1360 */
1361 if (h1s->ws_key[0]) {
1362 char key[29];
1363 h1_calculate_ws_output_key(h1s->ws_key, key);
1364 if (!isteqi(ist(key), v))
1365 break;
1366 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001367 ws_key_found = 1;
1368 break;
1369 }
1370 }
1371
1372 /* missing websocket key, reject the message */
1373 if (!ws_key_found) {
1374 htx->flags |= HTX_FL_PARSING_ERROR;
1375 return 0;
1376 }
1377
1378 return 1;
1379}
1380
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001381/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001382 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001383 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001384 * flag. If relies on the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001385 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001386static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
Christopher Fauletf2824e62018-10-01 12:12:37 +02001387 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001388{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001389 union h1_sl h1sl;
Christopher Faulet129817b2018-09-20 16:14:40 +02001390 int ret = 0;
1391
Willy Tarreau022e5e52020-09-10 09:33:15 +02001392 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 +02001393
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001394 if (h1s->meth == HTTP_METH_CONNECT)
1395 h1m->flags |= H1_MF_METH_CONNECT;
1396 if (h1s->meth == HTTP_METH_HEAD)
1397 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001398
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001399 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
1400 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001401 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 +02001402 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001403 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001404 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 +02001405 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1406 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001407 goto end;
1408 }
1409
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001410 /* If websocket handshake, search for the websocket key */
1411 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1412 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1413 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1414 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001415 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001416 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 +01001417 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1418
1419 ret = 0;
1420 goto end;
1421 }
1422 }
1423
Christopher Faulet486498c2019-10-11 14:22:00 +02001424 if (h1m->err_pos >= 0) {
1425 /* Maybe we found an error during the parsing while we were
1426 * configured not to block on that, so we have to capture it
1427 * now.
1428 */
1429 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1430 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1431 }
1432
Christopher Faulet5696f542020-12-02 16:08:38 +01001433 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001434 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001435 if (h1s->meth == HTTP_METH_HEAD)
1436 h1s->flags |= H1S_F_BODYLESS_RESP;
1437 }
1438 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001439 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001440 if (h1s->status == 204 || h1s->status == 304)
1441 h1s->flags |= H1S_F_BODYLESS_RESP;
1442 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001443 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001444 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001445
Christopher Faulet129817b2018-09-20 16:14:40 +02001446 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001447 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 +02001448 return ret;
1449}
1450
1451/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001452 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001453 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1454 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001455 */
Christopher Fauletaf542632019-10-01 21:52:49 +02001456static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001457 struct buffer *buf, size_t *ofs, size_t max,
Christopher Faulet30db3d72019-05-17 15:35:33 +02001458 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001459{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001460 int ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001461
Willy Tarreau022e5e52020-09-10 09:33:15 +02001462 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 +02001463 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001464 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001465 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 +01001466 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001467 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001468 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 +02001469 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001471 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001472 }
1473
Christopher Faulet02a02532019-11-15 09:36:28 +01001474 *ofs += ret;
1475
1476 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001477 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 +02001478 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001479}
1480
1481/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001482 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1483 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1484 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
1485 * responsible to update the parser state <h1m>.
1486 */
1487static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1488 struct buffer *buf, size_t *ofs, size_t max)
1489{
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001490 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001491
Willy Tarreau022e5e52020-09-10 09:33:15 +02001492 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 +02001493 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet02a02532019-11-15 09:36:28 +01001494 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001495 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 +01001496 if (htx->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001497 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001498 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 +02001499 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1500 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001501 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001502 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001503
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001504 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001505
1506 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001507 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 +02001508 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001509}
1510
1511/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001512 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001513 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1514 * it couldn't proceed.
Christopher Faulet129817b2018-09-20 16:14:40 +02001515 */
Christopher Faulet30db3d72019-05-17 15:35:33 +02001516static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001517{
Christopher Faulet539e0292018-11-19 10:40:09 +01001518 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001519 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001520 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001521 size_t data;
1522 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001523 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001524
Christopher Faulet539e0292018-11-19 10:40:09 +01001525 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001526 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001527
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001528 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001529 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001530
Christopher Faulet2eed8002020-12-07 11:26:13 +01001531 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001532 goto end;
1533
Christopher Faulet5be651d2021-01-22 15:28:03 +01001534 if (h1c->flags & H1C_F_WAIT_OUTPUT)
1535 goto end;
1536
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001537 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001538 size_t used = htx_used_space(htx);
1539
Christopher Faulet129817b2018-09-20 16:14:40 +02001540 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001541 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet539e0292018-11-19 10:40:09 +01001542 ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001543 if (!ret)
1544 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001545
1546 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1547 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1548
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001549 /* Reject Protocol upgrade request with payload */
1550 if ((h1m->flags & (H1_MF_RESP|H1_MF_CONN_UPG)) == H1_MF_CONN_UPG && h1m->state != H1_MSG_DONE) {
1551 h1s->flags |= H1S_F_NOT_IMPL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001552 TRACE_ERROR("Upgrade with body not implemented, reject H1 message",
1553 H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
Christopher Faulet1d2d77b2020-12-07 18:17:33 +01001554 break;
1555 }
1556
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001557 if ((h1m->flags & H1_MF_RESP) &&
1558 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1559 h1m_init_res(&h1s->res);
1560 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001561 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001562 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001563 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001564 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001565 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001566 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001567 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet129817b2018-09-20 16:14:40 +02001568 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001569
1570 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1571 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001572 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001573 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001574 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
1575 ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
1576 if (!ret && h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001577 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001578
Christopher Faulet76014fd2019-12-10 11:47:22 +01001579 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1580 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001581 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001582 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001583 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1584 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001585
Christopher Faulet5be651d2021-01-22 15:28:03 +01001586 if ((h1m->flags & H1_MF_RESP) &&
1587 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1588 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001589 else {
1590 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1591 /* Unfinished transaction: block this input side waiting the end of the output side */
1592 h1c->flags |= H1C_F_WAIT_OUTPUT;
1593 TRACE_STATE("Disable read on h1c (wait_output)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
1594 }
1595 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1596 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1597 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1598 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1599 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001600 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001601 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001602 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001603 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001604 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Fauletaf542632019-10-01 21:52:49 +02001605 ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001606 if (!ret)
1607 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001608
1609 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1610 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001611 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001612 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001613 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001614 break;
1615 }
1616
Christopher Faulet30db3d72019-05-17 15:35:33 +02001617 count -= htx_used_space(htx) - used;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001618 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) && !(h1c->flags & H1C_F_WAIT_OUTPUT));
1619
Christopher Faulet129817b2018-09-20 16:14:40 +02001620
Christopher Faulet2eed8002020-12-07 11:26:13 +01001621 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001622 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 +02001623 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001624 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001625
Christopher Faulet539e0292018-11-19 10:40:09 +01001626 b_del(&h1c->ibuf, total);
1627
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001628 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001629 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1630
Christopher Faulet30db3d72019-05-17 15:35:33 +02001631 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001632 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001633 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001634 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 +01001635 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001636 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001637
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001638 if (!b_data(&h1c->ibuf))
1639 h1_release_buf(h1c, &h1c->ibuf);
1640
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001641 if (!(h1c->flags & H1C_F_ST_READY)) {
1642 /* The H1 connection is not ready. Most of time, there is no CS
1643 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1644 * cases, it is only possible on the client side.
1645 */
1646 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1647
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001648 if (h1m->state <= H1_MSG_LAST_LF) {
1649 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1650 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1651 goto end;
1652 }
1653
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001654 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1655 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1656 BUG_ON(h1s->cs);
1657 if (!h1s_new_cs(h1s, buf)) {
1658 h1c->flags |= H1C_F_ST_ERROR;
1659 goto err;
1660 }
1661 }
1662 else {
1663 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1664 BUG_ON(h1s->cs == NULL);
1665 if (!h1s_upgrade_cs(h1s, buf)) {
1666 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001667 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001668 goto err;
1669 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001670 }
1671 }
1672
1673 /* Here h1s->cs is always defined */
Christopher Fauleta583af62020-09-24 15:35:37 +02001674 if (!(h1m->flags & H1_MF_CHNK) &&
1675 ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) {
1676 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1677 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1678 }
1679 else {
1680 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1681 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1682 }
1683
Christopher Faulet5be651d2021-01-22 15:28:03 +01001684 /* Don't set EOI on the conn-stream for protocol upgrade or connect
1685 * requests, wait the response to do so or not depending on the status
1686 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001687 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001688 if ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))
Christopher Fauleta583af62020-09-24 15:35:37 +02001689 h1s->cs->flags |= CS_FL_EOI;
1690
Christopher Faulet6716cc22019-12-20 17:33:24 +01001691 if (h1s_data_pending(h1s) && !htx_is_empty(htx))
Christopher Fauletcf56b992018-12-11 16:12:31 +01001692 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001693 else {
1694 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1695 if (h1s->flags & H1S_F_REOS) {
1696 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001697 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1698 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1699 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001700 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001701 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001702 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001703 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001704 TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1705 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001706
1707 if (h1s->h1c->flags & H1C_F_WAIT_INPUT) {
1708 h1s->h1c->flags &= ~H1C_F_WAIT_INPUT;
1709 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
1710 TRACE_STATE("Re-enable send on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1711 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001712 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001713 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001714
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001715 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001716 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001717 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001718
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001719 err:
Christopher Faulet47365272018-10-31 17:40:50 +01001720 b_reset(&h1c->ibuf);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001721 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001722 if (h1s->cs)
1723 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001724 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001725 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001726}
1727
Christopher Faulet129817b2018-09-20 16:14:40 +02001728/*
1729 * Process outgoing data. It parses data and transfer them from the channel buffer into
1730 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1731 * 0 if it couldn't proceed.
1732 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02001733static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count)
1734{
Christopher Faulet129817b2018-09-20 16:14:40 +02001735 struct h1s *h1s = h1c->h1s;
1736 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001737 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001738 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001739 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001740 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001741 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001742 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001743
Christopher Faulet94b2c762019-05-24 15:28:57 +02001744 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001745 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1746
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001747 if (htx_is_empty(chn_htx))
1748 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001749
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001750 if (h1s->flags & H1S_F_PROCESSING_ERROR)
1751 goto end;
1752
Christopher Fauletdea24742021-01-22 15:12:30 +01001753 if (h1c->flags & H1C_F_WAIT_INPUT)
1754 goto end;
1755
Christopher Faulet51dbc942018-09-13 09:05:15 +02001756 if (!h1_get_buf(h1c, &h1c->obuf)) {
1757 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001758 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 +02001759 goto end;
1760 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001761
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001762 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001763
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001764 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001765 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001766
Willy Tarreau3815b222018-12-11 19:50:43 +01001767 /* Perform some optimizations to reduce the number of buffer copies.
1768 * First, if the mux's buffer is empty and the htx area contains
1769 * exactly one data block of the same size as the requested count,
1770 * then it's possible to simply swap the caller's buffer with the
1771 * mux's output buffer and adjust offsets and length to match the
1772 * entire DATA HTX block in the middle. In this case we perform a
1773 * true zero-copy operation from end-to-end. This is the situation
1774 * that happens all the time with large files. Second, if this is not
1775 * possible, but the mux's output buffer is empty, we still have an
1776 * opportunity to avoid the copy to the intermediary buffer, by making
1777 * the intermediary buffer's area point to the output buffer's area.
1778 * In this case we want to skip the HTX header to make sure that copies
1779 * remain aligned and that this operation remains possible all the
1780 * time. This goes for headers, data blocks and any data extracted from
1781 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001782 */
1783 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001784 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
1785 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001786 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001787 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001788 void *old_area;
1789
1790 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
1791 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
1792 goto skip_zero_copy;
1793 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001794
Christopher Faulet6b81df72019-10-01 22:08:43 +02001795 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 +01001796 if (h1m->state == H1_MSG_DATA && chn_htx->flags & HTX_FL_EOM) {
1797 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1798 last_data = 1;
1799 }
1800
Christopher Faulete5596bf2020-12-02 16:13:22 +01001801 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001802 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001803 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001804 h1c->obuf.data = count;
1805
1806 buf->area = old_area;
1807 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001808
Christopher Faulet6b81df72019-10-01 22:08:43 +02001809 chn_htx = (struct htx *)buf->area;
1810 htx_reset(chn_htx);
1811
Christopher Fauletadb22202018-12-12 10:32:09 +01001812 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001813 * size and eventually the last chunk. We have at least
1814 * the size of the struct htx to write the chunk
1815 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001816 */
1817 if (h1m->flags & H1_MF_CHNK) {
1818 h1_emit_chunk_size(&h1c->obuf, count);
1819 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001820 if (last_data) {
1821 /* Emit the last chunk too at the buffer's end */
1822 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1823 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001824 }
1825
Christopher Faulet6b81df72019-10-01 22:08:43 +02001826 if (h1m->state == H1_MSG_DATA)
1827 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001828 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001829 else
1830 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001831 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001832
Christopher Faulete5596bf2020-12-02 16:13:22 +01001833 skip_zero_copy:
1834 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001835 if (last_data) {
1836 h1m->state = H1_MSG_DONE;
1837 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
1838 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
1839 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1840 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1841 }
1842
1843 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1844 H1_EV_TX_DATA, h1c->conn, h1s);
1845 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001846 goto out;
1847 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001848 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001849 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001850 else
1851 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02001852
Christopher Fauletc2518a52019-06-25 21:41:02 +02001853 tmp.data = 0;
1854 tmp.size = b_room(&h1c->obuf);
Christopher Fauletdea24742021-01-22 15:12:30 +01001855 while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && !(h1c->flags & H1C_F_WAIT_INPUT) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01001856 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02001857 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01001858 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02001859 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02001860 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02001861
Christopher Fauletb2e84162018-12-06 11:39:49 +01001862 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02001863 if (type != HTX_BLK_DATA && vlen > count)
1864 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001865
Christopher Faulet94b2c762019-05-24 15:28:57 +02001866 if (type == HTX_BLK_UNUSED)
1867 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02001868
Christopher Faulet94b2c762019-05-24 15:28:57 +02001869 switch (h1m->state) {
1870 case H1_MSG_RQBEFORE:
1871 if (type != HTX_BLK_REQ_SL)
1872 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001873 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 +02001874 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001875 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02001876 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001877 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001878 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001879 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001880 if (sl->flags & HTX_SL_F_BODYLESS)
1881 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02001882 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01001883 if (h1s->meth == HTTP_METH_HEAD)
1884 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Fauletb385b502021-01-13 18:47:57 +01001885 if (h1c->flags & H1C_F_WAIT_OUTPUT) {
1886 h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Christopher Faulet089acd52020-09-21 10:57:52 +02001887 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1888 TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1889 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001890 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02001891
Christopher Faulet94b2c762019-05-24 15:28:57 +02001892 case H1_MSG_RPBEFORE:
1893 if (type != HTX_BLK_RES_SL)
1894 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001895 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 +02001896 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01001897 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02001898 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001899 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001900 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01001901 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001902 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01001903 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02001904 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01001905 else if (h1s->status == 204 || h1s->status == 304)
1906 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02001907 h1m->state = H1_MSG_HDR_FIRST;
1908 break;
1909
Christopher Faulet94b2c762019-05-24 15:28:57 +02001910 case H1_MSG_HDR_FIRST:
1911 case H1_MSG_HDR_NAME:
1912 case H1_MSG_HDR_L2_LWS:
1913 if (type == HTX_BLK_EOH)
1914 goto last_lf;
1915 if (type != HTX_BLK_HDR)
1916 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001917
Christopher Faulet9768c262018-10-22 09:34:31 +02001918 h1m->state = H1_MSG_HDR_NAME;
1919 n = htx_get_blk_name(chn_htx, blk);
1920 v = htx_get_blk_value(chn_htx, blk);
1921
Christopher Faulet86d144c2019-08-14 16:32:25 +02001922 /* Skip all pseudo-headers */
1923 if (*(n.ptr) == ':')
1924 goto skip_hdr;
1925
Christopher Faulet91fcf212020-12-02 16:17:15 +01001926 if (isteq(n, ist("transfer-encoding"))) {
1927 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1928 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02001929 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01001930 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001931 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01001932 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
1933 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01001934 /* Only skip C-L header with invalid value. */
1935 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01001936 goto skip_hdr;
1937 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01001938 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001939 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02001940 if (!v.len)
1941 goto skip_hdr;
1942 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01001943 else if (isteq(n, ist("upgrade"))) {
1944 h1_parse_upgrade_header(h1m, v);
1945 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001946 else if ((isteq(n, ist("sec-websocket-accept")) &&
1947 h1m->flags & H1_MF_RESP) ||
1948 (isteq(n, ist("sec-websocket-key")) &&
1949 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001950 ws_key_found = 1;
1951 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001952
Christopher Faulet67d58092019-10-02 10:51:38 +02001953 /* Skip header if same name is used to add the server name */
1954 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
1955 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
1956 goto skip_hdr;
1957
Christopher Faulet98fbe952019-07-22 16:18:24 +02001958 /* Try to adjust the case of the header name */
1959 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1960 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001961 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001962 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02001963 skip_hdr:
1964 h1m->state = H1_MSG_HDR_L2_LWS;
1965 break;
1966
Christopher Faulet94b2c762019-05-24 15:28:57 +02001967 case H1_MSG_LAST_LF:
1968 if (type != HTX_BLK_EOH)
1969 goto error;
1970 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02001971 h1m->state = H1_MSG_LAST_LF;
1972 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02001973 /* If the reply comes from haproxy while the request is
1974 * not finished, we force the connection close. */
1975 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
1976 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
1977 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
1978 }
1979
1980 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02001981 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001982 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01001983 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001984 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02001985 /* Try to adjust the case of the header name */
1986 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
1987 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02001988 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02001989 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001990 }
Christopher Fauletada34b62019-05-24 16:36:43 +02001991 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01001992 }
Willy Tarreau4710d202019-01-03 17:39:54 +01001993
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001994 if ((h1s->meth != HTTP_METH_CONNECT &&
1995 (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 +01001996 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01001997 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01001998 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01001999 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2000 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002001 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002002 n = ist("transfer-encoding");
2003 v = ist("chunked");
2004 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2005 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002006 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002007 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002008 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002009 h1m->flags |= H1_MF_CHNK;
2010 }
2011
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002012 /* Now add the server name to a header (if requested) */
2013 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
2014 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
2015 struct server *srv = objt_server(h1c->conn->target);
2016
2017 if (srv) {
2018 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
2019 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002020
2021 /* Try to adjust the case of the header name */
2022 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2023 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002024 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002025 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002026 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002027 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002028 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2029 }
2030
Amaury Denoyellec1938232020-12-11 17:53:03 +01002031 /* Add websocket handshake key if needed */
2032 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2033 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002034 if (!(h1m->flags & H1_MF_RESP)) {
2035 /* generate a random websocket key
2036 * stored in the session to
2037 * verify it on the response side
2038 */
2039 h1_generate_random_ws_input_key(h1s->ws_key);
2040
2041 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2042 ist(h1s->ws_key),
2043 &tmp)) {
2044 goto full;
2045 }
2046 }
2047 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002048 /* add the response header key */
2049 char key[29];
2050 h1_calculate_ws_output_key(h1s->ws_key, key);
2051 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2052 ist(key),
2053 &tmp)) {
2054 goto full;
2055 }
2056 }
2057 }
2058
Christopher Faulet6b81df72019-10-01 22:08:43 +02002059 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2060 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2061
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002062 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002063 if (!chunk_memcat(&tmp, "\r\n", 2))
2064 goto full;
2065 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002066 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002067 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002068 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002069 if (!chunk_memcat(&tmp, "\r\n", 2))
2070 goto full;
2071 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002072 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002073 else if ((h1m->flags & H1_MF_RESP) &&
2074 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002075 if (!chunk_memcat(&tmp, "\r\n", 2))
2076 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002077 h1m_init_res(&h1s->res);
2078 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002079 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002080 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002081 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002082 else {
2083 /* EOM flag is set and it is the last block */
2084 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2085 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2086 goto full;
2087 else if (!chunk_memcat(&tmp, "\r\n", 2))
2088 goto full;
2089 goto done;
2090 }
2091 else if (!chunk_memcat(&tmp, "\r\n", 2))
2092 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002093 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002094 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002095 break;
2096
Christopher Faulet94b2c762019-05-24 15:28:57 +02002097 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002098 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002099 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002100 /* If the message is not chunked, never
2101 * add the last chunk. */
2102 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002103 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002104 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 +02002105 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002106 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002107 else if (type != HTX_BLK_DATA)
2108 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002109
Christopher Faulete5596bf2020-12-02 16:13:22 +01002110 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2111 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2112 break;
2113 }
2114
Christopher Faulet6b81df72019-10-01 22:08:43 +02002115 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 +02002116
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002117 /* It is the last block of this message. After this one,
2118 * only tunneled data may be forwarded. */
2119 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2120 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2121 last_data = 1;
2122 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002123
2124 if (vlen > count) {
2125 /* Get the maximum amount of data we can xferred */
2126 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002127 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002128 }
2129
2130 chklen = 0;
2131 if (h1m->flags & H1_MF_CHNK) {
2132 chklen = b_room(&tmp);
2133 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2134 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2135 (chklen < 1048576) ? 5 : 8);
2136 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002137
2138 /* If it is the end of the chunked message (without EOT), reserve the
2139 * last chunk size */
2140 if (last_data)
2141 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002142 }
2143
2144 if (vlen + chklen > b_room(&tmp)) {
2145 /* too large for the buffer */
2146 if (chklen >= b_room(&tmp))
2147 goto full;
2148 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002149 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002150 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002151 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002152 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002153 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002154 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002155
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002156 /* Space already reserved, so it must succeed */
2157 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2158 goto error;
2159
Christopher Faulet6b81df72019-10-01 22:08:43 +02002160 if (h1m->state == H1_MSG_DATA)
2161 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002162 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002163 else
2164 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002165 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002166 if (last_data)
2167 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 break;
2169
Christopher Faulet94b2c762019-05-24 15:28:57 +02002170 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002171 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002172 goto error;
2173 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002175
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002176 /* If the message is not chunked, ignore
2177 * trailers. It may happen with H2 messages. */
2178 if (!(h1m->flags & H1_MF_CHNK))
2179 break;
2180
Christopher Faulete5596bf2020-12-02 16:13:22 +01002181 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2182 TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2183 break;
2184 }
2185
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002186 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002187 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002188 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002189 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2190 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002191 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002192 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002193 else { // HTX_BLK_TLR
2194 n = htx_get_blk_name(chn_htx, blk);
2195 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002196
2197 /* Try to adjust the case of the header name */
2198 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2199 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002200 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002201 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002202 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002203 break;
2204
Christopher Faulet94b2c762019-05-24 15:28:57 +02002205 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002206 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2207 goto error; /* For now return an error */
2208
Christopher Faulet94b2c762019-05-24 15:28:57 +02002209 done:
2210 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002211 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
2212 h1c->flags |= H1C_F_WAIT_INPUT;
2213 TRACE_STATE("Disable send on h1c (wait_input)", H1_EV_TX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s);
2214 }
2215 else if ((h1m->flags & H1_MF_RESP) &&
2216 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2217 /* a successful reply to a CONNECT or a protocol switching is sent
2218 * to the client. Switch the response to tunnel mode.
2219 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002220 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002221 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 +01002222 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002223
2224 if (h1s->h1c->flags & H1C_F_WAIT_OUTPUT) {
Christopher Fauletb385b502021-01-13 18:47:57 +01002225 h1s->h1c->flags &= ~H1C_F_WAIT_OUTPUT;
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01002226 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet089acd52020-09-21 10:57:52 +02002227 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 +02002228 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002229
2230 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2231 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002232 break;
2233
Christopher Faulet9768c262018-10-22 09:34:31 +02002234 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002235 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002236 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002237 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002238 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002239 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002240 TRACE_ERROR("processing output error, set error on h1c/h1s",
2241 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 +02002242 break;
2243 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002244
2245 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002246 total += vlen;
2247 count -= vlen;
2248 if (sz == vlen)
2249 blk = htx_remove_blk(chn_htx, blk);
2250 else {
2251 htx_cut_data_blk(chn_htx, blk, vlen);
2252 break;
2253 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002254 }
2255
Christopher Faulet9768c262018-10-22 09:34:31 +02002256 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002257 /* when the output buffer is empty, tmp shares the same area so that we
2258 * only have to update pointers and lengths.
2259 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002260 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2261 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002262 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002263 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002264
Willy Tarreau3815b222018-12-11 19:50:43 +01002265 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002266 out:
2267 if (!buf_room_for_htx_data(&h1c->obuf)) {
2268 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002269 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002270 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002271 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002272 /* Both the request and the response reached the DONE state. So set EOI
2273 * flag on the conn-stream. Most of time, the flag will already be set,
2274 * except for protocol upgrades. Report an error if data remains blocked
2275 * in the output buffer.
2276 */
2277 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2278 if (!htx_is_empty(chn_htx)) {
2279 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002280 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 +01002281 }
2282 h1s->cs->flags |= CS_FL_EOI;
2283 }
2284
Christopher Faulet6b81df72019-10-01 22:08:43 +02002285 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002286 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002287
2288 full:
2289 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2290 h1c->flags |= H1C_F_OUT_FULL;
2291 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002292}
2293
Christopher Faulet51dbc942018-09-13 09:05:15 +02002294/*********************************************************/
2295/* functions below are I/O callbacks from the connection */
2296/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002297static void h1_wake_stream_for_recv(struct h1s *h1s)
2298{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002299 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002300 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002301 tasklet_wakeup(h1s->subs->tasklet);
2302 h1s->subs->events &= ~SUB_RETRY_RECV;
2303 if (!h1s->subs->events)
2304 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002305 }
2306}
2307static void h1_wake_stream_for_send(struct h1s *h1s)
2308{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002309 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002310 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002311 tasklet_wakeup(h1s->subs->tasklet);
2312 h1s->subs->events &= ~SUB_RETRY_SEND;
2313 if (!h1s->subs->events)
2314 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002315 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002316}
2317
Christopher Fauletad4daf62021-01-21 17:49:01 +01002318/* alerts the data layer following this sequence :
2319 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2320 * - if its subscribed to send, then it's woken up for send
2321 * - if it was subscribed to neither, its ->wake() callback is called
2322 */
2323static void h1_alert(struct h1s *h1s)
2324{
2325 if (h1s->subs) {
2326 h1_wake_stream_for_recv(h1s);
2327 h1_wake_stream_for_send(h1s);
2328 }
2329 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2330 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2331 h1s->cs->data_cb->wake(h1s->cs);
2332 }
2333}
2334
Christopher Fauletc18fc232020-10-06 17:41:36 +02002335/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2336 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2337 * retryable errors (allocation error or buffer full). On success, the error is
2338 * copied in the output buffer.
2339*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002340static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002341{
2342 int rc = http_get_status_idx(h1c->errcode);
2343 int ret = 0;
2344
2345 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2346
2347 /* Verify if the error is mapped on /dev/null or any empty file */
2348 /// XXX: do a function !
2349 if (h1c->px->replies[rc] &&
2350 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2351 h1c->px->replies[rc]->body.errmsg &&
2352 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2353 /* Empty error, so claim a success */
2354 ret = 1;
2355 goto out;
2356 }
2357
2358 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2359 h1c->flags |= H1C_F_ERR_PENDING;
2360 goto out;
2361 }
2362
2363 if (!h1_get_buf(h1c, &h1c->obuf)) {
2364 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2365 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2366 goto out;
2367 }
2368 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2369 if (unlikely(ret <= 0)) {
2370 if (!ret) {
2371 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2372 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2373 goto out;
2374 }
2375 else {
2376 /* we cannot report this error, so claim a success */
2377 ret = 1;
2378 }
2379 }
2380 h1c->flags &= ~H1C_F_ERR_PENDING;
2381 out:
2382 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2383 return ret;
2384}
2385
2386/* Try to send a 500 internal error. It relies on h1_send_error to send the
2387 * error. This function takes care of incrementing stats and tracked counters.
2388 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002389static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002390{
2391 struct session *sess = h1c->conn->owner;
2392 int ret = 1;
2393
2394 session_inc_http_req_ctr(sess);
2395 session_inc_http_err_ctr(sess);
2396 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2397 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[5], 1);
2398 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
2399 if (sess->listener->counters)
2400 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
2401
2402 h1c->errcode = 500;
2403 ret = h1_send_error(h1c);
2404 sess_log(sess);
2405 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002406}
2407
Christopher Fauletc18fc232020-10-06 17:41:36 +02002408/* Try to send a 400 bad request error. It relies on h1_send_error to send the
2409 * error. This function takes care of incrementing stats and tracked counters.
2410 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002411static int h1_handle_bad_req(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002412{
2413 struct session *sess = h1c->conn->owner;
2414 int ret = 1;
2415
2416 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2417 goto end;
2418
2419 session_inc_http_req_ctr(sess);
2420 session_inc_http_err_ctr(sess);
2421 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2422 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2423 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2424 if (sess->listener->counters)
2425 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2426
2427 h1c->errcode = 400;
2428 ret = h1_send_error(h1c);
2429 sess_log(sess);
2430
2431 end:
2432 return ret;
2433}
2434
Christopher Faulet2eed8002020-12-07 11:26:13 +01002435/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2436 * the error. This function takes care of incrementing stats and tracked
2437 * counters.
2438 */
2439static int h1_handle_not_impl_err(struct h1c *h1c)
2440{
2441 struct session *sess = h1c->conn->owner;
2442 int ret = 1;
2443
2444 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2445 goto end;
2446
2447 session_inc_http_req_ctr(sess);
2448 session_inc_http_err_ctr(sess);
2449 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2450 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2451 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2452 if (sess->listener->counters)
2453 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2454
2455 h1c->errcode = 501;
2456 ret = h1_send_error(h1c);
2457 sess_log(sess);
2458
2459 end:
2460 return ret;
2461}
2462
Christopher Fauletc18fc232020-10-06 17:41:36 +02002463/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2464 * error. This function takes care of incrementing stats and tracked counters.
2465 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002466static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002467{
2468 struct session *sess = h1c->conn->owner;
2469 int ret = 1;
2470
2471 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2472 goto end;
2473
2474 session_inc_http_req_ctr(sess);
2475 session_inc_http_err_ctr(sess);
2476 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
2477 _HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[4], 1);
2478 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
2479 if (sess->listener->counters)
2480 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
2481
2482 h1c->errcode = 408;
2483 ret = h1_send_error(h1c);
2484 sess_log(sess);
2485 end:
2486 return ret;
2487}
2488
2489
Christopher Faulet51dbc942018-09-13 09:05:15 +02002490/*
2491 * Attempt to read data, and subscribe if none available
2492 */
2493static int h1_recv(struct h1c *h1c)
2494{
2495 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002496 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002497 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002498
Christopher Faulet6b81df72019-10-01 22:08:43 +02002499 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2500
2501 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2502 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002503 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002505
Christopher Fauletae635762020-09-21 11:47:16 +02002506 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2507 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002508 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002509 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002510
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002511 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2512 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002513 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002514 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002515 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002516
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002517 /*
2518 * If we only have a small amount of data, realign it,
2519 * it's probably cheaper than doing 2 recv() calls.
2520 */
2521 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
2522 b_slow_realign(&h1c->ibuf, trash.area, 0);
2523
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002524 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002525 if (!h1c->h1s ||
2526 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2527 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002528 flags |= CO_RFL_READ_ONCE;
2529
Willy Tarreau45f2b892018-12-05 07:59:27 +01002530 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002531 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002532 if (h1c->flags & H1C_F_IN_FULL) {
2533 h1c->flags &= ~H1C_F_IN_FULL;
2534 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2535 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002536
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002537 b_realign_if_empty(&h1c->ibuf);
Willy Tarreau78f548f2018-12-05 10:02:39 +01002538 if (!b_data(&h1c->ibuf)) {
2539 /* try to pre-align the buffer like the rxbufs will be
2540 * to optimize memory copies.
2541 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002542 h1c->ibuf.head = sizeof(struct htx);
2543 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002544 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002545 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002546 if (max && !ret && h1_recv_allowed(h1c)) {
2547 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2548 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002549 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002550 else {
2551 h1_wake_stream_for_recv(h1c->h1s);
2552 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002553 }
2554
Christopher Faulet51dbc942018-09-13 09:05:15 +02002555 if (!b_data(&h1c->ibuf))
2556 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002557 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002558 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002559 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2560 }
2561
2562 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002563 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002564}
2565
2566
2567/*
2568 * Try to send data if possible
2569 */
2570static int h1_send(struct h1c *h1c)
2571{
2572 struct connection *conn = h1c->conn;
2573 unsigned int flags = 0;
2574 size_t ret;
2575 int sent = 0;
2576
Christopher Faulet6b81df72019-10-01 22:08:43 +02002577 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2578
2579 if (conn->flags & CO_FL_ERROR) {
2580 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002581 b_reset(&h1c->obuf);
2582 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002583 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002584
2585 if (!b_data(&h1c->obuf))
2586 goto end;
2587
Christopher Faulet46230362019-10-17 16:04:20 +02002588 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002589 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002590 if (h1c->flags & H1C_F_CO_STREAMER)
2591 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002592
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002593 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002594 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002595 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002596 if (h1c->flags & H1C_F_OUT_FULL) {
2597 h1c->flags &= ~H1C_F_OUT_FULL;
2598 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2599 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002600 b_del(&h1c->obuf, ret);
2601 sent = 1;
2602 }
2603
Christopher Faulet145aa472018-12-06 10:56:20 +01002604 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002605 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002606 /* error or output closed, nothing to send, clear the buffer to release it */
2607 b_reset(&h1c->obuf);
2608 }
2609
Christopher Faulet51dbc942018-09-13 09:05:15 +02002610 end:
Christopher Fauletb385b502021-01-13 18:47:57 +01002611 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_WAIT_INPUT)))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002612 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002613
Christopher Faulet51dbc942018-09-13 09:05:15 +02002614 /* We're done, no more to send */
2615 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002616 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002617 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002618 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002619 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01002620 h1_shutw_conn(conn, CS_SHW_NORMAL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002621 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002622 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002623 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2624 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002625 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002626 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002627
Christopher Faulet6b81df72019-10-01 22:08:43 +02002628 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002629 return sent;
2630}
2631
Christopher Faulet51dbc942018-09-13 09:05:15 +02002632/* callback called on any event by the connection handler.
2633 * It applies changes and returns zero, or < 0 if it wants immediate
2634 * destruction of the connection.
2635 */
2636static int h1_process(struct h1c * h1c)
2637{
2638 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002639 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002640
Christopher Faulet6b81df72019-10-01 22:08:43 +02002641 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2642
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002643 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002644 if (b_data(&h1c->ibuf) && /* Input data to be processed */
2645 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2646 !(h1c->flags & H1C_F_IN_SALLOC)) { /* No allocation failure on the stream rxbuf */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002647 struct buffer *buf;
2648 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002649
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002650 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2651 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002652 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002653
2654 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002655 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
2656 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE)) { /* H2 upgrade supported by the proxy */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002657 /* Try to match H2 preface before parsing the request headers. */
2658 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2659 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002660 if (h1c->flags & H1C_F_ST_ATTACHED) {
2661 /* Force the REOS here to be sure to release the CS.
2662 Here ATTACHED implies !READY, and h1s defined
2663 */
2664 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2665 h1s->flags |= H1S_F_REOS;
2666 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002667 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002668 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002669 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002670 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002671
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002672 /* Create the H1 stream if not already there */
2673 if (!h1s) {
2674 h1s = h1c_frt_stream_new(h1c);
2675 if (!h1s) {
2676 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002677 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002678 goto no_parsing;
2679 }
2680 }
2681
2682 if (h1s->sess->t_idle == -1)
2683 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2684
2685 /* Get the stream rxbuf */
2686 buf = h1_get_buf(h1c, &h1s->rxbuf);
2687 if (!buf) {
2688 h1c->flags |= H1C_F_IN_SALLOC;
2689 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2690 return 0;
2691 }
2692
2693 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
2694 h1_process_input(h1c, buf, count);
2695 h1_release_buf(h1c, &h1s->rxbuf);
2696 h1_set_idle_expiration(h1c);
2697
2698 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002699 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002700 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002701 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002702 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002703 }
2704 else if (h1s->flags & H1S_F_PARSING_ERROR) {
2705 h1_handle_bad_req(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002706 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002707 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002708 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002709 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2710 h1_handle_not_impl_err(h1c);
2711 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002712 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002713 }
Christopher Fauletae635762020-09-21 11:47:16 +02002714 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002715 h1_send(h1c);
2716
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002717 if ((conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn) || (h1c->flags & H1C_F_ST_ERROR)) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002718 if (!(h1c->flags & H1C_F_ST_READY)) {
2719 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002720 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002721 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR))) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002722 if (h1_handle_bad_req(h1c))
2723 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002724 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002725 }
2726
2727 /* Handle pending error, if any (only possible on frontend connection) */
2728 if (h1c->flags & H1C_F_ERR_PENDING) {
2729 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2730 if (h1_send_error(h1c))
2731 h1_send(h1c);
2732 }
Christopher Fauletae635762020-09-21 11:47:16 +02002733
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002734 /* If there is some pending outgoing data or error, just wait */
2735 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2736 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002737
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002738 /* Otherwise we can release the H1 connection */
2739 goto release;
2740 }
2741 else {
2742 /* Here there is still a H1 stream with a conn-stream.
2743 * Report the connection state at the stream level
2744 */
2745 if (conn_xprt_read0_pending(conn)) {
2746 h1s->flags |= H1S_F_REOS;
2747 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2748 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002749 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002750 h1s->cs->flags |= CS_FL_ERROR;
2751 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002752 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002753 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002754 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002755
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002756 if (!b_data(&h1c->ibuf))
2757 h1_release_buf(h1c, &h1c->ibuf);
2758
2759
2760 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2761 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2762 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002763 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002764
Christopher Faulet47365272018-10-31 17:40:50 +01002765 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002766 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002767 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002768 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002769
2770 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002771 if (h1c->flags & H1C_F_ST_ATTACHED) {
2772 /* Don't release the H1 connetion right now, we must destroy the
2773 * attached CS first. Here, the H1C must not be READY */
2774 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2775
2776 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2777 h1s->cs->flags |= CS_FL_EOS;
2778 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2779 h1s->cs->flags |= CS_FL_ERROR;
2780 h1_alert(h1s);
2781 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2782 }
2783 else {
2784 h1_release(h1c);
2785 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
2786 }
Christopher Faulet539e0292018-11-19 10:40:09 +01002787 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002788}
2789
Willy Tarreau691d5032021-01-20 14:55:01 +01002790struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002791{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002792 struct connection *conn;
2793 struct tasklet *tl = (struct tasklet *)t;
2794 int conn_in_list;
2795 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002796 int ret = 0;
2797
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002798
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002799 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002800 if (tl->context == NULL) {
2801 /* The connection has been taken over by another thread,
2802 * we're no longer responsible for it, so just free the
2803 * tasklet, and do nothing.
2804 */
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002805 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002806 tasklet_free(tl);
2807 return NULL;
2808 }
2809 h1c = ctx;
2810 conn = h1c->conn;
2811
2812 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2813
2814 /* Remove the connection from the list, to be sure nobody attempts
2815 * to use it while we handle the I/O events
2816 */
2817 conn_in_list = conn->flags & CO_FL_LIST_MASK;
2818 if (conn_in_list)
2819 MT_LIST_DEL(&conn->list);
2820
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002821 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002823 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002824 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002825 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02002826 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002827 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002828 ret = h1_process(h1c);
2829 /* If we were in an idle list, we want to add it back into it,
2830 * unless h1_process() returned -1, which mean it has destroyed
2831 * the connection (testing !ret is enough, if h1_process() wasn't
2832 * called then ret will be 0 anyway.
2833 */
2834 if (!ret && conn_in_list) {
2835 struct server *srv = objt_server(conn->target);
2836
2837 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002838 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002839 else
Willy Tarreaua9d7b762020-07-10 08:28:20 +02002840 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002841 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002842 return NULL;
2843}
2844
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002845static void h1_reset(struct connection *conn)
2846{
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002847
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01002848}
Christopher Faulet51dbc942018-09-13 09:05:15 +02002849
2850static int h1_wake(struct connection *conn)
2851{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002852 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01002853 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002854
Christopher Faulet6b81df72019-10-01 22:08:43 +02002855 TRACE_POINT(H1_EV_H1C_WAKE, conn);
2856
Christopher Faulet539e0292018-11-19 10:40:09 +01002857 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01002858 ret = h1_process(h1c);
2859 if (ret == 0) {
2860 struct h1s *h1s = h1c->h1s;
2861
Christopher Fauletad4daf62021-01-21 17:49:01 +01002862 if (h1c->flags & H1C_F_ST_ATTACHED)
2863 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002864 }
2865 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002866}
2867
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002868/* Connection timeout management. The principle is that if there's no receipt
2869 * nor sending for a certain amount of time, the connection is closed.
2870 */
Willy Tarreauac6322d2021-01-29 12:33:46 +01002871struct task *h1_timeout_task(struct task *t, void *context, unsigned short state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002872{
2873 struct h1c *h1c = context;
2874 int expired = tick_is_expired(t->expire, now_ms);
2875
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002876 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002877
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002878 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002879 /* Make sure nobody stole the connection from us */
2880 HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2881
2882 /* Somebody already stole the connection from us, so we should not
2883 * free it, we just have to free the task.
2884 */
2885 if (!t->context) {
2886 h1c = NULL;
2887 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2888 goto do_leave;
2889 }
2890
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002891 if (!expired) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002892 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2893 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002894 return t;
2895 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002896
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002897 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002898 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002899 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002900 if (h1c->flags & H1C_F_ST_READY) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002901 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2902 t->expire = TICK_ETERNITY;
2903 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
2904 return t;
2905 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002906
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002907 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002908 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
2909 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002910 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002911 if (h1_handle_req_tout(h1c))
2912 h1_send(h1c);
2913 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
2914 h1_refresh_timeout(h1c);
Christopher Fauletcc043f62020-12-14 10:06:12 +01002915 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002916 return t;
2917 }
2918 }
2919
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002920 if (h1c->flags & H1C_F_ST_ATTACHED) {
2921 /* Don't release the H1 connetion right now, we must destroy the
2922 * attached CS first. Here, the H1C must not be READY */
2923 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
2924 h1_alert(h1c->h1s);
2925 h1_refresh_timeout(h1c);
2926 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
2927 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2928 return t;
2929 }
2930
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002931 /* We're about to destroy the connection, so make sure nobody attempts
2932 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002933 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002934 if (h1c->conn->flags & CO_FL_LIST_MASK)
Olivier Houchard48ce6a32020-07-02 11:58:05 +02002935 MT_LIST_DEL(&h1c->conn->list);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002936
Olivier Houchardf8f4c2e2020-06-29 20:15:59 +02002937 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02002938 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01002939
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002940 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002941 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002942
2943 if (!h1c) {
2944 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02002945 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002946 return NULL;
2947 }
2948
2949 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02002950 h1_release(h1c);
2951 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01002952 return NULL;
2953}
2954
Christopher Faulet51dbc942018-09-13 09:05:15 +02002955/*******************************************/
2956/* functions below are used by the streams */
2957/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02002958
Christopher Faulet51dbc942018-09-13 09:05:15 +02002959/*
2960 * Attach a new stream to a connection
2961 * (Used for outgoing connections)
2962 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002963static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002964{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002965 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002966 struct conn_stream *cs = NULL;
2967 struct h1s *h1s;
2968
Christopher Faulet6b81df72019-10-01 22:08:43 +02002969 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002970 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01002971 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2972 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002973 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002974
Christopher Faulet236c93b2020-07-02 09:19:54 +02002975 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002976 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +01002977 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2978 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002979 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002980
Christopher Faulet2f0ec662020-09-24 10:30:15 +02002981 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002982 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01002983 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
2984 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002985 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002986
Christopher Faulet6b81df72019-10-01 22:08:43 +02002987 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002988 return cs;
Christopher Faulet26a26432021-01-27 11:27:50 +01002989 err:
Christopher Faulet51dbc942018-09-13 09:05:15 +02002990 cs_free(cs);
Christopher Faulet26a26432021-01-27 11:27:50 +01002991 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 +02002992 return NULL;
2993}
2994
2995/* Retrieves a valid conn_stream from this connection, or returns NULL. For
2996 * this mux, it's easy as we can only store a single conn_stream.
2997 */
2998static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
2999{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003000 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003001 struct h1s *h1s = h1c->h1s;
3002
3003 if (h1s)
3004 return h1s->cs;
3005
3006 return NULL;
3007}
3008
Christopher Faulet73c12072019-04-08 11:23:22 +02003009static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003010{
Christopher Faulet73c12072019-04-08 11:23:22 +02003011 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003012
Christopher Faulet6b81df72019-10-01 22:08:43 +02003013 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003014 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003015 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003016}
3017
3018/*
3019 * Detach the stream from the connection and possibly release the connection.
3020 */
3021static void h1_detach(struct conn_stream *cs)
3022{
3023 struct h1s *h1s = cs->ctx;
3024 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003025 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003026 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003027
Christopher Faulet6b81df72019-10-01 22:08:43 +02003028 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3029
Christopher Faulet51dbc942018-09-13 09:05:15 +02003030 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003031 if (!h1s) {
3032 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003033 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003034 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003035
Olivier Houchardf502aca2018-12-14 19:42:40 +01003036 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003037 h1c = h1s->h1c;
3038 h1s->cs = NULL;
3039
Christopher Faulet42849b02020-10-05 11:35:17 +02003040 sess->accept_date = date;
3041 sess->tv_accept = now;
3042 sess->t_handshake = 0;
3043 sess->t_idle = -1;
3044
Olivier Houchard8a786902018-12-15 16:05:40 +01003045 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3046 h1s_destroy(h1s);
3047
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003048 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 +02003049 /* If there are any excess server data in the input buffer,
3050 * release it and close the connection ASAP (some data may
3051 * remain in the output buffer). This happens if a server sends
3052 * invalid responses. So in such case, we don't want to reuse
3053 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003054 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003055 if (b_data(&h1c->ibuf)) {
3056 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003057 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003058 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003059 goto release;
3060 }
Christopher Faulet03627242019-07-19 11:34:08 +02003061
Christopher Faulet08016ab2020-07-01 16:10:06 +02003062 if (h1c->conn->flags & CO_FL_PRIVATE) {
3063 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003064 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3065 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003066 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003067 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003068 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003069 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003070 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003071 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003072 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3073 goto end;
3074 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003075 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003076 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003077 if (h1c->conn->owner == sess)
3078 h1c->conn->owner = NULL;
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003079 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003080 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003081 /* The server doesn't want it, let's kill the connection right away */
3082 h1c->conn->mux->destroy(h1c);
3083 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3084 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003085 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003086 /* At this point, the connection has been added to the
3087 * server idle list, so another thread may already have
3088 * hijacked it, so we can't do anything with it.
3089 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003090 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003091 }
3092 }
3093
Christopher Fauletf1204b82019-07-19 14:51:06 +02003094 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003095 /* 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 +01003096 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003097 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003098 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003099 !h1c->conn->owner) {
3100 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003101 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003102 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003103 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003104 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003105 /* If we have a new request, process it immediately or
3106 * subscribe for reads waiting for new data
3107 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003108 if (unlikely(b_data(&h1c->ibuf))) {
3109 if (h1_process(h1c) == -1)
3110 goto end;
3111 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003112 else
3113 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3114 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003115 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003116 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003117 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003118 end:
3119 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003120}
3121
3122
3123static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3124{
3125 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02003126 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003127
3128 if (!h1s)
3129 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003130 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003131
Christopher Faulet6b81df72019-10-01 22:08:43 +02003132 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s);
3133
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003134 if (cs->flags & CS_FL_SHR)
3135 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003136 if (cs->flags & CS_FL_KILL_CONN) {
3137 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3138 goto do_shutr;
3139 }
3140 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3141 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003142 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003143 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003144
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003145 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3146 /* Here attached is implicit because there is CS */
3147 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3148 goto end;
3149 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003150 if (h1s->flags & H1S_F_WANT_KAL) {
3151 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003152 goto end;
3153 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003154
Christopher Faulet7f366362019-04-08 10:51:20 +02003155 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003156 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3157 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003158 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003159 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003160 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003161 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003162 end:
3163 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003164}
3165
3166static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3167{
3168 struct h1s *h1s = cs->ctx;
3169 struct h1c *h1c;
3170
3171 if (!h1s)
3172 return;
3173 h1c = h1s->h1c;
3174
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_SHW)
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);
Christopher Faulet7f366362019-04-08 10:51:20 +02003181 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003182 }
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);
3185 goto do_shutw;
3186 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003187
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) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
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_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003199 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003200 if (!b_data(&h1c->obuf))
3201 h1_shutw_conn(cs->conn, mode);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003202 end:
3203 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003204}
3205
Christopher Faulet666a0c42019-01-08 11:12:04 +01003206static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003207{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003208 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003209
Christopher Faulet6b81df72019-10-01 22:08:43 +02003210 TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003211 conn_xprt_shutw(conn);
3212 conn_sock_shutw(conn, (mode == CS_SHW_NORMAL));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003213 TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003214}
3215
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003216/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3217 * The <es> pointer is not allowed to differ from the one passed to the
3218 * subscribe() call. It always returns zero.
3219 */
3220static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003221{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003222 struct h1s *h1s = cs->ctx;
3223
3224 if (!h1s)
3225 return 0;
3226
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003227 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003228 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003229
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003230 es->events &= ~event_type;
3231 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003232 h1s->subs = NULL;
3233
3234 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003235 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003236
3237 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003238 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003239
Christopher Faulet51dbc942018-09-13 09:05:15 +02003240 return 0;
3241}
3242
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003243/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3244 * event subscriber <es> is not allowed to change from a previous call as long
3245 * as at least one event is still subscribed. The <event_type> must only be a
3246 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3247 * the conn_stream <cs> was already detached, in which case it will return -1.
3248 */
3249static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003250{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003251 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003252 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003253
3254 if (!h1s)
3255 return -1;
3256
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003257 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003258 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003259
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003260 es->events |= event_type;
3261 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003262
3263 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003264 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003265
3266
Christopher Faulet6b81df72019-10-01 22:08:43 +02003267 if (event_type & SUB_RETRY_SEND) {
3268 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003269 /*
3270 * If the conn_stream attempt to subscribe, and the
3271 * mux isn't subscribed to the connection, then it
3272 * probably means the connection wasn't established
3273 * yet, so we have to subscribe.
3274 */
3275 h1c = h1s->h1c;
3276 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3277 h1c->conn->xprt->subscribe(h1c->conn,
3278 h1c->conn->xprt_ctx,
3279 SUB_RETRY_SEND,
3280 &h1c->wait_event);
3281 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003282 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003283}
3284
3285/* Called from the upper layer, to receive data */
3286static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3287{
3288 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003289 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003290 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003291 size_t ret = 0;
3292
Willy Tarreau022e5e52020-09-10 09:33:15 +02003293 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003294
3295 /* Do nothing for now if not READY */
3296 if (!(h1c->flags & H1C_F_ST_READY)) {
3297 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3298 goto end;
3299 }
3300
Christopher Faulet539e0292018-11-19 10:40:09 +01003301 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet30db3d72019-05-17 15:35:33 +02003302 ret = h1_process_input(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003303 else
3304 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003305
Christopher Faulete18777b2019-04-16 16:46:36 +02003306 if (flags & CO_RFL_BUF_FLUSH) {
Christopher Faulet2eaf3092020-07-03 14:51:15 +02003307 if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) {
Christopher Fauletae635762020-09-21 11:47:16 +02003308 h1c->flags |= H1C_F_WANT_SPLICE;
3309 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 +02003310 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003311 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003312 else {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003313 if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003314 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003315 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003316
3317 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003318 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003319 return ret;
3320}
3321
3322
3323/* Called from the upper layer, to send data */
3324static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3325{
3326 struct h1s *h1s = cs->ctx;
3327 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003328 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003329
3330 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003331 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003332 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003333
Willy Tarreau022e5e52020-09-10 09:33:15 +02003334 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003335
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003336 /* If we're not connected yet, or we're waiting for a handshake, stop
3337 * now, as we don't want to remove everything from the channel buffer
3338 * before we're sure we can send it.
3339 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003340 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003341 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003342 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003343 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003344
Christopher Fauletdea24742021-01-22 15:12:30 +01003345 if (h1c->flags & H1C_F_ST_ERROR) {
3346 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003347 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 +01003348 return 0;
3349 }
3350
Christopher Faulet46230362019-10-17 16:04:20 +02003351 /* Inherit some flags from the upper layer */
3352 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3353 if (flags & CO_SFL_MSG_MORE)
3354 h1c->flags |= H1C_F_CO_MSG_MORE;
3355 if (flags & CO_SFL_STREAMER)
3356 h1c->flags |= H1C_F_CO_STREAMER;
3357
Christopher Fauletc31872f2019-06-04 22:09:36 +02003358 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003359 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003360
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003361 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
3362 ret = h1_process_output(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003363 else
3364 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003365
3366 if ((count - ret) > 0)
3367 h1c->flags |= H1C_F_CO_MSG_MORE;
3368
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003369 if (!ret)
3370 break;
3371 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003372 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003373 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003374 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003375 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003376
3377 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletdea24742021-01-22 15:12:30 +01003378 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003379 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 +01003380 }
3381
Christopher Faulet7a145d62020-08-05 11:31:16 +02003382 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003383 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003384 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003385}
3386
Willy Tarreaue5733232019-05-22 19:24:06 +02003387#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003388/* Send and get, using splicing */
3389static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3390{
3391 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003392 struct h1c *h1c = h1s->h1c;
3393 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003394 int ret = 0;
3395
Willy Tarreau022e5e52020-09-10 09:33:15 +02003396 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003397
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003398 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003399 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003400 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 +02003401 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003402 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003403 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003404 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003405 goto end;
3406 }
3407
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003408 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
3409 h1c->flags |= H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003410 TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s);
3411 }
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003412 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003413 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003414 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003415 }
3416
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003417 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003418 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3419 goto end;
3420 }
3421
Christopher Faulet1be55f92018-10-02 15:59:23 +02003422 if (h1m->state == H1_MSG_DATA && count > h1m->curr_len)
3423 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003424 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet1146f972019-05-29 14:35:24 +02003425 if (h1m->state == H1_MSG_DATA && ret >= 0) {
Christopher Faulet1be55f92018-10-02 15:59:23 +02003426 h1m->curr_len -= ret;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003427 if (!h1m->curr_len) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003428 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003429 TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003430 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003431 }
3432
Christopher Faulet1be55f92018-10-02 15:59:23 +02003433 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003434 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003435 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003436 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003437 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003438 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003439
Christopher Fauleta131a8f2020-07-07 10:56:40 +02003440 if ((h1s->flags & H1S_F_REOS) ||
3441 (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) ||
Christopher Faulet27182292020-07-03 15:08:49 +02003442 (h1m->state == H1_MSG_DATA && !h1m->curr_len)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003443 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003444 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet27182292020-07-03 15:08:49 +02003445 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003446
Willy Tarreau022e5e52020-09-10 09:33:15 +02003447 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003448 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003449}
3450
3451static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3452{
3453 struct h1s *h1s = cs->ctx;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003454 int ret = 0;
3455
Willy Tarreau022e5e52020-09-10 09:33:15 +02003456 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003457
Christopher Faulet1be55f92018-10-02 15:59:23 +02003458 if (b_data(&h1s->h1c->obuf))
3459 goto end;
3460
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003461 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003462 end:
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003463 if (pipe->data) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003464 if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) {
3465 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003466 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003467 }
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003468 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003469
Willy Tarreau022e5e52020-09-10 09:33:15 +02003470 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003471 return ret;
3472}
3473#endif
3474
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003475static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3476{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003477 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003478 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003479
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003480 switch (mux_ctl) {
3481 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003482 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003483 ret |= MUX_STATUS_READY;
3484 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003485 case MUX_EXIT_STATUS:
Christopher Fauletc18fc232020-10-06 17:41:36 +02003486 ret = (h1c->errcode == 400 ? MUX_ES_INVALID_ERR :
3487 (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003488 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3489 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3490 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003491 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003492 default:
3493 return -1;
3494 }
3495}
3496
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003497/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003498static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003499{
3500 struct h1c *h1c = conn->ctx;
3501 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003502 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003503
Christopher Fauletf376a312019-01-04 15:16:06 +01003504 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3505 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003506 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3507 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3508 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3509 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3510
3511 if (h1s) {
3512 char *method;
3513
3514 if (h1s->meth < HTTP_METH_OTHER)
3515 method = http_known_methods[h1s->meth].ptr;
3516 else
3517 method = "UNKNOWN";
3518 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3519 " .meth=%s status=%d",
3520 h1s, h1s->flags,
3521 h1m_state_str(h1s->req.state),
3522 h1m_state_str(h1s->res.state), method, h1s->status);
3523 if (h1s->cs)
3524 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3525 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003526
3527 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3528 if (h1s->subs) {
3529 if (h1s->subs) {
3530 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3531 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3532 h1s->subs->tasklet->calls,
3533 h1s->subs->tasklet->context);
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003534 if (h1s->subs->tasklet->calls >= 1000000)
3535 ret = 1;
Willy Tarreau150c4f82021-01-20 17:05:58 +01003536 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3537 chunk_appendf(&trash, ")");
3538 }
3539 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003540 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003541 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003542}
3543
3544
3545/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3546static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3547{
3548 struct h1_hdr_entry *entry;
3549
3550 /* Be sure there is a non-empty <to> */
3551 if (!strlen(to)) {
3552 memprintf(err, "expect <to>");
3553 return -1;
3554 }
3555
3556 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003557 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003558 memprintf(err, "<from> and <to> must not differ execpt the case");
3559 return -1;
3560 }
3561
3562 /* Be sure <from> does not already existsin the tree */
3563 if (ebis_lookup(&hdrs_map.map, from)) {
3564 memprintf(err, "duplicate entry '%s'", from);
3565 return -1;
3566 }
3567
3568 /* Create the entry and insert it in the tree */
3569 entry = malloc(sizeof(*entry));
3570 if (!entry) {
3571 memprintf(err, "out of memory");
3572 return -1;
3573 }
3574
3575 entry->node.key = strdup(from);
3576 entry->name.ptr = strdup(to);
3577 entry->name.len = strlen(to);
3578 if (!entry->node.key || !entry->name.ptr) {
3579 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003580 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003581 free(entry);
3582 memprintf(err, "out of memory");
3583 return -1;
3584 }
3585 ebis_insert(&hdrs_map.map, &entry->node);
3586 return 0;
3587}
3588
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003589/* Migrate the the connection to the current thread.
3590 * Return 0 if successful, non-zero otherwise.
3591 * Expected to be called with the old thread lock held.
3592 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003593static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003594{
3595 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003596 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003597
3598 if (fd_takeover(conn->handle.fd, conn) != 0)
3599 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003600
3601 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3602 /* We failed to takeover the xprt, even if the connection may
3603 * still be valid, flag it as error'd, as we have already
3604 * taken over the fd, and wake the tasklet, so that it will
3605 * destroy it.
3606 */
3607 conn->flags |= CO_FL_ERROR;
3608 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3609 return -1;
3610 }
3611
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003612 if (h1c->wait_event.events)
3613 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3614 h1c->wait_event.events, &h1c->wait_event);
3615 /* To let the tasklet know it should free itself, and do nothing else,
3616 * set its context to NULL.
3617 */
3618 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003619 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003620
3621 task = h1c->task;
3622 if (task) {
3623 task->context = NULL;
3624 h1c->task = NULL;
3625 __ha_barrier_store();
3626 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003627
3628 h1c->task = task_new(tid_bit);
3629 if (!h1c->task) {
3630 h1_release(h1c);
3631 return -1;
3632 }
3633 h1c->task->process = h1_timeout_task;
3634 h1c->task->context = h1c;
3635 }
3636 h1c->wait_event.tasklet = tasklet_new();
3637 if (!h1c->wait_event.tasklet) {
3638 h1_release(h1c);
3639 return -1;
3640 }
3641 h1c->wait_event.tasklet->process = h1_io_cb;
3642 h1c->wait_event.tasklet->context = h1c;
3643 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3644 SUB_RETRY_RECV, &h1c->wait_event);
3645
3646 return 0;
3647}
3648
3649
Christopher Faulet98fbe952019-07-22 16:18:24 +02003650static void h1_hdeaders_case_adjust_deinit()
3651{
3652 struct ebpt_node *node, *next;
3653 struct h1_hdr_entry *entry;
3654
3655 node = ebpt_first(&hdrs_map.map);
3656 while (node) {
3657 next = ebpt_next(node);
3658 ebpt_delete(node);
3659 entry = container_of(node, struct h1_hdr_entry, node);
3660 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003661 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003662 free(entry);
3663 node = next;
3664 }
3665 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003666}
3667
Christopher Faulet98fbe952019-07-22 16:18:24 +02003668static int cfg_h1_headers_case_adjust_postparser()
3669{
3670 FILE *file = NULL;
3671 char *c, *key_beg, *key_end, *value_beg, *value_end;
3672 char *err;
3673 int rc, line = 0, err_code = 0;
3674
3675 if (!hdrs_map.name)
3676 goto end;
3677
3678 file = fopen(hdrs_map.name, "r");
3679 if (!file) {
3680 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
3681 hdrs_map.name);
3682 err_code |= ERR_ALERT | ERR_FATAL;
3683 goto end;
3684 }
3685
3686 /* now parse all lines. The file may contain only two header name per
3687 * line, separated by spaces. All heading and trailing spaces will be
3688 * ignored. Lines starting with a # are ignored.
3689 */
3690 while (fgets(trash.area, trash.size, file) != NULL) {
3691 line++;
3692 c = trash.area;
3693
3694 /* strip leading spaces and tabs */
3695 while (*c == ' ' || *c == '\t')
3696 c++;
3697
3698 /* ignore emptu lines, or lines beginning with a dash */
3699 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3700 continue;
3701
3702 /* look for the end of the key */
3703 key_beg = c;
3704 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3705 c++;
3706 key_end = c;
3707
3708 /* strip middle spaces and tabs */
3709 while (*c == ' ' || *c == '\t')
3710 c++;
3711
3712 /* look for the end of the value, it is the end of the line */
3713 value_beg = c;
3714 while (*c && *c != '\n' && *c != '\r')
3715 c++;
3716 value_end = c;
3717
3718 /* trim possibly trailing spaces and tabs */
3719 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3720 value_end--;
3721
3722 /* set final \0 and check entries */
3723 *key_end = '\0';
3724 *value_end = '\0';
3725
3726 err = NULL;
3727 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3728 if (rc < 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003729 ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3730 hdrs_map.name, err, line);
3731 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003732 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003733 goto end;
3734 }
3735 if (rc > 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003736 ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
3737 hdrs_map.name, err, line);
3738 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02003739 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003740 }
3741 }
3742
3743 end:
3744 if (file)
3745 fclose(file);
3746 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
3747 return err_code;
3748}
3749
3750
3751/* config parser for global "h1-outgoing-header-case-adjust" */
3752static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
3753 struct proxy *defpx, const char *file, int line,
3754 char **err)
3755{
3756 if (too_many_args(2, args, err, NULL))
3757 return -1;
3758 if (!*(args[1]) || !*(args[2])) {
3759 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
3760 return -1;
3761 }
3762 return add_hdr_case_adjust(args[1], args[2], err);
3763}
3764
3765/* config parser for global "h1-outgoing-headers-case-adjust-file" */
3766static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
3767 struct proxy *defpx, const char *file, int line,
3768 char **err)
3769{
3770 if (too_many_args(1, args, err, NULL))
3771 return -1;
3772 if (!*(args[1])) {
3773 memprintf(err, "'%s' expects <file> as argument.", args[0]);
3774 return -1;
3775 }
3776 free(hdrs_map.name);
3777 hdrs_map.name = strdup(args[1]);
3778 return 0;
3779}
3780
3781
3782/* config keyword parsers */
3783static struct cfg_kw_list cfg_kws = {{ }, {
3784 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
3785 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
3786 { 0, NULL, NULL },
3787 }
3788};
3789
3790INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3791REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
3792
3793
Christopher Faulet51dbc942018-09-13 09:05:15 +02003794/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003795/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003796/****************************************/
3797
3798/* The mux operations */
Willy Tarreauf77a1582019-01-10 10:00:08 +01003799static const struct mux_ops mux_h1_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02003800 .init = h1_init,
3801 .wake = h1_wake,
3802 .attach = h1_attach,
3803 .get_first_cs = h1_get_first_cs,
3804 .detach = h1_detach,
3805 .destroy = h1_destroy,
3806 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01003807 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02003808 .rcv_buf = h1_rcv_buf,
3809 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02003810#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003811 .rcv_pipe = h1_rcv_pipe,
3812 .snd_pipe = h1_snd_pipe,
3813#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02003814 .subscribe = h1_subscribe,
3815 .unsubscribe = h1_unsubscribe,
3816 .shutr = h1_shutr,
3817 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003818 .show_fd = h1_show_fd,
Olivier Houchard9a86fcb2018-12-11 16:47:14 +01003819 .reset = h1_reset,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003820 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003821 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02003822 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02003823 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02003824};
3825
3826
3827/* this mux registers default HTX proto */
3828static struct mux_proto_list mux_proto_htx =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02003829{ .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02003830
Willy Tarreau0108d902018-11-25 19:14:37 +01003831INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx);
3832
Christopher Faulet51dbc942018-09-13 09:05:15 +02003833/*
3834 * Local variables:
3835 * c-indent-level: 8
3836 * c-basic-offset: 8
3837 * End:
3838 */