blob: 7040d6de98dc403af5de298631e0e2210c03e7c0 [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>
Willy Tarreau63617db2021-10-06 18:23:40 +020013#include <import/ebmbtree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020014
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020018#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020019#include <haproxy/h1_htx.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020020#include <haproxy/h2.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/istbuf.h>
24#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020025#include <haproxy/pipe-t.h>
Willy Tarreauadc02402021-05-08 20:28:54 +020026#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020027#include <haproxy/session-t.h>
Christopher Fauletb4c584e2021-11-26 17:37:07 +010028#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020030#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020031#include <haproxy/trace.h>
Christopher Faulet51dbc942018-09-13 09:05:15 +020032
33/*
34 * H1 Connection flags (32 bits)
35 */
36#define H1C_F_NONE 0x00000000
37
38/* Flags indicating why writing output data are blocked */
39#define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */
40#define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */
41/* 0x00000004 - 0x00000008 unused */
42
43/* Flags indicating why reading input data are blocked. */
44#define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */
45#define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */
Christopher Fauletd17ad822020-09-24 15:14:29 +020046#define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */
Christopher Faulet51dbc942018-09-13 09:05:15 +020047
Christopher Faulet7b109f22019-12-05 11:18:31 +010048/* Flags indicating the connection state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +010049#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 +010050#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 +010051#define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused
52 * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */
53#define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */
54#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 +010055#define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY conn-stream. A conn-stream is not ready when
56 * 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 +010057#define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED)
Christopher Faulet75308302021-11-15 14:51:37 +010058#define H1C_F_ST_SILENT_SHUT 0x00004000 /* silent (or dirty) shutdown must be performed (implied ST_SHUTDOWN) */
Christopher Fauleta85c5222021-10-27 15:36:38 +020059/* 0x00008000 unused */
Christopher Faulet51dbc942018-09-13 09:05:15 +020060
Christopher Fauletb385b502021-01-13 18:47:57 +010061#define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */
62#define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */
63#define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */
64#define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */
65#define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */
66#define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */
Christopher Faulet129817b2018-09-20 16:14:40 +020067
Christopher Faulet10a86702021-04-07 14:18:11 +020068/* 0x00400000 - 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
Christopher Faulet10a86702021-04-07 14:18:11 +020075
76#define H1S_F_RX_BLK 0x00100000 /* Don't process more input data, waiting sync with output side */
77#define H1S_F_TX_BLK 0x00200000 /* Don't process more output data, waiting sync with input side */
Christopher Faulet46e058d2021-09-20 07:47:27 +020078#define H1S_F_RX_CONGESTED 0x00000004 /* Cannot process input data RX path is congested (waiting for more space in channel's buffer) */
79
Willy Tarreauc493c9c2019-06-03 14:18:22 +020080#define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */
Christopher Fauletf2824e62018-10-01 12:12:37 +020081#define H1S_F_WANT_KAL 0x00000010
82#define H1S_F_WANT_TUN 0x00000020
83#define H1S_F_WANT_CLO 0x00000040
84#define H1S_F_WANT_MSK 0x00000070
85#define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */
Christopher Faulet5696f542020-12-02 16:08:38 +010086#define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */
Christopher Faulet60ef12c2020-09-24 10:05:44 +020087
Ilya Shipitsinacf84592021-02-06 22:29:08 +050088/* 0x00000200 unused */
Christopher Faulet2eed8002020-12-07 11:26:13 +010089#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 +020090#define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */
91#define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */
92#define H1S_F_ERROR 0x00001800 /* stream error mask */
93
Christopher Faulet72ba6cd2019-09-24 16:20:05 +020094#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 +020095#define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */
Christopher Faulet51dbc942018-09-13 09:05:15 +020096
97/* H1 connection descriptor */
Christopher Faulet51dbc942018-09-13 09:05:15 +020098struct h1c {
99 struct connection *conn;
100 struct proxy *px;
101 uint32_t flags; /* Connection flags: H1C_F_* */
Christopher Fauletc18fc232020-10-06 17:41:36 +0200102 unsigned int errcode; /* Status code when an error occurred at the H1 connection level */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200103 struct buffer ibuf; /* Input buffer to store data before parsing */
104 struct buffer obuf; /* Output buffer to store data after reformatting */
105
106 struct buffer_wait buf_wait; /* Wait list for buffer allocation */
107 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
108
109 struct h1s *h1s; /* H1 stream descriptor */
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100110 struct task *task; /* timeout management task */
Christopher Faulet563c3452021-11-26 17:37:51 +0100111 struct h1_counters *px_counters; /* h1 counters attached to proxy */
Christopher Fauletdbe57792020-10-05 17:50:58 +0200112 int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */
113 int timeout; /* client/server timeout duration */
114 int shut_timeout; /* client-fin/server-fin timeout duration */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200115};
116
117/* H1 stream descriptor */
118struct h1s {
119 struct h1c *h1c;
120 struct conn_stream *cs;
Christopher Fauletfeb11742018-11-29 15:12:34 +0100121 uint32_t flags; /* Connection flags: H1S_F_* */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200122
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100123 struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */
Christopher Faulet129817b2018-09-20 16:14:40 +0200124
Olivier Houchardf502aca2018-12-14 19:42:40 +0100125 struct session *sess; /* Associated session */
Christopher Fauletd17ad822020-09-24 15:14:29 +0200126 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Christopher Faulet129817b2018-09-20 16:14:40 +0200127 struct h1m req;
128 struct h1m res;
129
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500130 enum http_meth_t meth; /* HTTP request method */
Christopher Faulet129817b2018-09-20 16:14:40 +0200131 uint16_t status; /* HTTP response status */
Amaury Denoyellec1938232020-12-11 17:53:03 +0100132
133 char ws_key[25]; /* websocket handshake key */
Christopher Faulet51dbc942018-09-13 09:05:15 +0200134};
135
Christopher Faulet98fbe952019-07-22 16:18:24 +0200136/* Map of headers used to convert outgoing headers */
137struct h1_hdrs_map {
138 char *name;
139 struct eb_root map;
140};
141
142/* An entry in a headers map */
143struct h1_hdr_entry {
144 struct ist name;
145 struct ebpt_node node;
146};
147
148/* Declare the headers map */
149static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT };
150
151
Christopher Faulet6b81df72019-10-01 22:08:43 +0200152/* trace source and events */
153static void h1_trace(enum trace_level level, uint64_t mask,
154 const struct trace_source *src,
155 const struct ist where, const struct ist func,
156 const void *a1, const void *a2, const void *a3, const void *a4);
157
158/* The event representation is split like this :
159 * h1c - internal H1 connection
160 * h1s - internal H1 stream
161 * strm - application layer
162 * rx - data receipt
163 * tx - data transmission
164 *
165 */
166static const struct trace_event h1_trace_events[] = {
167#define H1_EV_H1C_NEW (1ULL << 0)
168 { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" },
169#define H1_EV_H1C_RECV (1ULL << 1)
170 { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" },
171#define H1_EV_H1C_SEND (1ULL << 2)
172 { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" },
173#define H1_EV_H1C_BLK (1ULL << 3)
174 { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" },
175#define H1_EV_H1C_WAKE (1ULL << 4)
176 { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" },
177#define H1_EV_H1C_END (1ULL << 5)
178 { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" },
179#define H1_EV_H1C_ERR (1ULL << 6)
180 { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" },
181
182#define H1_EV_RX_DATA (1ULL << 7)
183 { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" },
184#define H1_EV_RX_EOI (1ULL << 8)
185 { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" },
186#define H1_EV_RX_HDRS (1ULL << 9)
187 { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" },
188#define H1_EV_RX_BODY (1ULL << 10)
189 { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" },
190#define H1_EV_RX_TLRS (1ULL << 11)
191 { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" },
192
193#define H1_EV_TX_DATA (1ULL << 12)
194 { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" },
195#define H1_EV_TX_EOI (1ULL << 13)
196 { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" },
197#define H1_EV_TX_HDRS (1ULL << 14)
198 { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" },
199#define H1_EV_TX_BODY (1ULL << 15)
200 { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" },
201#define H1_EV_TX_TLRS (1ULL << 16)
202 { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" },
203
204#define H1_EV_H1S_NEW (1ULL << 17)
205 { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" },
206#define H1_EV_H1S_BLK (1ULL << 18)
207 { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" },
208#define H1_EV_H1S_END (1ULL << 19)
209 { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" },
210#define H1_EV_H1S_ERR (1ULL << 20)
211 { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" },
212
213#define H1_EV_STRM_NEW (1ULL << 21)
214 { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
215#define H1_EV_STRM_RECV (1ULL << 22)
216 { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
217#define H1_EV_STRM_SEND (1ULL << 23)
218 { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
219#define H1_EV_STRM_WAKE (1ULL << 24)
220 { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
221#define H1_EV_STRM_SHUT (1ULL << 25)
222 { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
223#define H1_EV_STRM_END (1ULL << 26)
224 { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
225#define H1_EV_STRM_ERR (1ULL << 27)
226 { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
227
228 { }
229};
230
231static const struct name_desc h1_trace_lockon_args[4] = {
232 /* arg1 */ { /* already used by the connection */ },
233 /* arg2 */ { .name="h1s", .desc="H1 stream" },
234 /* arg3 */ { },
235 /* arg4 */ { }
236};
237
238static const struct name_desc h1_trace_decoding[] = {
239#define H1_VERB_CLEAN 1
240 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
241#define H1_VERB_MINIMAL 2
242 { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" },
243#define H1_VERB_SIMPLE 3
244 { .name="simple", .desc="add request/response status line or htx info when available" },
245#define H1_VERB_ADVANCED 4
246 { .name="advanced", .desc="add header fields or frame decoding when available" },
247#define H1_VERB_COMPLETE 5
248 { .name="complete", .desc="add full data dump when available" },
249 { /* end */ }
250};
251
Willy Tarreau6eb3d372021-04-10 19:29:26 +0200252static struct trace_source trace_h1 __read_mostly = {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200253 .name = IST("h1"),
254 .desc = "HTTP/1 multiplexer",
255 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
256 .default_cb = h1_trace,
257 .known_events = h1_trace_events,
258 .lockon_args = h1_trace_lockon_args,
259 .decoding = h1_trace_decoding,
260 .report_events = ~0, // report everything by default
261};
262
263#define TRACE_SOURCE &trace_h1
264INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
265
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100266
267/* h1 stats module */
268enum {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100269 H1_ST_OPEN_CONN,
270 H1_ST_OPEN_STREAM,
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100271 H1_ST_TOTAL_CONN,
272 H1_ST_TOTAL_STREAM,
273
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100274 H1_STATS_COUNT /* must be the last member of the enum */
275};
276
277
278static struct name_desc h1_stats[] = {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100279 [H1_ST_OPEN_CONN] = { .name = "h1_open_connections",
280 .desc = "Count of currently open connections" },
281 [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams",
282 .desc = "Count of currently open streams" },
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100283 [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections",
284 .desc = "Total number of connections" },
285 [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams",
286
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100287};
288
289static struct h1_counters {
Christopher Faulet60fa0512021-11-26 18:10:39 +0100290 long long open_conns; /* count of currently open connections */
291 long long open_streams; /* count of currently open streams */
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100292 long long total_conns; /* total number of connections */
293 long long total_streams; /* total number of streams */
294
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100295} h1_counters;
296
297static void h1_fill_stats(void *data, struct field *stats)
298{
Christopher Faulet60fa0512021-11-26 18:10:39 +0100299 struct h1_counters *counters = data;
300
301 stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
302 stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100303 stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
304 stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
Christopher Fauletb4c584e2021-11-26 17:37:07 +0100305}
306
307static struct stats_module h1_stats_module = {
308 .name = "h1",
309 .fill_stats = h1_fill_stats,
310 .stats = h1_stats,
311 .stats_count = H1_STATS_COUNT,
312 .counters = &h1_counters,
313 .counters_size = sizeof(h1_counters),
314 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
315 .clearable = 1,
316};
317
318INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module);
319
320
Christopher Faulet51dbc942018-09-13 09:05:15 +0200321/* the h1c and h1s pools */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100322DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c));
323DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s));
Christopher Faulet51dbc942018-09-13 09:05:15 +0200324
Christopher Faulet51dbc942018-09-13 09:05:15 +0200325static int h1_recv(struct h1c *h1c);
326static int h1_send(struct h1c *h1c);
327static int h1_process(struct h1c *h1c);
Willy Tarreau691d5032021-01-20 14:55:01 +0100328/* h1_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100329struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state);
330struct task *h1_timeout_task(struct task *t, void *context, unsigned int state);
Christopher Fauleta85c5222021-10-27 15:36:38 +0200331static void h1_shutw_conn(struct connection *conn);
Christopher Faulet660f6f32019-10-04 10:22:47 +0200332static void h1_wake_stream_for_recv(struct h1s *h1s);
333static void h1_wake_stream_for_send(struct h1s *h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200334
Christopher Faulet6b81df72019-10-01 22:08:43 +0200335/* the H1 traces always expect that arg1, if non-null, is of type connection
336 * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and
337 * that arg3, if non-null, is a htx for rx/tx headers.
338 */
339static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
340 const struct ist where, const struct ist func,
341 const void *a1, const void *a2, const void *a3, const void *a4)
342{
343 const struct connection *conn = a1;
344 const struct h1c *h1c = conn ? conn->ctx : NULL;
345 const struct h1s *h1s = a2;
346 const struct htx *htx = a3;
347 const size_t *val = a4;
348
349 if (!h1c)
350 h1c = (h1s ? h1s->h1c : NULL);
351
352 if (!h1c || src->verbosity < H1_VERB_CLEAN)
353 return;
354
355 /* Display frontend/backend info by default */
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200356 chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'));
Christopher Faulet6b81df72019-10-01 22:08:43 +0200357
358 /* Display request and response states if h1s is defined */
Christopher Faulet6580f282021-11-26 17:31:35 +0100359 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200360 chunk_appendf(&trace_buf, " [%s, %s]",
361 h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state));
362
Christopher Faulet6580f282021-11-26 17:31:35 +0100363 if (src->verbosity > H1_VERB_SIMPLE) {
364 chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
365 h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len);
366 chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)",
367 h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len);
368 }
369
370 }
371
Christopher Faulet6b81df72019-10-01 22:08:43 +0200372 if (src->verbosity == H1_VERB_CLEAN)
373 return;
374
375 /* Display the value to the 4th argument (level > STATE) */
376 if (src->level > TRACE_LEVEL_STATE && val)
Willy Tarreaue18f53e2019-11-27 15:41:31 +0100377 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200378
379 /* Display status-line if possible (verbosity > MINIMAL) */
380 if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) {
381 const struct htx_blk *blk = htx_get_head_blk(htx);
382 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
383 enum htx_blk_type type = htx_get_blk_type(blk);
384
385 if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
386 chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
387 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
388 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
389 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
390 }
391
392 /* Display h1c info and, if defined, h1s info (pointer + flags) */
393 chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100394 if (h1c->conn)
395 chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags);
396 if (h1s) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200397 chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags);
Christopher Faulet99293b02021-11-10 10:30:15 +0100398 if (h1s->cs)
399 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", h1s->cs, h1s->cs->flags);
400 }
Christopher Faulet6b81df72019-10-01 22:08:43 +0200401
402 if (src->verbosity == H1_VERB_MINIMAL)
403 return;
404
405 /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */
406 if (src->level > TRACE_LEVEL_USER) {
407 if (src->verbosity == H1_VERB_COMPLETE ||
408 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV))))
409 chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u",
410 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
411 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf));
412 if (src->verbosity == H1_VERB_COMPLETE ||
413 (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND))))
414 chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u",
415 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
416 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
417 }
418
419 /* Display htx info if defined (level > USER) */
420 if (src->level > TRACE_LEVEL_USER && htx) {
421 int full = 0;
422
423 /* Full htx info (level > STATE && verbosity > SIMPLE) */
424 if (src->level > TRACE_LEVEL_STATE) {
425 if (src->verbosity == H1_VERB_COMPLETE)
426 full = 1;
427 else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS)))
428 full = 1;
429 }
430
431 chunk_memcat(&trace_buf, "\n\t", 2);
432 htx_dump(&trace_buf, htx, full);
433 }
434}
435
436
Christopher Faulet51dbc942018-09-13 09:05:15 +0200437/*****************************************************/
438/* functions below are for dynamic buffer management */
439/*****************************************************/
440/*
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100441 * Indicates whether or not we may receive data. The rules are the following :
442 * - if an error or a shutdown for reads was detected on the connection we
Christopher Faulet119ac872020-09-30 17:33:22 +0200443 * must not attempt to receive
444 * - if we are waiting for the connection establishment, we must not attempt
445 * to receive
446 * - if an error was detected on the stream we must not attempt to receive
447 * - if reads are explicitly disabled, we must not attempt to receive
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100448 * - if the input buffer failed to be allocated or is full , we must not try
449 * to receive
Christopher Faulet119ac872020-09-30 17:33:22 +0200450 * - if the mux is not blocked on an input condition, we may attempt to receive
Christopher Faulet51dbc942018-09-13 09:05:15 +0200451 * - otherwise must may not attempt to receive
452 */
453static inline int h1_recv_allowed(const struct h1c *h1c)
454{
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100455 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet2545a0b2019-12-05 12:30:55 +0100456 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 +0200457 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200458 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200459
Willy Tarreau2febb842020-07-31 09:15:43 +0200460 if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
461 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 +0100462 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200463 }
Christopher Fauletcf56b992018-12-11 16:12:31 +0100464
Christopher Faulet119ac872020-09-30 17:33:22 +0200465 if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) {
466 TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
467 return 0;
468 }
469
Christopher Fauletd17ad822020-09-24 15:14:29 +0200470 if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC)))
Christopher Faulet51dbc942018-09-13 09:05:15 +0200471 return 1;
472
Christopher Faulet6b81df72019-10-01 22:08:43 +0200473 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 +0200474 return 0;
475}
476
477/*
478 * Tries to grab a buffer and to re-enables processing on mux <target>. The h1
479 * flags are used to figure what buffer was requested. It returns 1 if the
480 * allocation succeeds, in which case the connection is woken up, or 0 if it's
481 * impossible to wake up and we prefer to be woken up later.
482 */
483static int h1_buf_available(void *target)
484{
485 struct h1c *h1c = target;
486
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100487 if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200488 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 +0200489 h1c->flags &= ~H1C_F_IN_ALLOC;
490 if (h1_recv_allowed(h1c))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200491 tasklet_wakeup(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200492 return 1;
493 }
494
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100495 if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200496 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 +0200497 h1c->flags &= ~H1C_F_OUT_ALLOC;
Christopher Faulet660f6f32019-10-04 10:22:47 +0200498 if (h1c->h1s)
499 h1_wake_stream_for_send(h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200500 return 1;
501 }
502
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100503 if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) {
Christopher Fauletd17ad822020-09-24 15:14:29 +0200504 TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
505 h1c->flags &= ~H1C_F_IN_SALLOC;
506 tasklet_wakeup(h1c->wait_event.tasklet);
507 return 1;
508 }
509
Christopher Faulet51dbc942018-09-13 09:05:15 +0200510 return 0;
511}
512
513/*
514 * Allocate a buffer. If if fails, it adds the mux in buffer wait queue.
515 */
516static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr)
517{
518 struct buffer *buf = NULL;
519
Willy Tarreau2b718102021-04-21 07:32:39 +0200520 if (likely(!LIST_INLIST(&h1c->buf_wait.list)) &&
Willy Tarreaud68d4f12021-03-22 14:44:31 +0100521 unlikely((buf = b_alloc(bptr)) == NULL)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +0200522 h1c->buf_wait.target = h1c;
523 h1c->buf_wait.wakeup_cb = h1_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200524 LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200525 }
526 return buf;
527}
528
529/*
530 * Release a buffer, if any, and try to wake up entities waiting in the buffer
531 * wait queue.
532 */
533static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr)
534{
535 if (bptr->size) {
536 b_free(bptr);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +0100537 offer_buffers(h1c->buf_wait.target, 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200538 }
539}
540
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100541/* returns the number of streams in use on a connection to figure if it's idle
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100542 * 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 +0100543 * not. This flag is only set when no H1S is attached and when the previous
544 * stream, if any, was fully terminated without any error and in K/A mode.
Willy Tarreau00f18a32019-01-26 12:19:01 +0100545 */
546static int h1_used_streams(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200547{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100548 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200549
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100550 return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200551}
552
Willy Tarreau00f18a32019-01-26 12:19:01 +0100553/* returns the number of streams still available on a connection */
554static int h1_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100555{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100556 return 1 - h1_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100557}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200558
Christopher Faulet7a145d62020-08-05 11:31:16 +0200559/* Refresh the h1c task timeout if necessary */
560static void h1_refresh_timeout(struct h1c *h1c)
561{
562 if (h1c->task) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100563 if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) {
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200564 /* half-closed or dead connections : switch to clientfin/serverfin
565 * timeouts so that we don't hang too long on clients that have
566 * gone away (especially in tunnel mode).
Willy Tarreau4313d5a2020-09-08 15:40:57 +0200567 */
568 h1c->task->expire = tick_add(now_ms, h1c->shut_timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200569 TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
570 }
571 else if (b_data(&h1c->obuf)) {
572 /* connection with pending outgoing data, need a timeout (server or client). */
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200573 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200574 TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
575 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100576 else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) {
577 /* front connections waiting for a fully usable stream need a timeout. */
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200578 h1c->task->expire = tick_add(now_ms, h1c->timeout);
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100579 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 +0200580 }
Christopher Fauletadcd7892020-10-05 17:13:05 +0200581 else {
582 /* alive back connections of front connections with a conn-stream attached */
583 h1c->task->expire = TICK_ETERNITY;
584 TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn);
585 }
586
Christopher Fauletdbe57792020-10-05 17:50:58 +0200587 /* Finally set the idle expiration date if shorter */
588 h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp);
Christopher Fauletadcd7892020-10-05 17:13:05 +0200589 TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire});
590 task_queue(h1c->task);
Christopher Faulet7a145d62020-08-05 11:31:16 +0200591 }
592}
Christopher Fauletdbe57792020-10-05 17:50:58 +0200593
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200594static void h1_set_idle_expiration(struct h1c *h1c)
Christopher Fauletdbe57792020-10-05 17:50:58 +0200595{
596 if (h1c->flags & H1C_F_IS_BACK || !h1c->task) {
597 TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn);
598 h1c->idle_exp = TICK_ETERNITY;
599 return;
600 }
601
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100602 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200603 if (!tick_isset(h1c->idle_exp)) {
604 if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */
605 !b_data(&h1c->ibuf) && /* No input data */
606 tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */
607 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka);
608 TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn);
609 }
610 else {
611 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
612 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
613 }
614 }
615 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100616 else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) {
Christopher Fauletdbe57792020-10-05 17:50:58 +0200617 if (!tick_isset(h1c->idle_exp)) {
618 h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq);
619 TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn);
620 }
621 }
622 else { // CS_ATTACHED or SHUTDOWN
623 h1c->idle_exp = TICK_ETERNITY;
624 TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn);
625 }
626}
Christopher Faulet51dbc942018-09-13 09:05:15 +0200627/*****************************************************************/
628/* functions below are dedicated to the mux setup and management */
629/*****************************************************************/
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200630
631/* returns non-zero if there are input data pending for stream h1s. */
632static inline size_t h1s_data_pending(const struct h1s *h1s)
633{
634 const struct h1m *h1m;
635
Christopher Faulet0a799aa2020-09-24 09:52:52 +0200636 h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100637 return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf));
Willy Tarreaufbdf90a2019-06-03 13:42:54 +0200638}
639
Christopher Faulet16df1782020-12-04 16:47:41 +0100640/* Creates a new conn-stream and the associate stream. <input> is used as input
641 * buffer for the stream. On success, it is transferred to the stream and the
642 * mux is no longer responsible of it. On error, <input> is unchanged, thus the
643 * mux must still take care of it. However, there is nothing special to do
644 * because, on success, <input> is updated to points on BUF_NULL. Thus, calling
645 * b_free() on it is always safe. This function returns the conn-stream on
646 * success or NULL on error. */
Christopher Faulet26256f82020-09-14 11:40:13 +0200647static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
Christopher Faulet47365272018-10-31 17:40:50 +0100648{
649 struct conn_stream *cs;
650
Christopher Faulet6b81df72019-10-01 22:08:43 +0200651 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet236c93b2020-07-02 09:19:54 +0200652 cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200653 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100654 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 +0100655 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200656 }
Christopher Faulet47365272018-10-31 17:40:50 +0100657 h1s->cs = cs;
658 cs->ctx = h1s;
659
660 if (h1s->flags & H1S_F_NOT_FIRST)
661 cs->flags |= CS_FL_NOT_FIRST;
662
Amaury Denoyelle90ac6052021-10-18 14:45:49 +0200663 if (h1s->req.flags & H1_MF_UPG_WEBSOCKET)
664 cs->flags |= CS_FL_WEBSOCKET;
665
Christopher Faulet26256f82020-09-14 11:40:13 +0200666 if (stream_create_from_cs(cs, input) < 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200667 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 +0100668 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200669 }
670
Christopher Faulet60fa0512021-11-26 18:10:39 +0100671 HA_ATOMIC_INC(&h1s->h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100672 HA_ATOMIC_INC(&h1s->h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100673
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100674 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 +0200675 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100676 return cs;
677
678 err:
679 cs_free(cs);
680 h1s->cs = NULL;
Christopher Faulet26a26432021-01-27 11:27:50 +0100681 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet47365272018-10-31 17:40:50 +0100682 return NULL;
683}
684
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100685static struct conn_stream *h1s_upgrade_cs(struct h1s *h1s, struct buffer *input)
686{
687 TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
688
689 if (stream_upgrade_from_cs(h1s->cs, input) < 0) {
Christopher Faulet26a26432021-01-27 11:27:50 +0100690 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 +0100691 goto err;
692 }
693
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100694 h1s->h1c->flags |= H1C_F_ST_READY;
695 TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s);
696 return h1s->cs;
697
698 err:
Christopher Faulet26a26432021-01-27 11:27:50 +0100699 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100700 return NULL;
701}
702
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200703static struct h1s *h1s_new(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200704{
705 struct h1s *h1s;
706
Christopher Faulet6b81df72019-10-01 22:08:43 +0200707 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
708
Christopher Faulet51dbc942018-09-13 09:05:15 +0200709 h1s = pool_alloc(pool_head_h1s);
Christopher Faulet26a26432021-01-27 11:27:50 +0100710 if (!h1s) {
711 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 +0100712 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100713 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200714 h1s->h1c = h1c;
715 h1c->h1s = h1s;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200716 h1s->sess = NULL;
717 h1s->cs = NULL;
Christopher Fauletb992af02019-03-28 15:42:24 +0100718 h1s->flags = H1S_F_WANT_KAL;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100719 h1s->subs = NULL;
Christopher Fauletd17ad822020-09-24 15:14:29 +0200720 h1s->rxbuf = BUF_NULL;
Amaury Denoyellec1938232020-12-11 17:53:03 +0100721 memset(h1s->ws_key, 0, sizeof(h1s->ws_key));
Christopher Faulet129817b2018-09-20 16:14:40 +0200722
723 h1m_init_req(&h1s->req);
Christopher Fauletb992af02019-03-28 15:42:24 +0100724 h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet9768c262018-10-22 09:34:31 +0200725
Christopher Faulet129817b2018-09-20 16:14:40 +0200726 h1m_init_res(&h1s->res);
Christopher Fauletb992af02019-03-28 15:42:24 +0100727 h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet129817b2018-09-20 16:14:40 +0200728
729 h1s->status = 0;
730 h1s->meth = HTTP_METH_OTHER;
731
Christopher Faulet47365272018-10-31 17:40:50 +0100732 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
733 h1s->flags |= H1S_F_NOT_FIRST;
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100734 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC;
Christopher Faulet47365272018-10-31 17:40:50 +0100735
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200736 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
737 return h1s;
Christopher Faulete9b70722019-04-08 10:46:02 +0200738
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200739 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100740 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200741 return NULL;
742}
Christopher Fauletfeb11742018-11-29 15:12:34 +0100743
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200744static struct h1s *h1c_frt_stream_new(struct h1c *h1c)
745{
746 struct session *sess = h1c->conn->owner;
747 struct h1s *h1s;
748
749 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
750
751 h1s = h1s_new(h1c);
752 if (!h1s)
753 goto fail;
754
755 h1s->sess = sess;
756
757 if (h1c->px->options2 & PR_O2_REQBUG_OK)
758 h1s->req.err_pos = -1;
759
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200760 h1c->idle_exp = TICK_ETERNITY;
761 h1_set_idle_expiration(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +0200762 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200763 return h1s;
Christopher Faulet47365272018-10-31 17:40:50 +0100764
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200765 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100766 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200767 return NULL;
768}
769
770static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess)
771{
772 struct h1s *h1s;
773
774 TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn);
775
776 h1s = h1s_new(h1c);
777 if (!h1s)
778 goto fail;
779
Christopher Faulet10a86702021-04-07 14:18:11 +0200780 h1s->flags |= H1S_F_RX_BLK;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200781 h1s->cs = cs;
782 h1s->sess = sess;
783 cs->ctx = h1s;
784
Christopher Faulet39c7b6b2021-01-21 17:44:35 +0100785 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY;
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200786
787 if (h1c->px->options2 & PR_O2_RSPBUG_OK)
788 h1s->res.err_pos = -1;
789
Christopher Faulet60fa0512021-11-26 18:10:39 +0100790 HA_ATOMIC_INC(&h1c->px_counters->open_streams);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100791 HA_ATOMIC_INC(&h1c->px_counters->total_streams);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100792
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200793 TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s);
794 return h1s;
795
796 fail:
Christopher Faulet26a26432021-01-27 11:27:50 +0100797 TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn);
Christopher Faulet47365272018-10-31 17:40:50 +0100798 return NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200799}
800
801static void h1s_destroy(struct h1s *h1s)
802{
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803 if (h1s) {
804 struct h1c *h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200805
Christopher Faulet6b81df72019-10-01 22:08:43 +0200806 TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200807 h1c->h1s = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200808
Willy Tarreau1b0d4d12020-01-16 11:03:19 +0100809 if (h1s->subs)
810 h1s->subs->events = 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +0200811
Christopher Fauletd17ad822020-09-24 15:14:29 +0200812 h1_release_buf(h1c, &h1s->rxbuf);
813
Christopher Faulet10a86702021-04-07 14:18:11 +0200814 h1c->flags &= ~(H1C_F_WANT_SPLICE|
Christopher Fauletb385b502021-01-13 18:47:57 +0100815 H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY|
Christopher Faulet295b8d12020-09-30 17:14:55 +0200816 H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC|
817 H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
Christopher Faulet60ef12c2020-09-24 10:05:44 +0200818 if (h1s->flags & H1S_F_ERROR) {
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100819 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +0100820 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 +0200821 }
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100822
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100823 if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100824 !(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 +0100825 (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100826 h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100827 h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Fauletaaa67bc2019-12-05 10:23:37 +0100828 TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s);
829 }
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200830 else {
Christopher Faulet26a26432021-01-27 11:27:50 +0100831 TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s);
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100832 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Faulet3c82d8b2020-10-05 17:11:16 +0200833 }
Christopher Faulet60fa0512021-11-26 18:10:39 +0100834
835 HA_ATOMIC_DEC(&h1c->px_counters->open_streams);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200836 pool_free(pool_head_h1s, h1s);
837 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200838}
839
840/*
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200841 * Initialize the mux once it's attached. It is expected that conn->ctx points
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500842 * to the existing conn_stream (for outgoing connections or for incoming ones
843 * during a mux upgrade) or NULL (for incoming ones during the connection
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200844 * establishment). <input> is always used as Input buffer and may contain
845 * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on
846 * error.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200847 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200848static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess,
849 struct buffer *input)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200850{
Christopher Faulet51dbc942018-09-13 09:05:15 +0200851 struct h1c *h1c;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100852 struct task *t = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +0200853 void *conn_ctx = conn->ctx;
854
855 TRACE_ENTER(H1_EV_H1C_NEW);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200856
857 h1c = pool_alloc(pool_head_h1c);
Christopher Faulet26a26432021-01-27 11:27:50 +0100858 if (!h1c) {
859 TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200860 goto fail_h1c;
Christopher Faulet26a26432021-01-27 11:27:50 +0100861 }
Christopher Faulet51dbc942018-09-13 09:05:15 +0200862 h1c->conn = conn;
863 h1c->px = proxy;
864
Christopher Faulet3ced1d12020-11-27 16:44:01 +0100865 h1c->flags = H1C_F_ST_IDLE;
Christopher Fauletc18fc232020-10-06 17:41:36 +0200866 h1c->errcode = 0;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200867 h1c->ibuf = *input;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200868 h1c->obuf = BUF_NULL;
869 h1c->h1s = NULL;
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200870 h1c->task = NULL;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200871
Willy Tarreau90f366b2021-02-20 11:49:49 +0100872 LIST_INIT(&h1c->buf_wait.list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200873 h1c->wait_event.tasklet = tasklet_new();
874 if (!h1c->wait_event.tasklet)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200875 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200876 h1c->wait_event.tasklet->process = h1_io_cb;
877 h1c->wait_event.tasklet->context = h1c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100878 h1c->wait_event.events = 0;
Christopher Fauletdbe57792020-10-05 17:50:58 +0200879 h1c->idle_exp = TICK_ETERNITY;
Christopher Faulet51dbc942018-09-13 09:05:15 +0200880
Christopher Faulete9b70722019-04-08 10:46:02 +0200881 if (conn_is_back(conn)) {
Christopher Faulet10a86702021-04-07 14:18:11 +0200882 h1c->flags |= H1C_F_IS_BACK;
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100883 h1c->shut_timeout = h1c->timeout = proxy->timeout.server;
884 if (tick_isset(proxy->timeout.serverfin))
885 h1c->shut_timeout = proxy->timeout.serverfin;
Christopher Faulet563c3452021-11-26 17:37:51 +0100886
887 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be,
888 &h1_stats_module);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100889 } else {
890 h1c->shut_timeout = h1c->timeout = proxy->timeout.client;
891 if (tick_isset(proxy->timeout.clientfin))
892 h1c->shut_timeout = proxy->timeout.clientfin;
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200893
Christopher Faulet563c3452021-11-26 17:37:51 +0100894 h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe,
895 &h1_stats_module);
896
Amaury Denoyelled3a88c12021-05-03 10:47:51 +0200897 LIST_APPEND(&mux_stopping_data[tid].list,
898 &h1c->conn->stopping_list);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100899 }
900 if (tick_isset(h1c->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200901 t = task_new_here();
Christopher Faulet26a26432021-01-27 11:27:50 +0100902 if (!t) {
903 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 +0100904 goto fail;
Christopher Faulet26a26432021-01-27 11:27:50 +0100905 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100906
907 h1c->task = t;
908 t->process = h1_timeout_task;
909 t->context = h1c;
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200910
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100911 t->expire = tick_add(now_ms, h1c->timeout);
912 }
913
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100914 conn->ctx = h1c;
Christopher Faulet129817b2018-09-20 16:14:40 +0200915
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200916 if (h1c->flags & H1C_F_IS_BACK) {
917 /* Create a new H1S now for backend connection only */
Christopher Faulet2f0ec662020-09-24 10:30:15 +0200918 if (!h1c_bck_stream_new(h1c, conn_ctx, sess))
919 goto fail;
920 }
Christopher Faulet0f9395d2021-01-21 17:50:19 +0100921 else if (conn_ctx) {
922 /* Upgraded frontend connection (from TCP) */
923 struct conn_stream *cs = conn_ctx;
924
925 if (!h1c_frt_stream_new(h1c))
926 goto fail;
927
928 h1c->h1s->cs = cs;
929 cs->ctx = h1c->h1s;
930
931 /* Attach the CS but Not ready yet */
932 h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED;
933 TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade",
934 H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s);
935 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100936
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200937 if (t) {
938 h1_set_idle_expiration(h1c);
939 t->expire = tick_first(t->expire, h1c->idle_exp);
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100940 task_queue(t);
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200941 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +0100942
Christopher Fauletc4bfa592020-10-06 17:45:34 +0200943 /* prepare to read something */
Christopher Fauletd9ee7882021-01-21 17:45:45 +0100944 if (b_data(&h1c->ibuf))
945 tasklet_wakeup(h1c->wait_event.tasklet);
946 else if (h1_recv_allowed(h1c))
Christopher Faulet089acd52020-09-21 10:57:52 +0200947 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200948
Christopher Faulet60fa0512021-11-26 18:10:39 +0100949 HA_ATOMIC_INC(&h1c->px_counters->open_conns);
Christopher Faulet3bca28c2021-11-26 18:12:04 +0100950 HA_ATOMIC_INC(&h1c->px_counters->total_conns);
Christopher Faulet60fa0512021-11-26 18:10:39 +0100951
Christopher Faulet51dbc942018-09-13 09:05:15 +0200952 /* mux->wake will be called soon to complete the operation */
Christopher Faulet6b81df72019-10-01 22:08:43 +0200953 TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200954 return 0;
955
956 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200957 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200958 if (h1c->wait_event.tasklet)
959 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200960 pool_free(pool_head_h1c, h1c);
961 fail_h1c:
Christopher Faulet6b81df72019-10-01 22:08:43 +0200962 conn->ctx = conn_ctx; // restore saved context
963 TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR);
Christopher Faulet51dbc942018-09-13 09:05:15 +0200964 return -1;
965}
966
Christopher Faulet73c12072019-04-08 11:23:22 +0200967/* release function. This one should be called to free all resources allocated
968 * to the mux.
Christopher Faulet51dbc942018-09-13 09:05:15 +0200969 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200970static void h1_release(struct h1c *h1c)
Christopher Faulet51dbc942018-09-13 09:05:15 +0200971{
Christopher Faulet61840e72019-04-15 09:33:32 +0200972 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200973
Christopher Faulet6b81df72019-10-01 22:08:43 +0200974 TRACE_POINT(H1_EV_H1C_END);
975
Christopher Faulet51dbc942018-09-13 09:05:15 +0200976 if (h1c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200977 /* The connection must be aattached to this mux to be released */
978 if (h1c->conn && h1c->conn->ctx == h1c)
979 conn = h1c->conn;
980
Christopher Faulet6b81df72019-10-01 22:08:43 +0200981 TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn);
982
Christopher Faulet61840e72019-04-15 09:33:32 +0200983 if (conn && h1c->flags & H1C_F_UPG_H2C) {
Christopher Faulet6b81df72019-10-01 22:08:43 +0200984 TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +0200985 /* Make sure we're no longer subscribed to anything */
986 if (h1c->wait_event.events)
987 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
988 h1c->wait_event.events, &h1c->wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +0200989 if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200990 /* connection successfully upgraded to H2, this
991 * mux was already released */
992 return;
993 }
Christopher Faulet26a26432021-01-27 11:27:50 +0100994 TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn);
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200995 sess_log(conn->owner); /* Log if the upgrade failed */
996 }
997
Olivier Houchard45c44372019-06-11 14:06:23 +0200998
Willy Tarreau2b718102021-04-21 07:32:39 +0200999 if (LIST_INLIST(&h1c->buf_wait.list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01001000 LIST_DEL_INIT(&h1c->buf_wait.list);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001001
1002 h1_release_buf(h1c, &h1c->ibuf);
1003 h1_release_buf(h1c, &h1c->obuf);
1004
Christopher Fauletb8093cf2019-01-03 16:27:28 +01001005 if (h1c->task) {
1006 h1c->task->context = NULL;
1007 task_wakeup(h1c->task, TASK_WOKEN_OTHER);
1008 h1c->task = NULL;
1009 }
1010
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001011 if (h1c->wait_event.tasklet)
1012 tasklet_free(h1c->wait_event.tasklet);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001013
Christopher Fauletf2824e62018-10-01 12:12:37 +02001014 h1s_destroy(h1c->h1s);
Christopher Faulete76b4f02021-10-27 15:42:13 +02001015 if (conn) {
1016 if (h1c->wait_event.events != 0)
1017 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events,
1018 &h1c->wait_event);
1019 h1_shutw_conn(conn);
1020 }
Christopher Faulet60fa0512021-11-26 18:10:39 +01001021
1022 HA_ATOMIC_DEC(&h1c->px_counters->open_conns);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001023 pool_free(pool_head_h1c, h1c);
1024 }
1025
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001026 if (conn) {
Amaury Denoyelled3a88c12021-05-03 10:47:51 +02001027 if (!conn_is_back(conn))
1028 LIST_DEL_INIT(&conn->stopping_list);
1029
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001030 conn->mux = NULL;
1031 conn->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001032 TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02001033
Christopher Faulet39a96ee2019-04-08 10:52:21 +02001034 conn_stop_tracking(conn);
1035 conn_full_close(conn);
1036 if (conn->destroy_cb)
1037 conn->destroy_cb(conn);
1038 conn_free(conn);
1039 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001040}
1041
1042/******************************************************/
1043/* functions below are for the H1 protocol processing */
1044/******************************************************/
Christopher Faulet9768c262018-10-22 09:34:31 +02001045/* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is
1046 * greater or equal to 1.1
Christopher Fauletf2824e62018-10-01 12:12:37 +02001047 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001048static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001049{
Christopher Faulet570d1612018-11-26 11:13:57 +01001050 const char *p = HTX_SL_REQ_VPTR(sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001051
Christopher Faulet570d1612018-11-26 11:13:57 +01001052 if ((HTX_SL_REQ_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001053 (*(p + 5) > '1' ||
1054 (*(p + 5) == '1' && *(p + 7) >= '1')))
1055 h1m->flags |= H1_MF_VER_11;
1056}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001057
Christopher Faulet9768c262018-10-22 09:34:31 +02001058/* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is
1059 * greater or equal to 1.1
1060 */
Christopher Faulet570d1612018-11-26 11:13:57 +01001061static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl)
Christopher Faulet9768c262018-10-22 09:34:31 +02001062{
Christopher Faulet570d1612018-11-26 11:13:57 +01001063 const char *p = HTX_SL_RES_VPTR(sl);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001064
Christopher Faulet570d1612018-11-26 11:13:57 +01001065 if ((HTX_SL_RES_VLEN(sl) == 8) &&
Christopher Faulet9768c262018-10-22 09:34:31 +02001066 (*(p + 5) > '1' ||
1067 (*(p + 5) == '1' && *(p + 7) >= '1')))
1068 h1m->flags |= H1_MF_VER_11;
1069}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001070
Christopher Fauletf2824e62018-10-01 12:12:37 +02001071/* Deduce the connection mode of the client connection, depending on the
1072 * configuration and the H1 message flags. This function is called twice, the
1073 * first time when the request is parsed and the second time when the response
1074 * is parsed.
1075 */
1076static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
1077{
1078 struct proxy *fe = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001079
1080 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001081 /* Output direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001082 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001083 h1s->status == 101) {
1084 /* Either we've established an explicit tunnel, or we're
1085 * switching the protocol. In both cases, we're very unlikely to
1086 * understand the next protocols. We have to switch to tunnel
1087 * mode, so that we transfer the request and responses then let
1088 * this protocol pass unmodified. When we later implement
1089 * specific parsers for such protocols, we'll want to check the
1090 * Upgrade header which contains information about that protocol
1091 * for responses with status 101 (eg: see RFC2817 about TLS).
1092 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001093 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001094 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 +01001095 }
1096 else if (h1s->flags & H1S_F_WANT_KAL) {
1097 /* By default the client is in KAL mode. CLOSE mode mean
1098 * it is imposed by the client itself. So only change
1099 * KAL mode here. */
1100 if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) {
1101 /* no length known or explicit close => close */
1102 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001103 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 +01001104 }
1105 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1106 (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001107 /* no explicit keep-alive and option httpclose => close */
Christopher Fauletb992af02019-03-28 15:42:24 +01001108 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001109 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 +01001110 }
1111 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001112 }
1113 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001114 /* Input direction: first pass */
1115 if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) {
1116 /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/
Christopher Fauletf2824e62018-10-01 12:12:37 +02001117 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001118 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 +01001119 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001120 }
1121
1122 /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001123 if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001124 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001125 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);
1126 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001127}
1128
1129/* Deduce the connection mode of the client connection, depending on the
1130 * configuration and the H1 message flags. This function is called twice, the
1131 * first time when the request is parsed and the second time when the response
1132 * is parsed.
1133 */
1134static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
1135{
Olivier Houchardf502aca2018-12-14 19:42:40 +01001136 struct session *sess = h1s->sess;
Christopher Fauletb992af02019-03-28 15:42:24 +01001137 struct proxy *be = h1s->h1c->px;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001138 int fe_flags = sess ? sess->fe->options : 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001139
Christopher Fauletf2824e62018-10-01 12:12:37 +02001140 if (h1m->flags & H1_MF_RESP) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001141 /* Input direction: second pass */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001142 if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) ||
Christopher Fauletb992af02019-03-28 15:42:24 +01001143 h1s->status == 101) {
1144 /* Either we've established an explicit tunnel, or we're
1145 * switching the protocol. In both cases, we're very unlikely to
1146 * understand the next protocols. We have to switch to tunnel
1147 * mode, so that we transfer the request and responses then let
1148 * this protocol pass unmodified. When we later implement
1149 * specific parsers for such protocols, we'll want to check the
1150 * Upgrade header which contains information about that protocol
1151 * for responses with status 101 (eg: see RFC2817 about TLS).
1152 */
Christopher Fauletf2824e62018-10-01 12:12:37 +02001153 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001154 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 +01001155 }
1156 else if (h1s->flags & H1S_F_WANT_KAL) {
1157 /* By default the server is in KAL mode. CLOSE mode mean
1158 * it is imposed by haproxy itself. So only change KAL
1159 * mode here. */
1160 if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO ||
1161 !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){
1162 /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */
1163 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001164 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 +01001165 }
1166 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001167 }
Christopher Faulet70033782018-12-05 13:50:11 +01001168 else {
Christopher Fauletb992af02019-03-28 15:42:24 +01001169 /* Output direction: first pass */
1170 if (h1m->flags & H1_MF_CONN_CLO) {
1171 /* explicit close => close */
Christopher Faulet70033782018-12-05 13:50:11 +01001172 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001173 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 +01001174 }
1175 else if (!(h1m->flags & H1_MF_CONN_KAL) &&
1176 ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1177 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
1178 (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
1179 (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) {
1180 /* no explicit keep-alive option httpclose/server-close => close */
1181 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001182 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 +01001183 }
Christopher Faulet70033782018-12-05 13:50:11 +01001184 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001185
1186 /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001187 if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001188 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001189 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);
1190 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001191}
1192
Christopher Fauletb992af02019-03-28 15:42:24 +01001193static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001194{
1195 struct proxy *px = h1s->h1c->px;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001196
1197 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1198 * token is found
1199 */
1200 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001201 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001202
1203 if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001204 if (!(h1m->flags & H1_MF_VER_11)) {
1205 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 +01001206 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001207 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001208 }
1209 else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001210 if (h1m->flags & H1_MF_VER_11) {
1211 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001212 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001213 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001214 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001215}
1216
Christopher Fauletb992af02019-03-28 15:42:24 +01001217static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
Christopher Fauletf2824e62018-10-01 12:12:37 +02001218{
Christopher Fauletf2824e62018-10-01 12:12:37 +02001219 /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage"
1220 * token is found
1221 */
1222 if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG)
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001224
1225 if (h1s->flags & H1S_F_WANT_KAL) {
Christopher Fauletb992af02019-03-28 15:42:24 +01001226 if (!(h1m->flags & H1_MF_VER_11) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02001227 !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) {
1228 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 +01001229 *conn_val = ist("keep-alive");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001230 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001231 }
1232 else { /* H1S_F_WANT_CLO */
Christopher Faulet6b81df72019-10-01 22:08:43 +02001233 if (h1m->flags & H1_MF_VER_11) {
1234 TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
Christopher Fauletb992af02019-03-28 15:42:24 +01001235 *conn_val = ist("close");
Christopher Faulet6b81df72019-10-01 22:08:43 +02001236 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001237 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001238}
Christopher Fauletf2824e62018-10-01 12:12:37 +02001239
Christopher Fauletb992af02019-03-28 15:42:24 +01001240static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
Christopher Faulet9768c262018-10-22 09:34:31 +02001241{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001242 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Faulet9768c262018-10-22 09:34:31 +02001243 h1_set_cli_conn_mode(h1s, h1m);
Christopher Fauletb992af02019-03-28 15:42:24 +01001244 else
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 h1_set_srv_conn_mode(h1s, h1m);
Christopher Fauletf2824e62018-10-01 12:12:37 +02001246}
1247
Christopher Fauletb992af02019-03-28 15:42:24 +01001248static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val)
1249{
Christopher Faulet0a799aa2020-09-24 09:52:52 +02001250 if (!(h1s->h1c->flags & H1C_F_IS_BACK))
Christopher Fauletb992af02019-03-28 15:42:24 +01001251 h1_set_cli_conn_mode(h1s, h1m);
1252 else
1253 h1_set_srv_conn_mode(h1s, h1m);
1254
1255 if (!(h1m->flags & H1_MF_RESP))
1256 h1_update_req_conn_value(h1s, h1m, conn_val);
1257 else
1258 h1_update_res_conn_value(h1s, h1m, conn_val);
1259}
Christopher Faulete44769b2018-11-29 23:01:45 +01001260
Christopher Faulet98fbe952019-07-22 16:18:24 +02001261/* Try to adjust the case of the message header name using the global map
1262 * <hdrs_map>.
1263 */
1264static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name)
1265{
1266 struct ebpt_node *node;
1267 struct h1_hdr_entry *entry;
1268
1269 /* No entry in the map, do nothing */
1270 if (eb_is_empty(&hdrs_map.map))
1271 return;
1272
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001273 /* No conversion for the request headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001274 if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV))
1275 return;
1276
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001277 /* No conversion for the response headers */
Christopher Faulet98fbe952019-07-22 16:18:24 +02001278 if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI))
1279 return;
1280
1281 node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len);
1282 if (!node)
1283 return;
1284 entry = container_of(node, struct h1_hdr_entry, node);
1285 name->ptr = entry->name.ptr;
1286 name->len = entry->name.len;
1287}
1288
Christopher Faulete44769b2018-11-29 23:01:45 +01001289/* Append the description of what is present in error snapshot <es> into <out>.
1290 * The description must be small enough to always fit in a buffer. The output
1291 * buffer may be the trash so the trash must not be used inside this function.
1292 */
1293static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
1294{
1295 chunk_appendf(out,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001296 " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n"
1297 " H1 msg state %s(%d), H1 msg flags 0x%08x\n"
1298 " H1 chunk len %lld bytes, H1 body len %lld bytes :\n",
1299 es->ctx.h1.c_flags, es->ctx.h1.s_flags,
1300 h1m_state_str(es->ctx.h1.state), es->ctx.h1.state,
1301 es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen);
Christopher Faulete44769b2018-11-29 23:01:45 +01001302}
1303/*
1304 * Capture a bad request or response and archive it in the proxy's structure.
1305 * By default it tries to report the error position as h1m->err_pos. However if
1306 * this one is not set, it will then report h1m->next, which is the last known
1307 * parsing point. The function is able to deal with wrapping buffers. It always
1308 * displays buffers as a contiguous area starting at buf->p. The direction is
1309 * determined thanks to the h1m's flags.
1310 */
1311static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
1312 struct h1m *h1m, struct buffer *buf)
1313{
Christopher Fauletdb2c17d2020-10-15 17:19:46 +02001314 struct session *sess = h1s->sess;
Christopher Faulete44769b2018-11-29 23:01:45 +01001315 struct proxy *proxy = h1c->px;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001316 struct proxy *other_end;
Christopher Faulete44769b2018-11-29 23:01:45 +01001317 union error_snapshot_ctx ctx;
1318
Christopher Faulet3ced1d12020-11-27 16:44:01 +01001319 if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001320 if (sess == NULL)
1321 sess = si_strm(h1s->cs->data)->sess;
1322 if (!(h1m->flags & H1_MF_RESP))
1323 other_end = si_strm(h1s->cs->data)->be;
1324 else
1325 other_end = sess->fe;
1326 } else
1327 other_end = NULL;
Christopher Faulete44769b2018-11-29 23:01:45 +01001328
1329 /* http-specific part now */
1330 ctx.h1.state = h1m->state;
1331 ctx.h1.c_flags = h1c->flags;
1332 ctx.h1.s_flags = h1s->flags;
1333 ctx.h1.m_flags = h1m->flags;
1334 ctx.h1.m_clen = h1m->curr_len;
1335 ctx.h1.m_blen = h1m->body_len;
1336
1337 proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end,
1338 h1c->conn->target, sess, buf, 0, 0,
Christopher Fauletaa75b3d2018-12-05 16:20:40 +01001339 (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next,
1340 &ctx, h1_show_error_snapshot);
Christopher Faulete44769b2018-11-29 23:01:45 +01001341}
1342
Christopher Fauletadb22202018-12-12 10:32:09 +01001343/* Emit the chunksize followed by a CRLF in front of data of the buffer
1344 * <buf>. It goes backwards and starts with the byte before the buffer's
1345 * head. The caller is responsible for ensuring there is enough room left before
1346 * the buffer's head for the string.
1347 */
1348static void h1_emit_chunk_size(struct buffer *buf, size_t chksz)
1349{
1350 char *beg, *end;
1351
1352 beg = end = b_head(buf);
1353 *--beg = '\n';
1354 *--beg = '\r';
1355 do {
1356 *--beg = hextab[chksz & 0xF];
1357 } while (chksz >>= 4);
1358 buf->head -= (end - beg);
1359 b_add(buf, end - beg);
1360}
1361
1362/* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for
1363 * ensuring there is enough room left in the buffer for the string. */
1364static void h1_emit_chunk_crlf(struct buffer *buf)
1365{
1366 *(b_peek(buf, b_data(buf))) = '\r';
1367 *(b_peek(buf, b_data(buf) + 1)) = '\n';
1368 b_add(buf, 2);
1369}
1370
Christopher Faulet129817b2018-09-20 16:14:40 +02001371/*
Christopher Faulet5be651d2021-01-22 15:28:03 +01001372 * Switch the stream to tunnel mode. This function must only be called on 2xx
1373 * (successful) replies to CONNECT requests or on 101 (switching protocol).
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001374 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01001375static void h1_set_tunnel_mode(struct h1s *h1s)
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001376{
Christopher Faulet5be651d2021-01-22 15:28:03 +01001377 struct h1c *h1c = h1s->h1c;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001378
Christopher Faulet5be651d2021-01-22 15:28:03 +01001379 h1s->req.state = H1_MSG_TUNNEL;
1380 h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001381
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001382 h1s->res.state = H1_MSG_TUNNEL;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001383 h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK);
Christopher Fauletf3158e92019-11-15 11:14:23 +01001384
Christopher Faulet5be651d2021-01-22 15:28:03 +01001385 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 +01001386
Christopher Faulet10a86702021-04-07 14:18:11 +02001387 if (h1s->flags & H1S_F_RX_BLK) {
1388 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001389 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001390 TRACE_STATE("Re-enable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001391 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001392 if (h1s->flags & H1S_F_TX_BLK) {
1393 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001394 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001395 TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001396 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001397}
1398
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001399/* Search for a websocket key header. The message should have been identified
1400 * as a valid websocket handshake.
Amaury Denoyellec1938232020-12-11 17:53:03 +01001401 *
1402 * On the request side, if found the key is stored in the session. It might be
1403 * needed to calculate response key if the server side is using http/2.
1404 *
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001405 * On the response side, the key might be verified if haproxy has been
1406 * responsible for the generation of a key. This happens when a h2 client is
1407 * interfaced with a h1 server.
1408 *
1409 * Returns 0 if no key found or invalid key
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001410 */
1411static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx)
1412{
1413 struct htx_blk *blk;
1414 enum htx_blk_type type;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001415 struct ist n, v;
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001416 int ws_key_found = 0, idx;
1417
1418 idx = htx_get_head(htx); // returns the SL that we skip
1419 while ((idx = htx_get_next(htx, idx)) != -1) {
1420 blk = htx_get_blk(htx, idx);
1421 type = htx_get_blk_type(blk);
1422
1423 if (type == HTX_BLK_UNUSED)
1424 continue;
1425
1426 if (type != HTX_BLK_HDR)
1427 break;
1428
1429 n = htx_get_blk_name(htx, blk);
Amaury Denoyellec1938232020-12-11 17:53:03 +01001430 v = htx_get_blk_value(htx, blk);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001431
Amaury Denoyellec1938232020-12-11 17:53:03 +01001432 /* Websocket key is base64 encoded of 16 bytes */
1433 if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 &&
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001434 !(h1m->flags & H1_MF_RESP)) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01001435 /* Copy the key on request side
1436 * we might need it if the server is using h2 and does
1437 * not provide the response
1438 */
1439 memcpy(h1s->ws_key, v.ptr, 24);
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001440 ws_key_found = 1;
1441 break;
1442 }
1443 else if (isteqi(n, ist("sec-websocket-accept")) &&
1444 h1m->flags & H1_MF_RESP) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01001445 /* Need to verify the response key if the input was
1446 * generated by haproxy
1447 */
1448 if (h1s->ws_key[0]) {
1449 char key[29];
1450 h1_calculate_ws_output_key(h1s->ws_key, key);
1451 if (!isteqi(ist(key), v))
1452 break;
1453 }
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001454 ws_key_found = 1;
1455 break;
1456 }
1457 }
1458
1459 /* missing websocket key, reject the message */
1460 if (!ws_key_found) {
1461 htx->flags |= HTX_FL_PARSING_ERROR;
1462 return 0;
1463 }
1464
1465 return 1;
1466}
1467
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001468/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001469 * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
Christopher Fauletf2824e62018-10-01 12:12:37 +02001470 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
Christopher Faulet46e058d2021-09-20 07:47:27 +02001471 * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on
1472 * the function http_parse_msg_hdrs() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001473 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001474static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1475 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet129817b2018-09-20 16:14:40 +02001476{
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001477 union h1_sl h1sl;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001478 int ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001479
Willy Tarreau022e5e52020-09-10 09:33:15 +02001480 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 +02001481
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001482 if (h1s->meth == HTTP_METH_CONNECT)
1483 h1m->flags |= H1_MF_METH_CONNECT;
1484 if (h1s->meth == HTTP_METH_HEAD)
1485 h1m->flags |= H1_MF_METH_HEAD;
Christopher Faulet0ef372a2019-04-08 10:57:20 +02001486
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001487 ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001488 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001489 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001490 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001491 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001492 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 +02001493 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1494 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001495 else if (ret == -2) {
1496 TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1497 h1s->flags |= H1S_F_RX_CONGESTED;
1498 }
1499 ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001500 goto end;
1501 }
1502
Christopher Faulete136bd12021-09-21 18:44:55 +02001503
1504 /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload. There is a
1505 * payload if the c-l is not null or the the payload is chunk-encoded.
1506 * A parsing error is reported but a A 413-Payload-Too-Large is returned
1507 * instead of a 400-Bad-Request.
1508 */
1509 if (!(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) &&
1510 (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) &&
1511 (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) {
1512 h1s->flags |= H1S_F_PARSING_ERROR;
1513 htx->flags |= HTX_FL_PARSING_ERROR;
1514 h1s->h1c->errcode = 413;
1515 TRACE_ERROR("HTTP/1.0 GET/HEAD/DELETE request with a payload forbidden", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1516 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1517 ret = 0;
1518 goto end;
1519 }
1520
Christopher Fauletda3adeb2021-09-28 09:50:07 +02001521 /* Reject any message with an unknown transfer-encoding. In fact if any
1522 * encoding other than "chunked". A 422-Unprocessable-Content is
1523 * returned for an invalid request, a 502-Bad-Gateway for an invalid
1524 * response.
1525 */
1526 if (h1m->flags & H1_MF_TE_OTHER) {
1527 h1s->flags |= H1S_F_PARSING_ERROR;
1528 htx->flags |= HTX_FL_PARSING_ERROR;
1529 if (!(h1m->flags & H1_MF_RESP))
1530 h1s->h1c->errcode = 422;
1531 TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s);
1532 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1533 ret = 0;
1534 goto end;
1535 }
1536
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001537 /* If websocket handshake, search for the websocket key */
1538 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) ==
1539 (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) {
1540 int ws_ret = h1_search_websocket_key(h1s, h1m, htx);
1541 if (!ws_ret) {
Amaury Denoyelle18ee5c32020-12-11 17:53:02 +01001542 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001543 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 +01001544 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1545
1546 ret = 0;
1547 goto end;
1548 }
1549 }
1550
Christopher Faulet486498c2019-10-11 14:22:00 +02001551 if (h1m->err_pos >= 0) {
1552 /* Maybe we found an error during the parsing while we were
1553 * configured not to block on that, so we have to capture it
1554 * now.
1555 */
1556 TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s);
1557 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1558 }
1559
Christopher Faulet5696f542020-12-02 16:08:38 +01001560 if (!(h1m->flags & H1_MF_RESP)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001561 h1s->meth = h1sl.rq.meth;
Christopher Faulet5696f542020-12-02 16:08:38 +01001562 if (h1s->meth == HTTP_METH_HEAD)
1563 h1s->flags |= H1S_F_BODYLESS_RESP;
1564 }
1565 else {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001566 h1s->status = h1sl.st.status;
Christopher Faulet5696f542020-12-02 16:08:38 +01001567 if (h1s->status == 204 || h1s->status == 304)
1568 h1s->flags |= H1S_F_BODYLESS_RESP;
1569 }
Christopher Fauletb992af02019-03-28 15:42:24 +01001570 h1_process_input_conn_mode(h1s, h1m, htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001571 *ofs += ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001572
Christopher Faulet129817b2018-09-20 16:14:40 +02001573 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001574 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 +02001575 return ret;
1576}
1577
1578/*
Christopher Fauletf2824e62018-10-01 12:12:37 +02001579 * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001580 * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag.
1581 * If relies on the function http_parse_msg_data() to do the parsing.
Christopher Faulet129817b2018-09-20 16:14:40 +02001582 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001583static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx,
1584 struct buffer *buf, size_t *ofs, size_t max,
1585 struct buffer *htxbuf)
Christopher Faulet129817b2018-09-20 16:14:40 +02001586{
Christopher Fauletde471a42021-02-01 16:37:28 +01001587 size_t ret;
Christopher Faulet30db3d72019-05-17 15:35:33 +02001588
Willy Tarreau022e5e52020-09-10 09:33:15 +02001589 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 +02001590 ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf);
Christopher Faulet02a02532019-11-15 09:36:28 +01001591 if (!ret) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001592 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 +01001593 if ((*htx)->flags & HTX_FL_PARSING_ERROR) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001594 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001595 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 +02001596 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001597 }
Christopher Faulet02a02532019-11-15 09:36:28 +01001598 goto end;
Christopher Faulet129817b2018-09-20 16:14:40 +02001599 }
1600
Christopher Faulet02a02532019-11-15 09:36:28 +01001601 *ofs += ret;
1602
1603 end:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001604 if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) {
1605 TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1606 h1s->flags |= H1S_F_RX_CONGESTED;
1607 }
1608
Willy Tarreau022e5e52020-09-10 09:33:15 +02001609 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 +02001610 return ret;
Christopher Faulet129817b2018-09-20 16:14:40 +02001611}
1612
1613/*
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001614 * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
1615 * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR
1616 * flag and filling h1s->err_pos and h1s->err_state fields. This functions is
Christopher Faulet46e058d2021-09-20 07:47:27 +02001617 * responsible to update the parser state <h1m>. If more room is requested,
1618 * H1S_F_RX_CONGESTED flag is set.
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001619 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001620static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx,
1621 struct buffer *buf, size_t *ofs, size_t max)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001622{
Christopher Faulet46e058d2021-09-20 07:47:27 +02001623 int ret;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001624
Willy Tarreau022e5e52020-09-10 09:33:15 +02001625 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 +02001626 ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001627 if (ret <= 0) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001628 TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s);
Christopher Faulet46e058d2021-09-20 07:47:27 +02001629 if (ret == -1) {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001630 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001631 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 +02001632 h1_capture_bad_message(h1s->h1c, h1s, h1m, buf);
1633 }
Christopher Faulet46e058d2021-09-20 07:47:27 +02001634 else if (ret == -2) {
1635 TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s);
1636 h1s->flags |= H1S_F_RX_CONGESTED;
1637 }
1638 ret = 0;
Christopher Faulet02a02532019-11-15 09:36:28 +01001639 goto end;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001640 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02001641
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001642 *ofs += ret;
Christopher Faulet02a02532019-11-15 09:36:28 +01001643
1644 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02001645 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 +02001646 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001647}
1648
1649/*
Christopher Faulet129817b2018-09-20 16:14:40 +02001650 * Process incoming data. It parses data and transfer them from h1c->ibuf into
Christopher Faulet539e0292018-11-19 10:40:09 +01001651 * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if
1652 * it couldn't proceed.
Christopher Faulet46e058d2021-09-20 07:47:27 +02001653 *
1654 * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data.
Christopher Faulet129817b2018-09-20 16:14:40 +02001655 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001656static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001657{
Christopher Faulet539e0292018-11-19 10:40:09 +01001658 struct h1s *h1s = h1c->h1s;
Christopher Faulet129817b2018-09-20 16:14:40 +02001659 struct h1m *h1m;
Christopher Faulet9768c262018-10-22 09:34:31 +02001660 struct htx *htx;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001661 size_t data;
1662 size_t ret = 0;
Christopher Faulet129817b2018-09-20 16:14:40 +02001663 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001664
Christopher Faulet539e0292018-11-19 10:40:09 +01001665 htx = htx_from_buf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001666 TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count});
Willy Tarreau3a6190f2018-12-16 08:29:56 +01001667
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001668 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet74027762019-02-26 14:45:05 +01001669 data = htx->data;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001670
Christopher Faulet2eed8002020-12-07 11:26:13 +01001671 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR))
Christopher Faulet0e54d542019-07-04 21:22:34 +02001672 goto end;
1673
Christopher Faulet10a86702021-04-07 14:18:11 +02001674 if (h1s->flags & H1S_F_RX_BLK)
Christopher Fauletec4207c2021-04-08 18:34:30 +02001675 goto out;
Christopher Faulet5be651d2021-01-22 15:28:03 +01001676
Christopher Faulet46e058d2021-09-20 07:47:27 +02001677 /* Always remove congestion flags and try to process more input data */
1678 h1s->flags &= ~H1S_F_RX_CONGESTED;
1679
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01001680 do {
Christopher Faulet30db3d72019-05-17 15:35:33 +02001681 size_t used = htx_used_space(htx);
1682
Christopher Faulet129817b2018-09-20 16:14:40 +02001683 if (h1m->state <= H1_MSG_LAST_LF) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001684 TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001685 ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet129817b2018-09-20 16:14:40 +02001686 if (!ret)
1687 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001688
1689 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"),
1690 H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret});
1691
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001692 if ((h1m->flags & H1_MF_RESP) &&
1693 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
1694 h1m_init_res(&h1s->res);
1695 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001696 TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001697 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001698 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001699 else if (h1m->state < H1_MSG_TRAILERS) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001700 TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001701 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001702 if (h1m->state < H1_MSG_TRAILERS)
Christopher Faulet129817b2018-09-20 16:14:40 +02001703 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001704
1705 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"),
1706 H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet129817b2018-09-20 16:14:40 +02001707 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001708 else if (h1m->state == H1_MSG_TRAILERS) {
Christopher Faulet76014fd2019-12-10 11:47:22 +01001709 TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001710 ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count);
Christopher Faulet16a524c2021-02-02 21:16:03 +01001711 if (h1m->state != H1_MSG_DONE)
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001712 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001713
Christopher Faulet76014fd2019-12-10 11:47:22 +01001714 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"),
1715 H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001716 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001717 else if (h1m->state == H1_MSG_DONE) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001718 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"),
1719 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx);
Christopher Faulet76014fd2019-12-10 11:47:22 +01001720
Christopher Faulet5be651d2021-01-22 15:28:03 +01001721 if ((h1m->flags & H1_MF_RESP) &&
1722 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101))
1723 h1_set_tunnel_mode(h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001724 else {
1725 if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) {
1726 /* Unfinished transaction: block this input side waiting the end of the output side */
Christopher Faulet10a86702021-04-07 14:18:11 +02001727 h1s->flags |= H1S_F_RX_BLK;
1728 TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001729 }
Christopher Faulet10a86702021-04-07 14:18:11 +02001730 if (h1s->flags & H1S_F_TX_BLK) {
1731 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001732 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001733 TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001734 }
Christopher Faulet23021ad2020-07-10 10:01:26 +02001735 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001736 }
Christopher Fauletcb55f482018-12-10 11:56:47 +01001737 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02001738 else if (h1m->state == H1_MSG_TUNNEL) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02001739 TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001740 ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf);
Christopher Faulet9768c262018-10-22 09:34:31 +02001741 if (!ret)
1742 break;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001743
1744 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"),
1745 H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Fauletf2824e62018-10-01 12:12:37 +02001746 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001747 else {
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001748 h1s->flags |= H1S_F_PARSING_ERROR;
Christopher Faulet129817b2018-09-20 16:14:40 +02001749 break;
1750 }
1751
Christopher Faulet30db3d72019-05-17 15:35:33 +02001752 count -= htx_used_space(htx) - used;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001753 } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR|H1S_F_RX_BLK|H1S_F_RX_CONGESTED)));
Christopher Faulet5be651d2021-01-22 15:28:03 +01001754
Christopher Faulet129817b2018-09-20 16:14:40 +02001755
Christopher Faulet2eed8002020-12-07 11:26:13 +01001756 if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) {
Christopher Faulet26a26432021-01-27 11:27:50 +01001757 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 +02001758 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001759 }
Christopher Faulet129817b2018-09-20 16:14:40 +02001760
Christopher Faulet539e0292018-11-19 10:40:09 +01001761 b_del(&h1c->ibuf, total);
1762
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001763 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001764 TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
1765
Christopher Faulet30db3d72019-05-17 15:35:33 +02001766 ret = htx->data - data;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001767 if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet539e0292018-11-19 10:40:09 +01001768 h1c->flags &= ~H1C_F_IN_FULL;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001769 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 +01001770 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet47365272018-10-31 17:40:50 +01001771 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001772
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001773 if (!b_data(&h1c->ibuf))
1774 h1_release_buf(h1c, &h1c->ibuf);
1775
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01001776 if (!(h1c->flags & H1C_F_ST_READY)) {
1777 /* The H1 connection is not ready. Most of time, there is no CS
1778 * attached, except for TCP>H1 upgrade, from a TCP frontend. In both
1779 * cases, it is only possible on the client side.
1780 */
1781 BUG_ON(h1c->flags & H1C_F_IS_BACK);
1782
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001783 if (h1m->state <= H1_MSG_LAST_LF) {
1784 TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s);
1785 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
1786 goto end;
1787 }
1788
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001789 if (!(h1c->flags & H1C_F_ST_ATTACHED)) {
1790 TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s);
1791 BUG_ON(h1s->cs);
1792 if (!h1s_new_cs(h1s, buf)) {
1793 h1c->flags |= H1C_F_ST_ERROR;
1794 goto err;
1795 }
1796 }
1797 else {
1798 TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s);
1799 BUG_ON(h1s->cs == NULL);
1800 if (!h1s_upgrade_cs(h1s, buf)) {
1801 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001802 TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01001803 goto err;
1804 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001805 }
1806 }
1807
1808 /* Here h1s->cs is always defined */
Christopher Fauletf5ce3202021-11-26 17:26:19 +01001809 if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) {
Christopher Fauleta583af62020-09-24 15:35:37 +02001810 TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1811 h1s->cs->flags |= CS_FL_MAY_SPLICE;
1812 }
1813 else {
1814 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s);
1815 h1s->cs->flags &= ~CS_FL_MAY_SPLICE;
1816 }
1817
Christopher Fauleta22782b2021-02-08 17:18:01 +01001818 /* Set EOI on conn-stream in DONE state iff:
1819 * - it is a response
1820 * - it is a request but no a protocol upgrade nor a CONNECT
1821 *
1822 * If not set, Wait the response to do so or not depending on the status
Christopher Faulet5be651d2021-01-22 15:28:03 +01001823 * code.
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001824 */
Christopher Fauleta22782b2021-02-08 17:18:01 +01001825 if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) ||
1826 ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG)))
Christopher Fauleta583af62020-09-24 15:35:37 +02001827 h1s->cs->flags |= CS_FL_EOI;
1828
Christopher Fauletec4207c2021-04-08 18:34:30 +02001829 out:
Christopher Faulet46e058d2021-09-20 07:47:27 +02001830 /* When Input data are pending for this message, notify upper layer that
1831 * the mux need more space in the HTX buffer to continue if :
1832 *
1833 * - The parser is blocked in MSG_DATA or MSG_TUNNEL state
1834 * - Headers or trailers are pending to be copied.
1835 */
1836 if (h1s->flags & (H1S_F_RX_CONGESTED)) {
Christopher Fauletcf56b992018-12-11 16:12:31 +01001837 h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM;
Christopher Faulet46e058d2021-09-20 07:47:27 +02001838 TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
1839 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001840 else {
1841 h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1842 if (h1s->flags & H1S_F_REOS) {
1843 h1s->cs->flags |= CS_FL_EOS;
Christopher Faulet1e857782020-12-08 10:38:22 +01001844 if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) {
1845 /* DONE or TUNNEL or SHUTR without XFER_LEN, set
1846 * EOI on the conn-stream */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001847 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet3e1748b2020-12-07 18:21:27 +01001848 }
Christopher Faulet26a26432021-01-27 11:27:50 +01001849 else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001850 h1s->cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01001851 TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s);
1852 }
Christopher Fauletb385b502021-01-13 18:47:57 +01001853
Christopher Faulet10a86702021-04-07 14:18:11 +02001854 if (h1s->flags & H1S_F_TX_BLK) {
1855 h1s->flags &= ~H1S_F_TX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001856 h1_wake_stream_for_send(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001857 TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletb385b502021-01-13 18:47:57 +01001858 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001859 }
Christopher Faulet539e0292018-11-19 10:40:09 +01001860 }
Christopher Fauletf6ce9d62018-12-10 15:30:06 +01001861
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001862 end:
Christopher Faulet6b81df72019-10-01 22:08:43 +02001863 TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret});
Christopher Faulet30db3d72019-05-17 15:35:33 +02001864 return ret;
Christopher Faulet47365272018-10-31 17:40:50 +01001865
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001866 err:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001867 htx_to_buf(htx, buf);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02001868 if (h1s->cs)
1869 h1s->cs->flags |= CS_FL_EOI;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001870 TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02001871 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001872}
1873
Christopher Faulet129817b2018-09-20 16:14:40 +02001874/*
1875 * Process outgoing data. It parses data and transfer them from the channel buffer into
1876 * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or
1877 * 0 if it couldn't proceed.
1878 */
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01001879static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count)
Christopher Faulet51dbc942018-09-13 09:05:15 +02001880{
Christopher Faulet129817b2018-09-20 16:14:40 +02001881 struct h1s *h1s = h1c->h1s;
1882 struct h1m *h1m;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001883 struct htx *chn_htx = NULL;
Christopher Faulet9768c262018-10-22 09:34:31 +02001884 struct htx_blk *blk;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001885 struct buffer tmp;
Christopher Faulet129817b2018-09-20 16:14:40 +02001886 size_t total = 0;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001887 int last_data = 0;
Amaury Denoyellec1938232020-12-11 17:53:03 +01001888 int ws_key_found = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02001889
Christopher Faulet94b2c762019-05-24 15:28:57 +02001890 chn_htx = htxbuf(buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02001891 TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count});
1892
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001893 if (htx_is_empty(chn_htx))
1894 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02001895
Christopher Faulet10a86702021-04-07 14:18:11 +02001896 if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK))
Christopher Fauletdea24742021-01-22 15:12:30 +01001897 goto end;
1898
Christopher Faulet51dbc942018-09-13 09:05:15 +02001899 if (!h1_get_buf(h1c, &h1c->obuf)) {
1900 h1c->flags |= H1C_F_OUT_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02001901 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 +02001902 goto end;
1903 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02001904
Christopher Faulet60ef12c2020-09-24 10:05:44 +02001905 h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet9768c262018-10-22 09:34:31 +02001906
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001907 /* the htx is non-empty thus has at least one block */
Willy Tarreau3815b222018-12-11 19:50:43 +01001908 blk = htx_get_head_blk(chn_htx);
Christopher Faulet9768c262018-10-22 09:34:31 +02001909
Willy Tarreau3815b222018-12-11 19:50:43 +01001910 /* Perform some optimizations to reduce the number of buffer copies.
1911 * First, if the mux's buffer is empty and the htx area contains
1912 * exactly one data block of the same size as the requested count,
1913 * then it's possible to simply swap the caller's buffer with the
1914 * mux's output buffer and adjust offsets and length to match the
1915 * entire DATA HTX block in the middle. In this case we perform a
1916 * true zero-copy operation from end-to-end. This is the situation
1917 * that happens all the time with large files. Second, if this is not
1918 * possible, but the mux's output buffer is empty, we still have an
1919 * opportunity to avoid the copy to the intermediary buffer, by making
1920 * the intermediary buffer's area point to the output buffer's area.
1921 * In this case we want to skip the HTX header to make sure that copies
1922 * remain aligned and that this operation remains possible all the
1923 * time. This goes for headers, data blocks and any data extracted from
1924 * the HTX blocks.
Willy Tarreauc5efa332018-12-05 11:19:27 +01001925 */
1926 if (!b_data(&h1c->obuf)) {
Christopher Fauletdea24742021-01-22 15:12:30 +01001927 if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) &&
Christopher Faulet0d7e6342021-02-08 15:56:36 +01001928 (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) &&
Christopher Fauletdea24742021-01-22 15:12:30 +01001929 htx_nbblks(chn_htx) == 1 &&
Willy Tarreau37dd54d2018-12-15 14:48:31 +01001930 htx_get_blk_type(blk) == HTX_BLK_DATA &&
Willy Tarreau3815b222018-12-11 19:50:43 +01001931 htx_get_blk_value(chn_htx, blk).len == count) {
Christopher Faulete5596bf2020-12-02 16:13:22 +01001932 void *old_area;
1933
Christopher Faulet6b81df72019-10-01 22:08:43 +02001934 TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count});
Christopher Faulet140f1a52021-11-26 16:37:55 +01001935 if (h1m->state == H1_MSG_DATA) {
1936 if (h1m->flags & H1_MF_CLEN) {
1937 if (count > h1m->curr_len) {
1938 TRACE_ERROR("too much payload, more than announced",
1939 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
1940 goto error;
1941 }
1942 h1m->curr_len -= count;
1943 }
1944 if (chn_htx->flags & HTX_FL_EOM) {
1945 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
1946 last_data = 1;
1947 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001948 }
1949
Christopher Faulete5596bf2020-12-02 16:13:22 +01001950 old_area = h1c->obuf.area;
Willy Tarreau3815b222018-12-11 19:50:43 +01001951 h1c->obuf.area = buf->area;
Christopher Fauletc2518a52019-06-25 21:41:02 +02001952 h1c->obuf.head = sizeof(struct htx) + blk->addr;
Willy Tarreau3815b222018-12-11 19:50:43 +01001953 h1c->obuf.data = count;
1954
1955 buf->area = old_area;
1956 buf->data = buf->head = 0;
Christopher Fauletadb22202018-12-12 10:32:09 +01001957
Christopher Faulet6b81df72019-10-01 22:08:43 +02001958 chn_htx = (struct htx *)buf->area;
1959 htx_reset(chn_htx);
1960
Christopher Fauletadb22202018-12-12 10:32:09 +01001961 /* The message is chunked. We need to emit the chunk
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001962 * size and eventually the last chunk. We have at least
1963 * the size of the struct htx to write the chunk
1964 * envelope. It should be enough.
Christopher Fauletadb22202018-12-12 10:32:09 +01001965 */
1966 if (h1m->flags & H1_MF_CHNK) {
1967 h1_emit_chunk_size(&h1c->obuf, count);
1968 h1_emit_chunk_crlf(&h1c->obuf);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001969 if (last_data) {
1970 /* Emit the last chunk too at the buffer's end */
1971 b_putblk(&h1c->obuf, "0\r\n\r\n", 5);
1972 }
Christopher Fauletadb22202018-12-12 10:32:09 +01001973 }
1974
Christopher Faulet6b81df72019-10-01 22:08:43 +02001975 if (h1m->state == H1_MSG_DATA)
1976 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001977 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02001978 else
1979 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02001980 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001981
Christopher Faulete5596bf2020-12-02 16:13:22 +01001982 total += count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001983 if (last_data) {
1984 h1m->state = H1_MSG_DONE;
Christopher Faulet10a86702021-04-07 14:18:11 +02001985 if (h1s->flags & H1S_F_RX_BLK) {
1986 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02001987 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02001988 TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001989 }
1990
1991 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
1992 H1_EV_TX_DATA, h1c->conn, h1s);
1993 }
Willy Tarreau3815b222018-12-11 19:50:43 +01001994 goto out;
1995 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001996 tmp.area = h1c->obuf.area + h1c->obuf.head;
Willy Tarreauc5efa332018-12-05 11:19:27 +01001997 }
Christopher Fauletc2518a52019-06-25 21:41:02 +02001998 else
1999 tmp.area = trash.area;
Christopher Faulet9768c262018-10-22 09:34:31 +02002000
Christopher Fauletc2518a52019-06-25 21:41:02 +02002001 tmp.data = 0;
2002 tmp.size = b_room(&h1c->obuf);
Christopher Faulet10a86702021-04-07 14:18:11 +02002003 while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) {
Christopher Faulet570d1612018-11-26 11:13:57 +01002004 struct htx_sl *sl;
Christopher Faulet9768c262018-10-22 09:34:31 +02002005 struct ist n, v;
Christopher Fauletb2e84162018-12-06 11:39:49 +01002006 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9768c262018-10-22 09:34:31 +02002007 uint32_t sz = htx_get_blksz(blk);
Christopher Fauletd9233f02019-10-14 14:01:24 +02002008 uint32_t vlen, chklen;
Christopher Faulet9768c262018-10-22 09:34:31 +02002009
Christopher Fauletb2e84162018-12-06 11:39:49 +01002010 vlen = sz;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002011 if (type != HTX_BLK_DATA && vlen > count)
2012 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002013
Christopher Faulet94b2c762019-05-24 15:28:57 +02002014 if (type == HTX_BLK_UNUSED)
2015 goto nextblk;
Christopher Faulet9768c262018-10-22 09:34:31 +02002016
Christopher Faulet94b2c762019-05-24 15:28:57 +02002017 switch (h1m->state) {
2018 case H1_MSG_RQBEFORE:
2019 if (type != HTX_BLK_REQ_SL)
2020 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002021 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 +02002022 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002023 h1s->meth = sl->info.req.meth;
Christopher Faulet9768c262018-10-22 09:34:31 +02002024 h1_parse_req_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002025 if (!h1_format_htx_reqline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002026 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002027 h1m->flags |= H1_MF_XFER_LEN;
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002028 if (sl->flags & HTX_SL_F_BODYLESS)
2029 h1m->flags |= H1_MF_CLEN;
Christopher Faulet9768c262018-10-22 09:34:31 +02002030 h1m->state = H1_MSG_HDR_FIRST;
Christopher Faulet5696f542020-12-02 16:08:38 +01002031 if (h1s->meth == HTTP_METH_HEAD)
2032 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet10a86702021-04-07 14:18:11 +02002033 if (h1s->flags & H1S_F_RX_BLK) {
2034 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002035 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002036 TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Faulet089acd52020-09-21 10:57:52 +02002037 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002038 break;
Christopher Faulet129817b2018-09-20 16:14:40 +02002039
Christopher Faulet94b2c762019-05-24 15:28:57 +02002040 case H1_MSG_RPBEFORE:
2041 if (type != HTX_BLK_RES_SL)
2042 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002043 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 +02002044 sl = htx_get_blk_ptr(chn_htx, blk);
Christopher Faulet570d1612018-11-26 11:13:57 +01002045 h1s->status = sl->info.res.status;
Christopher Faulet9768c262018-10-22 09:34:31 +02002046 h1_parse_res_vsn(h1m, sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002047 if (!h1_format_htx_stline(sl, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002048 goto full;
Christopher Faulet03599112018-11-27 11:21:21 +01002049 if (sl->flags & HTX_SL_F_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002050 h1m->flags |= H1_MF_XFER_LEN;
Christopher Fauletf3e76192020-12-02 16:46:33 +01002051 if (h1s->status < 200)
Christopher Fauletada34b62019-05-24 16:36:43 +02002052 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Faulet5696f542020-12-02 16:08:38 +01002053 else if (h1s->status == 204 || h1s->status == 304)
2054 h1s->flags |= H1S_F_BODYLESS_RESP;
Christopher Faulet9768c262018-10-22 09:34:31 +02002055 h1m->state = H1_MSG_HDR_FIRST;
2056 break;
2057
Christopher Faulet94b2c762019-05-24 15:28:57 +02002058 case H1_MSG_HDR_FIRST:
2059 case H1_MSG_HDR_NAME:
2060 case H1_MSG_HDR_L2_LWS:
2061 if (type == HTX_BLK_EOH)
2062 goto last_lf;
2063 if (type != HTX_BLK_HDR)
2064 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002065
Christopher Faulet9768c262018-10-22 09:34:31 +02002066 h1m->state = H1_MSG_HDR_NAME;
2067 n = htx_get_blk_name(chn_htx, blk);
2068 v = htx_get_blk_value(chn_htx, blk);
2069
Christopher Faulet86d144c2019-08-14 16:32:25 +02002070 /* Skip all pseudo-headers */
2071 if (*(n.ptr) == ':')
2072 goto skip_hdr;
2073
Christopher Faulet91fcf212020-12-02 16:17:15 +01002074 if (isteq(n, ist("transfer-encoding"))) {
2075 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2076 goto skip_hdr;
Christopher Faulet9768c262018-10-22 09:34:31 +02002077 h1_parse_xfer_enc_header(h1m, v);
Christopher Faulet91fcf212020-12-02 16:17:15 +01002078 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002079 else if (isteq(n, ist("content-length"))) {
Christopher Faulet91fcf212020-12-02 16:17:15 +01002080 if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204))
2081 goto skip_hdr;
Christopher Faulet5220ef22019-03-27 15:44:56 +01002082 /* Only skip C-L header with invalid value. */
2083 if (h1_parse_cont_len_header(h1m, &v) < 0)
Willy Tarreau27cd2232019-01-03 21:52:42 +01002084 goto skip_hdr;
2085 }
Christopher Fauletb045bb22020-02-28 10:42:20 +01002086 else if (isteq(n, ist("connection"))) {
Christopher Fauletb992af02019-03-28 15:42:24 +01002087 h1_parse_connection_header(h1m, &v);
Christopher Faulet9768c262018-10-22 09:34:31 +02002088 if (!v.len)
2089 goto skip_hdr;
2090 }
Amaury Denoyellec1938232020-12-11 17:53:03 +01002091 else if (isteq(n, ist("upgrade"))) {
2092 h1_parse_upgrade_header(h1m, v);
2093 }
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002094 else if ((isteq(n, ist("sec-websocket-accept")) &&
2095 h1m->flags & H1_MF_RESP) ||
2096 (isteq(n, ist("sec-websocket-key")) &&
2097 !(h1m->flags & H1_MF_RESP))) {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002098 ws_key_found = 1;
2099 }
Christopher Fauletf56e8462021-09-28 10:56:36 +02002100 else if (isteq(n, ist("te"))) {
2101 /* "te" may only be sent with "trailers" if this value
2102 * is present, otherwise it must be deleted.
2103 */
2104 v = istist(v, ist("trailers"));
2105 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
2106 goto skip_hdr;
2107 v = ist("trailers");
2108 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002109
Christopher Faulet67d58092019-10-02 10:51:38 +02002110 /* Skip header if same name is used to add the server name */
2111 if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name &&
2112 isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len)))
2113 goto skip_hdr;
2114
Christopher Faulet98fbe952019-07-22 16:18:24 +02002115 /* Try to adjust the case of the header name */
2116 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2117 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002118 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002119 goto full;
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 skip_hdr:
2121 h1m->state = H1_MSG_HDR_L2_LWS;
2122 break;
2123
Christopher Faulet94b2c762019-05-24 15:28:57 +02002124 case H1_MSG_LAST_LF:
2125 if (type != HTX_BLK_EOH)
2126 goto error;
2127 last_lf:
Christopher Fauletada34b62019-05-24 16:36:43 +02002128 h1m->state = H1_MSG_LAST_LF;
2129 if (!(h1s->flags & H1S_F_HAVE_O_CONN)) {
Christopher Faulet04f89192019-10-16 09:41:07 +02002130 if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) {
Christopher Faulet631c7e82021-09-27 09:47:03 +02002131 /* If the reply comes from haproxy while the request is
2132 * not finished, we force the connection close. */
Christopher Faulet04f89192019-10-16 09:41:07 +02002133 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2134 TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2135 }
Christopher Faulet631c7e82021-09-27 09:47:03 +02002136 else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) {
2137 /* T-E + C-L: force close */
2138 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2139 TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2140 }
2141 else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) {
2142 /* T-E + HTTP/1.0: force close */
2143 h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
2144 TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
2145 }
Christopher Faulet04f89192019-10-16 09:41:07 +02002146
2147 /* the conn_mode must be processed. So do it */
Christopher Fauleta110ecb2019-06-17 14:07:46 +02002148 n = ist("connection");
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002149 v = ist("");
Christopher Fauletb992af02019-03-28 15:42:24 +01002150 h1_process_output_conn_mode(h1s, h1m, &v);
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002151 if (v.len) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02002152 /* Try to adjust the case of the header name */
2153 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2154 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002155 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002156 goto full;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002157 }
Christopher Fauletada34b62019-05-24 16:36:43 +02002158 h1s->flags |= H1S_F_HAVE_O_CONN;
Christopher Fauletd1ebb1e2018-11-28 16:32:50 +01002159 }
Willy Tarreau4710d202019-01-03 17:39:54 +01002160
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002161 if ((h1s->meth != HTTP_METH_CONNECT &&
2162 (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 +01002163 (H1_MF_VER_11|H1_MF_XFER_LEN)) ||
Christopher Faulete5596bf2020-12-02 16:13:22 +01002164 (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002165 !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) &&
Christopher Faulet1f890dd2019-02-18 10:33:16 +01002166 (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) ==
2167 (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) {
Willy Tarreau4710d202019-01-03 17:39:54 +01002168 /* chunking needed but header not seen */
Christopher Faulet7a991a92019-10-04 10:23:51 +02002169 n = ist("transfer-encoding");
2170 v = ist("chunked");
2171 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2172 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002173 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002174 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002175 TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Willy Tarreau4710d202019-01-03 17:39:54 +01002176 h1m->flags |= H1_MF_CHNK;
2177 }
2178
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002179 /* Now add the server name to a header (if requested) */
2180 if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) &&
2181 !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) {
2182 struct server *srv = objt_server(h1c->conn->target);
2183
2184 if (srv) {
2185 n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len);
2186 v = ist(srv->id);
Christopher Faulet5cef2a62019-10-02 11:06:13 +02002187
2188 /* Try to adjust the case of the header name */
2189 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2190 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002191 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002192 goto full;
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002193 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002194 TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02002195 h1s->flags |= H1S_F_HAVE_SRV_NAME;
2196 }
2197
Amaury Denoyellec1938232020-12-11 17:53:03 +01002198 /* Add websocket handshake key if needed */
2199 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) &&
2200 !ws_key_found) {
Amaury Denoyelleaad333a2020-12-11 17:53:07 +01002201 if (!(h1m->flags & H1_MF_RESP)) {
2202 /* generate a random websocket key
2203 * stored in the session to
2204 * verify it on the response side
2205 */
2206 h1_generate_random_ws_input_key(h1s->ws_key);
2207
2208 if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"),
2209 ist(h1s->ws_key),
2210 &tmp)) {
2211 goto full;
2212 }
2213 }
2214 else {
Amaury Denoyellec1938232020-12-11 17:53:03 +01002215 /* add the response header key */
2216 char key[29];
2217 h1_calculate_ws_output_key(h1s->ws_key, key);
2218 if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"),
2219 ist(key),
2220 &tmp)) {
2221 goto full;
2222 }
2223 }
2224 }
2225
Christopher Faulet6b81df72019-10-01 22:08:43 +02002226 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"),
2227 H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
2228
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002229 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002230 if (!chunk_memcat(&tmp, "\r\n", 2))
2231 goto full;
2232 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002233 }
Christopher Fauletf3158e92019-11-15 11:14:23 +01002234 else if ((h1m->flags & H1_MF_RESP) &&
Christopher Fauletc75668e2020-12-07 18:10:32 +01002235 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002236 if (!chunk_memcat(&tmp, "\r\n", 2))
2237 goto full;
2238 goto done;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002239 }
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002240 else if ((h1m->flags & H1_MF_RESP) &&
2241 h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) {
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002242 if (!chunk_memcat(&tmp, "\r\n", 2))
2243 goto full;
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002244 h1m_init_res(&h1s->res);
2245 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
Christopher Fauletada34b62019-05-24 16:36:43 +02002246 h1s->flags &= ~H1S_F_HAVE_O_CONN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002247 TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s);
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02002248 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002249 else {
2250 /* EOM flag is set and it is the last block */
2251 if (htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
Christopher Faulet3d6e0e32021-02-08 09:34:35 +01002252 if (h1m->flags & H1_MF_CHNK) {
2253 if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7))
2254 goto full;
2255 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002256 else if (!chunk_memcat(&tmp, "\r\n", 2))
2257 goto full;
2258 goto done;
2259 }
2260 else if (!chunk_memcat(&tmp, "\r\n", 2))
2261 goto full;
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002262 h1m->state = H1_MSG_DATA;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002263 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002264 break;
2265
Christopher Faulet94b2c762019-05-24 15:28:57 +02002266 case H1_MSG_DATA:
Christopher Fauletf8db73e2019-07-04 17:12:12 +02002267 case H1_MSG_TUNNEL:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002268 if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) {
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002269 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))
2270 goto trailers;
2271
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002272 /* If the message is not chunked, never
2273 * add the last chunk. */
2274 if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002275 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002276 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 +02002277 goto trailers;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002278 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002279 else if (type != HTX_BLK_DATA)
2280 goto error;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002281
2282 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 +02002283
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002284 /* It is the last block of this message. After this one,
2285 * only tunneled data may be forwarded. */
2286 if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) {
2287 TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s);
2288 last_data = 1;
2289 }
Christopher Fauletd9233f02019-10-14 14:01:24 +02002290
2291 if (vlen > count) {
2292 /* Get the maximum amount of data we can xferred */
2293 vlen = count;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002294 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002295 }
2296
Christopher Faulet140f1a52021-11-26 16:37:55 +01002297 if (h1m->state == H1_MSG_DATA) {
2298 if (h1m->flags & H1_MF_CLEN) {
2299 if (vlen > h1m->curr_len) {
2300 TRACE_ERROR("too much payload, more than announced",
2301 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2302 goto error;
2303 }
2304 h1m->curr_len -= vlen;
2305 }
2306 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2307 TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
2308 goto skip_data;
2309 }
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002310 }
2311
Christopher Fauletd9233f02019-10-14 14:01:24 +02002312 chklen = 0;
2313 if (h1m->flags & H1_MF_CHNK) {
2314 chklen = b_room(&tmp);
2315 chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 :
2316 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2317 (chklen < 1048576) ? 5 : 8);
2318 chklen += 4; /* 2 x CRLF */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002319
2320 /* If it is the end of the chunked message (without EOT), reserve the
2321 * last chunk size */
2322 if (last_data)
2323 chklen += 5;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002324 }
2325
2326 if (vlen + chklen > b_room(&tmp)) {
2327 /* too large for the buffer */
2328 if (chklen >= b_room(&tmp))
2329 goto full;
2330 vlen = b_room(&tmp) - chklen;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002331 last_data = 0;
Christopher Fauletd9233f02019-10-14 14:01:24 +02002332 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002333 v = htx_get_blk_value(chn_htx, blk);
Christopher Fauletb2e84162018-12-06 11:39:49 +01002334 v.len = vlen;
Christopher Faulet53a899b2019-10-08 16:38:42 +02002335 if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK)))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002336 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002337
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002338 /* Space already reserved, so it must succeed */
2339 if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5))
2340 goto error;
2341
Christopher Faulet6b81df72019-10-01 22:08:43 +02002342 if (h1m->state == H1_MSG_DATA)
2343 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002344 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002345 else
2346 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"),
Willy Tarreau022e5e52020-09-10 09:33:15 +02002347 H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len});
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002348
2349 skip_data:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002350 if (last_data)
2351 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002352 break;
2353
Christopher Faulet94b2c762019-05-24 15:28:57 +02002354 case H1_MSG_TRAILERS:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002355 if (type != HTX_BLK_TLR && type != HTX_BLK_EOT)
Christopher Faulet94b2c762019-05-24 15:28:57 +02002356 goto error;
2357 trailers:
Christopher Faulet9768c262018-10-22 09:34:31 +02002358 h1m->state = H1_MSG_TRAILERS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002359
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002360 /* If the message is not chunked, ignore
2361 * trailers. It may happen with H2 messages. */
Christopher Faulet0a916d22021-02-10 08:48:19 +01002362 if (!(h1m->flags & H1_MF_CHNK)) {
2363 if (type == HTX_BLK_EOT)
2364 goto done;
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002365 break;
Christopher Faulet0a916d22021-02-10 08:48:19 +01002366 }
Christopher Faulet5433a0b2019-06-27 17:40:14 +02002367
Christopher Faulete5596bf2020-12-02 16:13:22 +01002368 if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) {
2369 TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx);
Christopher Faulet0d7e6342021-02-08 15:56:36 +01002370 if (type == HTX_BLK_EOT)
2371 goto done;
Christopher Faulete5596bf2020-12-02 16:13:22 +01002372 break;
2373 }
2374
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002375 if (type == HTX_BLK_EOT) {
Christopher Fauletc2518a52019-06-25 21:41:02 +02002376 if (!chunk_memcat(&tmp, "\r\n", 2))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002377 goto full;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002378 TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"),
2379 H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s);
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002380 goto done;
Christopher Faulet32188212018-11-20 18:21:43 +01002381 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002382 else { // HTX_BLK_TLR
2383 n = htx_get_blk_name(chn_htx, blk);
2384 v = htx_get_blk_value(chn_htx, blk);
Christopher Faulet98fbe952019-07-22 16:18:24 +02002385
2386 /* Try to adjust the case of the header name */
2387 if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV))
2388 h1_adjust_case_outgoing_hdr(h1s, h1m, &n);
Christopher Faulet53a899b2019-10-08 16:38:42 +02002389 if (!h1_format_htx_hdr(n, v, &tmp))
Christopher Fauleta61aa542019-10-14 14:17:00 +02002390 goto full;
Christopher Faulet2d7c5392019-06-03 10:41:26 +02002391 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002392 break;
2393
Christopher Faulet94b2c762019-05-24 15:28:57 +02002394 case H1_MSG_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002395 TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2396 goto error; /* For now return an error */
2397
Christopher Faulet94b2c762019-05-24 15:28:57 +02002398 done:
Christopher Faulet36893672021-02-10 09:52:07 +01002399 if (!(chn_htx->flags & HTX_FL_EOM)) {
2400 TRACE_STATE("No EOM flags in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
2401 goto error; /* For now return an error */
2402 }
2403
Christopher Faulet94b2c762019-05-24 15:28:57 +02002404 h1m->state = H1_MSG_DONE;
Christopher Fauletdea24742021-01-22 15:12:30 +01002405 if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) {
Christopher Faulet10a86702021-04-07 14:18:11 +02002406 h1s->flags |= H1S_F_TX_BLK;
2407 TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002408 }
2409 else if ((h1m->flags & H1_MF_RESP) &&
2410 ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) {
2411 /* a successful reply to a CONNECT or a protocol switching is sent
2412 * to the client. Switch the response to tunnel mode.
2413 */
Christopher Faulet5be651d2021-01-22 15:28:03 +01002414 h1_set_tunnel_mode(h1s);
Christopher Fauletdea24742021-01-22 15:12:30 +01002415 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 +01002416 }
Christopher Faulet5be651d2021-01-22 15:28:03 +01002417
Christopher Faulet10a86702021-04-07 14:18:11 +02002418 if (h1s->flags & H1S_F_RX_BLK) {
2419 h1s->flags &= ~H1S_F_RX_BLK;
Christopher Faulet02c92c32021-04-09 12:31:48 +02002420 h1_wake_stream_for_recv(h1s);
Christopher Faulet10a86702021-04-07 14:18:11 +02002421 TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletcd67bff2019-06-14 16:54:15 +02002422 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002423
2424 TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"),
2425 H1_EV_TX_DATA, h1c->conn, h1s);
Christopher Faulet9768c262018-10-22 09:34:31 +02002426 break;
2427
Christopher Faulet9768c262018-10-22 09:34:31 +02002428 default:
Christopher Faulet94b2c762019-05-24 15:28:57 +02002429 error:
Christopher Fauletd87d3fa2019-06-26 15:16:28 +02002430 /* Unexpected error during output processing */
Christopher Faulet69b48212019-09-09 10:11:30 +02002431 chn_htx->flags |= HTX_FL_PROCESSING_ERROR;
Christopher Faulet60ef12c2020-09-24 10:05:44 +02002432 h1s->flags |= H1S_F_PROCESSING_ERROR;
Christopher Fauletdea24742021-01-22 15:12:30 +01002433 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002434 TRACE_ERROR("processing output error, set error on h1c/h1s",
2435 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01002436 goto end;
Christopher Faulet9768c262018-10-22 09:34:31 +02002437 }
Christopher Faulet94b2c762019-05-24 15:28:57 +02002438
2439 nextblk:
Christopher Fauletb2e84162018-12-06 11:39:49 +01002440 total += vlen;
2441 count -= vlen;
2442 if (sz == vlen)
2443 blk = htx_remove_blk(chn_htx, blk);
2444 else {
2445 htx_cut_data_blk(chn_htx, blk, vlen);
2446 break;
2447 }
Christopher Faulet129817b2018-09-20 16:14:40 +02002448 }
2449
Christopher Faulet9768c262018-10-22 09:34:31 +02002450 copy:
Willy Tarreauc5efa332018-12-05 11:19:27 +01002451 /* when the output buffer is empty, tmp shares the same area so that we
2452 * only have to update pointers and lengths.
2453 */
Christopher Fauletc2518a52019-06-25 21:41:02 +02002454 if (tmp.area == h1c->obuf.area + h1c->obuf.head)
2455 h1c->obuf.data = tmp.data;
Willy Tarreauc5efa332018-12-05 11:19:27 +01002456 else
Christopher Fauletc2518a52019-06-25 21:41:02 +02002457 b_putblk(&h1c->obuf, tmp.area, tmp.data);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002458
Willy Tarreau3815b222018-12-11 19:50:43 +01002459 htx_to_buf(chn_htx, buf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002460 out:
2461 if (!buf_room_for_htx_data(&h1c->obuf)) {
2462 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002463 h1c->flags |= H1C_F_OUT_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002464 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002465 end:
Christopher Fauletdea24742021-01-22 15:12:30 +01002466 /* Both the request and the response reached the DONE state. So set EOI
2467 * flag on the conn-stream. Most of time, the flag will already be set,
2468 * except for protocol upgrades. Report an error if data remains blocked
2469 * in the output buffer.
2470 */
2471 if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) {
2472 if (!htx_is_empty(chn_htx)) {
2473 h1c->flags |= H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002474 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 +01002475 }
2476 h1s->cs->flags |= CS_FL_EOI;
2477 }
2478
Christopher Faulet6b81df72019-10-01 22:08:43 +02002479 TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total});
Christopher Faulet9768c262018-10-22 09:34:31 +02002480 return total;
Christopher Fauleta61aa542019-10-14 14:17:00 +02002481
2482 full:
2483 TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s);
2484 h1c->flags |= H1C_F_OUT_FULL;
2485 goto copy;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002486}
2487
Christopher Faulet51dbc942018-09-13 09:05:15 +02002488/*********************************************************/
2489/* functions below are I/O callbacks from the connection */
2490/*********************************************************/
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002491static void h1_wake_stream_for_recv(struct h1s *h1s)
2492{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002493 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002494 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002495 tasklet_wakeup(h1s->subs->tasklet);
2496 h1s->subs->events &= ~SUB_RETRY_RECV;
2497 if (!h1s->subs->events)
2498 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002499 }
2500}
2501static void h1_wake_stream_for_send(struct h1s *h1s)
2502{
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002503 if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002504 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01002505 tasklet_wakeup(h1s->subs->tasklet);
2506 h1s->subs->events &= ~SUB_RETRY_SEND;
2507 if (!h1s->subs->events)
2508 h1s->subs = NULL;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002509 }
Christopher Fauletc18fc232020-10-06 17:41:36 +02002510}
2511
Christopher Fauletad4daf62021-01-21 17:49:01 +01002512/* alerts the data layer following this sequence :
2513 * - if the h1s' data layer is subscribed to recv, then it's woken up for recv
2514 * - if its subscribed to send, then it's woken up for send
2515 * - if it was subscribed to neither, its ->wake() callback is called
2516 */
2517static void h1_alert(struct h1s *h1s)
2518{
2519 if (h1s->subs) {
2520 h1_wake_stream_for_recv(h1s);
2521 h1_wake_stream_for_send(h1s);
2522 }
2523 else if (h1s->cs && h1s->cs->data_cb->wake != NULL) {
2524 TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s);
2525 h1s->cs->data_cb->wake(h1s->cs);
2526 }
2527}
2528
Christopher Fauletc18fc232020-10-06 17:41:36 +02002529/* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success
2530 * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for
2531 * retryable errors (allocation error or buffer full). On success, the error is
2532 * copied in the output buffer.
2533*/
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002534static int h1_send_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002535{
2536 int rc = http_get_status_idx(h1c->errcode);
2537 int ret = 0;
2538
2539 TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode});
2540
2541 /* Verify if the error is mapped on /dev/null or any empty file */
2542 /// XXX: do a function !
2543 if (h1c->px->replies[rc] &&
2544 h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG &&
2545 h1c->px->replies[rc]->body.errmsg &&
2546 b_is_null(h1c->px->replies[rc]->body.errmsg)) {
2547 /* Empty error, so claim a success */
2548 ret = 1;
2549 goto out;
2550 }
2551
2552 if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) {
2553 h1c->flags |= H1C_F_ERR_PENDING;
2554 goto out;
2555 }
2556
2557 if (!h1_get_buf(h1c, &h1c->obuf)) {
2558 h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING);
2559 TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2560 goto out;
2561 }
2562 ret = b_istput(&h1c->obuf, ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])));
2563 if (unlikely(ret <= 0)) {
2564 if (!ret) {
2565 h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING);
2566 TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn);
2567 goto out;
2568 }
2569 else {
2570 /* we cannot report this error, so claim a success */
2571 ret = 1;
2572 }
2573 }
2574 h1c->flags &= ~H1C_F_ERR_PENDING;
2575 out:
2576 TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn);
2577 return ret;
2578}
2579
2580/* Try to send a 500 internal error. It relies on h1_send_error to send the
2581 * error. This function takes care of incrementing stats and tracked counters.
2582 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002583static int h1_handle_internal_err(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002584{
2585 struct session *sess = h1c->conn->owner;
2586 int ret = 1;
2587
2588 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002589 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002590 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]);
2591 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002592 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002593 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002594
2595 h1c->errcode = 500;
2596 ret = h1_send_error(h1c);
2597 sess_log(sess);
2598 return ret;
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002599}
2600
Christopher Fauletb3230f72021-09-21 18:38:20 +02002601/* Try to send an error because of a parsing error. By default a 400 bad request
2602 * error is returned. But the status code may be specified by setting
2603 * h1c->errcode. It relies on h1_send_error to send the error. This function
2604 * takes care of incrementing stats and tracked counters.
Christopher Fauletc18fc232020-10-06 17:41:36 +02002605 */
Christopher Fauletb3230f72021-09-21 18:38:20 +02002606static int h1_handle_parsing_error(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002607{
2608 struct session *sess = h1c->conn->owner;
2609 int ret = 1;
2610
2611 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2612 goto end;
2613
2614 session_inc_http_req_ctr(sess);
2615 session_inc_http_err_ctr(sess);
2616 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002617 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2618 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002619 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002620 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002621
Christopher Fauletb3230f72021-09-21 18:38:20 +02002622 if (!h1c->errcode)
2623 h1c->errcode = 400;
Christopher Fauletc18fc232020-10-06 17:41:36 +02002624 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002625 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2626 sess_log(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002627
2628 end:
2629 return ret;
2630}
2631
Christopher Faulet2eed8002020-12-07 11:26:13 +01002632/* Try to send a 501 not implemented error. It relies on h1_send_error to send
2633 * the error. This function takes care of incrementing stats and tracked
2634 * counters.
2635 */
2636static int h1_handle_not_impl_err(struct h1c *h1c)
2637{
2638 struct session *sess = h1c->conn->owner;
2639 int ret = 1;
2640
2641 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2642 goto end;
2643
2644 session_inc_http_req_ctr(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002645 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002646 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2647 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002648 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002649 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002650
2651 h1c->errcode = 501;
2652 ret = h1_send_error(h1c);
Christopher Faulet07e10de2021-07-26 09:42:49 +02002653 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2654 sess_log(sess);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002655
2656 end:
2657 return ret;
2658}
2659
Christopher Fauletc18fc232020-10-06 17:41:36 +02002660/* Try to send a 408 timeout error. It relies on h1_send_error to send the
2661 * error. This function takes care of incrementing stats and tracked counters.
2662 */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002663static int h1_handle_req_tout(struct h1c *h1c)
Christopher Fauletc18fc232020-10-06 17:41:36 +02002664{
2665 struct session *sess = h1c->conn->owner;
2666 int ret = 1;
2667
2668 if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB)))
2669 goto end;
2670
2671 session_inc_http_req_ctr(sess);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002672 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Willy Tarreau4781b152021-04-06 13:53:36 +02002673 _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]);
2674 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01002675 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002676 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002677
2678 h1c->errcode = 408;
Christopher Faulet07e10de2021-07-26 09:42:49 +02002679 if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG))
2680 ret = h1_send_error(h1c);
Christopher Fauletc18fc232020-10-06 17:41:36 +02002681 sess_log(sess);
2682 end:
2683 return ret;
2684}
2685
2686
Christopher Faulet51dbc942018-09-13 09:05:15 +02002687/*
2688 * Attempt to read data, and subscribe if none available
2689 */
2690static int h1_recv(struct h1c *h1c)
2691{
2692 struct connection *conn = h1c->conn;
Olivier Houchard75159a92018-12-03 18:46:09 +01002693 size_t ret = 0, max;
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002694 int flags = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002695
Christopher Faulet6b81df72019-10-01 22:08:43 +02002696 TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn);
2697
2698 if (h1c->wait_event.events & SUB_RETRY_RECV) {
2699 TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc386a882018-12-04 16:06:28 +01002700 return (b_data(&h1c->ibuf));
Christopher Faulet6b81df72019-10-01 22:08:43 +02002701 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002702
Christopher Fauletae635762020-09-21 11:47:16 +02002703 if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) {
2704 TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002705 return 1;
Olivier Houchard75159a92018-12-03 18:46:09 +01002706 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002707
Christopher Fauletf7d5ff32019-04-16 13:55:08 +02002708 if (!h1_get_buf(h1c, &h1c->ibuf)) {
2709 h1c->flags |= H1C_F_IN_ALLOC;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002710 TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002711 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002712 }
Christopher Faulet1be55f92018-10-02 15:59:23 +02002713
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002714 /*
2715 * If we only have a small amount of data, realign it,
2716 * it's probably cheaper than doing 2 recv() calls.
2717 */
2718 if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128)
Christopher Faulet00d7cde2021-02-04 11:01:51 +01002719 b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx));
Olivier Houchard29a22bc2018-12-04 18:16:45 +01002720
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002721 /* avoid useless reads after first responses */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002722 if (!h1c->h1s ||
2723 (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) ||
2724 ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE))
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002725 flags |= CO_RFL_READ_ONCE;
2726
Willy Tarreau45f2b892018-12-05 07:59:27 +01002727 max = buf_room_for_htx_data(&h1c->ibuf);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002728 if (max) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002729 if (h1c->flags & H1C_F_IN_FULL) {
2730 h1c->flags &= ~H1C_F_IN_FULL;
2731 TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2732 }
Willy Tarreau78f548f2018-12-05 10:02:39 +01002733
2734 if (!b_data(&h1c->ibuf)) {
2735 /* try to pre-align the buffer like the rxbufs will be
2736 * to optimize memory copies.
2737 */
Willy Tarreau78f548f2018-12-05 10:02:39 +01002738 h1c->ibuf.head = sizeof(struct htx);
2739 }
Willy Tarreau6e59cb52020-02-20 11:11:50 +01002740 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002741 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002742 if (max && !ret && h1_recv_allowed(h1c)) {
2743 TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn);
2744 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Fauletcf56b992018-12-11 16:12:31 +01002745 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002746 else {
2747 h1_wake_stream_for_recv(h1c->h1s);
2748 TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret});
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02002749 }
2750
Christopher Faulet51dbc942018-09-13 09:05:15 +02002751 if (!b_data(&h1c->ibuf))
2752 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002753 else if (!buf_room_for_htx_data(&h1c->ibuf)) {
Christopher Faulet51dbc942018-09-13 09:05:15 +02002754 h1c->flags |= H1C_F_IN_FULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002755 TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK);
2756 }
2757
2758 TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002759 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002760}
2761
2762
2763/*
2764 * Try to send data if possible
2765 */
2766static int h1_send(struct h1c *h1c)
2767{
2768 struct connection *conn = h1c->conn;
2769 unsigned int flags = 0;
2770 size_t ret;
2771 int sent = 0;
2772
Christopher Faulet6b81df72019-10-01 22:08:43 +02002773 TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn);
2774
2775 if (conn->flags & CO_FL_ERROR) {
2776 TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002777 b_reset(&h1c->obuf);
2778 return 1;
Christopher Faulet6b81df72019-10-01 22:08:43 +02002779 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002780
2781 if (!b_data(&h1c->obuf))
2782 goto end;
2783
Christopher Faulet46230362019-10-17 16:04:20 +02002784 if (h1c->flags & H1C_F_CO_MSG_MORE)
Christopher Faulet51dbc942018-09-13 09:05:15 +02002785 flags |= CO_SFL_MSG_MORE;
Christopher Faulet46230362019-10-17 16:04:20 +02002786 if (h1c->flags & H1C_F_CO_STREAMER)
2787 flags |= CO_SFL_STREAMER;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002788
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002789 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002790 if (ret > 0) {
Willy Tarreau022e5e52020-09-10 09:33:15 +02002791 TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret});
Christopher Faulet6b81df72019-10-01 22:08:43 +02002792 if (h1c->flags & H1C_F_OUT_FULL) {
2793 h1c->flags &= ~H1C_F_OUT_FULL;
2794 TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn);
2795 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002796 b_del(&h1c->obuf, ret);
2797 sent = 1;
2798 }
2799
Christopher Faulet145aa472018-12-06 10:56:20 +01002800 if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002801 TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet145aa472018-12-06 10:56:20 +01002802 /* error or output closed, nothing to send, clear the buffer to release it */
2803 b_reset(&h1c->obuf);
2804 }
2805
Christopher Faulet51dbc942018-09-13 09:05:15 +02002806 end:
Christopher Faulet10a86702021-04-07 14:18:11 +02002807 if (!(h1c->flags & H1C_F_OUT_FULL))
Christopher Faulete17fa2f2018-12-11 16:25:36 +01002808 h1_wake_stream_for_send(h1c->h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002809
Christopher Faulet51dbc942018-09-13 09:05:15 +02002810 /* We're done, no more to send */
2811 if (!b_data(&h1c->obuf)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002812 TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002813 h1_release_buf(h1c, &h1c->obuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002814 if (h1c->flags & H1C_F_ST_SHUTDOWN) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02002815 TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02002816 h1_shutw_conn(conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002817 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002818 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002819 else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
2820 TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002821 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002822 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002823
Christopher Faulet6b81df72019-10-01 22:08:43 +02002824 TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002825 return sent;
2826}
2827
Christopher Faulet51dbc942018-09-13 09:05:15 +02002828/* callback called on any event by the connection handler.
2829 * It applies changes and returns zero, or < 0 if it wants immediate
2830 * destruction of the connection.
2831 */
2832static int h1_process(struct h1c * h1c)
2833{
2834 struct connection *conn = h1c->conn;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002835 struct h1s *h1s = h1c->h1s;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002836
Christopher Faulet6b81df72019-10-01 22:08:43 +02002837 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
2838
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002839 /* Try to parse now the first block of a request, creating the H1 stream if necessary */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002840 if (b_data(&h1c->ibuf) && /* Input data to be processed */
Christopher Fauletab7389d2021-09-16 08:16:23 +02002841 (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */
2842 !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ST_ERROR))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002843 struct buffer *buf;
2844 size_t count;
Christopher Faulet51dbc942018-09-13 09:05:15 +02002845
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002846 /* When it happens for a backend connection, we may release it (it is probably a 408) */
2847 if (h1c->flags & H1C_F_IS_BACK)
Christopher Faulet539e0292018-11-19 10:40:09 +01002848 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002849
2850 /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002851 if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */
Christopher Faulet143e9e52021-03-08 15:32:03 +01002852 !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
2853 !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002854 /* Try to match H2 preface before parsing the request headers. */
2855 if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
2856 h1c->flags |= H1C_F_UPG_H2C;
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002857 if (h1c->flags & H1C_F_ST_ATTACHED) {
2858 /* Force the REOS here to be sure to release the CS.
2859 Here ATTACHED implies !READY, and h1s defined
2860 */
2861 BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY));
2862 h1s->flags |= H1S_F_REOS;
2863 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002864 TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE);
Christopher Faulet539e0292018-11-19 10:40:09 +01002865 goto release;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002866 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002867 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02002868
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002869 /* Create the H1 stream if not already there */
2870 if (!h1s) {
2871 h1s = h1c_frt_stream_new(h1c);
2872 if (!h1s) {
2873 b_reset(&h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002874 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002875 goto no_parsing;
2876 }
2877 }
2878
2879 if (h1s->sess->t_idle == -1)
2880 h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake;
2881
2882 /* Get the stream rxbuf */
2883 buf = h1_get_buf(h1c, &h1s->rxbuf);
2884 if (!buf) {
2885 h1c->flags |= H1C_F_IN_SALLOC;
2886 TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn);
2887 return 0;
2888 }
2889
2890 count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite);
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01002891 h1_process_demux(h1c, buf, count);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002892 h1_release_buf(h1c, &h1s->rxbuf);
2893 h1_set_idle_expiration(h1c);
2894
2895 no_parsing:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002896 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002897 h1_handle_internal_err(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002898 h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ);
Christopher Faulet26a26432021-01-27 11:27:50 +01002899 TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002900 }
2901 else if (h1s->flags & H1S_F_PARSING_ERROR) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002902 h1_handle_parsing_error(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002903 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002904 TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002905 }
Christopher Faulet2eed8002020-12-07 11:26:13 +01002906 else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) {
2907 h1_handle_not_impl_err(h1c);
2908 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01002909 TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR);
Christopher Faulet2eed8002020-12-07 11:26:13 +01002910 }
Christopher Fauletae635762020-09-21 11:47:16 +02002911 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002912 h1_send(h1c);
2913
Christopher Faulet75308302021-11-15 14:51:37 +01002914 /* H1 connection must be released ASAP if:
2915 * - an error occurred on the connection or the H1C or
2916 * - a read0 was received or
2917 * - a silent shutdown was emitted and all outgoing data sent
2918 */
2919 if ((conn->flags & CO_FL_ERROR) ||
2920 conn_xprt_read0_pending(conn) ||
2921 (h1c->flags & H1C_F_ST_ERROR) ||
2922 ((h1c->flags & H1C_F_ST_SILENT_SHUT) && !b_data(&h1c->obuf))) {
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01002923 if (!(h1c->flags & H1C_F_ST_READY)) {
2924 /* No conn-stream or not ready */
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002925 /* shutdown for reads and error on the frontend connection: Send an error */
Christopher Faulet75308302021-11-15 14:51:37 +01002926 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN))) {
Christopher Fauletb3230f72021-09-21 18:38:20 +02002927 if (h1_handle_parsing_error(h1c))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002928 h1_send(h1c);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002929 h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR;
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002930 }
2931
2932 /* Handle pending error, if any (only possible on frontend connection) */
2933 if (h1c->flags & H1C_F_ERR_PENDING) {
2934 BUG_ON(h1c->flags & H1C_F_IS_BACK);
2935 if (h1_send_error(h1c))
2936 h1_send(h1c);
2937 }
Christopher Fauletae635762020-09-21 11:47:16 +02002938
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002939 /* If there is some pending outgoing data or error, just wait */
2940 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING))
2941 goto end;
Christopher Fauletfeb11742018-11-29 15:12:34 +01002942
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002943 /* Otherwise we can release the H1 connection */
2944 goto release;
2945 }
2946 else {
2947 /* Here there is still a H1 stream with a conn-stream.
2948 * Report the connection state at the stream level
2949 */
2950 if (conn_xprt_read0_pending(conn)) {
2951 h1s->flags |= H1S_F_REOS;
2952 TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s);
2953 }
Christopher Faulet3ced1d12020-11-27 16:44:01 +01002954 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002955 h1s->cs->flags |= CS_FL_ERROR;
2956 TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s);
Christopher Fauletad4daf62021-01-21 17:49:01 +01002957 h1_alert(h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002958 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02002959 }
Willy Tarreauc493c9c2019-06-03 14:18:22 +02002960
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002961 if (!b_data(&h1c->ibuf))
2962 h1_release_buf(h1c, &h1c->ibuf);
2963
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02002964 /* Check if a soft-stop is in progress.
2965 * Release idling front connection if this is the case.
2966 */
2967 if (!(h1c->flags & H1C_F_IS_BACK)) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002968 if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Amaury Denoyelleefc6e952021-05-05 11:11:11 +02002969 if (h1c->flags & H1C_F_WAIT_NEXT_REQ)
2970 goto release;
2971 }
2972 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002973
2974 if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) {
2975 TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn);
2976 h1_wake_stream_for_recv(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01002977 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02002978
Christopher Faulet47365272018-10-31 17:40:50 +01002979 end:
Christopher Faulet7a145d62020-08-05 11:31:16 +02002980 h1_refresh_timeout(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02002981 TRACE_LEAVE(H1_EV_H1C_WAKE, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02002982 return 0;
Christopher Faulet539e0292018-11-19 10:40:09 +01002983
2984 release:
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002985 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05002986 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01002987 * attached CS first. Here, the H1C must not be READY */
2988 BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY);
2989
2990 if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS))
2991 h1s->cs->flags |= CS_FL_EOS;
2992 if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR))
2993 h1s->cs->flags |= CS_FL_ERROR;
2994 h1_alert(h1s);
2995 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
2996 }
2997 else {
2998 h1_release(h1c);
2999 TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE);
3000 }
Christopher Faulet539e0292018-11-19 10:40:09 +01003001 return -1;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003002}
3003
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003004struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003005{
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003006 struct connection *conn;
3007 struct tasklet *tl = (struct tasklet *)t;
3008 int conn_in_list;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003009 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003010 int ret = 0;
3011
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003012 if (state & TASK_F_USR1) {
3013 /* the tasklet was idling on an idle connection, it might have
3014 * been stolen, let's be careful!
3015 */
3016 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3017 if (tl->context == NULL) {
3018 /* The connection has been taken over by another thread,
3019 * we're no longer responsible for it, so just free the
3020 * tasklet, and do nothing.
3021 */
3022 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3023 tasklet_free(tl);
3024 return NULL;
3025 }
3026 conn = h1c->conn;
3027 TRACE_POINT(H1_EV_H1C_WAKE, conn);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003028
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003029 /* Remove the connection from the list, to be sure nobody attempts
3030 * to use it while we handle the I/O events
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003031 */
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003032 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3033 if (conn_in_list)
3034 conn_delete_from_tree(&conn->hash_node->node);
3035
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003036 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003037 } else {
3038 /* we're certain the connection was not in an idle list */
3039 conn = h1c->conn;
3040 TRACE_ENTER(H1_EV_H1C_WAKE, conn);
3041 conn_in_list = 0;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003042 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003043
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003044 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003045 ret = h1_send(h1c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003046 if (!(h1c->wait_event.events & SUB_RETRY_RECV))
Christopher Faulet51dbc942018-09-13 09:05:15 +02003047 ret |= h1_recv(h1c);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003048 if (ret || b_data(&h1c->ibuf))
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003049 ret = h1_process(h1c);
Willy Tarreau74163142021-03-13 11:30:19 +01003050
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003051 /* If we were in an idle list, we want to add it back into it,
3052 * unless h1_process() returned -1, which mean it has destroyed
3053 * the connection (testing !ret is enough, if h1_process() wasn't
3054 * called then ret will be 0 anyway.
3055 */
Willy Tarreau74163142021-03-13 11:30:19 +01003056 if (ret < 0)
3057 t = NULL;
3058
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003059 if (!ret && conn_in_list) {
3060 struct server *srv = objt_server(conn->target);
3061
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003062 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003063 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003064 ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003065 else
Willy Tarreau430bf4a2021-03-04 09:45:32 +01003066 ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003067 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003068 }
Willy Tarreau74163142021-03-13 11:30:19 +01003069 return t;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003070}
3071
Christopher Faulet51dbc942018-09-13 09:05:15 +02003072static int h1_wake(struct connection *conn)
3073{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003074 struct h1c *h1c = conn->ctx;
Olivier Houchard75159a92018-12-03 18:46:09 +01003075 int ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003076
Christopher Faulet6b81df72019-10-01 22:08:43 +02003077 TRACE_POINT(H1_EV_H1C_WAKE, conn);
3078
Christopher Faulet539e0292018-11-19 10:40:09 +01003079 h1_send(h1c);
Olivier Houchard75159a92018-12-03 18:46:09 +01003080 ret = h1_process(h1c);
3081 if (ret == 0) {
3082 struct h1s *h1s = h1c->h1s;
3083
Christopher Fauletad4daf62021-01-21 17:49:01 +01003084 if (h1c->flags & H1C_F_ST_ATTACHED)
3085 h1_alert(h1s);
Olivier Houchard75159a92018-12-03 18:46:09 +01003086 }
3087 return ret;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003088}
3089
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003090/* Connection timeout management. The principle is that if there's no receipt
3091 * nor sending for a certain amount of time, the connection is closed.
3092 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003093struct task *h1_timeout_task(struct task *t, void *context, unsigned int state)
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003094{
3095 struct h1c *h1c = context;
3096 int expired = tick_is_expired(t->expire, now_ms);
3097
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003098 TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003099
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003100 if (h1c) {
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003101 /* Make sure nobody stole the connection from us */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003102 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003103
3104 /* Somebody already stole the connection from us, so we should not
3105 * free it, we just have to free the task.
3106 */
3107 if (!t->context) {
3108 h1c = NULL;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003109 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003110 goto do_leave;
3111 }
3112
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003113 if (!expired) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003114 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003115 TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003116 return t;
3117 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003118
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003119 /* If a conn-stream is still attached and ready to the mux, wait for the
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003120 * stream's timeout
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003121 */
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003122 if (h1c->flags & H1C_F_ST_READY) {
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003123 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003124 t->expire = TICK_ETERNITY;
3125 TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s);
3126 return t;
3127 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003128
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003129 /* Try to send an error to the client */
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003130 if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) {
3131 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003132 TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003133 if (h1_handle_req_tout(h1c))
3134 h1_send(h1c);
3135 if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) {
3136 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003137 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003138 return t;
3139 }
3140 }
3141
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003142 if (h1c->flags & H1C_F_ST_ATTACHED) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +05003143 /* Don't release the H1 connection right now, we must destroy the
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003144 * attached CS first. Here, the H1C must not be READY */
3145 h1c->h1s->cs->flags |= (CS_FL_EOS|CS_FL_ERROR);
3146 h1_alert(h1c->h1s);
3147 h1_refresh_timeout(h1c);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003148 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock);
Christopher Faulet0f9395d2021-01-21 17:50:19 +01003149 TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE);
3150 return t;
3151 }
3152
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003153 /* We're about to destroy the connection, so make sure nobody attempts
3154 * to steal it from us.
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003155 */
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003156 if (h1c->conn->flags & CO_FL_LIST_MASK)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01003157 conn_delete_from_tree(&h1c->conn->hash_node->node);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003158
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01003159 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau68d4ee92020-06-30 11:19:23 +02003160 }
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003161
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003162 do_leave:
Olivier Houchard3f795f72019-04-17 22:51:06 +02003163 task_destroy(t);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003164
3165 if (!h1c) {
3166 /* resources were already deleted */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003167 TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003168 return NULL;
3169 }
3170
3171 h1c->task = NULL;
Christopher Fauletc1c66a42020-09-29 15:30:15 +02003172 h1_release(h1c);
3173 TRACE_LEAVE(H1_EV_H1C_WAKE);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003174 return NULL;
3175}
3176
Christopher Faulet51dbc942018-09-13 09:05:15 +02003177/*******************************************/
3178/* functions below are used by the streams */
3179/*******************************************/
Christopher Faulet73c12072019-04-08 11:23:22 +02003180
Christopher Faulet51dbc942018-09-13 09:05:15 +02003181/*
3182 * Attach a new stream to a connection
3183 * (Used for outgoing connections)
3184 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003185static struct conn_stream *h1_attach(struct connection *conn, struct session *sess)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003186{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003187 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003188 struct conn_stream *cs = NULL;
3189 struct h1s *h1s;
3190
Christopher Faulet6b81df72019-10-01 22:08:43 +02003191 TRACE_ENTER(H1_EV_STRM_NEW, conn);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003192 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003193 TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3194 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003195 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003196
Christopher Faulet236c93b2020-07-02 09:19:54 +02003197 cs = cs_new(h1c->conn, h1c->conn->target);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003198 if (!cs) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003199 TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3200 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003201 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003202
Christopher Faulet2f0ec662020-09-24 10:30:15 +02003203 h1s = h1c_bck_stream_new(h1c, cs, sess);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003204 if (h1s == NULL) {
Christopher Faulet26a26432021-01-27 11:27:50 +01003205 TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn);
3206 goto err;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003207 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003208
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003209 /* the connection is not idle anymore, let's mark this */
3210 HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003211 xprt_set_used(conn, conn->xprt, conn->xprt_ctx);
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003212
Christopher Faulet6b81df72019-10-01 22:08:43 +02003213 TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003214 return cs;
Christopher Faulet26a26432021-01-27 11:27:50 +01003215 err:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003216 cs_free(cs);
Christopher Faulet26a26432021-01-27 11:27:50 +01003217 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 +02003218 return NULL;
3219}
3220
3221/* Retrieves a valid conn_stream from this connection, or returns NULL. For
3222 * this mux, it's easy as we can only store a single conn_stream.
3223 */
3224static const struct conn_stream *h1_get_first_cs(const struct connection *conn)
3225{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003226 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003227 struct h1s *h1s = h1c->h1s;
3228
3229 if (h1s)
3230 return h1s->cs;
3231
3232 return NULL;
3233}
3234
Christopher Faulet73c12072019-04-08 11:23:22 +02003235static void h1_destroy(void *ctx)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003236{
Christopher Faulet73c12072019-04-08 11:23:22 +02003237 struct h1c *h1c = ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003238
Christopher Faulet6b81df72019-10-01 22:08:43 +02003239 TRACE_POINT(H1_EV_H1C_END, h1c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003240 if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003241 h1_release(h1c);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003242}
3243
3244/*
3245 * Detach the stream from the connection and possibly release the connection.
3246 */
3247static void h1_detach(struct conn_stream *cs)
3248{
3249 struct h1s *h1s = cs->ctx;
3250 struct h1c *h1c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003251 struct session *sess;
Olivier Houchard8a786902018-12-15 16:05:40 +01003252 int is_not_first;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003253
Christopher Faulet6b81df72019-10-01 22:08:43 +02003254 TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s);
3255
Christopher Faulet51dbc942018-09-13 09:05:15 +02003256 cs->ctx = NULL;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003257 if (!h1s) {
3258 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003259 return;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003260 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003261
Olivier Houchardf502aca2018-12-14 19:42:40 +01003262 sess = h1s->sess;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003263 h1c = h1s->h1c;
3264 h1s->cs = NULL;
3265
Christopher Faulet42849b02020-10-05 11:35:17 +02003266 sess->accept_date = date;
3267 sess->tv_accept = now;
3268 sess->t_handshake = 0;
3269 sess->t_idle = -1;
3270
Olivier Houchard8a786902018-12-15 16:05:40 +01003271 is_not_first = h1s->flags & H1S_F_NOT_FIRST;
3272 h1s_destroy(h1s);
3273
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003274 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 +02003275 /* If there are any excess server data in the input buffer,
3276 * release it and close the connection ASAP (some data may
3277 * remain in the output buffer). This happens if a server sends
3278 * invalid responses. So in such case, we don't want to reuse
3279 * the connection
Christopher Faulet03627242019-07-19 11:34:08 +02003280 */
Christopher Fauletf1204b82019-07-19 14:51:06 +02003281 if (b_data(&h1c->ibuf)) {
3282 h1_release_buf(h1c, &h1c->ibuf);
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003283 h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003284 TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END);
Christopher Fauletf1204b82019-07-19 14:51:06 +02003285 goto release;
3286 }
Christopher Faulet03627242019-07-19 11:34:08 +02003287
Christopher Faulet08016ab2020-07-01 16:10:06 +02003288 if (h1c->conn->flags & CO_FL_PRIVATE) {
3289 /* Add the connection in the session server list, if not already done */
Olivier Houchard351411f2018-12-27 17:20:54 +01003290 if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) {
3291 h1c->conn->owner = NULL;
Olivier Houchard2444aa52020-01-20 13:56:01 +01003292 h1c->conn->mux->destroy(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003293 goto end;
Olivier Houchard351411f2018-12-27 17:20:54 +01003294 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003295 /* Always idle at this step */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003296 if (session_check_idle_conn(sess, h1c->conn)) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003297 /* The connection got destroyed, let's leave */
Christopher Faulet6b81df72019-10-01 22:08:43 +02003298 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3299 goto end;
3300 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003301 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02003302 else {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003303 if (h1c->conn->owner == sess)
3304 h1c->conn->owner = NULL;
Willy Tarreaue388f2f2021-03-02 16:51:09 +01003305
3306 /* mark that the tasklet may lose its context to another thread and
3307 * that the handler needs to check it under the idle conns lock.
3308 */
3309 HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1);
Olivier Houchard3c49c1b2020-03-22 19:56:03 +01003310 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Willy Tarreau4f8cd432021-03-02 17:27:58 +01003311 xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx);
3312
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003313 if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003314 /* The server doesn't want it, let's kill the connection right away */
3315 h1c->conn->mux->destroy(h1c);
3316 TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END);
3317 goto end;
Christopher Faulet9400a392018-11-23 23:10:39 +01003318 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003319 /* At this point, the connection has been added to the
3320 * server idle list, so another thread may already have
3321 * hijacked it, so we can't do anything with it.
3322 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003323 return;
Christopher Faulet9400a392018-11-23 23:10:39 +01003324 }
3325 }
3326
Christopher Fauletf1204b82019-07-19 14:51:06 +02003327 release:
Christopher Faulet3ac0f432019-06-28 17:41:42 +02003328 /* 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 +01003329 if ((h1c->flags & H1C_F_ST_ERROR) ||
Christopher Faulet37243bc2019-07-11 15:40:25 +02003330 (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) ||
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003331 ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) ||
Christopher Faulet6b81df72019-10-01 22:08:43 +02003332 !h1c->conn->owner) {
3333 TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003334 h1_release(h1c);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003335 }
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003336 else {
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003337 if (h1c->flags & H1C_F_ST_IDLE) {
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003338 /* If we have a new request, process it immediately or
3339 * subscribe for reads waiting for new data
3340 */
Christopher Faulet0c366a82020-12-18 15:13:47 +01003341 if (unlikely(b_data(&h1c->ibuf))) {
3342 if (h1_process(h1c) == -1)
3343 goto end;
3344 }
Christopher Faulet5d3c93c2020-10-05 17:14:28 +02003345 else
3346 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3347 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003348 h1_set_idle_expiration(h1c);
Christopher Faulet7a145d62020-08-05 11:31:16 +02003349 h1_refresh_timeout(h1c);
Christopher Fauletb8093cf2019-01-03 16:27:28 +01003350 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003351 end:
3352 TRACE_LEAVE(H1_EV_STRM_END);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003353}
3354
3355
3356static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3357{
3358 struct h1s *h1s = cs->ctx;
Christopher Faulet7f366362019-04-08 10:51:20 +02003359 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003360
3361 if (!h1s)
3362 return;
Christopher Faulet7f366362019-04-08 10:51:20 +02003363 h1c = h1s->h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003364
Christopher Faulet99293b02021-11-10 10:30:15 +01003365 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003366
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003367 if (cs->flags & CS_FL_SHR)
3368 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003369 if (cs->flags & CS_FL_KILL_CONN) {
3370 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
3371 goto do_shutr;
3372 }
3373 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3374 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003375 goto do_shutr;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003376 }
Christopher Faulet7f366362019-04-08 10:51:20 +02003377
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003378 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3379 /* Here attached is implicit because there is CS */
3380 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3381 goto end;
3382 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003383 if (h1s->flags & H1S_F_WANT_KAL) {
3384 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003385 goto end;
3386 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003387
Christopher Faulet7f366362019-04-08 10:51:20 +02003388 do_shutr:
Christopher Faulet51dbc942018-09-13 09:05:15 +02003389 /* NOTE: Be sure to handle abort (cf. h2_shutr) */
3390 if (cs->flags & CS_FL_SHR)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003391 goto end;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003392 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003393 cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx,
Christopher Faulet6b81df72019-10-01 22:08:43 +02003394 (mode == CS_SHR_DRAIN));
Christopher Faulet6b81df72019-10-01 22:08:43 +02003395 end:
3396 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003397}
3398
3399static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3400{
3401 struct h1s *h1s = cs->ctx;
3402 struct h1c *h1c;
3403
3404 if (!h1s)
3405 return;
3406 h1c = h1s->h1c;
3407
Christopher Faulet99293b02021-11-10 10:30:15 +01003408 TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003409
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003410 if (cs->flags & CS_FL_SHW)
3411 goto end;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003412 if (cs->flags & CS_FL_KILL_CONN) {
3413 TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet7f366362019-04-08 10:51:20 +02003414 goto do_shutw;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003415 }
3416 if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
3417 TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3418 goto do_shutw;
3419 }
Olivier Houchardd2e88c72018-12-19 15:55:23 +01003420
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003421 if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) {
3422 /* Here attached is implicit because there is CS */
3423 TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s);
3424 goto end;
3425 }
Christopher Fauletc4bfa592020-10-06 17:45:34 +02003426 if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) {
3427 TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003428 goto end;
3429 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02003430
Christopher Faulet7f366362019-04-08 10:51:20 +02003431 do_shutw:
Christopher Faulet3ced1d12020-11-27 16:44:01 +01003432 h1c->flags |= H1C_F_ST_SHUTDOWN;
Christopher Fauleta85c5222021-10-27 15:36:38 +02003433 if (mode != CS_SHW_NORMAL)
3434 h1c->flags |= H1C_F_ST_SILENT_SHUT;
3435
Christopher Faulet3c82d8b2020-10-05 17:11:16 +02003436 if (!b_data(&h1c->obuf))
Christopher Fauleta85c5222021-10-27 15:36:38 +02003437 h1_shutw_conn(cs->conn);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003438 end:
3439 TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003440}
3441
Christopher Fauleta85c5222021-10-27 15:36:38 +02003442static void h1_shutw_conn(struct connection *conn)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003443{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003444 struct h1c *h1c = conn->ctx;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003445
Willy Tarreau62592ad2021-03-26 09:09:42 +01003446 if (conn->flags & CO_FL_SOCK_WR_SH)
3447 return;
3448
Christopher Fauleta85c5222021-10-27 15:36:38 +02003449 TRACE_ENTER(H1_EV_H1C_END, conn);
Christopher Faulet666a0c42019-01-08 11:12:04 +01003450 conn_xprt_shutw(conn);
Christopher Fauleta85c5222021-10-27 15:36:38 +02003451 conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT)));
3452 TRACE_LEAVE(H1_EV_H1C_END, conn);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003453}
3454
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003455/* Called from the upper layer, to unsubscribe <es> from events <event_type>
3456 * The <es> pointer is not allowed to differ from the one passed to the
3457 * subscribe() call. It always returns zero.
3458 */
3459static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003460{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003461 struct h1s *h1s = cs->ctx;
3462
3463 if (!h1s)
3464 return 0;
3465
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003466 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003467 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003468
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003469 es->events &= ~event_type;
3470 if (!es->events)
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003471 h1s->subs = NULL;
3472
3473 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003474 TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003475
3476 if (event_type & SUB_RETRY_SEND)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003477 TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003478
Christopher Faulet51dbc942018-09-13 09:05:15 +02003479 return 0;
3480}
3481
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003482/* Called from the upper layer, to subscribe <es> to events <event_type>. The
3483 * event subscriber <es> is not allowed to change from a previous call as long
3484 * as at least one event is still subscribed. The <event_type> must only be a
3485 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless
3486 * the conn_stream <cs> was already detached, in which case it will return -1.
3487 */
3488static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Christopher Faulet51dbc942018-09-13 09:05:15 +02003489{
Christopher Faulet51dbc942018-09-13 09:05:15 +02003490 struct h1s *h1s = cs->ctx;
Christopher Faulet51bb1852019-09-04 10:22:34 +02003491 struct h1c *h1c;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003492
3493 if (!h1s)
3494 return -1;
3495
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003496 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003497 BUG_ON(h1s->subs && h1s->subs != es);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003498
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01003499 es->events |= event_type;
3500 h1s->subs = es;
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003501
3502 if (event_type & SUB_RETRY_RECV)
Christopher Faulet6b81df72019-10-01 22:08:43 +02003503 TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s);
Willy Tarreau1b0d4d12020-01-16 11:03:19 +01003504
3505
Christopher Faulet6b81df72019-10-01 22:08:43 +02003506 if (event_type & SUB_RETRY_SEND) {
3507 TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003508 /*
3509 * If the conn_stream attempt to subscribe, and the
3510 * mux isn't subscribed to the connection, then it
3511 * probably means the connection wasn't established
3512 * yet, so we have to subscribe.
3513 */
3514 h1c = h1s->h1c;
3515 if (!(h1c->wait_event.events & SUB_RETRY_SEND))
3516 h1c->conn->xprt->subscribe(h1c->conn,
3517 h1c->conn->xprt_ctx,
3518 SUB_RETRY_SEND,
3519 &h1c->wait_event);
3520 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003521 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003522}
3523
Christopher Faulet564e39c2021-09-21 15:50:55 +02003524/* Called from the upper layer, to receive data.
3525 *
3526 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
3527 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
3528 * means the caller wants to flush input data (from the mux buffer and the
3529 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
3530 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
3531 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
3532 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
3533 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
3534 * copy as much data as possible.
3535 */
Christopher Faulet51dbc942018-09-13 09:05:15 +02003536static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3537{
3538 struct h1s *h1s = cs->ctx;
Christopher Faulet539e0292018-11-19 10:40:09 +01003539 struct h1c *h1c = h1s->h1c;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003540 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003541 size_t ret = 0;
3542
Willy Tarreau022e5e52020-09-10 09:33:15 +02003543 TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003544
3545 /* Do nothing for now if not READY */
3546 if (!(h1c->flags & H1C_F_ST_READY)) {
3547 TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
3548 goto end;
3549 }
3550
Christopher Faulet539e0292018-11-19 10:40:09 +01003551 if (!(h1c->flags & H1C_F_IN_ALLOC))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003552 ret = h1_process_demux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003553 else
3554 TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003555
Christopher Faulet2b861bf2021-04-06 17:24:39 +02003556 if ((flags & CO_RFL_BUF_FLUSH) && (cs->flags & CS_FL_MAY_SPLICE)) {
3557 h1c->flags |= H1C_F_WANT_SPLICE;
3558 TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s);
Christopher Faulete18777b2019-04-16 16:46:36 +02003559 }
Olivier Houchard02bac852019-08-22 18:34:25 +02003560 else {
Christopher Faulet1baef152021-04-08 18:42:59 +02003561 if (((flags & CO_RFL_KEEP_RECV) || (h1m->state != H1_MSG_DONE)) && !(h1c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau2c1f37d2020-03-04 17:50:02 +01003562 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
Christopher Faulet51dbc942018-09-13 09:05:15 +02003563 }
Christopher Faulet39c7b6b2021-01-21 17:44:35 +01003564
3565 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003566 TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet51dbc942018-09-13 09:05:15 +02003567 return ret;
3568}
3569
3570
3571/* Called from the upper layer, to send data */
3572static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3573{
3574 struct h1s *h1s = cs->ctx;
3575 struct h1c *h1c;
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003576 size_t total = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003577
3578 if (!h1s)
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003579 return 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003580 h1c = h1s->h1c;
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003581
Willy Tarreau022e5e52020-09-10 09:33:15 +02003582 TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003583
Olivier Houchard305d5ab2019-07-24 18:07:06 +02003584 /* If we're not connected yet, or we're waiting for a handshake, stop
3585 * now, as we don't want to remove everything from the channel buffer
3586 * before we're sure we can send it.
3587 */
Willy Tarreau911db9b2020-01-23 16:27:54 +01003588 if (h1c->conn->flags & CO_FL_WAIT_XPRT) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003589 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s);
Christopher Faulet3b88b8d2018-10-26 17:36:03 +02003590 return 0;
Christopher Faulet6b81df72019-10-01 22:08:43 +02003591 }
Christopher Faulet51dbc942018-09-13 09:05:15 +02003592
Christopher Fauletdea24742021-01-22 15:12:30 +01003593 if (h1c->flags & H1C_F_ST_ERROR) {
3594 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003595 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 +01003596 return 0;
3597 }
3598
Christopher Faulet46230362019-10-17 16:04:20 +02003599 /* Inherit some flags from the upper layer */
3600 h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER);
3601 if (flags & CO_SFL_MSG_MORE)
3602 h1c->flags |= H1C_F_CO_MSG_MORE;
3603 if (flags & CO_SFL_STREAMER)
3604 h1c->flags |= H1C_F_CO_STREAMER;
3605
Christopher Fauletc31872f2019-06-04 22:09:36 +02003606 while (count) {
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003607 size_t ret = 0;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003608
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003609 if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC)))
Christopher Faulet44c0dcf2021-02-16 18:32:08 +01003610 ret = h1_process_mux(h1c, buf, count);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003611 else
3612 TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s);
Olivier Houchardc89a42f2020-06-19 16:15:05 +02003613
3614 if ((count - ret) > 0)
3615 h1c->flags |= H1C_F_CO_MSG_MORE;
3616
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003617 if (!ret)
3618 break;
3619 total += ret;
Christopher Fauletc31872f2019-06-04 22:09:36 +02003620 count -= ret;
Olivier Houchard68787ef2020-01-15 19:13:32 +01003621 if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c))
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003622 break;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003623 }
Christopher Fauletdea24742021-01-22 15:12:30 +01003624
3625 if (h1c->flags & H1C_F_ST_ERROR) {
Christopher Fauletdea24742021-01-22 15:12:30 +01003626 cs->flags |= CS_FL_ERROR;
Christopher Faulet26a26432021-01-27 11:27:50 +01003627 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 +01003628 }
3629
Christopher Faulet7a145d62020-08-05 11:31:16 +02003630 h1_refresh_timeout(h1c);
Willy Tarreau022e5e52020-09-10 09:33:15 +02003631 TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total});
Christopher Faulet5d37dac2018-11-22 10:58:42 +01003632 return total;
Christopher Faulet51dbc942018-09-13 09:05:15 +02003633}
3634
Willy Tarreaue5733232019-05-22 19:24:06 +02003635#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003636/* Send and get, using splicing */
3637static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
3638{
3639 struct h1s *h1s = cs->ctx;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003640 struct h1c *h1c = h1s->h1c;
3641 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003642 int ret = 0;
3643
Willy Tarreau022e5e52020-09-10 09:33:15 +02003644 TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003645
Christopher Faulet9fa40c42019-11-05 16:24:27 +01003646 if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003647 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003648 TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulete18777b2019-04-16 16:46:36 +02003649 goto end;
3650 }
3651
Christopher Fauletcf307562021-07-26 10:49:39 +02003652 h1c->flags |= H1C_F_WANT_SPLICE;
Willy Tarreaufbdf90a2019-06-03 13:42:54 +02003653 if (h1s_data_pending(h1s)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003654 TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003655 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003656 }
3657
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003658 if (!h1_recv_allowed(h1c)) {
Christopher Faulet0060be92020-07-03 15:02:25 +02003659 TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s);
3660 goto end;
3661 }
3662
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003663 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len)
Christopher Faulet1be55f92018-10-02 15:59:23 +02003664 count = h1m->curr_len;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003665 ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003666 if (ret >= 0) {
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003667 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003668 if (ret > h1m->curr_len) {
3669 h1s->flags |= H1S_F_PARSING_ERROR;
3670 h1c->flags |= H1C_F_ST_ERROR;
3671 cs->flags |= CS_FL_ERROR;
3672 TRACE_ERROR("too much payload, more than announced",
3673 H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, cs->conn, h1s);
3674 goto end;
3675 }
3676 h1m->curr_len -= ret;
3677 if (!h1m->curr_len) {
3678 h1m->state = H1_MSG_DONE;
3679 h1c->flags &= ~H1C_F_WANT_SPLICE;
3680 TRACE_STATE("payload fully received", H1_EV_STRM_RECV, cs->conn, h1s);
3681 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003682 }
Christopher Faulete18777b2019-04-16 16:46:36 +02003683 }
3684
Christopher Faulet1be55f92018-10-02 15:59:23 +02003685 end:
Christopher Faulet038ad812019-04-17 11:03:22 +02003686 if (conn_xprt_read0_pending(cs->conn)) {
Willy Tarreauc493c9c2019-06-03 14:18:22 +02003687 h1s->flags |= H1S_F_REOS;
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003688 h1c->flags &= ~H1C_F_WANT_SPLICE;
Christopher Fauletae635762020-09-21 11:47:16 +02003689 TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s);
Christopher Faulet038ad812019-04-17 11:03:22 +02003690 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003691
Christopher Faulet94d35102021-04-09 11:58:49 +02003692 if (!(h1c->flags & H1C_F_WANT_SPLICE)) {
Christopher Faulet0a799aa2020-09-24 09:52:52 +02003693 TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003694 cs->flags &= ~CS_FL_MAY_SPLICE;
Christopher Faulet94d35102021-04-09 11:58:49 +02003695 if (!(h1c->wait_event.events & SUB_RETRY_RECV)) {
3696 TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s);
3697 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
3698 }
Christopher Faulet27182292020-07-03 15:08:49 +02003699 }
Willy Tarreau17ccd1a2020-01-17 16:19:34 +01003700
Willy Tarreau022e5e52020-09-10 09:33:15 +02003701 TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003702 return ret;
Christopher Faulet1be55f92018-10-02 15:59:23 +02003703}
3704
3705static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
3706{
3707 struct h1s *h1s = cs->ctx;
Christopher Faulet140f1a52021-11-26 16:37:55 +01003708 struct h1c *h1c = h1s->h1c;
3709 struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req);
Christopher Faulet1be55f92018-10-02 15:59:23 +02003710 int ret = 0;
3711
Willy Tarreau022e5e52020-09-10 09:33:15 +02003712 TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data});
Christopher Faulet6b81df72019-10-01 22:08:43 +02003713
Christopher Faulet140f1a52021-11-26 16:37:55 +01003714 if (b_data(&h1c->obuf)) {
3715 if (!(h1c->wait_event.events & SUB_RETRY_SEND)) {
Christopher Faulet6b81df72019-10-01 22:08:43 +02003716 TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s);
Christopher Faulet140f1a52021-11-26 16:37:55 +01003717 cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event);
Christopher Faulet6b81df72019-10-01 22:08:43 +02003718 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003719 goto end;
Christopher Fauletd44ad5b2018-11-19 21:52:12 +01003720 }
Christopher Faulet6b81df72019-10-01 22:08:43 +02003721
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003722 ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe);
Christopher Fauletf5ce3202021-11-26 17:26:19 +01003723 if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) {
Christopher Faulet140f1a52021-11-26 16:37:55 +01003724 if (ret > h1m->curr_len) {
3725 h1s->flags |= H1S_F_PROCESSING_ERROR;
3726 h1c->flags |= H1C_F_ST_ERROR;
3727 cs->flags |= CS_FL_ERROR;
3728 TRACE_ERROR("too much payload, more than announced",
3729 H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, cs->conn, h1s);
3730 goto end;
3731 }
3732 h1m->curr_len -= ret;
3733 if (!h1m->curr_len) {
3734 h1m->state = H1_MSG_DONE;
3735 TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, cs->conn, h1s);
3736 }
3737 }
Christopher Faulet8454f2d2021-04-06 17:27:32 +02003738
3739 end:
Willy Tarreau022e5e52020-09-10 09:33:15 +02003740 TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret});
Christopher Faulet1be55f92018-10-02 15:59:23 +02003741 return ret;
3742}
3743#endif
3744
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003745static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3746{
Christopher Fauletc18fc232020-10-06 17:41:36 +02003747 const struct h1c *h1c = conn->ctx;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003748 int ret = 0;
Christopher Fauletc18fc232020-10-06 17:41:36 +02003749
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003750 switch (mux_ctl) {
3751 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +01003752 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003753 ret |= MUX_STATUS_READY;
3754 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +02003755 case MUX_EXIT_STATUS:
Christopher Faulet36e46aa2021-09-28 11:45:05 +02003756 if (output)
3757 *((int *)output) = h1c->errcode;
3758 ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR :
3759 (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR :
3760 (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR :
3761 ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR :
Christopher Faulet2eed8002020-12-07 11:26:13 +01003762 MUX_ES_SUCCESS))));
Christopher Fauletc18fc232020-10-06 17:41:36 +02003763 return ret;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003764 default:
3765 return -1;
3766 }
3767}
3768
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003769/* for debugging with CLI's "show fd" command */
Willy Tarreau8050efe2021-01-21 08:26:06 +01003770static int h1_show_fd(struct buffer *msg, struct connection *conn)
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003771{
3772 struct h1c *h1c = conn->ctx;
3773 struct h1s *h1s = h1c->h1s;
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003774 int ret = 0;
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003775
Christopher Fauletf376a312019-01-04 15:16:06 +01003776 chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u",
3777 h1c->flags, h1c->wait_event.events,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003778 (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf),
3779 (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf),
3780 (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf),
3781 (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf));
3782
3783 if (h1s) {
3784 char *method;
3785
3786 if (h1s->meth < HTTP_METH_OTHER)
3787 method = http_known_methods[h1s->meth].ptr;
3788 else
3789 method = "UNKNOWN";
3790 chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s"
3791 " .meth=%s status=%d",
3792 h1s, h1s->flags,
3793 h1m_state_str(h1s->req.state),
3794 h1m_state_str(h1s->res.state), method, h1s->status);
3795 if (h1s->cs)
3796 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
3797 h1s->cs->flags, h1s->cs->data);
Willy Tarreau150c4f82021-01-20 17:05:58 +01003798
3799 chunk_appendf(&trash, " .subs=%p", h1s->subs);
3800 if (h1s->subs) {
Christopher Faulet6c93c4e2021-02-25 10:06:29 +01003801 chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet);
3802 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
3803 h1s->subs->tasklet->calls,
3804 h1s->subs->tasklet->context);
3805 if (h1s->subs->tasklet->calls >= 1000000)
3806 ret = 1;
3807 resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process);
3808 chunk_appendf(&trash, ")");
Willy Tarreau150c4f82021-01-20 17:05:58 +01003809 }
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003810 }
Willy Tarreau0c0c0a22021-01-21 09:13:35 +01003811 return ret;
Christopher Faulet98fbe952019-07-22 16:18:24 +02003812}
3813
3814
3815/* Add an entry in the headers map. Returns -1 on error and 0 on success. */
3816static int add_hdr_case_adjust(const char *from, const char *to, char **err)
3817{
3818 struct h1_hdr_entry *entry;
3819
3820 /* Be sure there is a non-empty <to> */
3821 if (!strlen(to)) {
3822 memprintf(err, "expect <to>");
3823 return -1;
3824 }
3825
3826 /* Be sure only the case differs between <from> and <to> */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003827 if (strcasecmp(from, to) != 0) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003828 memprintf(err, "<from> and <to> must not differ execpt the case");
3829 return -1;
3830 }
3831
3832 /* Be sure <from> does not already existsin the tree */
3833 if (ebis_lookup(&hdrs_map.map, from)) {
3834 memprintf(err, "duplicate entry '%s'", from);
3835 return -1;
3836 }
3837
3838 /* Create the entry and insert it in the tree */
3839 entry = malloc(sizeof(*entry));
3840 if (!entry) {
3841 memprintf(err, "out of memory");
3842 return -1;
3843 }
3844
3845 entry->node.key = strdup(from);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01003846 entry->name = ist(strdup(to));
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003847 if (!entry->node.key || !isttest(entry->name)) {
Christopher Faulet98fbe952019-07-22 16:18:24 +02003848 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003849 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003850 free(entry);
3851 memprintf(err, "out of memory");
3852 return -1;
3853 }
3854 ebis_insert(&hdrs_map.map, &entry->node);
3855 return 0;
3856}
3857
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003858/* Migrate the the connection to the current thread.
3859 * Return 0 if successful, non-zero otherwise.
3860 * Expected to be called with the old thread lock held.
3861 */
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003862static int h1_takeover(struct connection *conn, int orig_tid)
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003863{
3864 struct h1c *h1c = conn->ctx;
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003865 struct task *task;
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003866
3867 if (fd_takeover(conn->handle.fd, conn) != 0)
3868 return -1;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02003869
3870 if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) {
3871 /* We failed to takeover the xprt, even if the connection may
3872 * still be valid, flag it as error'd, as we have already
3873 * taken over the fd, and wake the tasklet, so that it will
3874 * destroy it.
3875 */
3876 conn->flags |= CO_FL_ERROR;
3877 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
3878 return -1;
3879 }
3880
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003881 if (h1c->wait_event.events)
3882 h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx,
3883 h1c->wait_event.events, &h1c->wait_event);
3884 /* To let the tasklet know it should free itself, and do nothing else,
3885 * set its context to NULL.
3886 */
3887 h1c->wait_event.tasklet->context = NULL;
Olivier Houchard1662cdb2020-07-03 14:04:37 +02003888 tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid);
Willy Tarreau09e0d9e2020-07-01 16:39:33 +02003889
3890 task = h1c->task;
3891 if (task) {
3892 task->context = NULL;
3893 h1c->task = NULL;
3894 __ha_barrier_store();
3895 task_kill(task);
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003896
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003897 h1c->task = task_new_here();
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01003898 if (!h1c->task) {
3899 h1_release(h1c);
3900 return -1;
3901 }
3902 h1c->task->process = h1_timeout_task;
3903 h1c->task->context = h1c;
3904 }
3905 h1c->wait_event.tasklet = tasklet_new();
3906 if (!h1c->wait_event.tasklet) {
3907 h1_release(h1c);
3908 return -1;
3909 }
3910 h1c->wait_event.tasklet->process = h1_io_cb;
3911 h1c->wait_event.tasklet->context = h1c;
3912 h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx,
3913 SUB_RETRY_RECV, &h1c->wait_event);
3914
3915 return 0;
3916}
3917
3918
Christopher Faulet98fbe952019-07-22 16:18:24 +02003919static void h1_hdeaders_case_adjust_deinit()
3920{
3921 struct ebpt_node *node, *next;
3922 struct h1_hdr_entry *entry;
3923
3924 node = ebpt_first(&hdrs_map.map);
3925 while (node) {
3926 next = ebpt_next(node);
3927 ebpt_delete(node);
3928 entry = container_of(node, struct h1_hdr_entry, node);
3929 free(entry->node.key);
Tim Duesterhused526372020-03-05 17:56:33 +01003930 istfree(&entry->name);
Christopher Faulet98fbe952019-07-22 16:18:24 +02003931 free(entry);
3932 node = next;
3933 }
3934 free(hdrs_map.name);
Olivier Houcharda8f6b432018-12-21 15:20:29 +01003935}
3936
Christopher Faulet98fbe952019-07-22 16:18:24 +02003937static int cfg_h1_headers_case_adjust_postparser()
3938{
3939 FILE *file = NULL;
3940 char *c, *key_beg, *key_end, *value_beg, *value_end;
3941 char *err;
3942 int rc, line = 0, err_code = 0;
3943
3944 if (!hdrs_map.name)
3945 goto end;
3946
3947 file = fopen(hdrs_map.name, "r");
3948 if (!file) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003949 ha_alert("h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02003950 hdrs_map.name);
3951 err_code |= ERR_ALERT | ERR_FATAL;
3952 goto end;
3953 }
3954
3955 /* now parse all lines. The file may contain only two header name per
3956 * line, separated by spaces. All heading and trailing spaces will be
3957 * ignored. Lines starting with a # are ignored.
3958 */
3959 while (fgets(trash.area, trash.size, file) != NULL) {
3960 line++;
3961 c = trash.area;
3962
3963 /* strip leading spaces and tabs */
3964 while (*c == ' ' || *c == '\t')
3965 c++;
3966
3967 /* ignore emptu lines, or lines beginning with a dash */
3968 if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n')
3969 continue;
3970
3971 /* look for the end of the key */
3972 key_beg = c;
3973 while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
3974 c++;
3975 key_end = c;
3976
3977 /* strip middle spaces and tabs */
3978 while (*c == ' ' || *c == '\t')
3979 c++;
3980
3981 /* look for the end of the value, it is the end of the line */
3982 value_beg = c;
3983 while (*c && *c != '\n' && *c != '\r')
3984 c++;
3985 value_end = c;
3986
3987 /* trim possibly trailing spaces and tabs */
3988 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
3989 value_end--;
3990
3991 /* set final \0 and check entries */
3992 *key_end = '\0';
3993 *value_end = '\0';
3994
3995 err = NULL;
3996 rc = add_hdr_case_adjust(key_beg, value_beg, &err);
3997 if (rc < 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02003998 ha_alert("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02003999 hdrs_map.name, err, line);
4000 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004001 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004002 goto end;
4003 }
4004 if (rc > 0) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02004005 ha_warning("h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n",
Christopher Faulet98fbe952019-07-22 16:18:24 +02004006 hdrs_map.name, err, line);
4007 err_code |= ERR_WARN;
Christopher Fauletcac5c092019-07-30 16:51:42 +02004008 free(err);
Christopher Faulet98fbe952019-07-22 16:18:24 +02004009 }
4010 }
4011
4012 end:
4013 if (file)
4014 fclose(file);
4015 hap_register_post_deinit(h1_hdeaders_case_adjust_deinit);
4016 return err_code;
4017}
4018
4019
4020/* config parser for global "h1-outgoing-header-case-adjust" */
4021static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004022 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004023 char **err)
4024{
4025 if (too_many_args(2, args, err, NULL))
4026 return -1;
4027 if (!*(args[1]) || !*(args[2])) {
4028 memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]);
4029 return -1;
4030 }
4031 return add_hdr_case_adjust(args[1], args[2], err);
4032}
4033
4034/* config parser for global "h1-outgoing-headers-case-adjust-file" */
4035static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01004036 const struct proxy *defpx, const char *file, int line,
Christopher Faulet98fbe952019-07-22 16:18:24 +02004037 char **err)
4038{
4039 if (too_many_args(1, args, err, NULL))
4040 return -1;
4041 if (!*(args[1])) {
4042 memprintf(err, "'%s' expects <file> as argument.", args[0]);
4043 return -1;
4044 }
4045 free(hdrs_map.name);
4046 hdrs_map.name = strdup(args[1]);
4047 return 0;
4048}
4049
4050
4051/* config keyword parsers */
4052static struct cfg_kw_list cfg_kws = {{ }, {
4053 { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust },
4054 { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file },
4055 { 0, NULL, NULL },
4056 }
4057};
4058
4059INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4060REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser);
4061
4062
Christopher Faulet51dbc942018-09-13 09:05:15 +02004063/****************************************/
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05004064/* MUX initialization and instantiation */
Christopher Faulet51dbc942018-09-13 09:05:15 +02004065/****************************************/
4066
4067/* The mux operations */
Christopher Faulet3f612f72021-02-05 16:44:21 +01004068static const struct mux_ops mux_http_ops = {
Christopher Faulet51dbc942018-09-13 09:05:15 +02004069 .init = h1_init,
4070 .wake = h1_wake,
4071 .attach = h1_attach,
4072 .get_first_cs = h1_get_first_cs,
4073 .detach = h1_detach,
4074 .destroy = h1_destroy,
4075 .avail_streams = h1_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01004076 .used_streams = h1_used_streams,
Christopher Faulet51dbc942018-09-13 09:05:15 +02004077 .rcv_buf = h1_rcv_buf,
4078 .snd_buf = h1_snd_buf,
Willy Tarreaue5733232019-05-22 19:24:06 +02004079#if defined(USE_LINUX_SPLICE)
Christopher Faulet1be55f92018-10-02 15:59:23 +02004080 .rcv_pipe = h1_rcv_pipe,
4081 .snd_pipe = h1_snd_pipe,
4082#endif
Christopher Faulet51dbc942018-09-13 09:05:15 +02004083 .subscribe = h1_subscribe,
4084 .unsubscribe = h1_unsubscribe,
4085 .shutr = h1_shutr,
4086 .shutw = h1_shutw,
Olivier Houcharda8f6b432018-12-21 15:20:29 +01004087 .show_fd = h1_show_fd,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02004088 .ctl = h1_ctl,
Olivier Houchardf12ca9f2020-02-20 16:00:18 +01004089 .takeover = h1_takeover,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02004090 .flags = MX_FL_HTX,
Willy Tarreau0a7a4fb2019-05-22 11:36:54 +02004091 .name = "H1",
Christopher Faulet51dbc942018-09-13 09:05:15 +02004092};
4093
Christopher Faulet3f612f72021-02-05 16:44:21 +01004094static const struct mux_ops mux_h1_ops = {
4095 .init = h1_init,
4096 .wake = h1_wake,
4097 .attach = h1_attach,
4098 .get_first_cs = h1_get_first_cs,
4099 .detach = h1_detach,
4100 .destroy = h1_destroy,
4101 .avail_streams = h1_avail_streams,
4102 .used_streams = h1_used_streams,
4103 .rcv_buf = h1_rcv_buf,
4104 .snd_buf = h1_snd_buf,
4105#if defined(USE_LINUX_SPLICE)
4106 .rcv_pipe = h1_rcv_pipe,
4107 .snd_pipe = h1_snd_pipe,
4108#endif
4109 .subscribe = h1_subscribe,
4110 .unsubscribe = h1_unsubscribe,
4111 .shutr = h1_shutr,
4112 .shutw = h1_shutw,
4113 .show_fd = h1_show_fd,
4114 .ctl = h1_ctl,
4115 .takeover = h1_takeover,
4116 .flags = MX_FL_HTX|MX_FL_NO_UPG,
4117 .name = "H1",
4118};
Christopher Faulet51dbc942018-09-13 09:05:15 +02004119
Christopher Faulet3f612f72021-02-05 16:44:21 +01004120/* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */
4121static struct mux_proto_list mux_proto_h1 =
4122 { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops };
4123static struct mux_proto_list mux_proto_http =
4124 { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops };
Christopher Faulet51dbc942018-09-13 09:05:15 +02004125
Christopher Faulet3f612f72021-02-05 16:44:21 +01004126INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1);
4127INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http);
Willy Tarreau0108d902018-11-25 19:14:37 +01004128
Christopher Faulet51dbc942018-09-13 09:05:15 +02004129/*
4130 * Local variables:
4131 * c-indent-level: 8
4132 * c-basic-offset: 8
4133 * End:
4134 */